@@ -24,11 +24,12 @@ static VALUE rb_cSizedQueue;
24
24
static VALUE set_critical (VALUE value );
25
25
26
26
static VALUE
27
- thread_exclusive_do ( void )
27
+ thread_exclusive ( VALUE ( * func )( ANYARGS ), VALUE arg )
28
28
{
29
- rb_thread_critical = 1 ;
29
+ VALUE critical = rb_thread_critical ;
30
30
31
- return rb_yield (Qundef );
31
+ rb_thread_critical = 1 ;
32
+ return rb_ensure (func , arg , set_critical , (VALUE )critical );
32
33
}
33
34
34
35
/*
@@ -43,7 +44,7 @@ thread_exclusive_do(void)
43
44
static VALUE
44
45
rb_thread_exclusive (void )
45
46
{
46
- return rb_ensure ( thread_exclusive_do , Qundef , set_critical , rb_thread_critical );
47
+ return thread_exclusive ( rb_yield , Qundef );
47
48
}
48
49
49
50
typedef struct _Entry {
@@ -147,7 +148,7 @@ shift_list(List *list)
147
148
VALUE value ;
148
149
149
150
entry = list -> entries ;
150
- if (!entry ) return Qundef ;
151
+ if (!entry ) return Qnil ;
151
152
152
153
list -> entries = entry -> next ;
153
154
if (entry == list -> last_entry ) {
@@ -269,20 +270,12 @@ wait_list(List *list)
269
270
}
270
271
271
272
static void
272
- assert_no_survivors (List * waiting , const char * label , void * addr )
273
+ kill_waiting_threads (List * waiting )
273
274
{
274
275
Entry * entry ;
275
- VALUE ths = 0 ;
276
276
277
277
for (entry = waiting -> entries ; entry ; entry = entry -> next ) {
278
- if (RTEST (wake_thread (entry -> value ))) {
279
- if (!ths ) ths = rb_ary_new ();
280
- rb_ary_push (ths , entry -> value );
281
- }
282
- }
283
- if (ths ) {
284
- rb_bug ("%s %p freed with live thread(s) %s waiting" ,
285
- label , addr , RSTRING_PTR (rb_inspect (ths )));
278
+ rb_thread_kill (entry -> value );
286
279
}
287
280
}
288
281
@@ -334,7 +327,7 @@ finalize_mutex(Mutex *mutex)
334
327
static void
335
328
free_mutex (Mutex * mutex )
336
329
{
337
- assert_no_survivors (& mutex -> waiting , "mutex" , mutex );
330
+ kill_waiting_threads (& mutex -> waiting );
338
331
finalize_mutex (mutex );
339
332
xfree (mutex );
340
333
}
@@ -493,10 +486,8 @@ set_critical(VALUE value)
493
486
static VALUE
494
487
unlock_mutex (Mutex * mutex )
495
488
{
496
- VALUE waking ;
489
+ VALUE waking = thread_exclusive ( unlock_mutex_inner , ( VALUE ) mutex ) ;
497
490
498
- rb_thread_critical = 1 ;
499
- waking = rb_ensure (unlock_mutex_inner , (VALUE )mutex , set_critical , 0 );
500
491
if (!RTEST (waking )) {
501
492
return Qfalse ;
502
493
}
@@ -544,10 +535,9 @@ rb_mutex_exclusive_unlock(VALUE self)
544
535
VALUE waking ;
545
536
Data_Get_Struct (self , Mutex , mutex );
546
537
547
- rb_thread_critical = 1 ;
548
- waking = rb_ensure (rb_mutex_exclusive_unlock_inner , (VALUE )mutex , set_critical , 0 );
538
+ waking = thread_exclusive (rb_mutex_exclusive_unlock_inner , (VALUE )mutex );
549
539
550
- if (waking == Qundef || !RTEST (waking )) {
540
+ if (!RTEST (waking )) {
551
541
return Qnil ;
552
542
}
553
543
@@ -622,7 +612,7 @@ finalize_condvar(ConditionVariable *condvar)
622
612
static void
623
613
free_condvar (ConditionVariable * condvar )
624
614
{
625
- assert_no_survivors (& condvar -> waiting , "condition variable" , condvar );
615
+ kill_waiting_threads (& condvar -> waiting );
626
616
finalize_condvar (condvar );
627
617
xfree (condvar );
628
618
}
@@ -732,8 +722,7 @@ rb_condvar_broadcast(VALUE self)
732
722
733
723
Data_Get_Struct (self , ConditionVariable , condvar );
734
724
735
- rb_thread_critical = 1 ;
736
- rb_ensure (wake_all , (VALUE )& condvar -> waiting , set_critical , 0 );
725
+ thread_exclusive (wake_all , (VALUE )& condvar -> waiting );
737
726
rb_thread_schedule ();
738
727
739
728
return self ;
@@ -750,9 +739,8 @@ rb_condvar_broadcast(VALUE self)
750
739
static void
751
740
signal_condvar (ConditionVariable * condvar )
752
741
{
753
- VALUE waking ;
754
- rb_thread_critical = 1 ;
755
- waking = rb_ensure (wake_one , (VALUE )& condvar -> waiting , set_critical , 0 );
742
+ VALUE waking = thread_exclusive (wake_one , (VALUE )& condvar -> waiting );
743
+
756
744
if (RTEST (waking )) {
757
745
run_thread (waking );
758
746
}
@@ -827,9 +815,9 @@ finalize_queue(Queue *queue)
827
815
static void
828
816
free_queue (Queue * queue )
829
817
{
830
- assert_no_survivors (& queue -> mutex .waiting , "queue" , queue );
831
- assert_no_survivors (& queue -> space_available .waiting , "queue" , queue );
832
- assert_no_survivors (& queue -> value_available .waiting , "queue" , queue );
818
+ kill_waiting_threads (& queue -> mutex .waiting );
819
+ kill_waiting_threads (& queue -> space_available .waiting );
820
+ kill_waiting_threads (& queue -> value_available .waiting );
833
821
finalize_queue (queue );
834
822
xfree (queue );
835
823
}
0 commit comments