Skip to content

Commit 05e5e93

Browse files
committed
Merge pull request #145 from alexdowad/volatile_semantics_for_atomic_get_set
Volatile semantics for atomic get set
2 parents 7b7b3e2 + ed44c1a commit 05e5e93

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

ext/concurrent_ruby_ext/atomic_reference.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,31 @@ VALUE ir_initialize(int argc, VALUE* argv, VALUE self) {
2727
}
2828

2929
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
3037
return (VALUE) DATA_PTR(self);
3138
}
3239

3340
VALUE ir_set(VALUE self, VALUE new_value) {
3441
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
3549
return new_value;
3650
}
3751

3852
VALUE ir_get_and_set(VALUE self, VALUE new_value) {
39-
VALUE old_value;
40-
old_value = (VALUE) DATA_PTR(self);
41-
DATA_PTR(self) = (void *) new_value;
53+
VALUE old_value = ir_get(self);
54+
ir_set(self, new_value);
4255
return old_value;
4356
}
4457

ext/concurrent_ruby_ext/extconf.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,11 @@ def compiler_is_gcc
4343
end
4444
end
4545

46-
try_run(<<CODE,$CFLAGS) && ($defs << '-DHAVE_GCC_CAS')
47-
int main() {
48-
int i = 1;
49-
__sync_bool_compare_and_swap(&i, 1, 4);
50-
return (i != 4);
51-
}
46+
try_run(<<CODE,$CFLAGS) && ($defs << '-DHAVE_GCC_SYNC')
47+
int main() {
48+
__sync_synchronize();
49+
return 0;
50+
}
5251
CODE
5352

5453
create_makefile(EXTENSION_NAME)

0 commit comments

Comments
 (0)