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

Commit

Permalink
Merge pull request #4 from alexdowad/volatile_semantics_for_get_set
Browse files Browse the repository at this point in the history
Volatile semantics for #get, #set, and #swap
  • Loading branch information
jdantonio committed Nov 15, 2014
2 parents 3cd3b43 + a762883 commit 9e720c8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
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

0 comments on commit 9e720c8

Please sign in to comment.