From a6853a944892d370b3969e97fdcdacd5a3718034 Mon Sep 17 00:00:00 2001 From: Derek Buitenhuis Date: Mon, 24 Sep 2018 15:26:33 +0100 Subject: 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 --- src/thread.h | 5 +++++ src/win32/thread.c | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+) (limited to 'src') 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 +#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 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 -- cgit v1.2.3