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.
17 #include <folly/SmallLocks.h>
19 #include <folly/Random.h>
24 #include <condition_variable>
32 #include <gtest/gtest.h>
34 using folly::MSLGuard;
35 using folly::MicroLock;
36 using folly::MicroSpinLock;
39 #ifdef FOLLY_PICO_SPIN_LOCK_H_
40 using folly::PicoSpinLock;
51 memset(ar, 0, sizeof ar);
55 // Compile time test for packed struct support (requires that both of
56 // these classes are POD).
58 struct ignore1 { MicroSpinLock msl; int16_t foo; } FOLLY_PACK_ATTR;
59 static_assert(sizeof(ignore1) == 3, "Size check failed");
60 static_assert(sizeof(MicroSpinLock) == 1, "Size check failed");
61 #ifdef FOLLY_PICO_SPIN_LOCK_H_
62 struct ignore2 { PicoSpinLock<uint32_t> psl; int16_t foo; } FOLLY_PACK_ATTR;
63 static_assert(sizeof(ignore2) == 6, "Size check failed");
71 auto rng = folly::ThreadLocalPRNG();
72 for (int i = 0; i < max; i++) {
77 for (size_t i = 1; i < sizeof v.ar / sizeof i; ++i) {
78 EXPECT_EQ(first, v.ar[i]);
81 int byte = folly::Random::rand32(rng);
82 memset(v.ar, char(byte), sizeof v.ar);
86 #ifdef FOLLY_PICO_SPIN_LOCK_H_
87 template<class T> struct PslTest {
90 PslTest() { lock.init(); }
93 T ourVal = rand() % (T(1) << (sizeof(T) * 8 - 1));
94 for (int i = 0; i < 10000; ++i) {
95 std::lock_guard<PicoSpinLock<T>> guard(lock);
97 for (int n = 0; n < 10; ++n) {
98 folly::asm_volatile_pause();
99 EXPECT_EQ(lock.getData(), ourVal);
109 const int nthrs = 17;
110 std::vector<std::thread> threads;
111 for (int i = 0; i < nthrs; ++i) {
112 threads.push_back(std::thread(&PslTest<T>::doTest, &testObj));
114 for (auto& t : threads) {
126 std::lock_guard<MicroSpinLock> g(lock_);
127 // This bug depends on gcc register allocation and is very sensitive. We
128 // have to use DCHECK instead of EXPECT_*.
129 DCHECK(!lock_.try_lock());
138 TEST(SmallLocks, SpinLockCorrectness) {
139 EXPECT_EQ(sizeof(MicroSpinLock), 1);
141 int nthrs = sysconf(_SC_NPROCESSORS_ONLN) * 2;
142 std::vector<std::thread> threads;
143 for (int i = 0; i < nthrs; ++i) {
144 threads.push_back(std::thread(splock_test));
146 for (auto& t : threads) {
151 #ifdef FOLLY_PICO_SPIN_LOCK_H_
152 TEST(SmallLocks, PicoSpinCorrectness) {
153 doPslTest<int16_t>();
154 doPslTest<uint16_t>();
155 doPslTest<int32_t>();
156 doPslTest<uint32_t>();
157 doPslTest<int64_t>();
158 doPslTest<uint64_t>();
161 TEST(SmallLocks, PicoSpinSigned) {
162 typedef PicoSpinLock<int16_t,0> Lock;
165 EXPECT_EQ(val.getData(), -4);
168 std::lock_guard<Lock> guard(val);
169 EXPECT_EQ(val.getData(), -4);
171 EXPECT_EQ(val.getData(), -8);
173 EXPECT_EQ(val.getData(), -8);
177 TEST(SmallLocks, RegClobber) {
182 static_assert(sizeof(MicroLock) == 1, "Size check failed");
187 struct SimpleBarrier {
189 SimpleBarrier() : lock_(), cv_(), ready_(false) {}
192 std::unique_lock<std::mutex> lockHeld(lock_);
200 std::unique_lock<std::mutex> lockHeld(lock_);
209 std::condition_variable cv_;
214 static void runMicroLockTest() {
215 volatile uint64_t counters[4] = {0, 0, 0, 0};
216 std::vector<std::thread> threads;
217 static const unsigned nrThreads = 20;
218 static const unsigned iterPerThread = 10000;
219 SimpleBarrier startBarrier;
221 assert(iterPerThread % 4 == 0);
223 // Embed the lock in a larger structure to ensure that we do not
224 // affect bits outside the ones MicroLock is defined to affect.
243 // This thread touches other parts of the host word to show that
244 // MicroLock does not interfere with memory outside of the byte
246 std::thread adjacentMemoryToucher = std::thread([&] {
248 for (unsigned iter = 0; iter < iterPerThread; ++iter) {
257 for (unsigned i = 0; i < nrThreads; ++i) {
258 threads.emplace_back([&] {
260 for (unsigned iter = 0; iter < iterPerThread; ++iter) {
261 unsigned slotNo = iter % 4;
262 x.alock.lock(slotNo);
263 counters[slotNo] += 1;
264 // The occasional sleep makes it more likely that we'll
265 // exercise the futex-wait path inside MicroLock.
266 if (iter % 1000 == 0) {
267 struct timespec ts = {0, 10000};
268 (void)nanosleep(&ts, nullptr);
270 x.alock.unlock(slotNo);
277 for (auto it = threads.begin(); it != threads.end(); ++it) {
281 adjacentMemoryToucher.join();
284 EXPECT_EQ(x.b, (uint8_t)(origB + iterPerThread / 2));
286 EXPECT_EQ(x.d, (uint8_t)(origD + iterPerThread / 2));
287 for (unsigned i = 0; i < 4; ++i) {
288 EXPECT_EQ(counters[i], ((uint64_t)nrThreads * iterPerThread) / 4);
292 TEST(SmallLocks, MicroLock) { runMicroLockTest(); }
293 TEST(SmallLocks, MicroLockTryLock) {
296 EXPECT_TRUE(lock.try_lock());
297 EXPECT_FALSE(lock.try_lock());