Skip to content

Commit

Permalink
properly detect GCC atomics
Browse files Browse the repository at this point in the history
I was naive. GCC atomics were added in 4.1.2, and not easily detectable
without configure tests. 32bit platforms, centos5, etc.
  • Loading branch information
dormando committed Jan 26, 2012
1 parent 85db132 commit de1f30c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
14 changes: 14 additions & 0 deletions configure.ac
Expand Up @@ -454,6 +454,20 @@ fi

AC_C_ALIGNMENT

dnl Check for our specific usage of GCC atomics.
dnl These were added in 4.1.2, but 32bit OS's may lack shorts and 4.1.2
dnl lacks testable defines.
have_gcc_atomics=no
AC_MSG_CHECKING(for GCC atomics)
AC_TRY_LINK([],[
unsigned short a;
unsigned short b;
b = __sync_add_and_fetch(&a, 1);
b = __sync_sub_and_fetch(&a, 2);
],[have_gcc_atomics=yes
AC_DEFINE(HAVE_GCC_ATOMICS, 1, [GCC Atomics available])])
AC_MSG_RESULT($have_gcc_atomics)

dnl Check for the requirements for running memcached with less privileges
dnl than the default privilege set. On Solaris we need setppriv and priv.h
dnl If you want to add support for other platforms you should check for
Expand Down
10 changes: 5 additions & 5 deletions thread.c
Expand Up @@ -43,7 +43,7 @@ pthread_mutex_t cache_lock;
/* Connection lock around accepting new connections */
pthread_mutex_t conn_lock = PTHREAD_MUTEX_INITIALIZER;

#if !defined(__GNUC__) && !defined(__sun)
#if !defined(HAVE_GCC_ATOMICS) && !defined(__sun)
pthread_mutex_t atomics_mutex = PTHREAD_MUTEX_INITIALIZER;
#endif

Expand Down Expand Up @@ -79,29 +79,29 @@ static pthread_cond_t init_cond;
static void thread_libevent_process(int fd, short which, void *arg);

inline unsigned short refcount_incr(unsigned short *refcount) {
#ifdef __GNUC__
#ifdef HAVE_GCC_ATOMICS
return __sync_add_and_fetch(refcount, 1);
#elif defined(__sun)
return atomic_inc_ushort_nv(refcount);
#else
unsigned short res;
mutex_lock(&atomics_mutex);
*refcount++;
(*refcount)++;
res = *refcount;
pthread_mutex_unlock(&atomics_mutex);
return res;
#endif
}

inline unsigned short refcount_decr(unsigned short *refcount) {
#ifdef __GNUC__
#ifdef HAVE_GCC_ATOMICS
return __sync_sub_and_fetch(refcount, 1);
#elif defined(__sun)
return atomic_dec_ushort_nv(refcount);
#else
unsigned short res;
mutex_lock(&atomics_mutex);
*refcount--;
(*refcount)--;
res = *refcount;
pthread_mutex_unlock(&atomics_mutex);
return res;
Expand Down

0 comments on commit de1f30c

Please sign in to comment.