From fc52e9e44c2fe84d63f18dc0098720830f0b467d Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 3 May 2011 00:09:16 -0400 Subject: [PATCH] Implement win32 thread-safety stuff Patch from Bradley Grainger. --- src/hb-common.cc | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/hb-private.hh | 30 +++++++++++++++++++++++++----- 2 files changed, 79 insertions(+), 5 deletions(-) diff --git a/src/hb-common.cc b/src/hb-common.cc index b75146d..2f5e89b 100644 --- a/src/hb-common.cc +++ b/src/hb-common.cc @@ -263,4 +263,58 @@ hb_script_get_horizontal_direction (hb_script_t script) } + +/* System stuff */ + + +#ifdef _MSC_VER + +#include + +hb_mutex_t +_hb_win32_mutex_create () +{ + hb_mutex_t m; + _hb_win32_mutex_init (&m); + return m; +} + +void +_hb_win32_mutex_init (hb_mutex_t *m) +{ + LPCRITICAL_SECTION lpcs = (LPCRITICAL_SECTION) calloc(1, sizeof(CRITICAL_SECTION)); + InitializeCriticalSection (lpcs); + *m = (void*) lpcs; +} + +void +_hb_win32_mutex_lock (hb_mutex_t m) +{ + EnterCriticalSection ((LPCRITICAL_SECTION) m); +} + +int +_hb_win32_mutex_trylock (hb_mutex_t m) +{ + return TryEnterCriticalSection ((LPCRITICAL_SECTION) m); +} + +void +_hb_win32_mutex_unlock (hb_mutex_t m) +{ + LeaveCriticalSection ((LPCRITICAL_SECTION) m); +} + +void +_hb_win32_mutex_free (hb_mutex_t *m) +{ + LPCRITICAL_SECTION lpcs = (LPCRITICAL_SECTION) *m; + DeleteCriticalSection (lpcs); + free(lpcs); + *m = 0; +} + +#endif + + HB_END_DECLS diff --git a/src/hb-private.hh b/src/hb-private.hh index a45fece..cac434a 100644 --- a/src/hb-private.hh +++ b/src/hb-private.hh @@ -243,13 +243,31 @@ typedef GStaticMutex hb_mutex_t; #else #ifdef _MSC_VER -#define _HB__STR2__(x) #x -#define _HB__STR1__(x) _HB__STR2__(x) -#define _HB__LOC__ __FILE__ "("_HB__STR1__(__LINE__)") : Warning Msg: " -#pragma message(_HB__LOC__"Could not find any system to define platform macros, library will NOT be thread-safe") + +#include + +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))) + +typedef void * hb_mutex_t; +extern HB_INTERNAL hb_mutex_t _hb_win32_mutex_create (void); +extern HB_INTERNAL void _hb_win32_mutex_init (hb_mutex_t *m); +extern HB_INTERNAL void _hb_win32_mutex_lock (hb_mutex_t m); +extern HB_INTERNAL int _hb_win32_mutex_trylock (hb_mutex_t m); +extern HB_INTERNAL void _hb_win32_mutex_unlock (hb_mutex_t m); +extern HB_INTERNAL void _hb_win32_mutex_free (hb_mutex_t *m); +#define HB_MUTEX_INIT _hb_win32_mutex_create () +#define hb_mutex_init(M) _hb_win32_mutex_init (&(M)) +#define hb_mutex_lock(M) _hb_win32_mutex_lock ((M)) +#define hb_mutex_trylock(M) _hb_win32_mutex_trylock ((M)) +#define hb_mutex_unlock(M) _hb_win32_mutex_unlock ((M)) +#define hb_mutex_free(M) _hb_win32_mutex_free (&(M)) + #else + #warning "Could not find any system to define platform macros, library will NOT be thread-safe" -#endif typedef volatile int hb_atomic_int_t; #define hb_atomic_int_fetch_and_add(AI, V) ((AI) += (V), (AI) - (V)) @@ -266,6 +284,8 @@ typedef volatile int hb_mutex_t; #endif +#endif + HB_END_DECLS -- 2.7.4