Skip to content

Commit 0e1d60e

Browse files
authored
Merge pull request #708 from ianks/update-memory-barriers
Prefer platform specific memory barriers
2 parents 956fc91 + e43ad71 commit 0e1d60e

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

ext/concurrent/atomic_reference.c

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,38 @@
77
/*#include <libkern/OSAtomic.h>*/
88
/*#endif*/
99

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+
1020
#include "atomic_reference.h"
1121

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+
1242
void ir_mark(void *value) {
1343
rb_gc_mark_maybe((VALUE) value);
1444
}
@@ -27,25 +57,13 @@ VALUE ir_initialize(int argc, VALUE* argv, VALUE self) {
2757
}
2858

2959
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();
3761
return (VALUE) DATA_PTR(self);
3862
}
3963

4064
VALUE ir_set(VALUE self, VALUE new_value) {
4165
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();
4967
return new_value;
5068
}
5169

ext/concurrent/extconf.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,6 @@ def compiler_is_gcc
4343
end
4444
end
4545

46-
try_run(<<CODE,$CFLAGS) && ($defs << '-DHAVE_GCC_SYNC')
47-
int main() {
48-
__sync_synchronize();
49-
return 0;
50-
}
51-
CODE
52-
5346
create_makefile('concurrent/' + EXTENSION_NAME)
5447
rescue
5548
create_dummy_makefile

0 commit comments

Comments
 (0)