@@ -83,13 +83,15 @@ public class RubyThread extends RubyObject {
8383 private volatile boolean killed = false ;
8484 public Object killLock = new Object ();
8585 private RubyThread joinedByCriticalThread ;
86-
87- public final ReentrantLock lock = new ReentrantLock ();
88- private volatile boolean critical = false ;
89-
90- private static boolean USE_POOLING ;
91-
92- private static final boolean DEBUG = false ;
86+
87+ private boolean checkLocks = false ;
88+
89+ public final ReentrantLock lock = new ReentrantLock ();
90+ private volatile boolean critical = false ;
91+
92+ private static boolean USE_POOLING ;
93+
94+ private static final boolean DEBUG = false ;
9395
9496 static {
9597 if (Ruby .isSecurityRestricted ()) USE_POOLING = false ;
@@ -237,29 +239,22 @@ public void cleanTerminate(IRubyObject result) {
237239 }
238240
239241 public void pollThreadEvents () {
240- try {
241- // check for criticalization *before* locking ourselves
242- threadService .waitForCritical ();
243-
244- while (!this .lock .tryLock ());
245-
246- ensureCurrent ();
247-
248- if (DEBUG ) System .out .println ("thread " + Thread .currentThread () + " before" );
249- if (killed ) throw new ThreadKill ();
250-
251- if (DEBUG ) System .out .println ("thread " + Thread .currentThread () + " after" );
252- if (receivedException != null ) {
253- // clear this so we don't keep re-throwing
254- IRubyObject raiseException = receivedException ;
255- receivedException = null ;
256- RubyModule kernelModule = getRuntime ().getModule ("Kernel" );
257- if (DEBUG ) System .out .println ("thread " + Thread .currentThread () + " before propagating exception: " + killed );
258- kernelModule .callMethod (getRuntime ().getCurrentContext (), "raise" , raiseException );
259- }
260-
261- } finally {
262- if (this .lock .isHeldByCurrentThread ()) this .lock .unlock ();
242+ // check for criticalization *before* locking ourselves
243+ threadService .waitForCritical ();
244+
245+ ensureCurrent ();
246+
247+ if (DEBUG ) System .out .println ("thread " + Thread .currentThread () + " before" );
248+ if (killed ) throw new ThreadKill ();
249+
250+ if (DEBUG ) System .out .println ("thread " + Thread .currentThread () + " after" );
251+ if (receivedException != null ) {
252+ // clear this so we don't keep re-throwing
253+ IRubyObject raiseException = receivedException ;
254+ receivedException = null ;
255+ RubyModule kernelModule = getRuntime ().getModule ("Kernel" );
256+ if (DEBUG ) System .out .println ("thread " + Thread .currentThread () + " before propagating exception: " + killed );
257+ kernelModule .callMethod (getRuntime ().getCurrentContext (), "raise" , raiseException );
263258 }
264259 }
265260
@@ -531,6 +526,8 @@ public IRubyObject raise(IRubyObject[] args, Block block) {
531526 if (DEBUG ) System .out .println ("thread " + Thread .currentThread () + " before raising" );
532527 RubyThread currentThread = getRuntime ().getCurrentContext ().getThread ();
533528 try {
529+ this .checkLocks = true ;
530+
534531 while (!(currentThread .lock .tryLock () && this .lock .tryLock ())) {
535532 if (currentThread .lock .isHeldByCurrentThread ()) currentThread .lock .unlock ();
536533 }
@@ -544,6 +541,7 @@ public IRubyObject raise(IRubyObject[] args, Block block) {
544541 } finally {
545542 if (currentThread .lock .isHeldByCurrentThread ()) currentThread .lock .unlock ();
546543 if (this .lock .isHeldByCurrentThread ()) this .lock .unlock ();
544+ this .checkLocks = false ;
547545 }
548546
549547 return this ;
@@ -637,6 +635,7 @@ public IRubyObject kill() {
637635 RubyThread currentThread = getRuntime ().getCurrentContext ().getThread ();
638636
639637 try {
638+ this .checkLocks = true ;
640639 if (DEBUG ) System .out .println ("thread " + Thread .currentThread () + " trying to kill" );
641640 while (!(currentThread .lock .tryLock () && this .lock .tryLock ())) {
642641 if (currentThread .lock .isHeldByCurrentThread ()) currentThread .lock .unlock ();
@@ -651,6 +650,7 @@ public IRubyObject kill() {
651650 } finally {
652651 if (currentThread .lock .isHeldByCurrentThread ()) currentThread .lock .unlock ();
653652 if (this .lock .isHeldByCurrentThread ()) this .lock .unlock ();
653+ this .checkLocks = false ;
654654 }
655655
656656 try {
0 commit comments