e2b6de9aa36fcdc509419ae089eab0990157fbc0
[libcds.git] / test / stress / misc / mcslock_driver.cpp
1 #include <atomic>
2 #include <cds/gc/dhp.h>
3 #include <cds/gc/hp.h>
4 #include <cds/sync/mcs-lock.h>
5 #include <cds_test/stress_test.h>
6 #include <iostream>
7 #include <thread>
8
9 using namespace std;
10
11 namespace {
12
13 class MCSLockTest : public cds_test::stress_fixture {
14 protected:
15   static int x;
16   static cds_others::mcs_mutex *my_mutex;
17   static const int kThreads = 6;
18
19   static void SetUpTestCase() {}
20
21   static void Thread() {
22     cds_others::mcs_mutex::guard g(my_mutex);
23     x = 1;
24     my_mutex->unlock(&g);
25     for (int i = 0; i < 10000; i++) {
26       for (int j = 0; j < 300; j++) {
27         my_mutex->lock(&g);
28         x = i + j;
29         my_mutex->unlock(&g);
30       }
31     }
32     my_mutex->lock(&g);
33   }
34 };
35
36 int MCSLockTest::x;
37 cds_others::mcs_mutex *MCSLockTest::my_mutex;
38 const int MCSLockTest::kThreads;
39
40 TEST_F(MCSLockTest, BasicLockUnlock) {
41   my_mutex = new cds_others::mcs_mutex();
42   std::thread threads[kThreads];
43   for (int i = 0; i < kThreads; i++) {
44     threads[i] = std::thread(Thread);
45   }
46
47   for (int i = 0; i < kThreads; i++) {
48     threads[i].join();
49   }
50 }
51
52 } // namespace