mirror of
https://github.com/fluencelabs/musl
synced 2025-06-29 22:51:55 +00:00
initial check-in, version 0.5.0
This commit is contained in:
38
src/thread/pthread_once.c
Normal file
38
src/thread/pthread_once.c
Normal file
@ -0,0 +1,38 @@
|
||||
#include "pthread_impl.h"
|
||||
|
||||
static void undo(void *control)
|
||||
{
|
||||
a_store(control, 0);
|
||||
__wake(control, 1, 0);
|
||||
}
|
||||
|
||||
int pthread_once(pthread_once_t *control, void (*init)(void))
|
||||
{
|
||||
static int waiters;
|
||||
|
||||
/* Return immediately if init finished before */
|
||||
if (*control == 2) return 0;
|
||||
|
||||
/* Try to enter initializing state. Three possibilities:
|
||||
* 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 */
|
||||
|
||||
for (;;) switch (a_swap(control, 1)) {
|
||||
case 0:
|
||||
break;
|
||||
case 1:
|
||||
__wait(control, &waiters, 1, 0);
|
||||
continue;
|
||||
case 2:
|
||||
a_store(control, 2);
|
||||
return 0;
|
||||
}
|
||||
|
||||
pthread_cleanup_push(undo, control);
|
||||
init();
|
||||
pthread_cleanup_pop(0);
|
||||
|
||||
if (waiters) __wake(control, -1, 0);
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user