>From e80e911e8af19f525a4ba7f47b4524518e14b1f6 Mon Sep 17 00:00:00 2001
From: Grigori Goronzy <greg@blackbox>
Date: Tue, 23 Aug 2011 14:25:35 +0200
Subject: [PATCH 5/5] Use GCC builtin atomics, if no library support

This uses GCC builtin atomics for reference counting and a simple mutex
implementation to make harfbuzz threadsafe without any support from
system libraries or glib. This codepath is only used as a fallback.
---
 src/hb-mutex-private.hh  |   10 ++++++++++
 src/hb-object-private.hh |    8 ++++++++
 2 files changed, 18 insertions(+), 0 deletions(-)

diff --git a/src/hb-mutex-private.hh b/src/hb-mutex-private.hh
index 9855a56..d841178 100644
--- a/src/hb-mutex-private.hh
+++ b/src/hb-mutex-private.hh
@@ -64,6 +64,16 @@ typedef CRITICAL_SECTION hb_mutex_impl_t;
 #define hb_mutex_impl_free(M)	DeleteCriticalSection (M)
 
 
+#elif defined(__GNUC__)
+
+typedef volatile int hb_mutex_impl_t;
+#define HB_MUTEX_IMPL_INIT	0
+#define hb_mutex_impl_init(M)		__sync_lock_test_and_set((M), 0)
+#define hb_mutex_impl_lock(M) 		while (__sync_lock_test_and_set((M), 1));
+#define hb_mutex_impl_unlock(M)  	__sync_lock_release((M))
+#define hb_mutex_impl_free(M)
+
+
 #else
 
 #warning "Could not find any system to define platform macros, library will NOT be thread-safe"
diff --git a/src/hb-object-private.hh b/src/hb-object-private.hh
index 72ed8ba..db5095f 100644
--- a/src/hb-object-private.hh
+++ b/src/hb-object-private.hh
@@ -73,6 +73,14 @@ typedef long hb_atomic_int_t;
 #define hb_atomic_int_set(AI, V)	((void) _InterlockedExchange (&(AI), (V)))
 
 
+#elif defined(__GNUC__)
+
+typedef volatile int hb_atomic_int_t;
+#define hb_atomic_int_add(AI, V)            __sync_fetch_and_add(&(AI), (V))
+#define hb_atomic_int_get(AI)               (__sync_synchronize(), (AI))
+#define hb_atomic_int_set(AI, V)            __sync_lock_test_and_set(&(AI), (V))
+
+
 #else
 
 #warning "Could not find any system to define atomic_int macros, library will NOT be thread-safe"
-- 
1.7.2.3

