From a406cfaa4724b3a78683b732f016c9ad33cad187 Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Fri, 21 Mar 2014 20:41:56 +0100 Subject: common: add thread_once() implementation --- common/thread.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'common/thread.c') diff --git a/common/thread.c b/common/thread.c index 2cf4321..d6d6c1a 100644 --- a/common/thread.c +++ b/common/thread.c @@ -81,3 +81,19 @@ void mutex_unlock(mutex_t* mutex) pthread_mutex_unlock(mutex); #endif } + +void thread_once(thread_once_t *once_control, void (*init_routine)(void)) +{ +#ifdef WIN32 + while (InterlockedExchange(&(once_control->lock), 1) != 0) { + Sleep(1); + } + if (!once_control->state) { + once_control->state = 1; + init_routine(); + } + InterlockedExchange(&(once_control->lock), 0); +#else + pthread_once(once_control, init_routine); +#endif +} -- cgit v1.1-32-gdbae