Ruby: vm_sync.c Source File

Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)

vm_sync.c
Go to the documentation of this file.
1#include "vm_core.h"
2#include "vm_sync.h"
3#include "ractor_core.h"
4#include "vm_debug.h"
5#include "gc.h"
6
7static bool vm_barrier_finish_p(rb_vm_t *vm);
8
9static bool
10vm_locked(rb_vm_t *vm)
11{
12 return vm->ractor.sync.lock_owner == GET_RACTOR();
13}
14
15#if RUBY_DEBUG > 0
16void
17RUBY_ASSERT_vm_locking(void)
18{
19 if (rb_multi_ractor_p()) {
20 rb_vm_t *vm = GET_VM();
21 VM_ASSERT(vm_locked(vm));
22 }
23}
24
25void
26RUBY_ASSERT_vm_unlocking(void)
27{
28 if (rb_multi_ractor_p()) {
29 rb_vm_t *vm = GET_VM();
30 VM_ASSERT(!vm_locked(vm));
31 }
32}
33#endif
34
35bool
36rb_vm_locked_p(void)
37{
38 return vm_locked(GET_VM());
39}
40
41static void
42vm_lock_enter(rb_ractor_t *cr, rb_vm_t *vm, bool locked, bool no_barrier, unsigned int *lev APPEND_LOCATION_ARGS)
43{
44 RUBY_DEBUG_LOG2(file, line, "start locked:%d", locked);
45
46 if (locked) {
47 ASSERT_vm_locking();
48 }
49 else {
50#if RACTOR_CHECK_MODE
51 // locking ractor and acquire VM lock will cause deadlock
52 VM_ASSERT(cr->sync.locked_by != rb_ractor_self(cr));
53#endif
54
55 // lock
56 rb_native_mutex_lock(&vm->ractor.sync.lock);
57 VM_ASSERT(vm->ractor.sync.lock_owner == NULL);
58 vm->ractor.sync.lock_owner = cr;
59
60 if (!no_barrier) {
61 // barrier
62 while (vm->ractor.sync.barrier_waiting) {
63 unsigned int barrier_cnt = vm->ractor.sync.barrier_cnt;
64 rb_thread_t *th = GET_THREAD();
65 bool running;
66
67 RB_GC_SAVE_MACHINE_CONTEXT(th);
68
69 if (rb_ractor_status_p(cr, ractor_running)) {
70 rb_vm_ractor_blocking_cnt_inc(vm, cr, __FILE__, __LINE__);
71 running = true;
72 }
73 else {
74 running = false;
75 }
76 VM_ASSERT(rb_ractor_status_p(cr, ractor_blocking));
77
78 if (vm_barrier_finish_p(vm)) {
79 RUBY_DEBUG_LOG("wakeup barrier owner", 0);
80 rb_native_cond_signal(&vm->ractor.sync.barrier_cond);
81 }
82 else {
83 RUBY_DEBUG_LOG("wait for barrier finish", 0);
84 }
85
86 // wait for restart
87 while (barrier_cnt == vm->ractor.sync.barrier_cnt) {
88 vm->ractor.sync.lock_owner = NULL;
89 rb_native_cond_wait(&cr->barrier_wait_cond, &vm->ractor.sync.lock);
90 VM_ASSERT(vm->ractor.sync.lock_owner == NULL);
91 vm->ractor.sync.lock_owner = cr;
92 }
93
94 RUBY_DEBUG_LOG("barrier is released. Acquire vm_lock", 0);
95
96 if (running) {
97 rb_vm_ractor_blocking_cnt_dec(vm, cr, __FILE__, __LINE__);
98 }
99 }
100 }
101
102 VM_ASSERT(vm->ractor.sync.lock_rec == 0);
103 VM_ASSERT(vm->ractor.sync.lock_owner == cr);
104 }
105
106 vm->ractor.sync.lock_rec++;
107 *lev = vm->ractor.sync.lock_rec;
108
109 RUBY_DEBUG_LOG2(file, line, "rec:%u owner:%d", vm->ractor.sync.lock_rec, rb_ractor_id(vm->ractor.sync.lock_owner));
110}
111
112static void
113vm_lock_leave(rb_vm_t *vm, unsigned int *lev APPEND_LOCATION_ARGS)
114{
115 RUBY_DEBUG_LOG2(file, line, "rec:%u owner:%d", vm->ractor.sync.lock_rec, rb_ractor_id(vm->ractor.sync.lock_owner));
116
117 ASSERT_vm_locking();
118 VM_ASSERT(vm->ractor.sync.lock_rec > 0);
119 VM_ASSERT(vm->ractor.sync.lock_rec == *lev);
120
121 vm->ractor.sync.lock_rec--;
122 *lev = vm->ractor.sync.lock_rec;
123
124 if (vm->ractor.sync.lock_rec == 0) {
125 vm->ractor.sync.lock_owner = NULL;
126 rb_native_mutex_unlock(&vm->ractor.sync.lock);
127 }
128}
129
130 MJIT_FUNC_EXPORTED void
131rb_vm_lock_enter_body(unsigned int *lev APPEND_LOCATION_ARGS)
132{
133 rb_vm_t *vm = GET_VM();
134 if (vm_locked(vm)) {
135 vm_lock_enter(NULL, vm, true, false, lev APPEND_LOCATION_PARAMS);
136 }
137 else {
138 vm_lock_enter(GET_RACTOR(), vm, false, false, lev APPEND_LOCATION_PARAMS);
139 }
140}
141
142 MJIT_FUNC_EXPORTED void
143rb_vm_lock_enter_body_nb(unsigned int *lev APPEND_LOCATION_ARGS)
144{
145 rb_vm_t *vm = GET_VM();
146 if (vm_locked(vm)) {
147 vm_lock_enter(NULL, vm, true, true, lev APPEND_LOCATION_PARAMS);
148 }
149 else {
150 vm_lock_enter(GET_RACTOR(), vm, false, true, lev APPEND_LOCATION_PARAMS);
151 }
152}
153
154 MJIT_FUNC_EXPORTED void
155rb_vm_lock_enter_body_cr(rb_ractor_t *cr, unsigned int *lev APPEND_LOCATION_ARGS)
156{
157 rb_vm_t *vm = GET_VM();
158 vm_lock_enter(cr, vm, vm_locked(vm), false, lev APPEND_LOCATION_PARAMS);
159}
160
161 MJIT_FUNC_EXPORTED void
162rb_vm_lock_leave_body(unsigned int *lev APPEND_LOCATION_ARGS)
163{
164 vm_lock_leave(GET_VM(), lev APPEND_LOCATION_PARAMS);
165}
166
167void
168rb_vm_lock_body(LOCATION_ARGS)
169{
170 rb_vm_t *vm = GET_VM();
171 ASSERT_vm_unlocking();
172
173 vm_lock_enter(GET_RACTOR(), vm, false, false, &vm->ractor.sync.lock_rec APPEND_LOCATION_PARAMS);
174}
175
176void
177rb_vm_unlock_body(LOCATION_ARGS)
178{
179 rb_vm_t *vm = GET_VM();
180 ASSERT_vm_locking();
181 VM_ASSERT(vm->ractor.sync.lock_rec == 1);
182 vm_lock_leave(vm, &vm->ractor.sync.lock_rec APPEND_LOCATION_PARAMS);
183}
184
185static void
186vm_cond_wait(rb_vm_t *vm, rb_nativethread_cond_t *cond, unsigned long msec)
187{
188 ASSERT_vm_locking();
189 unsigned int lock_rec = vm->ractor.sync.lock_rec;
190 rb_ractor_t *cr = vm->ractor.sync.lock_owner;
191
192 vm->ractor.sync.lock_rec = 0;
193 vm->ractor.sync.lock_owner = NULL;
194 if (msec > 0) {
195 rb_native_cond_timedwait(cond, &vm->ractor.sync.lock, msec);
196 }
197 else {
198 rb_native_cond_wait(cond, &vm->ractor.sync.lock);
199 }
200 vm->ractor.sync.lock_rec = lock_rec;
201 vm->ractor.sync.lock_owner = cr;
202}
203
204void
205 rb_vm_cond_wait(rb_vm_t *vm, rb_nativethread_cond_t *cond)
206{
207 vm_cond_wait(vm, cond, 0);
208}
209
210void
211 rb_vm_cond_timedwait(rb_vm_t *vm, rb_nativethread_cond_t *cond, unsigned long msec)
212{
213 vm_cond_wait(vm, cond, msec);
214}
215
216static bool
217vm_barrier_finish_p(rb_vm_t *vm)
218{
219 RUBY_DEBUG_LOG("cnt:%u living:%u blocking:%u",
220 vm->ractor.sync.barrier_cnt,
221 vm->ractor.cnt,
222 vm->ractor.blocking_cnt);
223
224 VM_ASSERT(vm->ractor.blocking_cnt <= vm->ractor.cnt);
225 return vm->ractor.blocking_cnt == vm->ractor.cnt;
226}
227
228void
229rb_vm_barrier(void)
230{
231 RB_DEBUG_COUNTER_INC(vm_sync_barrier);
232
233 if (!rb_multi_ractor_p()) {
234 // no other ractors
235 return;
236 }
237 else {
238 rb_vm_t *vm = GET_VM();
239 VM_ASSERT(vm->ractor.sync.barrier_waiting == false);
240 ASSERT_vm_locking();
241
242 rb_ractor_t *cr = vm->ractor.sync.lock_owner;
243 VM_ASSERT(cr == GET_RACTOR());
244 VM_ASSERT(rb_ractor_status_p(cr, ractor_running));
245
246 vm->ractor.sync.barrier_waiting = true;
247
248 RUBY_DEBUG_LOG("barrier start. cnt:%u living:%u blocking:%u",
249 vm->ractor.sync.barrier_cnt,
250 vm->ractor.cnt,
251 vm->ractor.blocking_cnt);
252
253 rb_vm_ractor_blocking_cnt_inc(vm, cr, __FILE__, __LINE__);
254
255 // send signal
256 rb_ractor_t *r = 0;
257 list_for_each(&vm->ractor.set, r, vmlr_node) {
258 if (r != cr) {
259 rb_ractor_vm_barrier_interrupt_running_thread(r);
260 }
261 }
262
263 // wait
264 while (!vm_barrier_finish_p(vm)) {
265 rb_vm_cond_wait(vm, &vm->ractor.sync.barrier_cond);
266 }
267
268 RUBY_DEBUG_LOG("cnt:%u barrier success", vm->ractor.sync.barrier_cnt);
269
270 rb_vm_ractor_blocking_cnt_dec(vm, cr, __FILE__, __LINE__);
271
272 vm->ractor.sync.barrier_waiting = false;
273 vm->ractor.sync.barrier_cnt++;
274
275 list_for_each(&vm->ractor.set, r, vmlr_node) {
276 rb_native_cond_signal(&r->barrier_wait_cond);
277 }
278 }
279}
280
281void
282rb_ec_vm_lock_rec_release(const rb_execution_context_t *ec,
283 unsigned int recorded_lock_rec,
284 unsigned int current_lock_rec)
285{
286 VM_ASSERT(recorded_lock_rec != current_lock_rec);
287
288 if (UNLIKELY(recorded_lock_rec > current_lock_rec)) {
289 rb_bug("unexpected situation - recordd:%u current:%u",
290 recorded_lock_rec, current_lock_rec);
291 }
292 else {
293 while (recorded_lock_rec < current_lock_rec) {
294 RB_VM_LOCK_LEAVE_LEV(&current_lock_rec);
295 }
296 }
297
298 VM_ASSERT(recorded_lock_rec == rb_ec_vm_lock_rec(ec));
299}
RB_DEBUG_COUNTER_INC
#define RB_DEBUG_COUNTER_INC(type)
Definition: debug_counter.h:444
MJIT_FUNC_EXPORTED
#define MJIT_FUNC_EXPORTED
Definition: dllexport.h:55
UNLIKELY
#define UNLIKELY(x)
Definition: ffi_common.h:126
RB_GC_SAVE_MACHINE_CONTEXT
#define RB_GC_SAVE_MACHINE_CONTEXT(th)
Definition: gc.h:19
rb_bug
void rb_bug(const char *fmt,...)
Definition: error.c:768
gc.h
Internal header for GC.
rb_vm_ractor_blocking_cnt_dec
void rb_vm_ractor_blocking_cnt_dec(rb_vm_t *vm, rb_ractor_t *cr, const char *file, int line)
Definition: ractor.c:1798
rb_ractor_vm_barrier_interrupt_running_thread
void rb_ractor_vm_barrier_interrupt_running_thread(rb_ractor_t *r)
Definition: ractor.c:1892
rb_vm_ractor_blocking_cnt_inc
void rb_vm_ractor_blocking_cnt_inc(rb_vm_t *vm, rb_ractor_t *cr, const char *file, int line)
Definition: ractor.c:1790
ractor_core.h
NULL
#define NULL
Definition: regenc.h:69
file
Definition: gzappend.c:170
rb_execution_context_struct
Definition: vm_core.h:854
rb_ractor_struct
Definition: ractor_core.h:82
rb_ractor_struct::barrier_wait_cond
rb_nativethread_cond_t barrier_wait_cond
Definition: ractor_core.h:90
rb_ractor_struct::sync
struct rb_ractor_sync sync
Definition: ractor_core.h:85
rb_thread_cond_struct
Definition: thread_win32.h:24
rb_thread_struct
Definition: vm_core.h:935
rb_vm_struct
Definition: vm_core.h:564
rb_vm_struct::set
struct list_head set
Definition: vm_core.h:568
rb_vm_struct::barrier_waiting
bool barrier_waiting
Definition: vm_core.h:582
rb_vm_struct::sync
struct rb_vm_struct::@194::@197 sync
rb_vm_struct::barrier_cond
rb_nativethread_cond_t barrier_cond
Definition: vm_core.h:584
rb_vm_struct::barrier_cnt
unsigned int barrier_cnt
Definition: vm_core.h:583
rb_vm_struct::ractor
struct rb_vm_struct::@194 ractor
rb_vm_struct::lock
rb_nativethread_lock_t lock
Definition: vm_core.h:577
rb_vm_struct::lock_rec
unsigned int lock_rec
Definition: vm_core.h:579
rb_vm_struct::cnt
unsigned int cnt
Definition: vm_core.h:569
rb_vm_struct::blocking_cnt
unsigned int blocking_cnt
Definition: vm_core.h:570
rb_vm_struct::lock_owner
struct rb_ractor_struct * lock_owner
Definition: vm_core.h:578
rb_native_mutex_lock
void rb_native_mutex_lock(rb_nativethread_lock_t *lock)
rb_native_mutex_unlock
void rb_native_mutex_unlock(rb_nativethread_lock_t *lock)
rb_native_cond_signal
void rb_native_cond_signal(rb_nativethread_cond_t *cond)
rb_native_cond_wait
void rb_native_cond_wait(rb_nativethread_cond_t *cond, rb_nativethread_lock_t *mutex)
rb_native_cond_timedwait
void rb_native_cond_timedwait(rb_nativethread_cond_t *cond, rb_nativethread_lock_t *mutex, unsigned long msec)
vm_core.h
VM_ASSERT
#define VM_ASSERT(expr)
Definition: vm_core.h:61
vm_debug.h
RUBY_DEBUG_LOG2
#define RUBY_DEBUG_LOG2(file, line, fmt,...)
Definition: vm_debug.h:113
RUBY_DEBUG_LOG
#define RUBY_DEBUG_LOG(fmt,...)
Definition: vm_debug.h:112
rb_vm_unlock_body
void rb_vm_unlock_body(LOCATION_ARGS)
Definition: vm_sync.c:177
rb_vm_lock_enter_body
void rb_vm_lock_enter_body(unsigned int *lev APPEND_LOCATION_ARGS)
Definition: vm_sync.c:131
rb_vm_cond_wait
void rb_vm_cond_wait(rb_vm_t *vm, rb_nativethread_cond_t *cond)
Definition: vm_sync.c:205
rb_vm_lock_enter_body_nb
void rb_vm_lock_enter_body_nb(unsigned int *lev APPEND_LOCATION_ARGS)
Definition: vm_sync.c:143
rb_vm_lock_leave_body
void rb_vm_lock_leave_body(unsigned int *lev APPEND_LOCATION_ARGS)
Definition: vm_sync.c:162
rb_vm_lock_enter_body_cr
void rb_vm_lock_enter_body_cr(rb_ractor_t *cr, unsigned int *lev APPEND_LOCATION_ARGS)
Definition: vm_sync.c:155
rb_vm_barrier
void rb_vm_barrier(void)
Definition: vm_sync.c:229
rb_vm_cond_timedwait
void rb_vm_cond_timedwait(rb_vm_t *vm, rb_nativethread_cond_t *cond, unsigned long msec)
Definition: vm_sync.c:211
rb_vm_locked_p
bool rb_vm_locked_p(void)
Definition: vm_sync.c:36
rb_vm_lock_body
void rb_vm_lock_body(LOCATION_ARGS)
Definition: vm_sync.c:168
rb_ec_vm_lock_rec_release
void rb_ec_vm_lock_rec_release(const rb_execution_context_t *ec, unsigned int recorded_lock_rec, unsigned int current_lock_rec)
Definition: vm_sync.c:282
vm_sync.h
LOCATION_ARGS
#define LOCATION_ARGS
Definition: vm_sync.h:14
ASSERT_vm_locking
#define ASSERT_vm_locking()
Definition: vm_sync.h:134
APPEND_LOCATION_PARAMS
#define APPEND_LOCATION_PARAMS
Definition: vm_sync.h:17
ASSERT_vm_unlocking
#define ASSERT_vm_unlocking()
Definition: vm_sync.h:135
RB_VM_LOCK_LEAVE_LEV
#define RB_VM_LOCK_LEAVE_LEV(levp)
Definition: vm_sync.h:119
APPEND_LOCATION_ARGS
#define APPEND_LOCATION_ARGS
Definition: vm_sync.h:16