2 * Copyright 2015 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 <gtest/gtest.h>
21 using folly::SpinLockGuardImpl;
25 template <typename LOCK>
31 memset(ar, 0, sizeof ar);
35 template <typename LOCK>
36 void spinlockTestThread(LockedVal<LOCK>* v) {
38 unsigned int seed = (uintptr_t)pthread_self();
39 for (int i = 0; i < max; i++) {
41 SpinLockGuardImpl<LOCK> g(v->lock);
44 for (size_t i = 1; i < sizeof v->ar / sizeof i; ++i) {
45 EXPECT_EQ(first, v->ar[i]);
48 int byte = rand_r(&seed);
49 memset(v->ar, char(byte), sizeof v->ar);
53 template <typename LOCK>
62 template <typename LOCK>
63 void trylockTestThread(TryLockState<LOCK>* state, size_t count) {
66 SpinLockGuardImpl<LOCK> g(state->lock1);
67 if (state->obtained >= count) {
71 bool ret = state->lock2.trylock();
72 EXPECT_NE(state->locked, ret);
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();
88 state->locked = false;
89 state->lock2.unlock();
96 template <typename LOCK>
97 void correctnessTest() {
98 int nthrs = sysconf(_SC_NPROCESSORS_ONLN) * 2;
99 std::vector<std::thread> threads;
101 for (int i = 0; i < nthrs; ++i) {
102 threads.push_back(std::thread(spinlockTestThread<LOCK>, &v));
104 for (auto& t : threads) {
109 template <typename LOCK>
111 int nthrs = sysconf(_SC_NPROCESSORS_ONLN) + 4;
112 std::vector<std::thread> threads;
113 TryLockState<LOCK> state;
115 for (int i = 0; i < nthrs; ++i) {
116 threads.push_back(std::thread(trylockTestThread<LOCK>, &state, count));
118 for (auto& t : threads) {
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);
129 } // unnamed namespace
132 TEST(SpinLock, MslCorrectness) {
133 correctnessTest<folly::SpinLockMslImpl>();
135 TEST(SpinLock, MslTryLock) {
136 trylockTest<folly::SpinLockMslImpl>();
141 TEST(SpinLock, AppleCorrectness) {
142 correctnessTest<folly::SpinLockAppleImpl>();
144 TEST(SpinLock, AppleTryLock) {
145 trylockTest<folly::SpinLockAppleImpl>();
149 #if FOLLY_HAVE_PTHREAD_SPINLOCK_T
150 TEST(SpinLock, PthreadCorrectness) {
151 correctnessTest<folly::SpinLockPthreadImpl>();
153 TEST(SpinLock, PthreadTryLock) {
154 trylockTest<folly::SpinLockPthreadImpl>();
158 TEST(SpinLock, MutexCorrectness) {
159 correctnessTest<folly::SpinLockPthreadMutexImpl>();
161 TEST(SpinLock, MutexTryLock) {
162 trylockTest<folly::SpinLockPthreadMutexImpl>();