Ruby: vm_sync.h Source File

Ruby 4.0.5p0 (2026-05-20 revision 64336ffd0ee9e1f4c05891695a3d7b49cb709721)

vm_sync.h
1#ifndef RUBY_VM_SYNC_H
2#define RUBY_VM_SYNC_H
3
4#include "vm_debug.h"
5#include "debug_counter.h"
6
7#if USE_RUBY_DEBUG_LOG
8#define LOCATION_ARGS const char *file, int line
9#define LOCATION_PARAMS file, line
10#define APPEND_LOCATION_ARGS , const char *file, int line
11#define APPEND_LOCATION_PARAMS , file, line
12#else
13#define LOCATION_ARGS void
14#define LOCATION_PARAMS
15#define APPEND_LOCATION_ARGS
16#define APPEND_LOCATION_PARAMS
17#endif
18
19bool rb_vm_locked_p(void);
20void rb_vm_lock_body(LOCATION_ARGS);
21void rb_vm_unlock_body(LOCATION_ARGS);
22
23struct rb_ractor_struct;
24NOINLINE(void rb_vm_lock_enter_body_cr(struct rb_ractor_struct *cr, unsigned int *lev APPEND_LOCATION_ARGS));
25NOINLINE(void rb_vm_lock_enter_body_nb(unsigned int *lev APPEND_LOCATION_ARGS));
26NOINLINE(void rb_vm_lock_enter_body(unsigned int *lev APPEND_LOCATION_ARGS));
27void rb_vm_lock_leave_body_nb(unsigned int *lev APPEND_LOCATION_ARGS);
28void rb_vm_lock_leave_body(unsigned int *lev APPEND_LOCATION_ARGS);
29void rb_vm_barrier(void);
30
31RUBY_SYMBOL_EXPORT_BEGIN
32 VALUE rb_vm_lock_with_barrier(VALUE (*func)(void *args), void *args);
33RUBY_SYMBOL_EXPORT_END
34
35#if RUBY_DEBUG
36// GET_VM()
37#include "vm_core.h"
38#endif
39
40 RUBY_EXTERN struct rb_ractor_struct *ruby_single_main_ractor; // ractor.c
41
42static inline bool
43rb_multi_ractor_p(void)
44{
45 if (LIKELY(ruby_single_main_ractor)) {
46 // 0 on boot time.
47 RUBY_ASSERT(GET_VM()->ractor.cnt <= 1);
48 return false;
49 }
50 else {
51 // multi-ractor mode can run ractor.cnt == 1
52 return true;
53 }
54}
55
56static inline void
57rb_vm_lock(const char *file, int line)
58{
59 RB_DEBUG_COUNTER_INC(vm_sync_lock);
60
61 if (rb_multi_ractor_p()) {
62 rb_vm_lock_body(LOCATION_PARAMS);
63 }
64}
65
66static inline void
67rb_vm_unlock(const char *file, int line)
68{
69 if (rb_multi_ractor_p()) {
70 rb_vm_unlock_body(LOCATION_PARAMS);
71 }
72}
73
74static inline void
75rb_vm_lock_enter(unsigned int *lev, const char *file, int line)
76{
77 RB_DEBUG_COUNTER_INC(vm_sync_lock_enter);
78
79 if (rb_multi_ractor_p()) {
80 rb_vm_lock_enter_body(lev APPEND_LOCATION_PARAMS);
81 }
82}
83
84static inline void
85rb_vm_lock_enter_nb(unsigned int *lev, const char *file, int line)
86{
87 RB_DEBUG_COUNTER_INC(vm_sync_lock_enter_nb);
88
89 if (rb_multi_ractor_p()) {
90 rb_vm_lock_enter_body_nb(lev APPEND_LOCATION_PARAMS);
91 }
92}
93
94static inline void
95rb_vm_lock_leave_nb(unsigned int *lev, const char *file, int line)
96{
97 if (rb_multi_ractor_p()) {
98 rb_vm_lock_leave_body_nb(lev APPEND_LOCATION_PARAMS);
99 }
100}
101
102static inline void
103rb_vm_lock_leave(unsigned int *lev, const char *file, int line)
104{
105 if (rb_multi_ractor_p()) {
106 rb_vm_lock_leave_body(lev APPEND_LOCATION_PARAMS);
107 }
108}
109
110static inline void
111rb_vm_lock_enter_cr(struct rb_ractor_struct *cr, unsigned int *levp, const char *file, int line)
112{
113 RB_DEBUG_COUNTER_INC(vm_sync_lock_enter_cr);
114 rb_vm_lock_enter_body_cr(cr, levp APPEND_LOCATION_PARAMS);
115}
116
117static inline void
118rb_vm_lock_leave_cr(struct rb_ractor_struct *cr, unsigned int *levp, const char *file, int line)
119{
120 rb_vm_lock_leave_body(levp APPEND_LOCATION_PARAMS);
121}
122
123#define RB_VM_LOCKED_P() rb_vm_locked_p()
124
125#define RB_VM_LOCK() rb_vm_lock(__FILE__, __LINE__)
126#define RB_VM_UNLOCK() rb_vm_unlock(__FILE__, __LINE__)
127
128#define RB_VM_LOCK_ENTER_CR_LEV(cr, levp) rb_vm_lock_enter_cr(cr, levp, __FILE__, __LINE__)
129#define RB_VM_LOCK_LEAVE_CR_LEV(cr, levp) rb_vm_lock_leave_cr(cr, levp, __FILE__, __LINE__)
130#define RB_VM_LOCK_ENTER_LEV(levp) rb_vm_lock_enter(levp, __FILE__, __LINE__)
131#define RB_VM_LOCK_LEAVE_LEV(levp) rb_vm_lock_leave(levp, __FILE__, __LINE__)
132
133#define RB_VM_LOCK_ENTER() { unsigned int _lev; RB_VM_LOCK_ENTER_LEV(&_lev);
134#define RB_VM_LOCK_LEAVE() RB_VM_LOCK_LEAVE_LEV(&_lev); }
135#define RB_VM_LOCKING() \
136 for (unsigned int vm_locking_level, vm_locking_do = (RB_VM_LOCK_ENTER_LEV(&vm_locking_level), 1); \
137 vm_locking_do; RB_VM_LOCK_LEAVE_LEV(&vm_locking_level), vm_locking_do = 0)
138
139#define RB_VM_LOCK_ENTER_LEV_NB(levp) rb_vm_lock_enter_nb(levp, __FILE__, __LINE__)
140#define RB_VM_LOCK_LEAVE_LEV_NB(levp) rb_vm_lock_leave_nb(levp, __FILE__, __LINE__)
141#define RB_VM_LOCK_ENTER_NO_BARRIER() { unsigned int _lev; RB_VM_LOCK_ENTER_LEV_NB(&_lev);
142#define RB_VM_LOCK_LEAVE_NO_BARRIER() RB_VM_LOCK_LEAVE_LEV_NB(&_lev); }
143#define RB_VM_LOCKING_NO_BARRIER() \
144 for (unsigned int vm_locking_level, vm_locking_do = (RB_VM_LOCK_ENTER_LEV_NB(&vm_locking_level), 1); \
145 vm_locking_do; RB_VM_LOCK_LEAVE_LEV_NB(&vm_locking_level), vm_locking_do = 0)
146
147#if RUBY_DEBUG > 0
148void RUBY_ASSERT_vm_locking(void);
149void RUBY_ASSERT_vm_locking_with_barrier(void);
150void RUBY_ASSERT_vm_unlocking(void);
151#define ASSERT_vm_locking() RUBY_ASSERT_vm_locking()
152#define ASSERT_vm_locking_with_barrier() RUBY_ASSERT_vm_locking_with_barrier()
153#define ASSERT_vm_unlocking() RUBY_ASSERT_vm_unlocking()
154#else
155#define ASSERT_vm_locking()
156#define ASSERT_vm_locking_with_barrier()
157#define ASSERT_vm_unlocking()
158#endif
159
160#endif // RUBY_VM_SYNC_H
RUBY_ASSERT
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
RUBY_EXTERN
#define RUBY_EXTERN
Declaration of externally visible global variables.
Definition dllexport.h:45
rb_ractor_struct
Definition ractor_core.h:68
VALUE
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40