Adds sequential misc test cases
[libcds.git] / test / stress / sequential / sequential-misc / rwlock_driver.cpp
diff --git a/test/stress/sequential/sequential-misc/rwlock_driver.cpp b/test/stress/sequential/sequential-misc/rwlock_driver.cpp
new file mode 100644 (file)
index 0000000..61dc989
--- /dev/null
@@ -0,0 +1,73 @@
+#include "common.h"
+#include <atomic>
+#include <cds/gc/dhp.h>
+#include <cds/gc/hp.h>
+#include <cds/sync/rwlock.h>
+#include <cds_test/stress_test.h>
+#include <iostream>
+#include <memory>
+#include <thread>
+
+using namespace std;
+
+namespace {
+
+static size_t s_nRWLockThreadCount = 6;
+static size_t s_nRWLockPassCount = 200000;
+
+typedef cds_others::RWLock RWLock;
+class RWLockTest : public cds_test::stress_fixture {
+protected:
+  static size_t sum;
+  static size_t x;
+  static RWLock *rwlock;
+
+  static void SetUpTestCase() {
+    cds_test::config const &cfg = get_config("SequentialMisc");
+    GetConfig(RWLockThreadCount);
+    GetConfig(RWLockPassCount);
+  }
+
+  static void ReaderWriterThread(int write_percentage) {
+    for (size_t i = 0; i < s_nRWLockPassCount; i++) {
+      if (rand(100) < write_percentage) {
+        if (rwlock->read_can_lock()) {
+          if (!rwlock->read_trylock()) {
+            rwlock->read_lock();
+          }
+          sum += x;
+          rwlock->read_unlock();
+        } else {
+          rwlock->read_lock();
+          sum += x;
+          rwlock->read_unlock();
+        }
+      } else {
+        if (rwlock->write_can_lock()) {
+          if (!rwlock->write_trylock()) {
+            rwlock->write_lock();
+          }
+          x++;
+          rwlock->write_unlock();
+        } else {
+          rwlock->write_lock();
+          x++;
+          rwlock->write_unlock();
+        }
+      }
+    }
+  }
+};
+
+size_t RWLockTest::x;
+size_t RWLockTest::sum;
+RWLock *RWLockTest::rwlock;
+
+TEST_F(RWLockTest, ReadWriteLock) {
+  rwlock = new RWLock();
+  for (int write_percentage = 5; write_percentage < 40; write_percentage += 5) {
+    ReaderWriterThread(write_percentage);
+  }
+}
+
+} // namespace