Skip to content
Snippets Groups Projects
Commit c0e45c61 authored by deuce's avatar deuce
Browse files

Add a pthread_once() implementation for Win32 (untested on Win32).

parent ebf1bda7
Branches
Tags
No related merge requests found
......@@ -126,6 +126,28 @@ pthread_mutex_t DLLCALL pthread_mutex_initializer_np(BOOL recursive)
#if !defined(_POSIX_THREADS)
int DLLCALL pthread_once(pthread_once_t *oc, void (*init)(void))
{
if (oc == NULL || init == NULL)
return EINVAL;
switch(InterlockedCompareExchange(&(oc->state), 1, 0)) {
case 0: // Never called
init();
InterlockedIncrement(&(oc->state));
return 0;
case 1: // In init function
/* We may not need to use InterlockedCompareExchange() here,
* but I hate marking things as volatile, and hate tight loops
* testing things that aren't marked volatile.
*/
while(InterlockedCompareExchange(&(oc->state), 1, 0) != 2)
SLEEP(1);
return 0;
case 2: // Done.
return 0;
}
}
int DLLCALL pthread_mutex_init(pthread_mutex_t* mutex, void* attr)
{
(void)attr;
......
......@@ -123,6 +123,14 @@ DLLEXPORT int DLLCALL pthread_mutex_destroy(pthread_mutex_t*);
#define SetThreadName(c)
// A structure in case we need to add an event or something...
typedef struct {
uint32_t state;
} pthread_once_t;
#define PTHREAD_ONCE_INIT {0};
DLLEXPORT int DLLCALL pthread_once(pthread_once_t *oc, void (*init)(void));
#endif
#if !defined(PTHREAD_MUTEX_INITIALIZER_NP)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment