diff options
author | Derek Buitenhuis <derek.buitenhuis@gmail.com> | 2018-09-24 17:26:33 +0300 |
---|---|---|
committer | Derek Buitenhuis <derek.buitenhuis@gmail.com> | 2018-09-26 16:31:16 +0300 |
commit | a6853a944892d370b3969e97fdcdacd5a3718034 (patch) | |
tree | f315605cfdb15846738f55d11589d52ec2ac2806 /src | |
parent | cbeac008b7f5fd4970b67af619f73775ec7076a0 (diff) |
win32: Add implementation of pthread_once
This function will be required to make dav1d_init threadsafe.
Full erro checking support is included in this implementation,
for completeness, even though dav1d_init cannot fail, and thus
will ignore the return value of pthread_once.
Signed-off-by: Derek Buitenhuis <derek.buitenhuis@gmail.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/thread.h | 5 | ||||
-rw-r--r-- | src/win32/thread.c | 19 |
2 files changed, 24 insertions, 0 deletions
diff --git a/src/thread.h b/src/thread.h index 40ffb74..b5cdc2a 100644 --- a/src/thread.h +++ b/src/thread.h @@ -32,8 +32,11 @@ #include <windows.h> +#define PTHREAD_ONCE_INIT INIT_ONCE_STATIC_INIT + typedef CRITICAL_SECTION pthread_mutex_t; typedef CONDITION_VARIABLE pthread_cond_t; +typedef INIT_ONCE pthread_once_t; typedef void *pthread_t; typedef void *pthread_mutexattr_t; typedef void *pthread_condattr_t; @@ -54,6 +57,8 @@ int pthread_create(pthread_t* thread, const pthread_attr_t* attr, void*(*proc)(void*), void* param); void pthread_join(pthread_t thread, void** res); +int pthread_once(pthread_once_t *once_control, void (*init_routine)(void)); + #else #include <pthread.h> diff --git a/src/win32/thread.c b/src/win32/thread.c index c73dba1..41a04e5 100644 --- a/src/win32/thread.c +++ b/src/win32/thread.c @@ -125,4 +125,23 @@ void pthread_join(pthread_t thread, void** res) free(th); } +int pthread_once(pthread_once_t *once_control, void (*init_routine)(void)) +{ + BOOL fPending = FALSE; + BOOL fStatus; + + fStatus = InitOnceBeginInitialize(once_control, 0, &fPending, NULL); + if (fStatus != TRUE) + return EINVAL; + + if (fPending == TRUE) + init_routine(); + + fStatus = InitOnceComplete(once_control, 0, NULL); + if (!fStatus) + return EINVAL; + + return 0; +} + #endif |