7
7
/*#include <libkern/OSAtomic.h>*/
8
8
/*#endif*/
9
9
10
+ /*
11
+ Following the wisdom of postgres, we opt to use platform specific memory barriers when available.
12
+ These are generally more performant. In this PR, we add specific cases for i386, x86_64.
13
+
14
+ In the future, we could look at using pg's atomics library directly:
15
+ https://github.com/postgres/postgres/tree/9d4649ca49416111aee2c84b7e4441a0b7aa2fac/src/include/port/atomics
16
+
17
+ Point of contact @ianks
18
+ */
19
+
10
20
#include "atomic_reference.h"
11
21
22
+ #if (defined(__i386__ ) || defined(__i386 )) && (defined(__GNUC__ ) || defined(__INTEL_COMPILER ))
23
+ #define memory_barrier () \
24
+ __asm__ __volatile__ ("lock; addl $0,0(%%esp)" : : : "memory", "cc")
25
+ #elif defined(__x86_64__ ) && (defined(__GNUC__ ) || defined(__INTEL_COMPILER ))
26
+ #define memory_barrier () \
27
+ __asm__ __volatile__ ("lock; addl $0,0(%%rsp)" : : : "memory", "cc")
28
+ #elif defined(HAVE_GCC__ATOMIC_INT32_CAS )
29
+ #define memory_barrier () \
30
+ __atomic_thread_fence(__ATOMIC_SEQ_CST)
31
+ #elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1 ))
32
+ #define memory_barrier () \
33
+ __sync_synchronize();
34
+ #elif defined _MSC_VER
35
+ #define memory_barrier () \
36
+ MemoryBarrier();
37
+ #elif __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1050
38
+ #define memory_barrier () \
39
+ OSMemoryBarrier();
40
+ #endif
41
+
12
42
void ir_mark (void * value ) {
13
43
rb_gc_mark_maybe ((VALUE ) value );
14
44
}
@@ -27,25 +57,13 @@ VALUE ir_initialize(int argc, VALUE* argv, VALUE self) {
27
57
}
28
58
29
59
VALUE ir_get (VALUE self ) {
30
- #if HAVE_GCC_SYNC
31
- __sync_synchronize ();
32
- #elif defined _MSC_VER
33
- MemoryBarrier ();
34
- #elif __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1050
35
- OSMemoryBarrier ();
36
- #endif
60
+ memory_barrier ();
37
61
return (VALUE ) DATA_PTR (self );
38
62
}
39
63
40
64
VALUE ir_set (VALUE self , VALUE new_value ) {
41
65
DATA_PTR (self ) = (void * ) new_value ;
42
- #if HAVE_GCC_SYNC
43
- __sync_synchronize ();
44
- #elif defined _MSC_VER
45
- MemoryBarrier ();
46
- #elif __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1050
47
- OSMemoryBarrier ();
48
- #endif
66
+ memory_barrier ();
49
67
return new_value ;
50
68
}
51
69
0 commit comments