1 #include "pthread_impl.h"
3 static void undo(void *control
)
9 int pthread_once(pthread_once_t
*control
, void (*init
)(void))
13 /* Return immediately if init finished before */
14 if (*control
== 2) return 0;
16 /* Try to enter initializing state. Three possibilities:
17 * 0 - we're the first or the other cancelled; run init
18 * 1 - another thread is running init; wait
19 * 2 - another thread finished running init; just return */
21 for (;;) switch (a_swap(control
, 1)) {
23 pthread_cleanup_push(undo
, control
);
25 pthread_cleanup_pop(0);
28 if (waiters
) __wake(control
, -1, 0);
31 __wait(control
, &waiters
, 1, 0);