Skip to content

Commit

Permalink
RW locking: Add a pair of re-entrant functions for readers
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
liviuchircu committed Jan 11, 2024
1 parent c0ac7cf commit f938704
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions rw_locking.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,34 @@ inline static void lock_destroy_rw(rw_lock_t *_lock)
lock_release((_lock)->lock); \
} while (0)

/* to be defined in each function making use of re-entrance */
#define DEFS_RW_LOCKING_R \
int __r_read_changed = 0;

/**
* Re-entrant versions of the reader start/stop functions.
* @_r_read_acq: a process-local global variable to test the re-entrance
* Note: these functions *cannot* be called in a nested fashion
* within the same function!
*/
#define lock_start_read_r(_lock, _r_read_acq) \
do { \
if (!(_r_read_acq)) { \
(_r_read_acq) = 1; \
__r_read_changed = 1; \
lock_start_read(_lock); \
} \
} while (0)

#define lock_stop_read_r(_lock, _r_read_acq) \
do { \
if (__r_read_changed) { \
lock_stop_read(_lock); \
__r_read_changed = 0; \
(_r_read_acq) = 0; \
} \
} while (0)

#define lock_stop_sw_read(_lock) \
do { \
lock_get((_lock)->lock); \
Expand Down

0 comments on commit f938704

Please sign in to comment.