Skip to content
This repository was archived by the owner on Mar 15, 2022. It is now read-only.

Commit 9e720c8

Browse files
committed
Merge pull request #4 from alexdowad/volatile_semantics_for_get_set
Volatile semantics for #get, #set, and #swap
2 parents 3cd3b43 + a762883 commit 9e720c8

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

ext/atomic_reference.c

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

3939
static VALUE ir_get(VALUE self) {
40+
#if HAVE_GCC_SYNC
41+
__sync_synchronize();
42+
#elif defined _MSC_VER
43+
MemoryBarrier();
44+
#elif __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1050
45+
OSMemoryBarrier();
46+
#endif
4047
return (VALUE) DATA_PTR(self);
4148
}
4249

4350
static VALUE ir_set(VALUE self, VALUE new_value) {
4451
DATA_PTR(self) = (void *) new_value;
52+
#if HAVE_GCC_SYNC
53+
__sync_synchronize();
54+
#elif defined _MSC_VER
55+
MemoryBarrier();
56+
#elif __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1050
57+
OSMemoryBarrier();
58+
#endif
4559
return new_value;
4660
}
4761

4862
static VALUE ir_get_and_set(VALUE self, VALUE new_value) {
49-
VALUE old_value;
50-
old_value = (VALUE) DATA_PTR(self);
51-
DATA_PTR(self) = (void *) new_value;
63+
VALUE old_value = ir_get(self);
64+
ir_set(self, new_value);
5265
return old_value;
5366
}
5467

ext/extconf.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,10 @@ def compiler_is_gcc
3737
end
3838
end
3939

40-
try_run(<<CODE,$CFLAGS) && ($defs << '-DHAVE_GCC_CAS')
40+
try_run(<<CODE,$CFLAGS) && ($defs << '-DHAVE_GCC_SYNC')
4141
int main() {
42-
int i = 1;
43-
__sync_bool_compare_and_swap(&i, 1, 4);
44-
return (i != 4);
42+
__sync_synchronize();
43+
return 0;
4544
}
4645
CODE
4746

0 commit comments

Comments
 (0)