Index: src/threading.h ================================================================== --- src/threading.h +++ src/threading.h @@ -51,11 +51,11 @@ typedef CRITICAL_SECTION of_mutex_t; typedef struct { HANDLE event; int count; } of_condition_t; -typedef volatile LONG of_once_t; +typedef volatile int of_once_t; # define OF_ONCE_INIT 0 #else # error No threads available! #endif Index: src/threading.m ================================================================== --- src/threading.m +++ src/threading.m @@ -23,10 +23,30 @@ #elif defined(OF_WINDOWS) # include "threading_winapi.m" #else # error No threads available! #endif + +#ifndef OF_HAVE_PTHREADS +void +of_once(of_once_t *control, void (*func)(void)) +{ + if (of_atomic_int_cmpswap(control, 0, 1)) { + func(); + of_atomic_int_inc(control); + } else { + while (*control == 1) +# if defined(HAVE_SCHED_YIELD) + sched_yield(); +# elif defined(OF_WINDOWS) + Sleep(0); +# else + ; +# endif + } +} +#endif #if !defined(OF_HAVE_RECURSIVE_PTHREAD_MUTEXES) && !defined(OF_WINDOWS) bool of_rmutex_new(of_rmutex_t *rmutex) { Index: src/threading_winapi.m ================================================================== --- src/threading_winapi.m +++ src/threading_winapi.m @@ -88,25 +88,10 @@ void of_thread_set_name(of_thread_t thread, const char *name) { } -void -of_once(of_once_t *control, void (*func)(void)) -{ - switch (InterlockedCompareExchange(control, 1, 0)) { - case 0: - func(); - InterlockedIncrement(control); - break; - case 1: - while (*control == 1) - Sleep(0); - break; - } -} - bool of_mutex_new(of_mutex_t *mutex) { InitializeCriticalSection(mutex);