mirror of
https://github.com/fluencelabs/musl
synced 2025-06-30 15:11:55 +00:00
retry on cas failures in sem_trywait
this seems counter-intuitive since sem_trywait is supposed to just try once, not wait for the semaphore. however, the retry loop is not a wait. instead, it's to handle the case where the value changes due to a simultaneous post or wait from another thread while the semaphore value remains positive. in such a case, it's absolutely wrong for sem_trywait to fail with EAGAIN because the semaphore is not busy.
This commit is contained in:
@ -3,8 +3,8 @@
|
|||||||
|
|
||||||
int sem_trywait(sem_t *sem)
|
int sem_trywait(sem_t *sem)
|
||||||
{
|
{
|
||||||
int val = sem->__val[0];
|
int val;
|
||||||
if (val>0) {
|
while ((val=sem->__val[0]) > 0) {
|
||||||
int new = val-1-(val==1 && sem->__val[1]);
|
int new = val-1-(val==1 && sem->__val[1]);
|
||||||
if (a_cas(sem->__val, val, new)==val) return 0;
|
if (a_cas(sem->__val, val, new)==val) return 0;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user