]> git.proxmox.com Git - ceph.git/blob - ceph/src/test/simple_spin.cc
buildsys: switch source download to quincy
[ceph.git] / ceph / src / test / simple_spin.cc
1
2 #include <future>
3
4 #include "gtest/gtest.h"
5
6 #include "include/spinlock.h"
7
8 using ceph::spin_lock;
9 using ceph::spin_unlock;
10
11 static std::atomic_flag lock = ATOMIC_FLAG_INIT;
12 static int64_t counter = 0;
13
14 TEST(SimpleSpin, Test0)
15 {
16 std::atomic_flag lock0 = ATOMIC_FLAG_INIT;
17 spin_lock(&lock0);
18 spin_unlock(&lock0);
19 }
20
21 static void* mythread(void *v)
22 {
23 for (int j = 0; j < 1000000; ++j) {
24 spin_lock(&lock);
25 counter++;
26 spin_unlock(&lock);
27 }
28 return NULL;
29 }
30
31 TEST(SimpleSpin, Test1)
32 {
33 counter = 0;
34 const auto n = 2000000U;
35
36 int ret;
37 pthread_t thread1;
38 pthread_t thread2;
39 ret = pthread_create(&thread1, NULL, mythread, NULL);
40 ASSERT_EQ(0, ret);
41 ret = pthread_create(&thread2, NULL, mythread, NULL);
42 ASSERT_EQ(0, ret);
43 ret = pthread_join(thread1, NULL);
44 ASSERT_EQ(0, ret);
45 ret = pthread_join(thread2, NULL);
46 ASSERT_EQ(0, ret);
47 ASSERT_EQ(n, counter);
48
49 // Should also work with pass-by-reference:
50 // (Note that we don't care about cross-threading here as-such.)
51 counter = 0;
52 auto f = async(std::launch::async, []() {
53 for(int i = 0; n != i; ++i) {
54 spin_lock(lock);
55 counter++;
56 spin_unlock(lock);
57 }
58 });
59 f.wait();
60 ASSERT_EQ(n, counter);
61 }
62
63 template <typename LockT>
64 int64_t check_lock_unlock(const int64_t n, int64_t& cntr, LockT& lock)
65 {
66 auto do_lock_unlock = [&]() -> int64_t {
67 int64_t i = 0;
68
69 for(; n != i; ++i) {
70 spin_lock(lock);
71 cntr++;
72 spin_unlock(lock);
73 }
74
75 return i;
76 };
77
78 auto fone = async(std::launch::async, do_lock_unlock);
79 auto ftwo = async(std::launch::async, do_lock_unlock);
80 auto fthree = async(std::launch::async, do_lock_unlock);
81
82 auto one = fone.get();
83 auto two = ftwo.get();
84 auto three = fthree.get();
85
86 // Google test doesn't like us using its macros out of individual tests, so:
87 if(n != one || n != two || n != three)
88 return 0;
89
90 return one + two + three;
91 }
92
93 TEST(SimpleSpin, Test2)
94 {
95 const auto n = 2000000U;
96
97 // ceph::spinlock:
98 {
99 counter = 0;
100 ceph::spinlock l;
101
102 ASSERT_EQ(0, counter);
103 auto result = check_lock_unlock(n, counter, l);
104 ASSERT_NE(0, counter);
105 ASSERT_EQ(counter, result);
106 }
107 }
108
109 // ceph::spinlock should work with std::lock_guard<>:
110 TEST(SimpleSpin, spinlock_guard)
111 {
112 const auto n = 2000000U;
113
114 ceph::spinlock sl;
115
116 counter = 0;
117 auto f = async(std::launch::async, [&sl]() {
118 for(int i = 0; n != i; ++i) {
119 std::lock_guard<ceph::spinlock> g(sl);
120 counter++;
121 }
122 });
123
124 auto g = async(std::launch::async, [&sl]() {
125 for(int i = 0; n != i; ++i) {
126 std::lock_guard<ceph::spinlock> g(sl);
127 counter++;
128 }
129 });
130
131 f.wait();
132 g.wait();
133 ASSERT_EQ(2*n, counter);
134 }
135