]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | |
2 | #include <future> | |
3 | ||
7c673cae FG |
4 | #include "gtest/gtest.h" |
5 | ||
11fdf7f2 | 6 | #include "include/spinlock.h" |
7c673cae | 7 | |
11fdf7f2 TL |
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; | |
7c673cae FG |
13 | |
14 | TEST(SimpleSpin, Test0) | |
15 | { | |
16 | std::atomic_flag lock0 = ATOMIC_FLAG_INIT; | |
11fdf7f2 TL |
17 | spin_lock(&lock0); |
18 | spin_unlock(&lock0); | |
7c673cae FG |
19 | } |
20 | ||
7c673cae FG |
21 | static void* mythread(void *v) |
22 | { | |
23 | for (int j = 0; j < 1000000; ++j) { | |
11fdf7f2 | 24 | spin_lock(&lock); |
7c673cae | 25 | counter++; |
11fdf7f2 | 26 | spin_unlock(&lock); |
7c673cae FG |
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 | ||
7c673cae FG |
49 | // Should also work with pass-by-reference: |
50 | // (Note that we don't care about cross-threading here as-such.) | |
51 | counter = 0; | |
9f95a23c | 52 | auto f = async(std::launch::async, []() { |
7c673cae | 53 | for(int i = 0; n != i; ++i) { |
11fdf7f2 | 54 | spin_lock(lock); |
7c673cae | 55 | counter++; |
11fdf7f2 | 56 | spin_unlock(lock); |
7c673cae FG |
57 | } |
58 | }); | |
9f95a23c | 59 | f.wait(); |
7c673cae FG |
60 | ASSERT_EQ(n, counter); |
61 | } | |
62 | ||
11fdf7f2 TL |
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; | |
9f95a23c | 115 | |
11fdf7f2 | 116 | counter = 0; |
9f95a23c | 117 | auto f = async(std::launch::async, [&sl]() { |
11fdf7f2 TL |
118 | for(int i = 0; n != i; ++i) { |
119 | std::lock_guard<ceph::spinlock> g(sl); | |
120 | counter++; | |
121 | } | |
122 | }); | |
123 | ||
9f95a23c | 124 | auto g = async(std::launch::async, [&sl]() { |
11fdf7f2 TL |
125 | for(int i = 0; n != i; ++i) { |
126 | std::lock_guard<ceph::spinlock> g(sl); | |
127 | counter++; | |
128 | } | |
129 | }); | |
130 | ||
9f95a23c TL |
131 | f.wait(); |
132 | g.wait(); | |
11fdf7f2 TL |
133 | ASSERT_EQ(2*n, counter); |
134 | } | |
135 |