Skip to content

Commit 3377918

Browse files
committed
RW locking: Add a pair of re-entrant functions for readers
In some cases, the same lock_start_read() function could be reached multiple times in a nested fashion, e.g. after running a callback which returns the control flow to the same module through an API call done by the module which installed the callback.
1 parent fd89470 commit 3377918

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

rw_locking.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,34 @@ inline static void lock_destroy_rw(rw_lock_t *_lock)
133133
lock_release((_lock)->lock); \
134134
} while (0)
135135

136+
/* to be defined in each function making use of re-entrance */
137+
#define DEFS_RW_LOCKING_R \
138+
int __r_read_changed = 0;
139+
140+
/**
141+
* Re-entrant versions of the reader start/stop functions.
142+
* @_r_read_acq: a process-local global variable to test the re-entrance
143+
* Note: these functions *cannot* be called in a nested fashion
144+
* within the same function!
145+
*/
146+
#define lock_start_read_r(_lock, _r_read_acq) \
147+
do { \
148+
if (!(_r_read_acq)) { \
149+
(_r_read_acq) = 1; \
150+
__r_read_changed = 1; \
151+
lock_start_read(_lock); \
152+
} \
153+
} while (0)
154+
155+
#define lock_stop_read_r(_lock, _r_read_acq) \
156+
do { \
157+
if (__r_read_changed) { \
158+
lock_stop_read(_lock); \
159+
__r_read_changed = 0; \
160+
(_r_read_acq) = 0; \
161+
} \
162+
} while (0)
163+
136164
#define lock_stop_sw_read(_lock) \
137165
do { \
138166
lock_get((_lock)->lock); \

0 commit comments

Comments
 (0)