Reorgs added benchmarks (put them in misc folder)
[libcds.git] / test / stress / sequential / sequential-misc / mcslock_driver.cpp
1 #include "common.h"
2 #include <atomic>
3 #include <cds/gc/dhp.h>
4 #include <cds/gc/hp.h>
5 #include <cds/misc/mcs-lock.h>
6 #include <cds_test/stress_test.h>
7 #include <iostream>
8 #include <memory>
9 #include <thread>
10
11 using namespace std;
12
13 namespace {
14
15 static size_t s_nMCSLockThreadCount = 6;
16 static size_t s_nMCSLockPassCount = 3000000;
17
18 class MCSLockTest : public cds_test::stress_fixture {
19 protected:
20   static ullong x;
21   static cds_others::mcs_mutex *my_mutex;
22
23   static void SetUpTestCase() {
24     cds_test::config const &cfg = get_config("SequentialMisc");
25     GetConfig(MCSLockPassCount);
26     GetConfig(MCSLockThreadCount);
27   }
28
29   static void Thread() {
30     cds_others::mcs_mutex::guard g(my_mutex);
31     my_mutex->unlock(&g);
32     for (ullong i = 0; i < s_nMCSLockPassCount; i++) {
33       my_mutex->lock(&g);
34       x++;
35       my_mutex->unlock(&g);
36     }
37     my_mutex->lock(&g);
38   }
39 };
40
41 ullong MCSLockTest::x;
42 cds_others::mcs_mutex *MCSLockTest::my_mutex;
43
44 TEST_F(MCSLockTest, MCSLock) {
45   my_mutex = new cds_others::mcs_mutex();
46   cds_others::mcs_mutex::guard g(my_mutex);
47   my_mutex->unlock(&g);
48   for (size_t i = 0; i < s_nMCSLockPassCount; i++) {
49     my_mutex->lock(&g);
50     x++;
51     my_mutex->unlock(&g);
52   }
53   my_mutex->lock(&g);
54 }
55
56 } // namespace