Blame SOURCES/0005-Should-fix-issue-7.-We-now-do-a-write-barrier-before.patch

b00be6
From 7c6b1e1f5ed08361d3926e672a5a5129ee51c6d3 Mon Sep 17 00:00:00 2001
b00be6
From: Hiram Chirino <hiram@hiramchirino.com>
b00be6
Date: Mon, 9 Sep 2013 11:31:08 -0400
b00be6
Subject: [PATCH 5/5] Should fix issue #7.  We now do a write barrier before
b00be6
 setting the 'cached' field to 1 so that reader don't see this get re-ordered
b00be6
 before all the fields are readable.
b00be6
b00be6
---
b00be6
 .../hawtjni/generator/StructsGenerator.java           |  2 ++
b00be6
 hawtjni-generator/src/main/resources/hawtjni.h        | 19 +++++++++++++++++++
b00be6
 2 files changed, 21 insertions(+)
b00be6
b00be6
diff --git a/hawtjni-generator/src/main/java/org/fusesource/hawtjni/generator/StructsGenerator.java b/hawtjni-generator/src/main/java/org/fusesource/hawtjni/generator/StructsGenerator.java
b00be6
index d8d591e..c12063c 100755
b00be6
--- a/hawtjni-generator/src/main/java/org/fusesource/hawtjni/generator/StructsGenerator.java
b00be6
+++ b/hawtjni-generator/src/main/java/org/fusesource/hawtjni/generator/StructsGenerator.java
b00be6
@@ -240,6 +240,8 @@ public class StructsGenerator extends JNIGenerator {
b00be6
                 output("\"");
b00be6
             outputln(");");
b00be6
         }
b00be6
+        // Makes sure compiler/cpu does not reorder the following write before the previous updates are done.
b00be6
+        outputln("\thawtjni_w_barrier();");
b00be6
         output("\t");
b00be6
         output(simpleName);
b00be6
         outputln("Fc.cached = 1;");
b00be6
diff --git a/hawtjni-generator/src/main/resources/hawtjni.h b/hawtjni-generator/src/main/resources/hawtjni.h
b00be6
index 79ae703..c8ba601 100755
b00be6
--- a/hawtjni-generator/src/main/resources/hawtjni.h
b00be6
+++ b/hawtjni-generator/src/main/resources/hawtjni.h
b00be6
@@ -168,6 +168,25 @@ void throwOutOfMemory(JNIEnv *env);
b00be6
   #endif 
b00be6
 #endif /* JNI64 */
b00be6
 
b00be6
+#ifdef __GNUC__
b00be6
+  #define hawtjni_w_barrier() __sync_synchronize()
b00be6
+#elif defined(SOLARIS2) && SOLARIS2 >= 10
b00be6
+  #include <mbarrier.h>
b00be6
+  #define hawtjni_w_barrier() __machine_w_barrier()
b00be6
+#elif defined(__APPLE__)
b00be6
+  #include <libkern/OSAtomic.h>
b00be6
+  #define hawtjni_w_barrier() OSMemoryBarrier()
b00be6
+#elif defined(_WIN32) || defined(_WIN64)
b00be6
+  #include <intrin.h>
b00be6
+  #define hawtjni_w_barrier() _mm_sfence(); _WriteBarrier()
b00be6
+#else
b00be6
+  #pragma message ( "Don't know how to do a memory barrier on this platform" )
b00be6
+  #define hawtjni_w_barrier()
b00be6
+#endif
b00be6
+
b00be6
+void hawtjni_atomic_set(jlong *target, jlong value);
b00be6
+jlong hawtjni_atomic_get(jlong *target);
b00be6
+
b00be6
 #ifdef __cplusplus
b00be6
 }
b00be6
 #endif 
b00be6
-- 
b00be6
1.8.1.4
b00be6