5 #include "debug_counter.h"
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
13 #define LOCATION_ARGS void
14 #define LOCATION_PARAMS
15 #define APPEND_LOCATION_ARGS
16 #define APPEND_LOCATION_PARAMS
19 bool rb_vm_locked_p(void);
20 void rb_vm_lock_body(LOCATION_ARGS
);
21 void rb_vm_unlock_body(LOCATION_ARGS
);
23 struct rb_ractor_struct
;
24 void rb_vm_lock_enter_body_cr(struct rb_ractor_struct
*cr
, unsigned int *lev APPEND_LOCATION_ARGS
);
25 void rb_vm_lock_enter_body_nb(unsigned int *lev APPEND_LOCATION_ARGS
);
26 void rb_vm_lock_enter_body(unsigned int *lev APPEND_LOCATION_ARGS
);
27 void rb_vm_lock_leave_body(unsigned int *lev APPEND_LOCATION_ARGS
);
28 void rb_vm_barrier(void);
35 RUBY_EXTERN
struct rb_ractor_struct
*ruby_single_main_ractor
; // ractor.c
38 rb_multi_ractor_p(void)
40 if (LIKELY(ruby_single_main_ractor
)) {
42 RUBY_ASSERT(GET_VM()->ractor
.cnt
<= 1);
46 // multi-ractor mode can run ractor.cnt == 1
52 rb_vm_lock(const char *file
, int line
)
54 RB_DEBUG_COUNTER_INC(vm_sync_lock
);
56 if (rb_multi_ractor_p()) {
57 rb_vm_lock_body(LOCATION_PARAMS
);
62 rb_vm_unlock(const char *file
, int line
)
64 if (rb_multi_ractor_p()) {
65 rb_vm_unlock_body(LOCATION_PARAMS
);
70 rb_vm_lock_enter(unsigned int *lev
, const char *file
, int line
)
72 RB_DEBUG_COUNTER_INC(vm_sync_lock_enter
);
74 if (rb_multi_ractor_p()) {
75 rb_vm_lock_enter_body(lev APPEND_LOCATION_PARAMS
);
80 rb_vm_lock_enter_nb(unsigned int *lev
, const char *file
, int line
)
82 RB_DEBUG_COUNTER_INC(vm_sync_lock_enter_nb
);
84 if (rb_multi_ractor_p()) {
85 rb_vm_lock_enter_body_nb(lev APPEND_LOCATION_PARAMS
);
90 rb_vm_lock_leave(unsigned int *lev
, const char *file
, int line
)
92 if (rb_multi_ractor_p()) {
93 rb_vm_lock_leave_body(lev APPEND_LOCATION_PARAMS
);
98 rb_vm_lock_enter_cr(struct rb_ractor_struct
*cr
, unsigned int *levp
, const char *file
, int line
)
100 RB_DEBUG_COUNTER_INC(vm_sync_lock_enter_cr
);
101 rb_vm_lock_enter_body_cr(cr
, levp APPEND_LOCATION_PARAMS
);
105 rb_vm_lock_leave_cr(struct rb_ractor_struct
*cr
, unsigned int *levp
, const char *file
, int line
)
107 rb_vm_lock_leave_body(levp APPEND_LOCATION_PARAMS
);
110 #define RB_VM_LOCKED_P() rb_vm_locked_p()
112 #define RB_VM_LOCK() rb_vm_lock(__FILE__, __LINE__)
113 #define RB_VM_UNLOCK() rb_vm_unlock(__FILE__, __LINE__)
115 #define RB_VM_LOCK_ENTER_CR_LEV(cr, levp) rb_vm_lock_enter_cr(cr, levp, __FILE__, __LINE__)
116 #define RB_VM_LOCK_LEAVE_CR_LEV(cr, levp) rb_vm_lock_leave_cr(cr, levp, __FILE__, __LINE__)
117 #define RB_VM_LOCK_ENTER_LEV(levp) rb_vm_lock_enter(levp, __FILE__, __LINE__)
118 #define RB_VM_LOCK_LEAVE_LEV(levp) rb_vm_lock_leave(levp, __FILE__, __LINE__)
120 #define RB_VM_LOCK_ENTER() { unsigned int _lev; RB_VM_LOCK_ENTER_LEV(&_lev);
121 #define RB_VM_LOCK_LEAVE() RB_VM_LOCK_LEAVE_LEV(&_lev); }
123 #define RB_VM_LOCK_ENTER_LEV_NB(levp) rb_vm_lock_enter_nb(levp, __FILE__, __LINE__)
124 #define RB_VM_LOCK_ENTER_NO_BARRIER() { unsigned int _lev; RB_VM_LOCK_ENTER_LEV_NB(&_lev);
125 #define RB_VM_LOCK_LEAVE_NO_BARRIER() RB_VM_LOCK_LEAVE_LEV(&_lev); }
128 void RUBY_ASSERT_vm_locking(void);
129 void RUBY_ASSERT_vm_unlocking(void);
130 #define ASSERT_vm_locking() RUBY_ASSERT_vm_locking()
131 #define ASSERT_vm_unlocking() RUBY_ASSERT_vm_unlocking()
133 #define ASSERT_vm_locking()
134 #define ASSERT_vm_unlocking()
137 #endif // RUBY_VM_SYNC_H