From: Alex Rousskov Date: Thu, 15 Sep 2011 03:44:50 +0000 (-0600) Subject: Check for and use __sync_sub_and_fetch() and such for atomic decrement. X-Git-Tag: BumpSslServerFirst.take01~153 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d92c39afcf222da9ff2f41abf127ccf15ff68094;p=thirdparty%2Fsquid.git Check for and use __sync_sub_and_fetch() and such for atomic decrement. This is possibly more efficient or perhaps even the only correct way to decrement atomically. It also helps ICC compiler happier. Somehow, I missed that __sync_fetch_and_sub() and such exist! --- diff --git a/configure.ac b/configure.ac index b46685ea05..0e87181c13 100644 --- a/configure.ac +++ b/configure.ac @@ -383,9 +383,10 @@ AC_MSG_CHECKING([for atomic operations support]) AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[ int n = 0; ]],[[ - __sync_add_and_fetch(&n, 1); // n becomes 1 - __sync_fetch_and_add(&n, 1); // n becomes 2 - __sync_bool_compare_and_swap(&n, 2, 201); // n becomes 201 + __sync_add_and_fetch(&n, 10); // n becomes 10 + __sync_fetch_and_add(&n, 20); // n becomes 30 + __sync_sub_and_fetch(&n, 15); // n becomes 15 + __sync_bool_compare_and_swap(&n, 15, 201); // n becomes 201 __sync_fetch_and_and(&n, 200); // n becomes 200 return (n == 200) ? 0 : -1; ]])], diff --git a/src/ipc/AtomicWord.h b/src/ipc/AtomicWord.h index 04f42fffb8..046b24ea60 100644 --- a/src/ipc/AtomicWord.h +++ b/src/ipc/AtomicWord.h @@ -17,10 +17,11 @@ public: AtomicWordT(Value aValue): value(aValue) {} // XXX: unsafe Value operator +=(int delta) { return __sync_add_and_fetch(&value, delta); } + Value operator -=(int delta) { return __sync_sub_and_fetch(&value, delta); } Value operator ++() { return *this += 1; } - Value operator --() { return *this += -1; } + Value operator --() { return *this -= 1; } Value operator ++(int) { return __sync_fetch_and_add(&value, 1); } - Value operator --(int) { return __sync_fetch_and_add(&value, -1); } + Value operator --(int) { return __sync_fetch_and_sub(&value, 1); } bool swap_if(const int comparand, const int replacement) { return __sync_bool_compare_and_swap(&value, comparand, replacement); }