@@ -81,7 +81,7 @@ static void rb_mutex_abandon_all(rb_mutex_t *mutexes);
8181static void rb_mutex_abandon_keeping_mutexes (rb_thread_t * th );
8282static void rb_mutex_abandon_locking_mutex (rb_thread_t * th );
8383#endif
84- static const char * rb_mutex_unlock_th (rb_mutex_t * mutex , rb_thread_t * th , rb_fiber_t * fiber );
84+ static const char * rb_mutex_unlock_th (rb_mutex_t * mutex , rb_thread_t * th , rb_serial_t fiber_serial );
8585
8686/*
8787 * Document-class: Thread::Mutex
@@ -133,7 +133,7 @@ mutex_free(void *ptr)
133133{
134134 rb_mutex_t * mutex = ptr ;
135135 if (mutex_locked_p (mutex )) {
136- const char * err = rb_mutex_unlock_th (mutex , rb_thread_ptr (mutex -> thread ), NULL );
136+ const char * err = rb_mutex_unlock_th (mutex , rb_thread_ptr (mutex -> thread ), 0 );
137137 if (err ) rb_bug ("%s" , err );
138138 }
139139 ruby_xfree (ptr );
@@ -221,26 +221,26 @@ thread_mutex_remove(rb_thread_t *thread, rb_mutex_t *mutex)
221221}
222222
223223static void
224- mutex_set_owner (rb_mutex_t * mutex , rb_thread_t * th , rb_fiber_t * fiber )
224+ mutex_set_owner (rb_mutex_t * mutex , rb_thread_t * th , rb_serial_t fiber_serial )
225225{
226226 mutex -> thread = th -> self ;
227- mutex -> fiber_serial = rb_fiber_serial ( fiber ) ;
227+ mutex -> fiber_serial = fiber_serial ;
228228}
229229
230230static void
231- mutex_locked (rb_mutex_t * mutex , rb_thread_t * th , rb_fiber_t * fiber )
231+ mutex_locked (rb_mutex_t * mutex , rb_thread_t * th , rb_serial_t fiber_serial )
232232{
233- mutex_set_owner (mutex , th , fiber );
233+ mutex_set_owner (mutex , th , fiber_serial );
234234 thread_mutex_insert (th , mutex );
235235}
236236
237237static inline bool
238- mutex_trylock (rb_mutex_t * mutex , rb_thread_t * th , rb_fiber_t * fiber )
238+ mutex_trylock (rb_mutex_t * mutex , rb_thread_t * th , rb_serial_t fiber_serial )
239239{
240240 if (mutex -> fiber_serial == 0 ) {
241241 RUBY_DEBUG_LOG ("%p ok" , mutex );
242242
243- mutex_locked (mutex , th , fiber );
243+ mutex_locked (mutex , th , fiber_serial );
244244 return true;
245245 }
246246 else {
@@ -252,7 +252,7 @@ mutex_trylock(rb_mutex_t *mutex, rb_thread_t *th, rb_fiber_t *fiber)
252252static VALUE
253253rb_mut_trylock (rb_execution_context_t * ec , VALUE self )
254254{
255- return RBOOL (mutex_trylock (mutex_ptr (self ), ec -> thread_ptr , ec -> fiber_ptr ));
255+ return RBOOL (mutex_trylock (mutex_ptr (self ), ec -> thread_ptr , rb_ec_fiber_serial ( ec ) ));
256256}
257257
258258VALUE
@@ -262,9 +262,9 @@ rb_mutex_trylock(VALUE self)
262262}
263263
264264static VALUE
265- mutex_owned_p (rb_fiber_t * fiber , rb_mutex_t * mutex )
265+ mutex_owned_p (rb_serial_t fiber_serial , rb_mutex_t * mutex )
266266{
267- return RBOOL (mutex -> fiber_serial == rb_fiber_serial ( fiber ) );
267+ return RBOOL (mutex -> fiber_serial == fiber_serial );
268268}
269269
270270static VALUE
@@ -305,6 +305,7 @@ do_mutex_lock(struct mutex_args *args, int interruptible_p)
305305 rb_execution_context_t * ec = args -> ec ;
306306 rb_thread_t * th = ec -> thread_ptr ;
307307 rb_fiber_t * fiber = ec -> fiber_ptr ;
308+ rb_serial_t fiber_serial = rb_ec_fiber_serial (ec );
308309 rb_mutex_t * mutex = args -> mutex ;
309310 rb_atomic_t saved_ints = 0 ;
310311
@@ -314,12 +315,12 @@ do_mutex_lock(struct mutex_args *args, int interruptible_p)
314315 rb_raise (rb_eThreadError , "can't be called from trap context" );
315316 }
316317
317- if (!mutex_trylock (mutex , th , fiber )) {
318- if (mutex -> fiber_serial == rb_fiber_serial ( fiber ) ) {
318+ if (!mutex_trylock (mutex , th , fiber_serial )) {
319+ if (mutex -> fiber_serial == fiber_serial ) {
319320 rb_raise (rb_eThreadError , "deadlock; recursive locking" );
320321 }
321322
322- while (mutex -> fiber_serial != rb_fiber_serial ( fiber ) ) {
323+ while (mutex -> fiber_serial != fiber_serial ) {
323324 VM_ASSERT (mutex -> fiber_serial != 0 );
324325
325326 VALUE scheduler = rb_fiber_scheduler_current ();
@@ -335,7 +336,7 @@ do_mutex_lock(struct mutex_args *args, int interruptible_p)
335336 rb_ensure (call_rb_fiber_scheduler_block , self , delete_from_waitq , (VALUE )& sync_waiter );
336337
337338 if (!mutex -> fiber_serial ) {
338- mutex_set_owner (mutex , th , fiber );
339+ mutex_set_owner (mutex , th , fiber_serial );
339340 }
340341 }
341342 else {
@@ -376,7 +377,7 @@ do_mutex_lock(struct mutex_args *args, int interruptible_p)
376377
377378 // unlocked by another thread while sleeping
378379 if (!mutex -> fiber_serial ) {
379- mutex_set_owner (mutex , th , fiber );
380+ mutex_set_owner (mutex , th , fiber_serial );
380381 }
381382
382383 rb_ractor_sleeper_threads_dec (th -> ractor );
@@ -389,13 +390,13 @@ do_mutex_lock(struct mutex_args *args, int interruptible_p)
389390 if (interruptible_p ) {
390391 /* release mutex before checking for interrupts...as interrupt checking
391392 * code might call rb_raise() */
392- if (mutex -> fiber_serial == rb_fiber_serial ( fiber ) ) {
393+ if (mutex -> fiber_serial == fiber_serial ) {
393394 mutex -> thread = Qfalse ;
394395 mutex -> fiber_serial = 0 ;
395396 }
396397 RUBY_VM_CHECK_INTS_BLOCKING (th -> ec ); /* may release mutex */
397398 if (!mutex -> fiber_serial ) {
398- mutex_set_owner (mutex , th , fiber );
399+ mutex_set_owner (mutex , th , fiber_serial );
399400 }
400401 }
401402 else {
@@ -414,13 +415,13 @@ do_mutex_lock(struct mutex_args *args, int interruptible_p)
414415 }
415416
416417 if (saved_ints ) th -> ec -> interrupt_flag = saved_ints ;
417- if (mutex -> fiber_serial == rb_fiber_serial ( fiber )) mutex_locked (mutex , th , fiber );
418+ if (mutex -> fiber_serial == fiber_serial ) mutex_locked (mutex , th , fiber_serial );
418419 }
419420
420421 RUBY_DEBUG_LOG ("%p locked" , mutex );
421422
422423 // assertion
423- if (mutex_owned_p (fiber , mutex ) == Qfalse ) rb_bug ("do_mutex_lock: mutex is not owned." );
424+ if (mutex_owned_p (fiber_serial , mutex ) == Qfalse ) rb_bug ("do_mutex_lock: mutex is not owned." );
424425
425426 return self ;
426427}
@@ -455,7 +456,7 @@ rb_mutex_lock(VALUE self)
455456static VALUE
456457rb_mut_owned_p (rb_execution_context_t * ec , VALUE self )
457458{
458- return mutex_owned_p (ec -> fiber_ptr , mutex_ptr (self ));
459+ return mutex_owned_p (rb_ec_fiber_serial ( ec ) , mutex_ptr (self ));
459460}
460461
461462VALUE
@@ -465,14 +466,14 @@ rb_mutex_owned_p(VALUE self)
465466}
466467
467468static const char *
468- rb_mutex_unlock_th (rb_mutex_t * mutex , rb_thread_t * th , rb_fiber_t * fiber )
469+ rb_mutex_unlock_th (rb_mutex_t * mutex , rb_thread_t * th , rb_serial_t fiber_serial )
469470{
470471 RUBY_DEBUG_LOG ("%p" , mutex );
471472
472473 if (mutex -> fiber_serial == 0 ) {
473474 return "Attempt to unlock a mutex which is not locked" ;
474475 }
475- else if (fiber && mutex -> fiber_serial != rb_fiber_serial ( fiber ) ) {
476+ else if (fiber_serial && mutex -> fiber_serial != fiber_serial ) {
476477 return "Attempt to unlock a mutex which is locked by another thread/fiber" ;
477478 }
478479
@@ -516,7 +517,7 @@ do_mutex_unlock(struct mutex_args *args)
516517 rb_mutex_t * mutex = args -> mutex ;
517518 rb_thread_t * th = rb_ec_thread_ptr (args -> ec );
518519
519- err = rb_mutex_unlock_th (mutex , th , args -> ec -> fiber_ptr );
520+ err = rb_mutex_unlock_th (mutex , th , rb_ec_fiber_serial ( args -> ec ) );
520521 if (err ) rb_raise (rb_eThreadError , "%s" , err );
521522}
522523
0 commit comments