From 8d81ba8c0bc6fe31136cb15c9c82ef4c24965040 Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Fri, 22 May 2020 17:45:47 -0400 Subject: restore lock-skipping for processes that return to single-threaded state the design used here relies on the barrier provided by the first lock operation after the process returns to single-threaded state to synchronize with actions by the last thread that exited. by storing the intent to change modes in the same object used to detect whether locking is needed, it's possible to avoid an extra (possibly costly) memory load after the lock is taken. --- src/thread/__lock.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/thread/__lock.c') diff --git a/src/thread/__lock.c b/src/thread/__lock.c index 5b9b144e..60eece49 100644 --- a/src/thread/__lock.c +++ b/src/thread/__lock.c @@ -18,9 +18,11 @@ void __lock(volatile int *l) { - if (!libc.threaded) return; + int need_locks = libc.need_locks; + if (!need_locks) return; /* fast path: INT_MIN for the lock, +1 for the congestion */ int current = a_cas(l, 0, INT_MIN + 1); + if (need_locks < 0) libc.need_locks = 0; if (!current) return; /* A first spin loop, for medium congestion. */ for (unsigned i = 0; i < 10; ++i) { -- cgit v1.2.1