From d5608b38af69baf354705e03085346fb55b75b30 Mon Sep 17 00:00:00 2001 From: Brent Cook Date: Wed, 14 Mar 2018 07:30:55 -0500 Subject: add pthread_once(3) implementation for Windows --- include/compat/pthread.h | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 include/compat/pthread.h 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 @@ +/* + * Public domain + * pthread.h compatibility shim + */ + +#ifdef _WIN32 + +#include + +/* + * Static once initialization values. + */ +#define PTHREAD_ONCE_INIT { INIT_ONCE_STATIC_INIT } + +/* + * Once definitions. + */ +struct pthread_once { + INIT_ONCE once; +}; + +typedef struct pthread_once pthread_once_t; + +static BOOL CALLBACK _pthread_once_win32_cb(PINIT_ONCE once, PVOID param, PVOID *context) +{ + void (*cb) (void) = param; + cb(); + return TRUE; +} + +static int pthread_once(pthread_once_t *once, void (*cb) (void)) +{ + BOOL rc = InitOnceExecuteOnce(&once->once, _pthread_once_win32_cb, cb, NULL); + if (rc == 0) { + return -1; + } else { + return 0; + } +} + +#else +#include_next +#endif -- cgit v1.2.3-55-g6feb