libclod
C library for interacting with NBTs, region files, LOD data and other things.
Loading...
Searching...
No Matches
once.c
1#include "clod_thread_config.h"
2#include "debug.h"
3#include <clod/thread.h>
4#include "yield.h"
5
6#define ZERO 0
7#define BUSY 1
8#define DONE 2
9
11 while (1) {
12 clod_once state = clod_atomic_load(once);
13
14 #if CLOD_DEBUG_THREAD
15 if (state != ZERO && state != BUSY && state != DONE) {
16 debug(CLOD_DEBUG_THREAD, "called clod_once_do with an invalid value.");
17 }
18 #endif
19
20 if (state == ZERO && clod_atomic_cas(once, &state, BUSY))
21 return true;
22
23 while (state == BUSY) {
24 for (int i = 0; i < 500 && state == BUSY; i++) {
25 clod_pause();
26 state = clod_atomic_load(once);
27 }
28
29 clod_yield();
30 state = clod_atomic_load(once);
31 }
32
33 if (state == DONE)
34 return false;
35 }
36}
37
39 #if CLOD_DEBUG_THREAD
40 clod_once expected = BUSY;
41 if (!clod_atomic_cas(once, &expected, DONE)) {
42 debug(CLOD_DEBUG_THREAD, "called clod_once_done with an invalid value.");
43 }
44 #else
45 clod_atomic_store(once, DONE);
46 #endif
47}
char clod_once
Definition thread.h:141
void clod_once_done(clod_once *once)
Definition once.c:38
bool clod_once_do(clod_once *once)
Definition once.c:10