bd2c42a39695f790c0a34e53bdb4604e7c17f65b
[libcds.git] / test / stress / sequential / sequential-misc / spinlock_driver.cpp
1 #include "common.h"
2 #include <atomic>
3 #include <cds/gc/dhp.h>
4 #include <cds/gc/hp.h>
5 #include <cds/sync/spinlock.h>
6 #include <cds/sync/ticket_lock.h>
7 #include <cds_test/stress_test.h>
8 #include <iostream>
9 #include <iostream>
10 #include <memory>
11 #include <thread>
12
13 using namespace std;
14
15 namespace {
16
17 typedef cds_others::TicketLock TicketLock;
18 typedef cds::sync::spin SpinLock;
19 typedef cds::sync::reentrant_spin32 Reentrant32;
20 typedef cds::sync::reentrant_spin64 Reentrant64;
21 static size_t s_nSpinLockThreadCount = 6;
22 static size_t s_nSpinLockPassCount = 2500000000;
23 static size_t s_nTicketLockPassCount = 4000000;
24
25 #define TASK(lock_type, lock_ptr, pass_cnt)                                    \
26   static void Thread##lock_type() {                                            \
27     for (size_t i = 0; i < pass_cnt; i++) {                                    \
28       lock_ptr->lock();                                                        \
29       x++;                                                                     \
30       lock_ptr->unlock();                                                      \
31     }                                                                          \
32   }
33
34 #define LOCK_TEST(lock_type, lock_ptr, pass_cnt)                               \
35   TEST_F(SpinLockTest, lock_type) {                                            \
36     lock_ptr = new lock_type();                                                \
37     x = 0;                                                                     \
38     Thread##lock_type();                                                       \
39   }
40
41 class SpinLockTest : public cds_test::stress_fixture {
42 protected:
43   static size_t x;
44   static TicketLock *ticket_mutex;
45   static SpinLock *spin_mutex;
46   static Reentrant32 *reentrant_mutex32;
47   static Reentrant64 *reentrant_mutex64;
48
49   static void SetUpTestCase() {
50     cds_test::config const &cfg = get_config("SequentialMisc");
51     GetConfig(SpinLockThreadCount);
52     GetConfig(SpinLockPassCount);
53     GetConfig(TicketLockPassCount);
54   }
55
56   TASK(TicketLock, ticket_mutex, s_nTicketLockPassCount)
57   TASK(SpinLock, spin_mutex, s_nSpinLockPassCount)
58   TASK(Reentrant32, reentrant_mutex32, s_nSpinLockPassCount)
59   TASK(Reentrant64, reentrant_mutex64, s_nSpinLockPassCount)
60 };
61
62 size_t SpinLockTest::x;
63 TicketLock *SpinLockTest::ticket_mutex;
64 SpinLock *SpinLockTest::spin_mutex;
65 Reentrant32 *SpinLockTest::reentrant_mutex32;
66 Reentrant64 *SpinLockTest::reentrant_mutex64;
67
68 LOCK_TEST(TicketLock, ticket_mutex, s_nTicketLockPassCount)
69 LOCK_TEST(SpinLock, spin_mutex, s_nSpinLockPassCount)
70 LOCK_TEST(Reentrant32, reentrant_mutex32, s_nSpinLockPassCount)
71 LOCK_TEST(Reentrant64, reentrant_mutex64, s_nSpinLockPassCount)
72
73 } // namespace