summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Nikias Bassen2014-03-21 20:41:56 +0100
committerGravatar Nikias Bassen2014-03-21 20:41:56 +0100
commita406cfaa4724b3a78683b732f016c9ad33cad187 (patch)
treeb635afe13c5544e58e5385da448d36a54e1b2d56
parent982bc6a5ec6395151501175be2e16d5c056e166d (diff)
downloadlibimobiledevice-a406cfaa4724b3a78683b732f016c9ad33cad187.tar.gz
libimobiledevice-a406cfaa4724b3a78683b732f016c9ad33cad187.tar.bz2
common: add thread_once() implementation
-rw-r--r--common/thread.c16
-rw-r--r--common/thread.h9
2 files changed, 25 insertions, 0 deletions
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
+}
diff --git a/common/thread.h b/common/thread.h
index e74ee74..5d20083 100644
--- a/common/thread.h
+++ b/common/thread.h
@@ -26,10 +26,17 @@
#include <windows.h>
typedef HANDLE thread_t;
typedef CRITICAL_SECTION mutex_t;
+typedef volatile struct {
+ LONG lock;
+ int state;
+} thread_once_t;
+#define THREAD_ONCE_INIT {0, 0}
#else
#include <pthread.h>
typedef pthread_t thread_t;
typedef pthread_mutex_t mutex_t;
+typedef pthread_once_t thread_once_t;
+#define THREAD_ONCE_INIT PTHREAD_ONCE_INIT
#endif
typedef void* (*thread_func_t)(void* data);
@@ -42,4 +49,6 @@ void mutex_destroy(mutex_t* mutex);
void mutex_lock(mutex_t* mutex);
void mutex_unlock(mutex_t* mutex);
+void thread_once(thread_once_t *once_control, void (*init_routine)(void));
+
#endif