]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Check for and use __sync_sub_and_fetch() and such for atomic decrement.
authorAlex Rousskov <rousskov@measurement-factory.com>
Thu, 15 Sep 2011 03:44:50 +0000 (21:44 -0600)
committerAlex Rousskov <rousskov@measurement-factory.com>
Thu, 15 Sep 2011 03:44:50 +0000 (21:44 -0600)
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!

configure.ac
src/ipc/AtomicWord.h

index b46685ea051b7edf71c326fc7fb7071c1ac4656f..0e87181c13bfe0fb0530095dce73a96eff53b74e 100644 (file)
@@ -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;
 ]])],
index 04f42fffb8a6523b202efc0c53dcb86be54cd388..046b24ea60f24a1f6d7b76e5f839b7428bc8bc9c 100644 (file)
@@ -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); }