From c658a2be7355ceee72736cc17754022dc7abfa9f Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sat, 18 Sep 2010 06:43:19 +0000 Subject: allow using libatomic_ops headers from HP This allows non-GCC 4.x users to experience Raindrops. --- TODO | 1 - ext/raindrops/extconf.rb | 26 ++++++++++++++++++++++++++ ext/raindrops/linux_inet_diag.c | 2 +- ext/raindrops/raindrops.c | 1 + ext/raindrops/raindrops_atomic.h | 23 +++++++++++++++++++++++ 5 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 ext/raindrops/raindrops_atomic.h diff --git a/TODO b/TODO index 6935888..33f360a 100644 --- a/TODO +++ b/TODO @@ -1,2 +1 @@ -* more portable atomics for non-GCC systems (libatomicops?) * pure Ruby version for non-forking servers diff --git a/ext/raindrops/extconf.rb b/ext/raindrops/extconf.rb index d637287..09d6e36 100644 --- a/ext/raindrops/extconf.rb +++ b/ext/raindrops/extconf.rb @@ -7,5 +7,31 @@ have_func('munmap', 'sys/mman.h') or abort 'munmap() not found' have_func("rb_struct_alloc_noinit") have_func('rb_thread_blocking_region') +checking_for "GCC 4+ atomic builtins" do + src = < #include #include +#include "raindrops_atomic.h" /* * most modern CPUs have a cache-line size of 64 or 128. diff --git a/ext/raindrops/raindrops_atomic.h b/ext/raindrops/raindrops_atomic.h new file mode 100644 index 0000000..fd5f23b --- /dev/null +++ b/ext/raindrops/raindrops_atomic.h @@ -0,0 +1,23 @@ +/* + * use wrappers around libatomic-ops for folks that don't have GCC + * or a new enough version of GCC + */ +#ifndef HAVE_GCC_ATOMIC_BUILTINS +#include + +static inline unsigned long +__sync_add_and_fetch(unsigned long *dst, unsigned long incr) +{ + AO_t tmp = AO_fetch_and_add((AO_t *)dst, (AO_t)incr); + + return (unsigned long)tmp + incr; +} + +static inline unsigned long +__sync_sub_and_fetch(unsigned long *dst, unsigned long incr) +{ + AO_t tmp = AO_fetch_and_add((AO_t *)dst, (AO_t)(-(long)incr)); + + return (unsigned long)tmp - incr; +} +#endif /* HAVE_GCC_ATOMIC_BUILTINS */ -- cgit v1.2.3-24-ge0c7