ARM64 assembler fixes for Folly.
[folly.git] / folly / test / SpinLockTest.cpp
1 /*
2  * Copyright 2015 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <folly/SpinLock.h>
17
18 #include <gtest/gtest.h>
19 #include <thread>
20
21 using folly::SpinLockGuardImpl;
22
23 namespace {
24
25 template <typename LOCK>
26 struct LockedVal {
27   int ar[1024];
28   LOCK lock;
29
30   LockedVal() {
31     memset(ar, 0, sizeof ar);
32   }
33 };
34
35 template <typename LOCK>
36 void spinlockTestThread(LockedVal<LOCK>* v) {
37   const int max = 1000;
38   unsigned int seed = (uintptr_t)pthread_self();
39   for (int i = 0; i < max; i++) {
40     folly::asm_pause();
41     SpinLockGuardImpl<LOCK> g(v->lock);
42
43     int first = v->ar[0];
44     for (size_t i = 1; i < sizeof v->ar / sizeof i; ++i) {
45       EXPECT_EQ(first, v->ar[i]);
46     }
47
48     int byte = rand_r(&seed);
49     memset(v->ar, char(byte), sizeof v->ar);
50   }
51 }
52
53 template <typename LOCK>
54 struct TryLockState {
55   LOCK lock1;
56   LOCK lock2;
57   bool locked{false};
58   uint64_t obtained{0};
59   uint64_t failed{0};
60 };
61
62 template <typename LOCK>
63 void trylockTestThread(TryLockState<LOCK>* state, size_t count) {
64   while (true) {
65     folly::asm_pause();
66     SpinLockGuardImpl<LOCK> g(state->lock1);
67     if (state->obtained >= count) {
68       break;
69     }
70
71     bool ret = state->lock2.trylock();
72     EXPECT_NE(state->locked, ret);
73
74     if (ret) {
75       // We got lock2.
76       ++state->obtained;
77       state->locked = true;
78
79       // Release lock1 and wait until at least one other thread fails to
80       // obtain the lock2 before continuing.
81       auto oldFailed = state->failed;
82       while (state->failed == oldFailed && state->obtained < count) {
83         state->lock1.unlock();
84         folly::asm_pause();
85         state->lock1.lock();
86       }
87
88       state->locked = false;
89       state->lock2.unlock();
90     } else {
91       ++state->failed;
92     }
93   }
94 }
95
96 template <typename LOCK>
97 void correctnessTest() {
98   int nthrs = sysconf(_SC_NPROCESSORS_ONLN) * 2;
99   std::vector<std::thread> threads;
100   LockedVal<LOCK> v;
101   for (int i = 0; i < nthrs; ++i) {
102     threads.push_back(std::thread(spinlockTestThread<LOCK>, &v));
103   }
104   for (auto& t : threads) {
105     t.join();
106   }
107 }
108
109 template <typename LOCK>
110 void trylockTest() {
111   int nthrs = sysconf(_SC_NPROCESSORS_ONLN) + 4;
112   std::vector<std::thread> threads;
113   TryLockState<LOCK> state;
114   size_t count = 100;
115   for (int i = 0; i < nthrs; ++i) {
116     threads.push_back(std::thread(trylockTestThread<LOCK>, &state, count));
117   }
118   for (auto& t : threads) {
119     t.join();
120   }
121
122   EXPECT_EQ(count, state.obtained);
123   // Each time the code obtains lock2 it waits for another thread to fail
124   // to acquire it.  The only time this might not happen is on the very last
125   // loop when no other threads are left.
126   EXPECT_GE(state.failed + 1, state.obtained);
127 }
128
129 } // unnamed namespace
130
131 #if __x86_64__
132 TEST(SpinLock, MslCorrectness) {
133   correctnessTest<folly::SpinLockMslImpl>();
134 }
135 TEST(SpinLock, MslTryLock) {
136   trylockTest<folly::SpinLockMslImpl>();
137 }
138 #endif
139
140 #if __APPLE__
141 TEST(SpinLock, AppleCorrectness) {
142   correctnessTest<folly::SpinLockAppleImpl>();
143 }
144 TEST(SpinLock, AppleTryLock) {
145   trylockTest<folly::SpinLockAppleImpl>();
146 }
147 #endif
148
149 #if FOLLY_HAVE_PTHREAD_SPINLOCK_T
150 TEST(SpinLock, PthreadCorrectness) {
151   correctnessTest<folly::SpinLockPthreadImpl>();
152 }
153 TEST(SpinLock, PthreadTryLock) {
154   trylockTest<folly::SpinLockPthreadImpl>();
155 }
156 #endif
157
158 TEST(SpinLock, MutexCorrectness) {
159   correctnessTest<folly::SpinLockPthreadMutexImpl>();
160 }
161 TEST(SpinLock, MutexTryLock) {
162   trylockTest<folly::SpinLockPthreadMutexImpl>();
163 }