[PATCH] [C++11] Replace LLVM atomics with std::atomic.

Benjamin Kramer benny.kra at gmail.com
Sun Mar 2 17:25:30 PST 2014



================
Comment at: include/llvm/ADT/Statistic.h:100-113
@@ -101,8 +99,16 @@
   const Statistic &operator*=(const unsigned &V) {
-    sys::AtomicMul(&Value, V);
+    unsigned Original, Result;
+    do {
+      Original = Value;
+      Result = Original * V;
+    } while (!Value.compare_exchange_strong(Original, Result));
     return init();
   }
 
   const Statistic &operator/=(const unsigned &V) {
-    sys::AtomicDiv(&Value, V);
+    unsigned Original, Result;
+    do {
+      Original = Value;
+      Result = Original / V;
+    } while (!Value.compare_exchange_strong(Original, Result));
     return init();
----------------
Ahmed Charles wrote:
> I read the standard on the definition of compare_exchange_strong and compare_exchange_weak and I don't seem to know enough to decide one way or another. I'm just curious about the rationale applied here.
This code was adapted from Atomic.cpp, which uses __sync_val_compare_and_swap (equivalent to the strong version) on GCC-style platforms and InterlockedCompareExchange on windows.

The strong cmpxchg is a superset of the weak one and it turns out that when compiled with clang both functions result in exactly the same IR being generated. The weak form was probably never implemented.


http://llvm-reviews.chandlerc.com/D2915



More information about the llvm-commits mailing list