Skip to content

Commit 52f48ca

Browse files
committed
Use field initializers for RubyThread
1 parent 13e14ce commit 52f48ca

File tree

1 file changed

+18
-34
lines changed

1 file changed

+18
-34
lines changed

src/main/java/org/truffleruby/core/thread/RubyThread.java

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -41,30 +41,31 @@
4141

4242
public class RubyThread extends RubyDynamicObject implements ObjectGraphNode {
4343

44-
// All fields are explicitly initialized in the constructor to make the ordering clear
45-
public final ThreadLocalGlobals threadLocalGlobals;
46-
public volatile InterruptMode interruptMode;
47-
public volatile ThreadStatus status;
48-
public final List<Lock> ownedLocks;
44+
// Fields initialized here are initialized just after the super() call, and before the rest of the constructor
45+
public final ThreadLocalGlobals threadLocalGlobals = new ThreadLocalGlobals();
46+
public volatile InterruptMode interruptMode = InterruptMode.IMMEDIATE;
47+
public volatile ThreadStatus status = ThreadStatus.RUN;
48+
public final List<Lock> ownedLocks = new ArrayList<>();
4949
public final FiberManager fiberManager;
50-
CountDownLatch finishedLatch;
50+
CountDownLatch finishedLatch = new CountDownLatch(1);
5151
final RubyHash threadLocalVariables;
5252
final RubyHash recursiveObjects;
5353
final RubyHash recursiveObjectsSingle;
5454
final RubyRandomizer randomizer;
55-
public final TracePointState tracePointState;
55+
public final TracePointState tracePointState = new TracePointState();
5656
boolean reportOnException;
5757
boolean abortOnException;
58-
public volatile Thread thread;
59-
volatile RubyException exception;
60-
volatile Object value;
61-
public final AtomicBoolean wakeUp;
62-
volatile int priority;
63-
public ThreadLocalBuffer ioBuffer;
64-
public final Queue<SafepointAction> pendingSafepointActions;
58+
public volatile Thread thread = null;
59+
volatile RubyException exception = null;
60+
volatile Object value = null;
61+
public final AtomicBoolean wakeUp = new AtomicBoolean(false);
62+
volatile int priority = Thread.NORM_PRIORITY;
63+
public ThreadLocalBuffer ioBuffer = ThreadLocalBuffer.NULL_BUFFER;
64+
// Needs to be a thread-safe queue because multiple Fibers of the same Thread might enqueue concurrently
65+
public final Queue<SafepointAction> pendingSafepointActions = newLinkedBlockingQueue();
6566
Object threadGroup;
6667
String sourceLocation;
67-
Object name;
68+
Object name = Nil.INSTANCE;
6869

6970
public RubyThread(
7071
RubyClass rubyClass,
@@ -76,33 +77,16 @@ public RubyThread(
7677
Object threadGroup,
7778
String sourceLocation) {
7879
super(rubyClass, shape);
79-
this.threadLocalGlobals = new ThreadLocalGlobals();
80-
this.interruptMode = InterruptMode.IMMEDIATE;
81-
this.status = ThreadStatus.RUN;
82-
this.ownedLocks = new ArrayList<>();
83-
this.finishedLatch = new CountDownLatch(1);
8480
this.threadLocalVariables = HashOperations.newEmptyHash(context, language);
8581
this.recursiveObjects = HashOperations.newEmptyHash(context, language);
8682
this.recursiveObjectsSingle = HashOperations.newEmptyHash(context, language);
8783
this.recursiveObjectsSingle.compareByIdentity = true;
88-
this.randomizer = RandomizerNodes.newRandomizer(
89-
context,
90-
language,
91-
false); // This random instance is only for this thread and thus does not need to be thread-safe
92-
this.tracePointState = new TracePointState();
84+
// This random instance is only for this thread and thus does not need to be thread-safe
85+
this.randomizer = RandomizerNodes.newRandomizer(context, language, false);
9386
this.reportOnException = reportOnException;
9487
this.abortOnException = abortOnException;
95-
this.thread = null;
96-
this.exception = null;
97-
this.value = null;
98-
this.wakeUp = new AtomicBoolean(false);
99-
this.priority = Thread.NORM_PRIORITY;
100-
this.ioBuffer = ThreadLocalBuffer.NULL_BUFFER;
101-
// Needs to be a thread-safe queue because multiple Fibers of the same Thread might enqueue concurrently
102-
this.pendingSafepointActions = newLinkedBlockingQueue();
10388
this.threadGroup = threadGroup;
10489
this.sourceLocation = sourceLocation;
105-
this.name = Nil.INSTANCE;
10690
// Initialized last as it captures `this`
10791
this.fiberManager = new FiberManager(language, context, this);
10892
}

0 commit comments

Comments
 (0)