mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-04-16 11:54:58 +00:00
Avoid pthread_rwlock_wrlock() starvation
This commit is contained in:
parent
55b7aa1632
commit
ec2db4e40e
3 changed files with 6 additions and 0 deletions
|
@ -42,6 +42,9 @@ errno_t pthread_rwlock_rdlock(pthread_rwlock_t *lk) {
|
||||||
for (;;)
|
for (;;)
|
||||||
if (~(w = atomic_load_explicit(&lk->_word, memory_order_relaxed)) & 1)
|
if (~(w = atomic_load_explicit(&lk->_word, memory_order_relaxed)) & 1)
|
||||||
break;
|
break;
|
||||||
|
// xxx: avoid writer starvation in pthread_rwlock_rdlock_test
|
||||||
|
while (atomic_load(&lk->_waiters))
|
||||||
|
pthread_yield_np();
|
||||||
if (atomic_compare_exchange_weak_explicit(
|
if (atomic_compare_exchange_weak_explicit(
|
||||||
&lk->_word, &w, w + 2, memory_order_acquire, memory_order_relaxed))
|
&lk->_word, &w, w + 2, memory_order_acquire, memory_order_relaxed))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -42,8 +42,10 @@ errno_t pthread_rwlock_wrlock(pthread_rwlock_t *rwlock) {
|
||||||
if (atomic_compare_exchange_weak_explicit(
|
if (atomic_compare_exchange_weak_explicit(
|
||||||
&rwlock->_word, &w, 1, memory_order_acquire, memory_order_relaxed))
|
&rwlock->_word, &w, 1, memory_order_acquire, memory_order_relaxed))
|
||||||
return 0;
|
return 0;
|
||||||
|
atomic_fetch_add(&rwlock->_waiters, 1);
|
||||||
for (;;)
|
for (;;)
|
||||||
if (!(w = atomic_load_explicit(&rwlock->_word, memory_order_relaxed)))
|
if (!(w = atomic_load_explicit(&rwlock->_word, memory_order_relaxed)))
|
||||||
break;
|
break;
|
||||||
|
atomic_fetch_sub(&rwlock->_waiters, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -111,6 +111,7 @@ typedef struct pthread_rwlock_s {
|
||||||
char _pshared;
|
char _pshared;
|
||||||
char _iswrite;
|
char _iswrite;
|
||||||
_PTHREAD_ATOMIC(uint32_t) _word;
|
_PTHREAD_ATOMIC(uint32_t) _word;
|
||||||
|
_PTHREAD_ATOMIC(uint32_t) _waiters;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
} pthread_rwlock_t;
|
} pthread_rwlock_t;
|
||||||
|
|
Loading…
Add table
Reference in a new issue