Skip to content

Commit 0e71923

Browse files
committed
thread_sync: Mutex keep rb_thread_t * instead of VALUE
We never need the actual thread object and this avoid any issue if the thread object is ever moved.
1 parent bfd28d5 commit 0e71923

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

thread_sync.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ static VALUE rb_eClosedQueueError;
88
/* Mutex */
99
typedef struct rb_mutex_struct {
1010
rb_serial_t ec_serial;
11-
VALUE thread; // even if the fiber is collected, we might need access to the thread in mutex_free
11+
rb_thread_t *th; // even if the fiber is collected, we might need access to the thread in mutex_free
1212
struct rb_mutex_struct *next_mutex;
1313
struct ccan_list_head waitq; /* protected by GVL */
1414
} rb_mutex_t;
@@ -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), 0);
136+
const char *err = rb_mutex_unlock_th(mutex, mutex->th, 0);
137137
if (err) rb_bug("%s", err);
138138
}
139139
ruby_xfree(ptr);
@@ -223,7 +223,7 @@ thread_mutex_remove(rb_thread_t *thread, rb_mutex_t *mutex)
223223
static void
224224
mutex_set_owner(rb_mutex_t *mutex, rb_thread_t *th, rb_serial_t ec_serial)
225225
{
226-
mutex->thread = th->self;
226+
mutex->th = th;
227227
mutex->ec_serial = ec_serial;
228228
}
229229

@@ -340,7 +340,7 @@ do_mutex_lock(struct mutex_args *args, int interruptible_p)
340340
}
341341
}
342342
else {
343-
if (!th->vm->thread_ignore_deadlock && rb_thread_ptr(mutex->thread) == th) {
343+
if (!th->vm->thread_ignore_deadlock && mutex->th == th) {
344344
rb_raise(rb_eThreadError, "deadlock; lock already owned by another fiber belonging to the same thread");
345345
}
346346

@@ -391,7 +391,7 @@ do_mutex_lock(struct mutex_args *args, int interruptible_p)
391391
/* release mutex before checking for interrupts...as interrupt checking
392392
* code might call rb_raise() */
393393
if (mutex->ec_serial == ec_serial) {
394-
mutex->thread = Qfalse;
394+
mutex->th = NULL;
395395
mutex->ec_serial = 0;
396396
}
397397
RUBY_VM_CHECK_INTS_BLOCKING(th->ec); /* may release mutex */

0 commit comments

Comments
 (0)