about summary refs log tree commit diff
path: root/src/thread/__lock.c
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2012-04-24 13:55:06 -0400
committerRich Felker <dalias@aerifal.cx>2012-04-24 13:55:06 -0400
commite7655ed37bc9c2d79d921af4f287ee5cf2788661 (patch)
treeb21322ca5850ec44a9be633956a280f6786ab9de /src/thread/__lock.c
parentf34d0ea511e552851c8c6148fb113816f41e6759 (diff)
downloadmusl-e7655ed37bc9c2d79d921af4f287ee5cf2788661.tar.gz
musl-e7655ed37bc9c2d79d921af4f287ee5cf2788661.tar.xz
musl-e7655ed37bc9c2d79d921af4f287ee5cf2788661.zip
internal locks: new owner of contended lock must set waiters flag
this bug probably would have gone unnoticed since it's only used in
the fallback code for systems where priority-inheritance locking
fails. unfortunately this approach results in one spurious wake
syscall on the final unlock, when there are no waiters remaining. the
alternative (possibly better) would be to use broadcast wakes instead
of reflagging the waiter unconditionally, and let each waiter reflag
itself; this saves one syscall at the expense of invoking the
"thundering herd" effect (worse performance degredation) when there
are many waiters.

ideally we would be able to update all of our locks to use an array of
two ints rather than a single int, and use a separate counter system
like proper mutexes use; then we could avoid all spurious wake calls
without resorting to broadcasts. however, it's not clear to me that
priority inheritance futexes support this usage. the kernel sets the
waiters flag for them (just like we're doing now) and i can't tell if
it's safe to bypass the kernel when unlocking just because we know
(from private data, the waiter count) that there are no waiters. this
is something that could be explored in the future.
Diffstat (limited to 'src/thread/__lock.c')
-rw-r--r--src/thread/__lock.c2
1 files changed, 1 insertions, 1 deletions
diff --git a/src/thread/__lock.c b/src/thread/__lock.c
index d1734096..5ba5dc5e 100644
--- a/src/thread/__lock.c
+++ b/src/thread/__lock.c
@@ -4,7 +4,7 @@ void __lock_2(volatile int *l)
 {
 	if (!__syscall(SYS_futex, l, FUTEX_LOCK_PI, 0, 0))
 		return;
-	int old, tid = __pthread_self()->tid;
+	int old, tid = __pthread_self()->tid|INT_MIN;
 	while ((old = a_cas(l, 0, tid))) {
 		a_cas(l, old, old|INT_MIN);
 		__syscall(SYS_futex, l, FUTEX_WAIT, old|INT_MIN, 0);