Fixes misc test cases
[libcds.git] / test / stress / 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 <thread>
11
12 using namespace std;
13
14 namespace {
15
16 typedef cds_others::TicketLock TicketLock;
17 typedef cds::sync::spin SpinLock;
18 typedef cds::sync::reentrant_spin32 Reentrant32;
19 typedef cds::sync::reentrant_spin64 Reentrant64;
20 static size_t s_nSpinLockThreadCount = 6;
21 static size_t s_nSpinLockPassCount = 2500000000;
22 static size_t s_nTicketLockPassCount = 4000000;
23
24 #define TASK(lock_type, lock_ptr, pass_cnt)                                    \
25   static void Thread##lock_type() {                                            \
26     for (int i = 0; i < pass_cnt; i++) {                                       \
27       lock_ptr->lock();                                                        \
28       x++;                                                                     \
29       lock_ptr->unlock();                                                      \
30     }                                                                          \
31   }
32
33 #define LOCK_TEST(lock_type, lock_ptr, pass_cnt)                               \
34   TEST_F(SpinLockTest, lock_type) {                                            \
35     lock_ptr = new lock_type();                                                \
36     x = 0;                                                                     \
37     std::thread *threads = new std::thread[s_nSpinLockThreadCount];            \
38     for (int i = 0; i < s_nSpinLockThreadCount; i++) {                         \
39       threads[i] = std::thread(Thread##lock_type);                             \
40     }                                                                          \
41     for (int i = 0; i < s_nSpinLockThreadCount; i++) {                         \
42       threads[i].join();                                                       \
43     }                                                                          \
44     if (x != s_nSpinLockThreadCount * pass_cnt) {                              \
45       cout << "Incorrect " << #lock_type << "\n";                              \
46     }                                                                          \
47   }
48
49 class SpinLockTest : public cds_test::stress_fixture {
50 protected:
51   static int x;
52   static TicketLock *ticket_mutex;
53   static SpinLock *spin_mutex;
54   static Reentrant32 *reentrant_mutex32;
55   static Reentrant64 *reentrant_mutex64;
56
57   static void SetUpTestCase() {
58     cds_test::config const &cfg = get_config("Misc");
59     GetConfig(SpinLockThreadCount);
60     GetConfig(SpinLockPassCount);
61     GetConfig(TicketLockPassCount);
62   }
63
64   TASK(TicketLock, ticket_mutex, s_nTicketLockPassCount)
65   TASK(SpinLock, spin_mutex, s_nSpinLockPassCount)
66   TASK(Reentrant32, reentrant_mutex32, s_nSpinLockPassCount)
67   TASK(Reentrant64, reentrant_mutex64, s_nSpinLockPassCount)
68 };
69
70 int SpinLockTest::x;
71 TicketLock *SpinLockTest::ticket_mutex;
72 SpinLock *SpinLockTest::spin_mutex;
73 Reentrant32 *SpinLockTest::reentrant_mutex32;
74 Reentrant64 *SpinLockTest::reentrant_mutex64;
75
76 LOCK_TEST(TicketLock, ticket_mutex, s_nTicketLockPassCount)
77 LOCK_TEST(SpinLock, spin_mutex, s_nSpinLockPassCount)
78 LOCK_TEST(Reentrant32, reentrant_mutex32, s_nSpinLockPassCount)
79 LOCK_TEST(Reentrant64, reentrant_mutex64, s_nSpinLockPassCount)
80
81 } // namespace