rename io::PortableSpinLock to SpinLock
[folly.git] / folly / test / SpinLockTest.cpp
1 /*
2  * Copyright 2014 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::SpinLock;
22 using folly::SpinLockGuard;
23
24 namespace {
25
26 struct LockedVal {
27   int ar[1024];
28   SpinLock lock;
29
30   LockedVal() {
31     memset(ar, 0, sizeof ar);
32   }
33 };
34
35 LockedVal v;
36 void splock_test() {
37   const int max = 1000;
38   unsigned int seed = (uintptr_t)pthread_self();
39   for (int i = 0; i < max; i++) {
40     asm("pause");
41     SpinLockGuard 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 struct TryLockState {
54   SpinLock lock1;
55   SpinLock lock2;
56   bool locked{false};
57   uint64_t obtained{0};
58   uint64_t failed{0};
59 };
60 TryLockState tryState;
61
62 void trylock_test() {
63   const int max = 1000;
64   while (true) {
65     asm("pause");
66     SpinLockGuard g(tryState.lock1);
67     if (tryState.obtained >= max) {
68       break;
69     }
70
71     bool ret = tryState.lock2.trylock();
72     EXPECT_NE(tryState.locked, ret);
73
74     if (ret) {
75       // We got lock2.
76       ++tryState.obtained;
77       tryState.locked = true;
78
79       // Release lock1 and let other threads try to obtain lock2
80       tryState.lock1.unlock();
81       asm("pause");
82       tryState.lock1.lock();
83
84       tryState.locked = false;
85       tryState.lock2.unlock();
86     } else {
87       ++tryState.failed;
88     }
89   }
90 }
91
92 } // unnamed namespace
93
94 TEST(SpinLock, Correctness) {
95   int nthrs = sysconf(_SC_NPROCESSORS_ONLN) * 2;
96   std::vector<std::thread> threads;
97   for (int i = 0; i < nthrs; ++i) {
98     threads.push_back(std::thread(splock_test));
99   }
100   for (auto& t : threads) {
101     t.join();
102   }
103 }
104
105 TEST(SpinLock, TryLock) {
106   int nthrs = sysconf(_SC_NPROCESSORS_ONLN) * 2;
107   std::vector<std::thread> threads;
108   for (int i = 0; i < nthrs; ++i) {
109     threads.push_back(std::thread(trylock_test));
110   }
111   for (auto& t : threads) {
112     t.join();
113   }
114
115   EXPECT_EQ(1000, tryState.obtained);
116   EXPECT_GT(tryState.failed, 0);
117   LOG(INFO) << "failed count: " << tryState.failed;
118 }