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

Volatile semantics for #get, #set, and #swap #4

Merged
merged 5 commits into from
Nov 15, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions ext/atomic_reference.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,31 @@ static VALUE ir_initialize(int argc, VALUE* argv, VALUE self) {
}

static VALUE ir_get(VALUE self) {
#if HAVE_GCC_SYNC
__sync_synchronize();
#elif defined _MSC_VER
MemoryBarrier();
#elif __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1050
OSMemoryBarrier();
#endif
return (VALUE) DATA_PTR(self);
}

static VALUE ir_set(VALUE self, VALUE new_value) {
DATA_PTR(self) = (void *) new_value;
#if HAVE_GCC_SYNC
__sync_synchronize();
#elif defined _MSC_VER
MemoryBarrier();
#elif __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1050
OSMemoryBarrier();
#endif
return new_value;
}

static VALUE ir_get_and_set(VALUE self, VALUE new_value) {
VALUE old_value;
old_value = (VALUE) DATA_PTR(self);
DATA_PTR(self) = (void *) new_value;
VALUE old_value = ir_get(self);
ir_set(self, new_value);
return old_value;
}

Expand Down
7 changes: 3 additions & 4 deletions ext/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@ def compiler_is_gcc
end
end

try_run(<<CODE,$CFLAGS) && ($defs << '-DHAVE_GCC_CAS')
try_run(<<CODE,$CFLAGS) && ($defs << '-DHAVE_GCC_SYNC')
int main() {
int i = 1;
__sync_bool_compare_and_swap(&i, 1, 4);
return (i != 4);
__sync_synchronize();
return 0;
}
CODE

Expand Down