2 * Copyright 2016 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <folly/SpinLock.h>
18 #include <folly/Random.h>
20 #include <gtest/gtest.h>
23 #include <folly/portability/Asm.h>
25 using folly::SpinLockGuardImpl;
29 template <typename LOCK>
35 memset(ar, 0, sizeof ar);
39 template <typename LOCK>
40 void spinlockTestThread(LockedVal<LOCK>* v) {
42 auto rng = folly::ThreadLocalPRNG();
43 for (int i = 0; i < max; i++) {
45 SpinLockGuardImpl<LOCK> g(v->lock);
48 for (size_t i = 1; i < sizeof v->ar / sizeof i; ++i) {
49 EXPECT_EQ(first, v->ar[i]);
52 int byte = folly::Random::rand32(rng);
53 memset(v->ar, char(byte), sizeof v->ar);
57 template <typename LOCK>
66 template <typename LOCK>
67 void trylockTestThread(TryLockState<LOCK>* state, size_t count) {
70 SpinLockGuardImpl<LOCK> g(state->lock1);
71 if (state->obtained >= count) {
75 bool ret = state->lock2.trylock();
76 EXPECT_NE(state->locked, ret);
83 // Release lock1 and wait until at least one other thread fails to
84 // obtain the lock2 before continuing.
85 auto oldFailed = state->failed;
86 while (state->failed == oldFailed && state->obtained < count) {
87 state->lock1.unlock();
92 state->locked = false;
93 state->lock2.unlock();
100 template <typename LOCK>
101 void correctnessTest() {
102 int nthrs = sysconf(_SC_NPROCESSORS_ONLN) * 2;
103 std::vector<std::thread> threads;
105 for (int i = 0; i < nthrs; ++i) {
106 threads.push_back(std::thread(spinlockTestThread<LOCK>, &v));
108 for (auto& t : threads) {
113 template <typename LOCK>
115 int nthrs = sysconf(_SC_NPROCESSORS_ONLN) + 4;
116 std::vector<std::thread> threads;
117 TryLockState<LOCK> state;
119 for (int i = 0; i < nthrs; ++i) {
120 threads.push_back(std::thread(trylockTestThread<LOCK>, &state, count));
122 for (auto& t : threads) {
126 EXPECT_EQ(count, state.obtained);
127 // Each time the code obtains lock2 it waits for another thread to fail
128 // to acquire it. The only time this might not happen is on the very last
129 // loop when no other threads are left.
130 EXPECT_GE(state.failed + 1, state.obtained);
133 } // unnamed namespace
136 TEST(SpinLock, MslCorrectness) {
137 correctnessTest<folly::SpinLockMslImpl>();
139 TEST(SpinLock, MslTryLock) {
140 trylockTest<folly::SpinLockMslImpl>();
145 TEST(SpinLock, AppleCorrectness) {
146 correctnessTest<folly::SpinLockAppleImpl>();
148 TEST(SpinLock, AppleTryLock) {
149 trylockTest<folly::SpinLockAppleImpl>();
153 #if FOLLY_HAVE_PTHREAD_SPINLOCK_T
154 TEST(SpinLock, PthreadCorrectness) {
155 correctnessTest<folly::SpinLockPthreadImpl>();
157 TEST(SpinLock, PthreadTryLock) {
158 trylockTest<folly::SpinLockPthreadImpl>();
162 TEST(SpinLock, MutexCorrectness) {
163 correctnessTest<folly::SpinLockPthreadMutexImpl>();
165 TEST(SpinLock, MutexTryLock) {
166 trylockTest<folly::SpinLockPthreadMutexImpl>();