1#include "internal/gc.h"
2#include "internal/thread.h"
5#include "ractor_core.h"
14 return vm->ractor.sync.lock_owner == GET_RACTOR();
19RUBY_ASSERT_vm_locking(
void)
21 if (rb_multi_ractor_p()) {
23 VM_ASSERT(vm_locked(vm));
28RUBY_ASSERT_vm_unlocking(
void)
30 if (rb_multi_ractor_p()) {
32 VM_ASSERT(!vm_locked(vm));
40 return vm_locked(GET_VM());
44vm_need_barrier_waiting(
const rb_vm_t *vm)
46#ifdef RUBY_THREAD_PTHREAD_H
47 return vm->ractor.sched.barrier_waiting;
49 return vm->ractor.sync.barrier_waiting;
56#ifdef RUBY_THREAD_PTHREAD_H
57 return !no_barrier && cr->threads.sched.running != NULL && vm_need_barrier_waiting(vm);
59 return !no_barrier && vm_need_barrier_waiting(vm);
64vm_lock_enter(
rb_ractor_t *cr,
rb_vm_t *vm,
bool locked,
bool no_barrier,
unsigned int *lev APPEND_LOCATION_ARGS)
66 RUBY_DEBUG_LOG2(file, line,
"start locked:%d", locked);
74 VM_ASSERT(cr->sync.locked_by != rb_ractor_self(cr));
78 VM_ASSERT(vm->ractor.sync.lock_owner == NULL);
79 VM_ASSERT(vm->ractor.sync.lock_rec == 0);
82 if (vm_need_barrier(no_barrier, cr, vm)) {
84 RB_VM_SAVE_MACHINE_CONTEXT(rb_ec_thread_ptr(ec));
87 VM_ASSERT(vm_need_barrier_waiting(vm));
88 RUBY_DEBUG_LOG(
"barrier serial:%u", vm->ractor.sched.barrier_serial);
89 rb_ractor_sched_barrier_join(vm, cr);
90 }
while (vm_need_barrier_waiting(vm));
93 VM_ASSERT(vm->ractor.sync.lock_rec == 0);
94 VM_ASSERT(vm->ractor.sync.lock_owner == NULL);
95 vm->ractor.sync.lock_owner = cr;
98 vm->ractor.sync.lock_rec++;
99 *lev = vm->ractor.sync.lock_rec;
101 RUBY_DEBUG_LOG2(file, line,
"rec:%u owner:%u", vm->ractor.sync.lock_rec,
102 (
unsigned int)rb_ractor_id(vm->ractor.sync.lock_owner));
106vm_lock_leave(
rb_vm_t *vm,
unsigned int *lev APPEND_LOCATION_ARGS)
108 RUBY_DEBUG_LOG2(file, line,
"rec:%u owner:%u%s", vm->ractor.sync.lock_rec,
109 (
unsigned int)rb_ractor_id(vm->ractor.sync.lock_owner),
110 vm->ractor.sync.lock_rec == 1 ?
" (leave)" :
"");
113 VM_ASSERT(vm->ractor.sync.lock_rec > 0);
114 VM_ASSERT(vm->ractor.sync.lock_rec == *lev);
116 vm->ractor.sync.lock_rec--;
117 *lev = vm->ractor.sync.lock_rec;
119 if (vm->ractor.sync.lock_rec == 0) {
120 vm->ractor.sync.lock_owner = NULL;
126rb_vm_lock_enter_body(
unsigned int *lev APPEND_LOCATION_ARGS)
130 vm_lock_enter(NULL, vm,
true,
false, lev APPEND_LOCATION_PARAMS);
133 vm_lock_enter(GET_RACTOR(), vm,
false,
false, lev APPEND_LOCATION_PARAMS);
138rb_vm_lock_enter_body_nb(
unsigned int *lev APPEND_LOCATION_ARGS)
142 vm_lock_enter(NULL, vm,
true,
true, lev APPEND_LOCATION_PARAMS);
145 vm_lock_enter(GET_RACTOR(), vm,
false,
true, lev APPEND_LOCATION_PARAMS);
150rb_vm_lock_enter_body_cr(
rb_ractor_t *cr,
unsigned int *lev APPEND_LOCATION_ARGS)
153 vm_lock_enter(cr, vm, vm_locked(vm),
false, lev APPEND_LOCATION_PARAMS);
157rb_vm_lock_leave_body(
unsigned int *lev APPEND_LOCATION_ARGS)
159 vm_lock_leave(GET_VM(), lev APPEND_LOCATION_PARAMS);
163rb_vm_lock_body(LOCATION_ARGS)
166 ASSERT_vm_unlocking();
168 vm_lock_enter(GET_RACTOR(), vm,
false,
false, &vm->ractor.sync.lock_rec APPEND_LOCATION_PARAMS);
172rb_vm_unlock_body(LOCATION_ARGS)
176 VM_ASSERT(vm->ractor.sync.lock_rec == 1);
177 vm_lock_leave(vm, &vm->ractor.sync.lock_rec APPEND_LOCATION_PARAMS);
181vm_cond_wait(
rb_vm_t *vm, rb_nativethread_cond_t *cond,
unsigned long msec)
184 unsigned int lock_rec = vm->ractor.sync.lock_rec;
187 vm->ractor.sync.lock_rec = 0;
188 vm->ractor.sync.lock_owner = NULL;
195 vm->ractor.sync.lock_rec = lock_rec;
196 vm->ractor.sync.lock_owner = cr;
200rb_vm_cond_wait(
rb_vm_t *vm, rb_nativethread_cond_t *cond)
202 vm_cond_wait(vm, cond, 0);
206rb_vm_cond_timedwait(
rb_vm_t *vm, rb_nativethread_cond_t *cond,
unsigned long msec)
208 vm_cond_wait(vm, cond, msec);
214 RB_DEBUG_COUNTER_INC(vm_sync_barrier);
216 if (!rb_multi_ractor_p()) {
222 VM_ASSERT(!vm->ractor.sched.barrier_waiting);
225 VM_ASSERT(cr == GET_RACTOR());
226 VM_ASSERT(rb_ractor_status_p(cr, ractor_running));
228 rb_ractor_sched_barrier_start(vm, cr);
234 unsigned int recorded_lock_rec,
235 unsigned int current_lock_rec)
237 VM_ASSERT(recorded_lock_rec != current_lock_rec);
239 if (UNLIKELY(recorded_lock_rec > current_lock_rec)) {
240 rb_bug(
"unexpected situation - recordd:%u current:%u",
241 recorded_lock_rec, current_lock_rec);
244 while (recorded_lock_rec < current_lock_rec) {
245 RB_VM_LOCK_LEAVE_LEV(¤t_lock_rec);
249 VM_ASSERT(recorded_lock_rec == rb_ec_vm_lock_rec(ec));
void rb_native_mutex_lock(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_lock.
void rb_native_mutex_unlock(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_unlock.
void rb_native_cond_wait(rb_nativethread_cond_t *cond, rb_nativethread_lock_t *mutex)
Waits for the passed condition variable to be signalled.
void rb_native_cond_timedwait(rb_nativethread_cond_t *cond, rb_nativethread_lock_t *mutex, unsigned long msec)
Identical to rb_native_cond_wait(), except it additionally takes timeout in msec resolution.