1 #include "pthread_impl.h"
3 static void undo(void *control)
5 /* Wake all waiters, since the waiter status is lost when
6 * resetting control to the initial state. */
7 if (a_swap(control, 0) == 3)
8 __wake(control, -1, 1);
11 int __pthread_once(pthread_once_t *control, void (*init)(void))
13 /* Return immediately if init finished before, but ensure that
14 * effects of the init routine are visible to the caller. */
20 /* Try to enter initializing state. Four possibilities:
21 * 0 - we're the first or the other cancelled; run init
22 * 1 - another thread is running init; wait
23 * 2 - another thread finished running init; just return
24 * 3 - another thread is running init, waiters present; wait */
26 for (;;) switch (a_cas(control, 0, 1)) {
28 pthread_cleanup_push(undo, control);
30 pthread_cleanup_pop(0);
32 if (a_swap(control, 2) == 3)
33 __wake(control, -1, 1);
36 /* If this fails, so will __wait. */
39 __wait(control, 0, 3, 1);
46 weak_alias(__pthread_once, pthread_once);