initial check-in, version 0.5.0
[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                 break;
24         case 1:
25                 __wait(control, &waiters, 1, 0);
26                 continue;
27         case 2:
28                 a_store(control, 2);
29                 return 0;
30         }
31
32         pthread_cleanup_push(undo, control);
33         init();
34         pthread_cleanup_pop(0);
35
36         if (waiters) __wake(control, -1, 0);
37         return 0;
38 }