Refactors misc test cases
[libcds.git] / cds / sync / seqlock.h
index a84c0c4..7427053 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef _SEQLOCK_H
 #define _SEQLOCK_H
 
+#include "backoff.h"
 #include <atomic>
 
 namespace cds_others {
@@ -24,30 +25,37 @@ public:
   }
 
   int read() {
+    ExpBackoff backoff;
     while (true) {
-      int old_seq = seq_.load(memory_order_acquire); // acquire
-      if (old_seq % 2 == 1)
+      int old_seq = seq_.load(memory_order_acquire);
+      if (old_seq % 2 == 1) {
+        backoff();
         continue;
+      }
 
-      int res = data_.load(memory_order_acquire);       // acquire
-      if (seq_.load(memory_order_relaxed) == old_seq) { // relaxed
+      int res = data_.load(memory_order_acquire);
+      if (seq_.load(memory_order_relaxed) == old_seq) {
         return res;
       }
     }
   }
 
   void write(int new_data) {
+    ExpBackoff backoff;
     while (true) {
       // This might be a relaxed too
-      int old_seq = seq_.load(memory_order_acquire); // acquire
-      if (old_seq % 2 == 1)
+      int old_seq = seq_.load(memory_order_acquire);
+      if (old_seq % 2 == 1) {
+        backoff();
         continue; // Retry
+      }
 
       // Should be relaxed!!!
       if (seq_.compare_exchange_strong(old_seq, old_seq + 1,
                                        memory_order_relaxed,
-                                       memory_order_relaxed)) // relaxed
+                                       memory_order_relaxed)) {
         break;
+      }
     }
 
     // Update the data