add legacy getloadavg api
[musl.git] / src / thread / pthread_once.c
blob41872f16cb47462845c62d70417e2143448fdfa5
1 #include "pthread_impl.h"
3 static void undo(void *control)
5 a_store(control, 0);
6 __wake(control, 1, 0);
9 int pthread_once(pthread_once_t *control, void (*init)(void))
11 static int waiters;
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)) {
22 case 0:
23 pthread_cleanup_push(undo, control);
24 init();
25 pthread_cleanup_pop(0);
27 a_store(control, 2);
28 if (waiters) __wake(control, -1, 0);
29 return 0;
30 case 1:
31 __wait(control, &waiters, 1, 0);
32 continue;
33 case 2:
34 a_store(control, 2);
35 return 0;