]> git.proxmox.com Git - ceph.git/blobdiff - ceph/src/test/simple_spin.cc
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / test / simple_spin.cc
index 04b4fc07e5f6b6033d0196ac6ab16f6eca571d03..81194d84991531773e4d73ceecaf5592759181bb 100644 (file)
@@ -1,25 +1,29 @@
+
+#include <future>
+
 #include "gtest/gtest.h"
 
-#include "common/simple_spin.h"
+#include "include/spinlock.h"
 
-#include <future>
+using ceph::spin_lock;
+using ceph::spin_unlock;
+
+static std::atomic_flag lock = ATOMIC_FLAG_INIT;
+static int64_t counter = 0;
 
 TEST(SimpleSpin, Test0)
 {
   std::atomic_flag lock0 = ATOMIC_FLAG_INIT;
-  simple_spin_lock(&lock0);
-  simple_spin_unlock(&lock0);
+  spin_lock(&lock0);
+  spin_unlock(&lock0);
 }
 
-static std::atomic_flag lock = ATOMIC_FLAG_INIT;
-static uint32_t counter = 0;
-
 static void* mythread(void *v)
 {
   for (int j = 0; j < 1000000; ++j) {
-    simple_spin_lock(&lock);
+    spin_lock(&lock);
     counter++;
-    simple_spin_unlock(&lock);
+    spin_unlock(&lock);
   }
   return NULL;
 }
@@ -42,17 +46,87 @@ TEST(SimpleSpin, Test1)
   ASSERT_EQ(0, ret);
   ASSERT_EQ(n, counter);
 
-
   // Should also work with pass-by-reference:
   // (Note that we don't care about cross-threading here as-such.)
   counter = 0;
   async(std::launch::async, []() {
         for(int i = 0; n != i; ++i) {
-            simple_spin_lock(lock);
+            spin_lock(lock);
             counter++;
-            simple_spin_unlock(lock);
+            spin_unlock(lock);
         }
        });
   ASSERT_EQ(n, counter);
 }
 
+template <typename LockT>
+int64_t check_lock_unlock(const int64_t n, int64_t& cntr, LockT& lock)
+{
+ auto do_lock_unlock = [&]() -> int64_t {
+        int64_t i = 0;
+
+        for(; n != i; ++i) {
+           spin_lock(lock);
+           cntr++;
+           spin_unlock(lock);
+        }
+
+        return i;
+      };
+
+ auto fone   = async(std::launch::async, do_lock_unlock);
+ auto ftwo   = async(std::launch::async, do_lock_unlock);
+ auto fthree = async(std::launch::async, do_lock_unlock);
+
+ auto one = fone.get();
+ auto two = ftwo.get();
+ auto three = fthree.get();
+
+ // Google test doesn't like us using its macros out of individual tests, so:
+ if(n != one || n != two || n != three)
+  return 0;
+
+ return one + two + three;
+}
+
+TEST(SimpleSpin, Test2)
+{
+ const auto n = 2000000U;
+
+ // ceph::spinlock:
+ {
+ counter = 0;
+ ceph::spinlock l;
+
+ ASSERT_EQ(0, counter);
+ auto result = check_lock_unlock(n, counter, l);
+ ASSERT_NE(0, counter);
+ ASSERT_EQ(counter, result);
+ }
+}
+
+// ceph::spinlock should work with std::lock_guard<>:
+TEST(SimpleSpin, spinlock_guard)
+{
+  const auto n = 2000000U;
+
+  ceph::spinlock sl;
+  counter = 0;
+  async(std::launch::async, [&sl]() {
+        for(int i = 0; n != i; ++i) {
+            std::lock_guard<ceph::spinlock> g(sl);
+            counter++;
+        }
+       });
+
+  async(std::launch::async, [&sl]() {
+        for(int i = 0; n != i; ++i) {
+            std::lock_guard<ceph::spinlock> g(sl);
+            counter++;
+        }
+       });
+
+  ASSERT_EQ(2*n, counter);
+}
+