Skip to content

Commit

Permalink
Should fix issue #7. We now do a write barrier before setting the 'ca…
Browse files Browse the repository at this point in the history
…ched' field to 1 so that reader don't see this get re-ordered before all the fields are readable.
  • Loading branch information
chirino committed Sep 9, 2013
1 parent ae07fd2 commit d9cd0ab
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ void generateCacheFunction(JNIClass clazz) {
output("\"");
outputln(");");
}
// Makes sure compiler/cpu does not reorder the following write before the previous updates are done.
outputln("\thawtjni_w_barrier();");
output("\t");
output(simpleName);
outputln("Fc.cached = 1;");
Expand Down
19 changes: 19 additions & 0 deletions hawtjni-generator/src/main/resources/hawtjni.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,25 @@ void throwOutOfMemory(JNIEnv *env);
#endif
#endif /* JNI64 */

#ifdef __GNUC__
#define hawtjni_w_barrier() __sync_synchronize()
#elif defined(SOLARIS2) && SOLARIS2 >= 10
#include <mbarrier.h>
#define hawtjni_w_barrier() __machine_w_barrier()
#elif defined(__APPLE__)
#include <libkern/OSAtomic.h>
#define hawtjni_w_barrier() OSMemoryBarrier()
#elif defined(_WIN32) || defined(_WIN64)
#include <intrin.h>
#define hawtjni_w_barrier() _mm_sfence(); _WriteBarrier()
#else
#pragma message ( "Don't know how to do a memory barrier on this platform" )
#define hawtjni_w_barrier()
#endif

void hawtjni_atomic_set(jlong *target, jlong value);
jlong hawtjni_atomic_get(jlong *target);

#ifdef __cplusplus
}
#endif
Expand Down

0 comments on commit d9cd0ab

Please sign in to comment.