RW_SPINLOCK: recursive readers support (#15217)
readers should be able to recursively acquire the lock, even when there is a writer waiting
Costa Tsaousis committed
Jun 20, 2023 at 11:21 UTC
554855bd5c7c1d02e87a2cd5e23f3084af6607f6
1 file changed
+12
-5
libnetdata/locks/locks.c
+12
-5
@@ -378,13 +378,20 @@ void rw_spinlock_read_unlock(RW_SPINLOCK *rw_spinlock) {
378
void rw_spinlock_write_lock(RW_SPINLOCK *rw_spinlock) {
379
static const struct timespec ns = { .tv_sec = 0, .tv_nsec = 1 };
380
381
- spinlock_lock(&rw_spinlock->spinlock);
382
- size_t count = 0;
383
- while (__atomic_load_n(&rw_spinlock->readers, __ATOMIC_RELAXED) > 0) {
381
+ size_t spins = 0;
382
+ while(1) {
383
+ spins++;
384
+ spinlock_lock(&rw_spinlock->spinlock);
385
+
386
+ if(__atomic_load_n(&rw_spinlock->readers, __ATOMIC_RELAXED) == 0)
387
+ break;
388
+
389
// Busy wait until all readers have released their locks.
385
- if(++count > 1000)
386
- nanosleep(&ns, NULL);
390
+ spinlock_unlock(&rw_spinlock->spinlock);
391
+ nanosleep(&ns, NULL);
392
}
393
+
394
+ (void)spins;
395
}
396
397
void rw_spinlock_write_unlock(RW_SPINLOCK *rw_spinlock) {