Skip to content

Commit

Permalink
readers should be able to recursively acquire the lock, even when the…
Browse files Browse the repository at this point in the history
…re is a writer waiting
  • Loading branch information
ktsaou committed Jun 20, 2023
1 parent 43c749b commit c90998d
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions libnetdata/locks/locks.c
Original file line number Diff line number Diff line change
Expand Up @@ -378,13 +378,20 @@ void rw_spinlock_read_unlock(RW_SPINLOCK *rw_spinlock) {
void rw_spinlock_write_lock(RW_SPINLOCK *rw_spinlock) {
static const struct timespec ns = { .tv_sec = 0, .tv_nsec = 1 };

spinlock_lock(&rw_spinlock->spinlock);
size_t count = 0;
while (__atomic_load_n(&rw_spinlock->readers, __ATOMIC_RELAXED) > 0) {
size_t spins = 0;
while(1) {
spins++;
spinlock_lock(&rw_spinlock->spinlock);

if(__atomic_load_n(&rw_spinlock->readers, __ATOMIC_RELAXED) == 0)
break;

// Busy wait until all readers have released their locks.
if(++count > 1000)
nanosleep(&ns, NULL);
spinlock_unlock(&rw_spinlock->spinlock);
nanosleep(&ns, NULL);
}

(void)spins;
}

void rw_spinlock_write_unlock(RW_SPINLOCK *rw_spinlock) {
Expand Down

0 comments on commit c90998d

Please sign in to comment.