Avoid pthread_rwlock_wrlock() starvation

This commit is contained in:
Justine Tunney 2024-12-24 10:30:11 -08:00
parent 55b7aa1632
commit ec2db4e40e
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
3 changed files with 6 additions and 0 deletions

View file

@ -42,8 +42,10 @@ errno_t pthread_rwlock_wrlock(pthread_rwlock_t *rwlock) {
if (atomic_compare_exchange_weak_explicit(
&rwlock->_word, &w, 1, memory_order_acquire, memory_order_relaxed))
return 0;
atomic_fetch_add(&rwlock->_waiters, 1);
for (;;)
if (!(w = atomic_load_explicit(&rwlock->_word, memory_order_relaxed)))
break;
atomic_fetch_sub(&rwlock->_waiters, 1);
}
}