From f5fb20b0e934770c37093105524ea644dcaba5e2 Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Mon, 25 Aug 2014 20:24:07 -0400 Subject: refrain from spinning on locks when there is already a waiter if there is already a waiter for a lock, spinning on the lock is essentially an attempt to steal it from whichever waiter would obtain it via any priority rules in place, and is therefore undesirable. in the current implementation, there is always an inherent race window at unlock during which a newly-arriving thread may steal the lock from the existing waiters, but we should aim to keep this window minimal rather than enlarging it. --- src/thread/sem_timedwait.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/thread/sem_timedwait.c') diff --git a/src/thread/sem_timedwait.c b/src/thread/sem_timedwait.c index df5f3a6c..b5a60add 100644 --- a/src/thread/sem_timedwait.c +++ b/src/thread/sem_timedwait.c @@ -11,7 +11,7 @@ int sem_timedwait(sem_t *restrict sem, const struct timespec *restrict at) if (!sem_trywait(sem)) return 0; int spins = 100; - while (spins-- && sem->__val[0] <= 0) a_spin(); + while (spins-- && sem->__val[0] <= 0 && !sem->__val[1]) a_spin(); while (sem_trywait(sem)) { int r; -- cgit v1.2.1