fix missing barrier in pthread_once/call_once shortcut path
authorRich Felker <dalias@aerifal.cx>
Fri, 10 Oct 2014 22:20:33 +0000 (18:20 -0400)
committerRich Felker <dalias@aerifal.cx>
Mon, 30 Mar 2015 05:34:13 +0000 (01:34 -0400)
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.

(cherry picked from commit df37d3960abec482e17fad2274a99b790f6cc08b)

(edited not to depend on a_barrier, which is not available in 1.0.x)

src/thread/pthread_once.c

index e01f6d481cef5ef607675a76954d2864379782d0..7b9ccf252de67c620110fa55524bc4607a640f59 100644 (file)
@@ -10,8 +10,13 @@ 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) {
+               /* Otherwise-useless cas just to get a barrier. */
+               a_cas(&(int){0},0,0);
+               return 0;
+       }
 
        /* Try to enter initializing state. Three possibilities:
         *  0 - we're the first or the other cancelled; run init