Skip to content

Commit 4031beb

Browse files
authored
Add documentation for RUBY_ASSERT_CRITICAL_SECTION. (ruby#11982)
1 parent c7708d2 commit 4031beb

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

vm_core.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,30 @@
7070
#define RUBY_ASSERT_MUTEX_OWNED(mutex) VM_ASSERT(rb_mutex_owned_p(mutex))
7171

7272
#if defined(RUBY_ASSERT_CRITICAL_SECTION)
73-
// TODO add documentation
73+
/*
74+
# Critical Section Assertions
75+
76+
These assertions are used to ensure that context switching does not occur between two points in the code. In theory,
77+
such code should already be protected by a mutex, but these assertions are used to ensure that the mutex is held.
78+
79+
The specific case where it can be useful is where a mutex is held further up the call stack, and the code in question
80+
may not directly hold the mutex. In this case, the critical section assertions can be used to ensure that the mutex is
81+
held by someone else.
82+
83+
These assertions are only enabled when RUBY_ASSERT_CRITICAL_SECTION is defined, which is only defined if VM_CHECK_MODE
84+
is set.
85+
86+
## Example Usage
87+
88+
```c
89+
RUBY_ASSERT_CRITICAL_SECTION_ENTER();
90+
// ... some code which does not invoke rb_vm_check_ints() ...
91+
RUBY_ASSERT_CRITICAL_SECTION_LEAVE();
92+
```
93+
94+
If `rb_vm_check_ints()` is called between the `RUBY_ASSERT_CRITICAL_SECTION_ENTER()` and
95+
`RUBY_ASSERT_CRITICAL_SECTION_LEAVE()`, a failed assertion will result.
96+
*/
7497
extern int ruby_assert_critical_section_entered;
7598
#define RUBY_ASSERT_CRITICAL_SECTION_ENTER() do{ruby_assert_critical_section_entered += 1;}while(false)
7699
#define RUBY_ASSERT_CRITICAL_SECTION_LEAVE() do{VM_ASSERT(ruby_assert_critical_section_entered > 0);ruby_assert_critical_section_entered -= 1;}while(false)

0 commit comments

Comments
 (0)