Adds misc benchmarks
[libcds.git] / test / stress / misc / mcslock_driver.cpp
diff --git a/test/stress/misc/mcslock_driver.cpp b/test/stress/misc/mcslock_driver.cpp
new file mode 100644 (file)
index 0000000..e2b6de9
--- /dev/null
@@ -0,0 +1,52 @@
+#include <atomic>
+#include <cds/gc/dhp.h>
+#include <cds/gc/hp.h>
+#include <cds/sync/mcs-lock.h>
+#include <cds_test/stress_test.h>
+#include <iostream>
+#include <thread>
+
+using namespace std;
+
+namespace {
+
+class MCSLockTest : public cds_test::stress_fixture {
+protected:
+  static int x;
+  static cds_others::mcs_mutex *my_mutex;
+  static const int kThreads = 6;
+
+  static void SetUpTestCase() {}
+
+  static void Thread() {
+    cds_others::mcs_mutex::guard g(my_mutex);
+    x = 1;
+    my_mutex->unlock(&g);
+    for (int i = 0; i < 10000; i++) {
+      for (int j = 0; j < 300; j++) {
+        my_mutex->lock(&g);
+        x = i + j;
+        my_mutex->unlock(&g);
+      }
+    }
+    my_mutex->lock(&g);
+  }
+};
+
+int MCSLockTest::x;
+cds_others::mcs_mutex *MCSLockTest::my_mutex;
+const int MCSLockTest::kThreads;
+
+TEST_F(MCSLockTest, BasicLockUnlock) {
+  my_mutex = new cds_others::mcs_mutex();
+  std::thread threads[kThreads];
+  for (int i = 0; i < kThreads; i++) {
+    threads[i] = std::thread(Thread);
+  }
+
+  for (int i = 0; i < kThreads; i++) {
+    threads[i].join();
+  }
+}
+
+} // namespace