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, Deprecated) {
63 testDeprecated<TypeParam>();
66 TYPED_TEST(SynchronizedTest, Concurrency) {
67 testConcurrency<TypeParam>();
70 TYPED_TEST(SynchronizedTest, AcquireLocked) {
71 testAcquireLocked<TypeParam>();
74 TYPED_TEST(SynchronizedTest, AcquireLockedWithConst) {
75 testAcquireLockedWithConst<TypeParam>();
78 TYPED_TEST(SynchronizedTest, DualLocking) {
79 testDualLocking<TypeParam>();
82 TYPED_TEST(SynchronizedTest, DualLockingWithConst) {
83 testDualLockingWithConst<TypeParam>();
86 TYPED_TEST(SynchronizedTest, ConstCopy) {
87 testConstCopy<TypeParam>();
90 template <class Mutex>
91 class SynchronizedTimedTest : public testing::Test {};
93 using SynchronizedTimedTestTypes = testing::Types<
94 #if FOLLY_LOCK_TRAITS_HAVE_TIMED_MUTEXES
96 std::recursive_timed_mutex,
98 boost::recursive_timed_mutex,
101 #ifdef RW_SPINLOCK_USE_X86_INTRINSIC_
102 folly::RWTicketSpinLock32,
103 folly::RWTicketSpinLock64,
105 folly::SharedMutexReadPriority,
106 folly::SharedMutexWritePriority>;
107 TYPED_TEST_CASE(SynchronizedTimedTest, SynchronizedTimedTestTypes);
109 TYPED_TEST(SynchronizedTimedTest, Timed) {
110 testTimed<TypeParam>();
113 TYPED_TEST(SynchronizedTimedTest, TimedSynchronized) {
114 testTimedSynchronized<TypeParam>();
117 template <class Mutex>
118 class SynchronizedTimedWithConstTest : public testing::Test {};
120 using SynchronizedTimedWithConstTestTypes = testing::Types<
121 #if FOLLY_LOCK_TRAITS_HAVE_TIMED_MUTEXES
124 #ifdef RW_SPINLOCK_USE_X86_INTRINSIC_
125 folly::RWTicketSpinLock32,
126 folly::RWTicketSpinLock64,
128 folly::SharedMutexReadPriority,
129 folly::SharedMutexWritePriority>;
131 SynchronizedTimedWithConstTest, SynchronizedTimedWithConstTestTypes);
133 TYPED_TEST(SynchronizedTimedWithConstTest, TimedShared) {
134 testTimedShared<TypeParam>();
137 TYPED_TEST(SynchronizedTimedWithConstTest, TimedSynchronizeWithConst) {
138 testTimedSynchronizedWithConst<TypeParam>();
141 TYPED_TEST(SynchronizedTest, InPlaceConstruction) {
142 testInPlaceConstruction<TypeParam>();
145 using CountPair = std::pair<int, int>;
146 // This class is specialized only to be uesed in SynchronizedLockTest
159 static CountPair getLockUnlockCount() {
160 return CountPair{lockCount_, unlockCount_};
163 static void resetLockUnlockCount() {
168 // Keep these two static for test access
169 // Keep them thread_local in case of tests are run in parallel within one
171 static FOLLY_TLS int lockCount_;
172 static FOLLY_TLS int unlockCount_;
174 FOLLY_TLS int FakeMutex::lockCount_{0};
175 FOLLY_TLS int FakeMutex::unlockCount_{0};
177 // SynchronizedLockTest is used to verify the correct lock unlock behavior
178 // happens per design
179 class SynchronizedLockTest : public testing::Test {
181 void SetUp() override {
182 FakeMutex::resetLockUnlockCount();
186 // Single level of SYNCHRONIZED and UNSYNCHRONIZED, although nested test are
187 // super set of it, it is possible single level test passes while nested tests
189 TEST_F(SynchronizedLockTest, SyncUnSync) {
190 folly::Synchronized<std::vector<int>, FakeMutex> obj;
191 EXPECT_EQ((CountPair{0, 0}), FakeMutex::getLockUnlockCount());
193 EXPECT_EQ((CountPair{1, 0}), FakeMutex::getLockUnlockCount());
194 UNSYNCHRONIZED(obj) {
195 EXPECT_EQ((CountPair{1, 1}), FakeMutex::getLockUnlockCount());
197 EXPECT_EQ((CountPair{2, 1}), FakeMutex::getLockUnlockCount());
199 EXPECT_EQ((CountPair{2, 2}), FakeMutex::getLockUnlockCount());
202 // Nested SYNCHRONIZED UNSYNCHRONIZED test, 2 levels of synchronization
203 TEST_F(SynchronizedLockTest, NestedSyncUnSync) {
204 folly::Synchronized<std::vector<int>, FakeMutex> obj;
205 EXPECT_EQ((CountPair{0, 0}), FakeMutex::getLockUnlockCount());
206 SYNCHRONIZED(objCopy, obj) {
207 EXPECT_EQ((CountPair{1, 0}), FakeMutex::getLockUnlockCount());
209 EXPECT_EQ((CountPair{2, 0}), FakeMutex::getLockUnlockCount());
210 // Note: UNSYNCHRONIZED has always been kind of broken here.
211 // The input parameter is ignored (other than to overwrite what the input
212 // variable name refers to), and it unlocks the most object acquired in
213 // the most recent SYNCHRONIZED scope.
214 UNSYNCHRONIZED(obj) {
215 EXPECT_EQ((CountPair{2, 1}), FakeMutex::getLockUnlockCount());
217 EXPECT_EQ((CountPair{3, 1}), FakeMutex::getLockUnlockCount());
218 UNSYNCHRONIZED(obj) {
219 EXPECT_EQ((CountPair{3, 2}), FakeMutex::getLockUnlockCount());
221 EXPECT_EQ((CountPair{4, 2}), FakeMutex::getLockUnlockCount());
223 EXPECT_EQ((CountPair{4, 3}), FakeMutex::getLockUnlockCount());
225 EXPECT_EQ((CountPair{4, 4}), FakeMutex::getLockUnlockCount());
228 // Different nesting behavior, UNSYNCHRONIZED called on different depth of
230 TEST_F(SynchronizedLockTest, NestedSyncUnSync2) {
231 folly::Synchronized<std::vector<int>, FakeMutex> obj;
232 EXPECT_EQ((CountPair{0, 0}), FakeMutex::getLockUnlockCount());
233 SYNCHRONIZED(objCopy, obj) {
234 EXPECT_EQ((CountPair{1, 0}), FakeMutex::getLockUnlockCount());
236 EXPECT_EQ((CountPair{2, 0}), FakeMutex::getLockUnlockCount());
237 UNSYNCHRONIZED(obj) {
238 EXPECT_EQ((CountPair{2, 1}), FakeMutex::getLockUnlockCount());
240 EXPECT_EQ((CountPair{3, 1}), FakeMutex::getLockUnlockCount());
242 EXPECT_EQ((CountPair{3, 2}), FakeMutex::getLockUnlockCount());
243 UNSYNCHRONIZED(obj) {
244 EXPECT_EQ((CountPair{3, 3}), FakeMutex::getLockUnlockCount());
246 EXPECT_EQ((CountPair{4, 3}), FakeMutex::getLockUnlockCount());
248 EXPECT_EQ((CountPair{4, 4}), FakeMutex::getLockUnlockCount());