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 // @author: Andrei Alexandrescu (aalexandre)
19 // Test bed for folly/Synchronized.h
21 #include <folly/LockTraitsBoost.h>
22 #include <folly/Portability.h>
23 #include <folly/RWSpinLock.h>
24 #include <folly/SharedMutex.h>
25 #include <folly/SpinLock.h>
26 #include <folly/Synchronized.h>
27 #include <folly/test/SynchronizedTestLib.h>
28 #include <gtest/gtest.h>
30 using namespace folly::sync_tests;
32 template <class Mutex>
33 class SynchronizedTest : public testing::Test {};
35 using SynchronizedTestTypes = testing::Types<
36 folly::SharedMutexReadPriority,
37 folly::SharedMutexWritePriority,
40 #if FOLLY_LOCK_TRAITS_HAVE_TIMED_MUTEXES
42 std::recursive_timed_mutex,
45 boost::recursive_mutex,
46 #if FOLLY_LOCK_TRAITS_HAVE_TIMED_MUTEXES
48 boost::recursive_timed_mutex,
50 #ifdef RW_SPINLOCK_USE_X86_INTRINSIC_
51 folly::RWTicketSpinLock32,
52 folly::RWTicketSpinLock64,
56 TYPED_TEST_CASE(SynchronizedTest, SynchronizedTestTypes);
58 TYPED_TEST(SynchronizedTest, Basic) {
59 testBasic<TypeParam>();
62 TYPED_TEST(SynchronizedTest, WithLock) {
63 testWithLock<TypeParam>();
66 TYPED_TEST(SynchronizedTest, Deprecated) {
67 testDeprecated<TypeParam>();
70 TYPED_TEST(SynchronizedTest, Concurrency) {
71 testConcurrency<TypeParam>();
74 TYPED_TEST(SynchronizedTest, AcquireLocked) {
75 testAcquireLocked<TypeParam>();
78 TYPED_TEST(SynchronizedTest, AcquireLockedWithConst) {
79 testAcquireLockedWithConst<TypeParam>();
82 TYPED_TEST(SynchronizedTest, DualLocking) {
83 testDualLocking<TypeParam>();
86 TYPED_TEST(SynchronizedTest, DualLockingWithConst) {
87 testDualLockingWithConst<TypeParam>();
90 TYPED_TEST(SynchronizedTest, ConstCopy) {
91 testConstCopy<TypeParam>();
94 template <class Mutex>
95 class SynchronizedTimedTest : public testing::Test {};
97 using SynchronizedTimedTestTypes = testing::Types<
98 #if FOLLY_LOCK_TRAITS_HAVE_TIMED_MUTEXES
100 std::recursive_timed_mutex,
102 boost::recursive_timed_mutex,
105 #ifdef RW_SPINLOCK_USE_X86_INTRINSIC_
106 folly::RWTicketSpinLock32,
107 folly::RWTicketSpinLock64,
109 folly::SharedMutexReadPriority,
110 folly::SharedMutexWritePriority>;
111 TYPED_TEST_CASE(SynchronizedTimedTest, SynchronizedTimedTestTypes);
113 TYPED_TEST(SynchronizedTimedTest, Timed) {
114 testTimed<TypeParam>();
117 TYPED_TEST(SynchronizedTimedTest, TimedSynchronized) {
118 testTimedSynchronized<TypeParam>();
121 template <class Mutex>
122 class SynchronizedTimedWithConstTest : public testing::Test {};
124 using SynchronizedTimedWithConstTestTypes = testing::Types<
125 #if FOLLY_LOCK_TRAITS_HAVE_TIMED_MUTEXES
128 #ifdef RW_SPINLOCK_USE_X86_INTRINSIC_
129 folly::RWTicketSpinLock32,
130 folly::RWTicketSpinLock64,
132 folly::SharedMutexReadPriority,
133 folly::SharedMutexWritePriority>;
135 SynchronizedTimedWithConstTest, SynchronizedTimedWithConstTestTypes);
137 TYPED_TEST(SynchronizedTimedWithConstTest, TimedShared) {
138 testTimedShared<TypeParam>();
141 TYPED_TEST(SynchronizedTimedWithConstTest, TimedSynchronizeWithConst) {
142 testTimedSynchronizedWithConst<TypeParam>();
145 TYPED_TEST(SynchronizedTest, InPlaceConstruction) {
146 testInPlaceConstruction<TypeParam>();
149 using CountPair = std::pair<int, int>;
150 // This class is specialized only to be uesed in SynchronizedLockTest
163 static CountPair getLockUnlockCount() {
164 return CountPair{lockCount_, unlockCount_};
167 static void resetLockUnlockCount() {
172 // Keep these two static for test access
173 // Keep them thread_local in case of tests are run in parallel within one
175 static FOLLY_TLS int lockCount_;
176 static FOLLY_TLS int unlockCount_;
178 FOLLY_TLS int FakeMutex::lockCount_{0};
179 FOLLY_TLS int FakeMutex::unlockCount_{0};
181 // SynchronizedLockTest is used to verify the correct lock unlock behavior
182 // happens per design
183 class SynchronizedLockTest : public testing::Test {
185 void SetUp() override {
186 FakeMutex::resetLockUnlockCount();
190 // Single level of SYNCHRONIZED and UNSYNCHRONIZED, although nested test are
191 // super set of it, it is possible single level test passes while nested tests
193 TEST_F(SynchronizedLockTest, SyncUnSync) {
194 folly::Synchronized<std::vector<int>, FakeMutex> obj;
195 EXPECT_EQ((CountPair{0, 0}), FakeMutex::getLockUnlockCount());
197 EXPECT_EQ((CountPair{1, 0}), FakeMutex::getLockUnlockCount());
198 UNSYNCHRONIZED(obj) {
199 EXPECT_EQ((CountPair{1, 1}), FakeMutex::getLockUnlockCount());
201 EXPECT_EQ((CountPair{2, 1}), FakeMutex::getLockUnlockCount());
203 EXPECT_EQ((CountPair{2, 2}), FakeMutex::getLockUnlockCount());
206 // Nested SYNCHRONIZED UNSYNCHRONIZED test, 2 levels of synchronization
207 TEST_F(SynchronizedLockTest, NestedSyncUnSync) {
208 folly::Synchronized<std::vector<int>, FakeMutex> obj;
209 EXPECT_EQ((CountPair{0, 0}), FakeMutex::getLockUnlockCount());
210 SYNCHRONIZED(objCopy, obj) {
211 EXPECT_EQ((CountPair{1, 0}), FakeMutex::getLockUnlockCount());
213 EXPECT_EQ((CountPair{2, 0}), FakeMutex::getLockUnlockCount());
214 // Note: UNSYNCHRONIZED has always been kind of broken here.
215 // The input parameter is ignored (other than to overwrite what the input
216 // variable name refers to), and it unlocks the most object acquired in
217 // the most recent SYNCHRONIZED scope.
218 UNSYNCHRONIZED(obj) {
219 EXPECT_EQ((CountPair{2, 1}), FakeMutex::getLockUnlockCount());
221 EXPECT_EQ((CountPair{3, 1}), FakeMutex::getLockUnlockCount());
222 UNSYNCHRONIZED(obj) {
223 EXPECT_EQ((CountPair{3, 2}), FakeMutex::getLockUnlockCount());
225 EXPECT_EQ((CountPair{4, 2}), FakeMutex::getLockUnlockCount());
227 EXPECT_EQ((CountPair{4, 3}), FakeMutex::getLockUnlockCount());
229 EXPECT_EQ((CountPair{4, 4}), FakeMutex::getLockUnlockCount());
232 // Different nesting behavior, UNSYNCHRONIZED called on different depth of
234 TEST_F(SynchronizedLockTest, NestedSyncUnSync2) {
235 folly::Synchronized<std::vector<int>, FakeMutex> obj;
236 EXPECT_EQ((CountPair{0, 0}), FakeMutex::getLockUnlockCount());
237 SYNCHRONIZED(objCopy, obj) {
238 EXPECT_EQ((CountPair{1, 0}), FakeMutex::getLockUnlockCount());
240 EXPECT_EQ((CountPair{2, 0}), FakeMutex::getLockUnlockCount());
241 UNSYNCHRONIZED(obj) {
242 EXPECT_EQ((CountPair{2, 1}), FakeMutex::getLockUnlockCount());
244 EXPECT_EQ((CountPair{3, 1}), FakeMutex::getLockUnlockCount());
246 EXPECT_EQ((CountPair{3, 2}), FakeMutex::getLockUnlockCount());
247 UNSYNCHRONIZED(obj) {
248 EXPECT_EQ((CountPair{3, 3}), FakeMutex::getLockUnlockCount());
250 EXPECT_EQ((CountPair{4, 3}), FakeMutex::getLockUnlockCount());
252 EXPECT_EQ((CountPair{4, 4}), FakeMutex::getLockUnlockCount());