[glib] [win32] Add fallback implementations for gatomic.c on mingw32



commit e1ccae841658854a5db0d907edb2b1f2c0a68ef5
Author: Behdad Esfahbod <behdad behdad org>
Date:   Mon Feb 25 22:01:11 2013 -0500

    [win32] Add fallback implementations for gatomic.c on mingw32
    
    Bug 682896 - glib doesn't build on mingw32

 glib/gatomic.c |   72 ++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 67 insertions(+), 5 deletions(-)
---
diff --git a/glib/gatomic.c b/glib/gatomic.c
index 2fc4f4a..461a8e6 100644
--- a/glib/gatomic.c
+++ b/glib/gatomic.c
@@ -467,19 +467,81 @@ gsize
 #elif defined (G_PLATFORM_WIN32)
 
 #include <windows.h>
-#if !defined(_M_AMD64) && !defined (_M_IA64) && !defined(_M_X64)
+#if !defined(_M_AMD64) && !defined (_M_IA64) && !defined(_M_X64) && !(defined _MSC_VER && _MSC_VER <= 1200)
 #define InterlockedAnd _InterlockedAnd
 #define InterlockedOr _InterlockedOr
 #define InterlockedXor _InterlockedXor
 #endif
 
+#if !defined (_MSC_VER) || _MSC_VER <= 1200
+#include "gmessages.h"
+/* Inlined versions for older compiler */
+static LONG
+_gInterlockedAnd (volatile guint *atomic,
+                  guint           val)
+{
+  LONG i, j;
+
+  j = *atomic;
+  do {
+    i = j;
+    j = InterlockedCompareExchange(atomic, i & val, i);
+  } while (i != j);
+
+  return j;
+}
+#define InterlockedAnd(a,b) _gInterlockedAnd(a,b)
+static LONG
+_gInterlockedOr (volatile guint *atomic,
+                 guint           val)
+{
+  LONG i, j;
+
+  j = *atomic;
+  do {
+    i = j;
+    j = InterlockedCompareExchange(atomic, i | val, i);
+  } while (i != j);
+
+  return j;
+}
+#define InterlockedOr(a,b) _gInterlockedOr(a,b)
+static LONG
+_gInterlockedXor (volatile guint *atomic,
+                  guint           val)
+{
+  LONG i, j;
+
+  j = *atomic;
+  do {
+    i = j;
+    j = InterlockedCompareExchange(atomic, i ^ val, i);
+  } while (i != j);
+
+  return j;
+}
+#define InterlockedXor(a,b) _gInterlockedXor(a,b)
+#endif
+
+/* mingw32 does not have MemoryBarrier.
+ * MemoryBarrier may be defined as a macro or a function.
+ * Just make a failsafe version for ourselves. */
+#ifdef MemoryBarrier
+#define _GMemoryBarrier MemoryBarrier
+#else
+static inline void _GMemoryBarrier (void) {
+  long dummy = 0;
+  InterlockedExchange (&dummy, 1);
+}
+#endif
+
 /*
  * http://msdn.microsoft.com/en-us/library/ms684122(v=vs.85).aspx
  */
 gint
 (g_atomic_int_get) (const volatile gint *atomic)
 {
-  MemoryBarrier ();
+  _GMemoryBarrier ();
   return *atomic;
 }
 
@@ -488,7 +550,7 @@ void
                     gint           newval)
 {
   *atomic = newval;
-  MemoryBarrier ();
+  _GMemoryBarrier ();
 }
 
 void
@@ -545,7 +607,7 @@ gpointer
 {
   const volatile gpointer *ptr = atomic;
 
-  MemoryBarrier ();
+  _GMemoryBarrier ();
   return *ptr;
 }
 
@@ -556,7 +618,7 @@ void
   volatile gpointer *ptr = atomic;
 
   *ptr = newval;
-  MemoryBarrier ();
+  _GMemoryBarrier ();
 }
 
 gboolean


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]