diff options
author | Rich Felker <dalias@aerifal.cx> | 2012-07-24 00:51:36 -0400 |
---|---|---|
committer | Rich Felker <dalias@aerifal.cx> | 2012-07-24 00:51:36 -0400 |
commit | 07e62953c768f9f09485388c22ffaed98d11d676 (patch) | |
tree | 5590ab7130d72e33cd5ef3be1659ea18367121e8 /src | |
parent | 5d26d5d15bf6c2fc8c0e58b76fb418b51e207af7 (diff) | |
download | musl-07e62953c768f9f09485388c22ffaed98d11d676.tar.gz musl-07e62953c768f9f09485388c22ffaed98d11d676.tar.bz2 musl-07e62953c768f9f09485388c22ffaed98d11d676.tar.xz musl-07e62953c768f9f09485388c22ffaed98d11d676.zip |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/thread/sem_trywait.c | 4 |
1 files changed, 2 insertions, 2 deletions
diff --git a/src/thread/sem_trywait.c b/src/thread/sem_trywait.c index 55d90075..04edf46b 100644 --- a/src/thread/sem_trywait.c +++ b/src/thread/sem_trywait.c @@ -3,8 +3,8 @@ int sem_trywait(sem_t *sem) { - int val = sem->__val[0]; - if (val>0) { + int val; + while ((val=sem->__val[0]) > 0) { int new = val-1-(val==1 && sem->__val[1]); if (a_cas(sem->__val, val, new)==val) return 0; } |