minor locking optimizations
[musl] / src / thread / pthread_once.c
1 #include "pthread_impl.h"
2
3 static void undo(void *control)
4 {
5         a_store(control, 0);
6         __wake(control, 1, 0);
7 }
8
9 int pthread_once(pthread_once_t *control, void (*init)(void))
10 {
11         static int waiters;
12
13         /* Return immediately if init finished before */
14         if (*control == 2) return 0;
15
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 */
20
21         for (;;) switch (a_swap(control, 1)) {
22         case 0:
23                 pthread_cleanup_push(undo, control);
24                 init();
25                 pthread_cleanup_pop(0);
26
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;
36         }
37 }