@@ -117,45 +117,50 @@ private void registerMainThread(Thread thread, StaticObject guest) {
117117 public final AtomicLong createdThreadCount = new AtomicLong ();
118118 public final AtomicLong peakThreadCount = new AtomicLong ();
119119
120- public void registerThread (Thread host , StaticObject guest ) {
121- activeThreads .add (guest );
122-
123- // Update java.lang.management counters.
124- createdThreadCount .incrementAndGet ();
125- peakThreadCount .updateAndGet (new LongUnaryOperator () {
126- @ Override
127- public long applyAsLong (long oldPeak ) {
128- return Math .max (oldPeak , activeThreads .size ());
120+ public StaticObject registerThread (Thread host , StaticObject guest ) {
121+ synchronized (activeThreadLock ) {
122+ StaticObject existingThread = getGuestThreadFromHost (host );
123+ if (existingThread != null ) {
124+ // There is already a guest thread for this host thread.
125+ return existingThread ;
129126 }
130- });
131127
132- if (finalizerThreadId == -1 ) {
133- if (getMeta ().java_lang_ref_Finalizer$FinalizerThread == guest .getKlass ()) {
134- synchronized (activeThreadLock ) {
128+ activeThreads .add (guest );
129+
130+ // Update java.lang.management counters.
131+ createdThreadCount .incrementAndGet ();
132+ peakThreadCount .updateAndGet (new LongUnaryOperator () {
133+ @ Override
134+ public long applyAsLong (long oldPeak ) {
135+ return Math .max (oldPeak , activeThreads .size ());
136+ }
137+ });
138+
139+ if (finalizerThreadId == -1 ) {
140+ if (getMeta ().java_lang_ref_Finalizer$FinalizerThread == guest .getKlass ()) {
135141 if (finalizerThreadId == -1 ) {
136142 CompilerDirectives .transferToInterpreterAndInvalidate ();
137143 finalizerThreadId = getThreadId (host );
138144 guestFinalizerThread = guest ;
139- return ;
145+ return guest ;
140146 }
141147 }
142148 }
143- }
144- if (referenceHandlerThreadId == -1 ) {
145- if (getMeta ().java_lang_ref_Reference$ReferenceHandler == guest .getKlass ()) {
146- synchronized (activeThreadLock ) {
149+ if (referenceHandlerThreadId == -1 ) {
150+ if (getMeta ().java_lang_ref_Reference$ReferenceHandler == guest .getKlass ()) {
147151 if (referenceHandlerThreadId == -1 ) {
148152 CompilerDirectives .transferToInterpreterAndInvalidate ();
149153 referenceHandlerThreadId = getThreadId (host );
150154 guestReferenceHandlerThread = guest ;
151- return ;
155+ return guest ;
152156 }
153157 }
154158 }
155- }
156- pushThread (Math .toIntExact (getThreadId (host )), guest );
157- if (host == Thread .currentThread ()) {
158- getContext ().registerCurrentThread (guest );
159+ pushThread (Math .toIntExact (getThreadId (host )), guest );
160+ if (host == Thread .currentThread ()) {
161+ getContext ().registerCurrentThread (guest );
162+ }
163+ return guest ;
159164 }
160165 }
161166
@@ -166,18 +171,18 @@ public long applyAsLong(long oldPeak) {
166171 */
167172 @ SuppressFBWarnings (value = "NN" , justification = "Removing a thread from the active set is the state change we need." )
168173 public boolean unregisterThread (StaticObject thread ) {
169- if (!activeThreads .remove (thread )) {
170- // Already unregistered
171- return false ;
172- }
173- logger .fine (() -> {
174- String guestName = getThreadAccess ().getThreadName (thread );
175- long guestId = getThreadAccess ().getThreadId (thread );
176- return String .format ("unregisterThread([GUEST:%s, %d])" , guestName , guestId );
177- });
178- Thread hostThread = getThreadAccess ().getHost (thread );
179- int id = Math .toIntExact (getThreadId (hostThread ));
180174 synchronized (activeThreadLock ) {
175+ if (!activeThreads .remove (thread )) {
176+ // Already unregistered
177+ return false ;
178+ }
179+ logger .fine (() -> {
180+ String guestName = getThreadAccess ().getThreadName (thread );
181+ long guestId = getThreadAccess ().getThreadId (thread );
182+ return String .format ("unregisterThread([GUEST:%s, %d])" , guestName , guestId );
183+ });
184+ Thread hostThread = getThreadAccess ().getHost (thread );
185+ int id = Math .toIntExact (getThreadId (hostThread ));
181186 if (id == mainThreadId ) {
182187 mainThreadId = -1 ;
183188 guestMainThread = null ;
@@ -264,16 +269,17 @@ public StaticObject createGuestThreadFromHost(Thread hostThread, Meta meta, VM v
264269 return null ;
265270 }
266271 synchronized (activeThreadLock ) {
267- StaticObject exisitingThread = getGuestThreadFromHost (hostThread );
268- if (exisitingThread != null ) {
272+ StaticObject existingThread = getGuestThreadFromHost (hostThread );
273+ if (existingThread != null ) {
269274 // already a live guest thread for this host thread
270- return exisitingThread ;
275+ return existingThread ;
271276 }
272277 StaticObject effectiveThreadGroup = threadGroup ;
273278 if (effectiveThreadGroup == null || StaticObject .isNull (effectiveThreadGroup )) {
274279 effectiveThreadGroup = getContext ().getMainThreadGroup ();
275280 }
276281 vm .attachThread (hostThread );
282+
277283 StaticObject guestThread = meta .java_lang_Thread .allocateInstance (getContext ());
278284
279285 // Allow guest Thread.currentThread() to work.
@@ -282,7 +288,9 @@ public StaticObject createGuestThreadFromHost(Thread hostThread, Meta meta, VM v
282288 }
283289 getThreadAccess ().setEETopAlive (guestThread );
284290 getThreadAccess ().initializeHiddenFields (guestThread , hostThread , managedByEspresso );
285- registerThread (hostThread , guestThread );
291+ // Associate host and guest.
292+ existingThread = registerThread (hostThread , guestThread );
293+ assert existingThread == guestThread ;
286294 assert getThreadAccess ().getCurrentGuestThread () != null ;
287295
288296 if (name == null ) {
0 commit comments