#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)
+typedef GStaticMutex hb_mutex_impl_t;
+#define HB_MUTEX_IMPL_INIT G_STATIC_MUTEX_INIT
+#define hb_mutex_impl_init(M) g_static_mutex_init (M)
+#define hb_mutex_impl_lock(M) g_static_mutex_lock (M)
+#define hb_mutex_impl_unlock(M) g_static_mutex_unlock (M)
+#define hb_mutex_impl_free(M) g_static_mutex_free (M)
#elif defined(_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)
+typedef CRITICAL_SECTION hb_mutex_impl_t;
+#define HB_MUTEX_IMPL_INIT { NULL, 0, 0, NULL, NULL, 0 }
+#define hb_mutex_impl_init(M) InitializeCriticalSection (M)
+#define hb_mutex_impl_lock(M) EnterCriticalSection (M)
+#define hb_mutex_impl_unlock(M) LeaveCriticalSection (M)
+#define hb_mutex_impl_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))
+typedef volatile int hb_mutex_impl_t;
+#define HB_MUTEX_IMPL_INIT 0
+#define hb_mutex_impl_init(M) ((void) (*(M) = 0))
+#define hb_mutex_impl_lock(M) ((void) (*(M) = 1))
+#define hb_mutex_impl_unlock(M) ((void) (*(M) = 0))
+#define hb_mutex_impl_free(M) ((void) (*(M) = 2))
#endif
-struct hb_static_mutex_t : hb_mutex_t
+struct hb_mutex_t
{
- hb_static_mutex_t (void) {
- hb_mutex_init (this);
- }
-};
+ hb_mutex_impl_t m;
+ inline void init (void) { hb_mutex_impl_init (&m); }
+ inline void lock (void) { hb_mutex_impl_lock (&m); }
+ inline void unlock (void) { hb_mutex_impl_unlock (&m); }
+ inline void free (void) { hb_mutex_impl_free (&m); }
+};
-HB_END_DECLS
-
+#define HB_MUTEX_INIT {HB_MUTEX_IMPL_INIT}
+#define hb_mutex_init(M) (M)->init ()
+#define hb_mutex_lock(M) (M)->lock ()
+#define hb_mutex_unlock(M) (M)->unlock ()
+#define hb_mutex_free(M) (M)->free ()
-/* XXX If the finish() callbacks of items in the set recursively try to
- * modify the set, deadlock occurs. This needs fixing in set proper in
- * fact. */
-template <typename item_t>
-struct hb_threadsafe_set_t
+struct hb_static_mutex_t : hb_mutex_t
{
- hb_set_t <item_t> set;
- hb_static_mutex_t mutex;
-
- template <typename T>
- inline item_t *insert (T v)
- {
- hb_mutex_lock (&mutex);
- item_t *item = set.insert (v);
- hb_mutex_unlock (&mutex);
- return item;
- }
-
- template <typename T>
- inline void remove (T v)
- {
- hb_mutex_lock (&mutex);
- set.remove (v);
- hb_mutex_unlock (&mutex);
- }
-
- template <typename T>
- inline item_t *find (T v)
- {
- hb_mutex_lock (&mutex);
- item_t *item = set.find (v);
- hb_mutex_unlock (&mutex);
- return item;
- }
-
- template <typename T>
- inline item_t *find_or_insert (T v) {
- hb_mutex_lock (&mutex);
- item_t *item = set.find_or_insert (v);
- hb_mutex_unlock (&mutex);
- return item;
- }
-
- void finish (void) {
- hb_mutex_lock (&mutex);
- set.finish ();
- hb_mutex_unlock (&mutex);
- }
+ hb_static_mutex_t (void) { this->init (); }
+ ~hb_static_mutex_t (void) { this->free (); }
+ private:
+ NO_COPY (hb_static_mutex_t);
};
-HB_BEGIN_DECLS
-
HB_END_DECLS
#endif /* HB_MUTEX_PRIVATE_HH */