forked from heimdal/MKShim
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdispatch_once.c
54 lines (43 loc) · 1.19 KB
/
dispatch_once.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "dispatch_once.h"
#if _WIN32_WINNT >= 0x0600
struct worker_data {
dispatch_once_func_t function;
void *context;
};
static BOOL CALLBACK
dispatch_worker(PINIT_ONCE ponce,
PVOID param, PVOID *ctx)
{
struct worker_data *d = (struct worker_data *) param;
d->function(d->context);
return TRUE;
}
void
dispatch_once_f(dispatch_once_t * predicate, void * context,
dispatch_once_func_t function)
{
struct worker_data d;
d.function = function;
d.context = context;
InitOnceExecuteOnce(&predicate->once, dispatch_worker, &d, NULL);
}
#else
void
dispatch_once_f(dispatch_once_t * predicate, void * context,
dispatch_once_func_t function)
{
if (InterlockedIncrement(&predicate->initializing) == 1) {
if (InterlockedIncrement(&predicate->initialized) == 1) {
(*function)(context);
} else {
InterlockedDecrement(&predicate->initialized);
}
InterlockedDecrement(&predicate->initializing);
} else {
InterlockedDecrement(&predicate->initializing);
do {
Sleep(0);
} while (predicate->initializing > 0);
}
}
#endif