about summary refs log tree commit diff
path: root/src/thread/pthread_once.c
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2014-10-10 18:20:33 -0400
committerRich Felker <dalias@aerifal.cx>2014-10-10 18:21:31 -0400
commitdf37d3960abec482e17fad2274a99b790f6cc08b (patch)
treeff2d8efd637e564dd846a3c96a425ea483246734 /src/thread/pthread_once.c
parent867b1822f30a76cb9c8342da29eb28ed75908fa9 (diff)
downloadmusl-df37d3960abec482e17fad2274a99b790f6cc08b.tar.gz
musl-df37d3960abec482e17fad2274a99b790f6cc08b.tar.xz
musl-df37d3960abec482e17fad2274a99b790f6cc08b.zip
fix missing barrier in pthread_once/call_once shortcut path
these functions need to be fast when the init routine has already run,
since they may be called very often from code which depends on global
initialization having taken place. as such, a fast path bypassing
atomic cas on the once control object was used to avoid heavy memory
contention. however, on archs with weakly ordered memory, the fast
path failed to ensure that the caller actually observes the side
effects of the init routine.

preliminary performance testing showed that simply removing the fast
path was not practical; a performance drop of roughly 85x was observed
with 20 threads hammering the same once control on a 24-core machine.
so the new explicit barrier operation from atomic.h is used to retain
the fast path while ensuring memory visibility.

performance may be reduced on some archs where the barrier actually
makes a difference, but the previous behavior was unsafe and incorrect
on these archs. future improvements to the implementation of a_barrier
should reduce the impact.
Diffstat (limited to 'src/thread/pthread_once.c')
-rw-r--r--src/thread/pthread_once.c8
1 files changed, 6 insertions, 2 deletions
diff --git a/src/thread/pthread_once.c b/src/thread/pthread_once.c
index 05ebe69c..b7388b93 100644
--- a/src/thread/pthread_once.c
+++ b/src/thread/pthread_once.c
@@ -10,8 +10,12 @@ int __pthread_once(pthread_once_t *control, void (*init)(void))
 {
 	static int waiters;
 
-	/* Return immediately if init finished before */
-	if (*control == 2) return 0;
+	/* Return immediately if init finished before, but ensure that
+	 * effects of the init routine are visible to the caller. */
+	if (*control == 2) {
+		a_barrier();
+		return 0;
+	}
 
 	/* Try to enter initializing state. Three possibilities:
 	 *  0 - we're the first or the other cancelled; run init