Files
musl/src/thread/pthread_once.c

47 lines
1.1 KiB
C
Raw Normal View History

2011-02-12 00:22:29 -05:00
#include "pthread_impl.h"
static void undo(void *control)
{
/* Wake all waiters, since the waiter status is lost when
* resetting control to the initial state. */
if (a_swap(control, 0) == 3)
__wake(control, -1, 1);
2011-02-12 00:22:29 -05:00
}
int __pthread_once(pthread_once_t *control, void (*init)(void))
2011-02-12 00:22:29 -05:00
{
/* Return immediately if init finished before, but ensure that
* effects of the init routine are visible to the caller. */
if (*control == 2) {
a_barrier();
return 0;
}
2011-02-12 00:22:29 -05:00
/* Try to enter initializing state. Four possibilities:
2011-02-12 00:22:29 -05:00
* 0 - we're the first or the other cancelled; run init
* 1 - another thread is running init; wait
* 2 - another thread finished running init; just return
* 3 - another thread is running init, waiters present; wait */
2011-02-12 00:22:29 -05:00
for (;;) switch (a_cas(control, 0, 1)) {
2011-02-12 00:22:29 -05:00
case 0:
pthread_cleanup_push(undo, control);
init();
pthread_cleanup_pop(0);
if (a_swap(control, 2) == 3)
__wake(control, -1, 1);
return 0;
2011-02-12 00:22:29 -05:00
case 1:
/* If this fails, so will __wait. */
a_cas(control, 1, 3);
case 3:
__wait(control, 0, 3, 1);
2011-02-12 00:22:29 -05:00
continue;
case 2:
return 0;
}
}
weak_alias(__pthread_once, pthread_once);