diff options
| author | Brent Cook <busterb@gmail.com> | 2018-03-14 07:30:55 -0500 |
|---|---|---|
| committer | Brent Cook <busterb@gmail.com> | 2018-03-14 07:36:45 -0500 |
| commit | d5608b38af69baf354705e03085346fb55b75b30 (patch) | |
| tree | 5a4b6e061b2c67c3582960307a3b2e77e58b3cef /include | |
| parent | 3fb9e63b907186c70fb79e818d899ccb67b4b421 (diff) | |
| download | portable-d5608b38af69baf354705e03085346fb55b75b30.tar.gz portable-d5608b38af69baf354705e03085346fb55b75b30.tar.bz2 portable-d5608b38af69baf354705e03085346fb55b75b30.zip | |
add pthread_once(3) implementation for Windows
Diffstat (limited to 'include')
| -rw-r--r-- | include/compat/pthread.h | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/include/compat/pthread.h b/include/compat/pthread.h new file mode 100644 index 0000000..53d2bbc --- /dev/null +++ b/include/compat/pthread.h | |||
| @@ -0,0 +1,43 @@ | |||
| 1 | /* | ||
| 2 | * Public domain | ||
| 3 | * pthread.h compatibility shim | ||
| 4 | */ | ||
| 5 | |||
| 6 | #ifdef _WIN32 | ||
| 7 | |||
| 8 | #include <synchapi.h> | ||
| 9 | |||
| 10 | /* | ||
| 11 | * Static once initialization values. | ||
| 12 | */ | ||
| 13 | #define PTHREAD_ONCE_INIT { INIT_ONCE_STATIC_INIT } | ||
| 14 | |||
| 15 | /* | ||
| 16 | * Once definitions. | ||
| 17 | */ | ||
| 18 | struct pthread_once { | ||
| 19 | INIT_ONCE once; | ||
| 20 | }; | ||
| 21 | |||
| 22 | typedef struct pthread_once pthread_once_t; | ||
| 23 | |||
| 24 | static BOOL CALLBACK _pthread_once_win32_cb(PINIT_ONCE once, PVOID param, PVOID *context) | ||
| 25 | { | ||
| 26 | void (*cb) (void) = param; | ||
| 27 | cb(); | ||
| 28 | return TRUE; | ||
| 29 | } | ||
| 30 | |||
| 31 | static int pthread_once(pthread_once_t *once, void (*cb) (void)) | ||
| 32 | { | ||
| 33 | BOOL rc = InitOnceExecuteOnce(&once->once, _pthread_once_win32_cb, cb, NULL); | ||
| 34 | if (rc == 0) { | ||
| 35 | return -1; | ||
| 36 | } else { | ||
| 37 | return 0; | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | #else | ||
| 42 | #include_next <pthread.h> | ||
| 43 | #endif | ||
