Ruby: vm_sync.h Source File

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

vm_sync.h
Go to the documentation of this file.
1
2#ifndef RUBY_VM_SYNC_H
3#define RUBY_VM_SYNC_H
4
5#include "vm_debug.h"
6#include "debug_counter.h"
7
8#if USE_RUBY_DEBUG_LOG
9#define LOCATION_ARGS const char *file, int line
10#define LOCATION_PARAMS file, line
11#define APPEND_LOCATION_ARGS , const char *file, int line
12#define APPEND_LOCATION_PARAMS , file, line
13#else
14#define LOCATION_ARGS void
15#define LOCATION_PARAMS
16#define APPEND_LOCATION_ARGS
17#define APPEND_LOCATION_PARAMS
18#endif
19
20bool rb_vm_locked_p(void);
21void rb_vm_lock_body(LOCATION_ARGS);
22void rb_vm_unlock_body(LOCATION_ARGS);
23
24struct rb_ractor_struct;
25void rb_vm_lock_enter_body_cr(struct rb_ractor_struct *cr, unsigned int *lev APPEND_LOCATION_ARGS);
26void rb_vm_lock_enter_body_nb(unsigned int *lev APPEND_LOCATION_ARGS);
27void rb_vm_lock_enter_body(unsigned int *lev APPEND_LOCATION_ARGS);
28void rb_vm_lock_leave_body(unsigned int *lev APPEND_LOCATION_ARGS);
29void rb_vm_barrier(void);
30
31#if RUBY_DEBUG
32// GET_VM()
33#include "vm_core.h"
34#endif
35
36 RUBY_EXTERN struct rb_ractor_struct *ruby_single_main_ractor; // ractor.c
37
38static inline bool
39rb_multi_ractor_p(void)
40{
41 if (LIKELY(ruby_single_main_ractor)) {
42 // 0 on boot time.
43 RUBY_ASSERT(GET_VM()->ractor.cnt <= 1);
44 return false;
45 }
46 else {
47 // multi-ractor mode can run ractor.cnt == 1
48 return true;
49 }
50}
51
52static inline void
53rb_vm_lock(const char *file, int line)
54{
55 RB_DEBUG_COUNTER_INC(vm_sync_lock);
56
57 if (rb_multi_ractor_p()) {
58 rb_vm_lock_body(LOCATION_PARAMS);
59 }
60}
61
62static inline void
63rb_vm_unlock(const char *file, int line)
64{
65 if (rb_multi_ractor_p()) {
66 rb_vm_unlock_body(LOCATION_PARAMS);
67 }
68}
69
70static inline void
71rb_vm_lock_enter(unsigned int *lev, const char *file, int line)
72{
73 RB_DEBUG_COUNTER_INC(vm_sync_lock_enter);
74
75 if (rb_multi_ractor_p()) {
76 rb_vm_lock_enter_body(lev APPEND_LOCATION_PARAMS);
77 }
78}
79
80static inline void
81rb_vm_lock_enter_nb(unsigned int *lev, const char *file, int line)
82{
83 RB_DEBUG_COUNTER_INC(vm_sync_lock_enter_nb);
84
85 if (rb_multi_ractor_p()) {
86 rb_vm_lock_enter_body_nb(lev APPEND_LOCATION_PARAMS);
87 }
88}
89
90static inline void
91rb_vm_lock_leave(unsigned int *lev, const char *file, int line)
92{
93 if (rb_multi_ractor_p()) {
94 rb_vm_lock_leave_body(lev APPEND_LOCATION_PARAMS);
95 }
96}
97
98static inline void
99rb_vm_lock_enter_cr(struct rb_ractor_struct *cr, unsigned int *levp, const char *file, int line)
100{
101 RB_DEBUG_COUNTER_INC(vm_sync_lock_enter_cr);
102 rb_vm_lock_enter_body_cr(cr, levp APPEND_LOCATION_PARAMS);
103}
104
105static inline void
106rb_vm_lock_leave_cr(struct rb_ractor_struct *cr, unsigned int *levp, const char *file, int line)
107{
108 rb_vm_lock_leave_body(levp APPEND_LOCATION_PARAMS);
109}
110
111#define RB_VM_LOCKED_P() rb_vm_locked_p()
112
113#define RB_VM_LOCK() rb_vm_lock(__FILE__, __LINE__)
114#define RB_VM_UNLOCK() rb_vm_unlock(__FILE__, __LINE__)
115
116#define RB_VM_LOCK_ENTER_CR_LEV(cr, levp) rb_vm_lock_enter_cr(cr, levp, __FILE__, __LINE__)
117#define RB_VM_LOCK_LEAVE_CR_LEV(cr, levp) rb_vm_lock_leave_cr(cr, levp, __FILE__, __LINE__)
118#define RB_VM_LOCK_ENTER_LEV(levp) rb_vm_lock_enter(levp, __FILE__, __LINE__)
119#define RB_VM_LOCK_LEAVE_LEV(levp) rb_vm_lock_leave(levp, __FILE__, __LINE__)
120
121#define RB_VM_LOCK_ENTER() { unsigned int _lev; RB_VM_LOCK_ENTER_LEV(&_lev);
122#define RB_VM_LOCK_LEAVE() RB_VM_LOCK_LEAVE_LEV(&_lev); }
123
124#define RB_VM_LOCK_ENTER_LEV_NB(levp) rb_vm_lock_enter_nb(levp, __FILE__, __LINE__)
125#define RB_VM_LOCK_ENTER_NO_BARRIER() { unsigned int _lev; RB_VM_LOCK_ENTER_LEV_NB(&_lev);
126#define RB_VM_LOCK_LEAVE_NO_BARRIER() RB_VM_LOCK_LEAVE_LEV(&_lev); }
127
128#if RUBY_DEBUG > 0
129void RUBY_ASSERT_vm_locking(void);
130void RUBY_ASSERT_vm_unlocking(void);
131#define ASSERT_vm_locking() RUBY_ASSERT_vm_locking()
132#define ASSERT_vm_unlocking() RUBY_ASSERT_vm_unlocking()
133#else
134#define ASSERT_vm_locking()
135#define ASSERT_vm_unlocking()
136#endif
137
138#endif // RUBY_VM_SYNC_H
RUBY_ASSERT
#define RUBY_ASSERT(expr)
Asserts that the given expression is truthy iff RUBY_DEBUG is truthy.
Definition: assert.h:177
debug_counter.h
RB_DEBUG_COUNTER_INC
#define RB_DEBUG_COUNTER_INC(type)
Definition: debug_counter.h:444
RUBY_EXTERN
#define RUBY_EXTERN
Definition: dllexport.h:36
LIKELY
#define LIKELY(x)
Definition: ffi_common.h:125
file
Definition: gzappend.c:170
rb_ractor_struct
Definition: ractor_core.h:82
vm_core.h
vm_debug.h
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_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
LOCATION_ARGS
#define LOCATION_ARGS
Definition: vm_sync.h:14
LOCATION_PARAMS
#define LOCATION_PARAMS
Definition: vm_sync.h:15
rb_vm_barrier
void rb_vm_barrier(void)
Definition: vm_sync.c:229
rb_vm_locked_p
bool rb_vm_locked_p(void)
Definition: vm_sync.c:36
APPEND_LOCATION_PARAMS
#define APPEND_LOCATION_PARAMS
Definition: vm_sync.h:17
rb_vm_lock_body
void rb_vm_lock_body(LOCATION_ARGS)
Definition: vm_sync.c:168
ruby_single_main_ractor
struct rb_ractor_struct * ruby_single_main_ractor
Definition: vm.c:381
rb_vm_lock_enter_body_cr
void rb_vm_lock_enter_body_cr(struct rb_ractor_struct *cr, unsigned int *lev APPEND_LOCATION_ARGS)
Definition: vm_sync.c:155
APPEND_LOCATION_ARGS
#define APPEND_LOCATION_ARGS
Definition: vm_sync.h:16