ref: a6853a944892d370b3969e97fdcdacd5a3718034
parent: cbeac008b7f5fd4970b67af619f73775ec7076a0
author: Derek Buitenhuis <derek.buitenhuis@gmail.com>
date: Mon Sep 24 11:26:33 EDT 2018
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>
--- 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;
@@ -53,6 +56,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
--- a/src/win32/thread.c
+++ b/src/win32/thread.c
@@ -125,4 +125,23 @@
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