From 49388d03c4545ba0fdcffe1989b1dea9ddfaf489 Mon Sep 17 00:00:00 2001
From: Bradley Grainger <bgrainger@logos.com>
Date: Mon, 25 Apr 2011 09:48:17 -0700
Subject: [PATCH 3/3] Implement threading primitives on Windows.

- hb_atomic_int_* functions use MSVC's _Interlocked* intrinsics.

- hb_mutex_* functions use Win32 CRITICAL_SECTION objects.
---
 src/hb-common.cc  |   49 +++++++++++++++++++++++++++++++++++++++++++++++++
 src/hb-private.hh |   30 +++++++++++++++++++++++++-----
 2 files changed, 74 insertions(+), 5 deletions(-)

diff --git a/src/hb-common.cc b/src/hb-common.cc
index ece0980..f01bd3f 100644
--- a/src/hb-common.cc
+++ b/src/hb-common.cc
@@ -218,5 +218,54 @@ hb_script_get_horizontal_direction (hb_script_t script)
   return HB_DIRECTION_LTR;
 }
 
+#ifdef _MSC_VER
+
+#include <Windows.h>
+
+hb_mutex_t
+win_mutex_create ()
+{
+  hb_mutex_t m;
+  win_mutex_init (&m);
+  return m;
+}
+
+void
+win_mutex_init (hb_mutex_t *m)
+{
+  LPCRITICAL_SECTION lpcs = (LPCRITICAL_SECTION) calloc(1, sizeof(CRITICAL_SECTION));
+  InitializeCriticalSection (lpcs);
+  *m = (void*) lpcs;
+}
+
+void
+win_mutex_lock (hb_mutex_t m)
+{
+  EnterCriticalSection ((LPCRITICAL_SECTION) m);
+}
+
+int
+win_mutex_trylock (hb_mutex_t m)
+{
+  return TryEnterCriticalSection ((LPCRITICAL_SECTION) m);
+}
+
+void
+win_mutex_unlock (hb_mutex_t m)
+{
+  LeaveCriticalSection ((LPCRITICAL_SECTION) m);
+}
+
+void
+win_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 c4f50be..6e446a4 100644
--- a/src/hb-private.hh
+++ b/src/hb-private.hh
@@ -230,13 +230,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 <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)))
+
+typedef void * hb_mutex_t;
+extern hb_mutex_t win_mutex_create ();
+extern void win_mutex_init (hb_mutex_t *m);
+extern void win_mutex_lock (hb_mutex_t m);
+extern int win_mutex_trylock (hb_mutex_t m);
+extern void win_mutex_unlock (hb_mutex_t m);
+extern void win_mutex_free (hb_mutex_t *m);
+#define HB_MUTEX_INIT				win_mutex_create ()
+#define hb_mutex_init(M)			win_mutex_init (&(M))
+#define hb_mutex_lock(M)			win_mutex_lock ((M))
+#define hb_mutex_trylock(M)			win_mutex_trylock ((M))
+#define hb_mutex_unlock(M)			win_mutex_unlock ((M))
+#define hb_mutex_free(M)			win_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))
@@ -253,6 +271,8 @@ typedef volatile int hb_mutex_t;
 
 #endif
 
+#endif
+
 
 /* Big-endian handling */
 
-- 
1.7.3.1.msysgit.0

