Move Win32 thread-safety stuff to hb-object-private.h
[apps/core/preloaded/video-player.git] / src / hb-object-private.hh
index 47cef7e..d77b6a5 100644 (file)
@@ -44,6 +44,72 @@ HB_BEGIN_DECLS
 #endif
 
 
+/* atomic_int and mutex */
+
+/* We need external help for these */
+
+#ifdef HAVE_GLIB
+
+#include <glib.h>
+
+typedef volatile int hb_atomic_int_t;
+#define hb_atomic_int_fetch_and_add(AI, V)     g_atomic_int_exchange_and_add (&(AI), V)
+#define hb_atomic_int_get(AI)                  g_atomic_int_get (&(AI))
+#define hb_atomic_int_set(AI, V)               g_atomic_int_set (&(AI), V)
+
+typedef GStaticMutex hb_mutex_t;
+#define HB_MUTEX_INIT                  G_STATIC_MUTEX_INIT
+#define hb_mutex_init(M)               g_static_mutex_init (&(M))
+#define hb_mutex_lock(M)               g_static_mutex_lock (&(M))
+#define hb_mutex_trylock(M)            g_static_mutex_trylock (&(M))
+#define hb_mutex_unlock(M)             g_static_mutex_unlock (&(M))
+#define hb_mutex_free(M)               g_static_mutex_free (&(M))
+
+#else
+
+#ifdef _MSC_VER
+
+#include <intrin.h>
+
+typedef long hb_atomic_int_t;
+#define hb_atomic_int_fetch_and_add(AI, V)     _InterlockedExchangeAdd (&(AI), V)
+#define hb_atomic_int_get(AI)                  (_ReadBarrier (), (AI))
+#define hb_atomic_int_set(AI, V)               ((void) _InterlockedExchange (&(AI), (V)))
+
+#include <Windows.h>
+
+typedef CRITICAL_SECTION hb_mutex_t;
+#define HB_MUTEX_INIT                          { NULL, 0, 0, NULL, NULL, 0 }
+#define hb_mutex_init(M)                       InitializeCriticalSection (&(M))
+#define hb_mutex_lock(M)                       EnterCriticalSection (&(M))
+#define hb_mutex_trylock(M)                    TryEnterCriticalSection (&(M))
+#define hb_mutex_unlock(M)                     LeaveCriticalSection (&(M))
+#define hb_mutex_free(M)                       DeleteCriticalSection (&(M))
+
+#else
+
+#warning "Could not find any system to define platform macros, library will NOT be thread-safe"
+
+typedef volatile int hb_atomic_int_t;
+#define hb_atomic_int_fetch_and_add(AI, V)     ((AI) += (V), (AI) - (V))
+#define hb_atomic_int_get(AI)                  (AI)
+#define hb_atomic_int_set(AI, V)               ((void) ((AI) = (V)))
+
+typedef volatile int hb_mutex_t;
+#define HB_MUTEX_INIT                          0
+#define hb_mutex_init(M)                       ((void) ((M) = 0))
+#define hb_mutex_lock(M)                       ((void) ((M) = 1))
+#define hb_mutex_trylock(M)                    ((M) = 1, 1)
+#define hb_mutex_unlock(M)                     ((void) ((M) = 0))
+#define hb_mutex_free(M)                       ((void) ((M) = 2))
+
+#endif
+
+#endif
+
+
+
+
 /* reference_count */
 
 typedef struct {