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.
19 #include <gtest/gtest.h>
21 #include <folly/Foreach.h>
22 #include <folly/Random.h>
23 #include <folly/Synchronized.h>
24 #include <glog/logging.h>
26 #include <condition_variable>
34 namespace sync_tests {
36 inline std::mt19937& getRNG() {
37 static const auto seed = folly::randomNumberSeed();
38 static std::mt19937 rng(seed);
42 void randomSleep(std::chrono::milliseconds min, std::chrono::milliseconds max) {
43 std::uniform_int_distribution<> range(min.count(), max.count());
44 std::chrono::milliseconds duration(range(getRNG()));
45 std::this_thread::sleep_for(duration);
49 * Run a functon simultaneously in a number of different threads.
51 * The function will be passed the index number of the thread it is running in.
52 * This function makes an attempt to synchronize the start of the threads as
53 * best as possible. It waits for all threads to be allocated and started
54 * before invoking the function.
56 template <class Function>
57 void runParallel(size_t numThreads, const Function& function) {
58 std::vector<std::thread> threads;
59 threads.reserve(numThreads);
61 // Variables used to synchronize all threads to try and start them
62 // as close to the same time as possible
63 folly::Synchronized<size_t, std::mutex> threadsReady(0);
64 std::condition_variable readyCV;
65 folly::Synchronized<bool, std::mutex> go(false);
66 std::condition_variable goCV;
68 auto worker = [&](size_t threadIndex) {
69 // Signal that we are ready
70 ++(*threadsReady.lock());
73 // Wait until we are given the signal to start
74 // The purpose of this is to try and make sure all threads start
75 // as close to the same time as possible.
77 auto lockedGo = go.lock();
78 goCV.wait(lockedGo.getUniqueLock(), [&] { return *lockedGo; });
81 function(threadIndex);
84 // Start all of the threads
85 for (size_t threadIndex = 0; threadIndex < numThreads; ++threadIndex) {
86 threads.emplace_back([threadIndex, &worker]() { worker(threadIndex); });
89 // Wait for all threads to become ready
91 auto readyLocked = threadsReady.lock();
92 readyCV.wait(readyLocked.getUniqueLock(), [&] {
93 return *readyLocked == numThreads;
96 // Now signal the threads that they can go
100 // Wait for all threads to finish
101 for (auto& thread : threads) {
106 // testBasic() version for shared lock types
107 template <class Mutex>
108 typename std::enable_if<folly::LockTraits<Mutex>::is_shared>::type
110 folly::Synchronized<std::vector<int>, Mutex> obj;
112 obj.wlock()->resize(1000);
114 folly::Synchronized<std::vector<int>, Mutex> obj2{*obj.wlock()};
115 EXPECT_EQ(1000, obj2.rlock()->size());
118 auto lockedObj = obj.wlock();
119 lockedObj->push_back(10);
120 EXPECT_EQ(1001, lockedObj->size());
121 EXPECT_EQ(10, lockedObj->back());
122 EXPECT_EQ(1000, obj2.wlock()->size());
123 EXPECT_EQ(1000, obj2.rlock()->size());
126 auto unlocker = lockedObj.scopedUnlock();
127 EXPECT_EQ(1001, obj.wlock()->size());
132 auto lockedObj = obj.rlock();
133 EXPECT_EQ(1001, lockedObj->size());
134 EXPECT_EQ(1001, obj.rlock()->size());
136 auto unlocker = lockedObj.scopedUnlock();
137 EXPECT_EQ(1001, obj.wlock()->size());
141 obj.wlock()->front() = 2;
144 const auto& constObj = obj;
145 // contextualLock() on a const reference should grab a shared lock
146 auto lockedObj = constObj.contextualLock();
147 EXPECT_EQ(2, lockedObj->front());
148 EXPECT_EQ(2, constObj.rlock()->front());
149 EXPECT_EQ(2, obj.rlock()->front());
152 EXPECT_EQ(1001, obj.rlock()->size());
153 EXPECT_EQ(2, obj.rlock()->front());
154 EXPECT_EQ(10, obj.rlock()->back());
155 EXPECT_EQ(1000, obj2.rlock()->size());
158 // testBasic() version for non-shared lock types
159 template <class Mutex>
160 typename std::enable_if<!folly::LockTraits<Mutex>::is_shared>::type
162 folly::Synchronized<std::vector<int>, Mutex> obj;
164 obj.lock()->resize(1000);
166 folly::Synchronized<std::vector<int>, Mutex> obj2{*obj.lock()};
167 EXPECT_EQ(1000, obj2.lock()->size());
170 auto lockedObj = obj.lock();
171 lockedObj->push_back(10);
172 EXPECT_EQ(1001, lockedObj->size());
173 EXPECT_EQ(10, lockedObj->back());
174 EXPECT_EQ(1000, obj2.lock()->size());
177 auto unlocker = lockedObj.scopedUnlock();
178 EXPECT_EQ(1001, obj.lock()->size());
182 obj.lock()->front() = 2;
184 EXPECT_EQ(1001, obj.lock()->size());
185 EXPECT_EQ(2, obj.lock()->front());
186 EXPECT_EQ(2, obj.contextualLock()->front());
187 EXPECT_EQ(10, obj.lock()->back());
188 EXPECT_EQ(1000, obj2.lock()->size());
191 template <class Mutex>
193 testBasicImpl<Mutex>();
196 // Testing the deprecated SYNCHRONIZED and SYNCHRONIZED_CONST APIs
197 template <class Mutex>
198 void testDeprecated() {
199 folly::Synchronized<std::vector<int>, Mutex> obj;
204 EXPECT_EQ(1000, obj2->size());
208 EXPECT_EQ(1001, obj.size());
209 EXPECT_EQ(10, obj.back());
210 EXPECT_EQ(1000, obj2->size());
212 UNSYNCHRONIZED(obj) {
213 EXPECT_EQ(1001, obj->size());
217 SYNCHRONIZED_CONST (obj) {
218 EXPECT_EQ(1001, obj.size());
219 UNSYNCHRONIZED(obj) {
220 EXPECT_EQ(1001, obj->size());
224 SYNCHRONIZED (lockedObj, *&obj) {
225 lockedObj.front() = 2;
228 EXPECT_EQ(1001, obj->size());
229 EXPECT_EQ(10, obj->back());
230 EXPECT_EQ(1000, obj2->size());
232 EXPECT_EQ(FB_ARG_2_OR_1(1, 2), 2);
233 EXPECT_EQ(FB_ARG_2_OR_1(1), 1);
236 template <class Mutex> void testConcurrency() {
237 folly::Synchronized<std::vector<int>, Mutex> v;
238 static const size_t numThreads = 100;
239 // Note: I initially tried using itersPerThread = 1000,
240 // which works fine for most lock types, but std::shared_timed_mutex
241 // appears to be extraordinarily slow. It could take around 30 seconds
242 // to run this test with 1000 iterations per thread using shared_timed_mutex.
243 static const size_t itersPerThread = 100;
245 auto pushNumbers = [&](size_t threadIdx) {
247 for (size_t n = 0; n < itersPerThread; ++n) {
248 v.contextualLock()->push_back((itersPerThread * threadIdx) + n);
252 runParallel(numThreads, pushNumbers);
254 std::vector<int> result;
257 EXPECT_EQ(numThreads * itersPerThread, result.size());
258 sort(result.begin(), result.end());
260 for (size_t i = 0; i < itersPerThread * numThreads; ++i) {
261 EXPECT_EQ(i, result[i]);
265 template <class Mutex>
266 void testAcquireLocked() {
267 folly::Synchronized<std::vector<int>, Mutex> v;
268 folly::Synchronized<std::map<int, int>, Mutex> m;
270 auto dualLockWorker = [&](size_t threadIdx) {
271 // Note: this will be less awkward with C++ 17's structured
272 // binding functionality, which will make it easier to use the returned
275 auto ret = acquireLocked(v, m);
276 std::get<0>(ret)->push_back(threadIdx);
277 (*std::get<1>(ret))[threadIdx] = threadIdx + 1;
279 auto ret = acquireLocked(m, v);
280 std::get<1>(ret)->push_back(threadIdx);
281 (*std::get<0>(ret))[threadIdx] = threadIdx + 1;
284 static const size_t numThreads = 100;
285 runParallel(numThreads, dualLockWorker);
287 std::vector<int> result;
290 EXPECT_EQ(numThreads, result.size());
291 sort(result.begin(), result.end());
293 for (size_t i = 0; i < numThreads; ++i) {
294 EXPECT_EQ(i, result[i]);
298 template <class Mutex>
299 void testAcquireLockedWithConst() {
300 folly::Synchronized<std::vector<int>, Mutex> v;
301 folly::Synchronized<std::map<int, int>, Mutex> m;
303 auto dualLockWorker = [&](size_t threadIdx) {
306 auto ret = acquireLocked(v, cm);
307 (void)std::get<1>(ret)->size();
308 std::get<0>(ret)->push_back(threadIdx);
310 auto ret = acquireLocked(cm, v);
311 (void)std::get<0>(ret)->size();
312 std::get<1>(ret)->push_back(threadIdx);
315 static const size_t numThreads = 100;
316 runParallel(numThreads, dualLockWorker);
318 std::vector<int> result;
321 EXPECT_EQ(numThreads, result.size());
322 sort(result.begin(), result.end());
324 for (size_t i = 0; i < numThreads; ++i) {
325 EXPECT_EQ(i, result[i]);
329 // Testing the deprecated SYNCHRONIZED_DUAL API
330 template <class Mutex> void testDualLocking() {
331 folly::Synchronized<std::vector<int>, Mutex> v;
332 folly::Synchronized<std::map<int, int>, Mutex> m;
334 auto dualLockWorker = [&](size_t threadIdx) {
336 SYNCHRONIZED_DUAL(lv, v, lm, m) {
337 lv.push_back(threadIdx);
338 lm[threadIdx] = threadIdx + 1;
341 SYNCHRONIZED_DUAL(lm, m, lv, v) {
342 lv.push_back(threadIdx);
343 lm[threadIdx] = threadIdx + 1;
347 static const size_t numThreads = 100;
348 runParallel(numThreads, dualLockWorker);
350 std::vector<int> result;
353 EXPECT_EQ(numThreads, result.size());
354 sort(result.begin(), result.end());
356 for (size_t i = 0; i < numThreads; ++i) {
357 EXPECT_EQ(i, result[i]);
361 // Testing the deprecated SYNCHRONIZED_DUAL API
362 template <class Mutex> void testDualLockingWithConst() {
363 folly::Synchronized<std::vector<int>, Mutex> v;
364 folly::Synchronized<std::map<int, int>, Mutex> m;
366 auto dualLockWorker = [&](size_t threadIdx) {
369 SYNCHRONIZED_DUAL(lv, v, lm, cm) {
371 lv.push_back(threadIdx);
374 SYNCHRONIZED_DUAL(lm, cm, lv, v) {
376 lv.push_back(threadIdx);
380 static const size_t numThreads = 100;
381 runParallel(numThreads, dualLockWorker);
383 std::vector<int> result;
386 EXPECT_EQ(numThreads, result.size());
387 sort(result.begin(), result.end());
389 for (size_t i = 0; i < numThreads; ++i) {
390 EXPECT_EQ(i, result[i]);
394 template <class Mutex>
396 folly::Synchronized<std::vector<int>, Mutex> v;
397 folly::Synchronized<uint64_t, Mutex> numTimeouts;
399 auto worker = [&](size_t threadIdx) {
400 // Test directly using operator-> on the lock result
401 v.contextualLock()->push_back(2 * threadIdx);
403 // Test using lock with a timeout
405 auto lv = v.contextualLock(std::chrono::milliseconds(5));
407 ++(*numTimeouts.contextualLock());
411 // Sleep for a random time to ensure we trigger timeouts
413 randomSleep(std::chrono::milliseconds(5), std::chrono::milliseconds(15));
414 lv->push_back(2 * threadIdx + 1);
419 static const size_t numThreads = 100;
420 runParallel(numThreads, worker);
422 std::vector<int> result;
425 EXPECT_EQ(2 * numThreads, result.size());
426 sort(result.begin(), result.end());
428 for (size_t i = 0; i < 2 * numThreads; ++i) {
429 EXPECT_EQ(i, result[i]);
431 // We generally expect a large number of number timeouts here.
432 // I'm not adding a check for it since it's theoretically possible that
433 // we might get 0 timeouts depending on the CPU scheduling if our threads
434 // don't get to run very often.
435 LOG(INFO) << "testTimed: " << *numTimeouts.contextualRLock() << " timeouts";
437 // Make sure we can lock with various timeout duration units
439 auto lv = v.contextualLock(std::chrono::milliseconds(5));
441 EXPECT_FALSE(lv.isNull());
442 auto lv2 = v.contextualLock(std::chrono::microseconds(5));
443 // We may or may not acquire lv2 successfully, depending on whether
444 // or not this is a recursive mutex type.
447 auto lv = v.contextualLock(std::chrono::seconds(1));
452 template <class Mutex>
453 void testTimedShared() {
454 folly::Synchronized<std::vector<int>, Mutex> v;
455 folly::Synchronized<uint64_t, Mutex> numTimeouts;
457 auto worker = [&](size_t threadIdx) {
458 // Test directly using operator-> on the lock result
459 v.wlock()->push_back(threadIdx);
461 // Test lock() with a timeout
463 auto lv = v.rlock(std::chrono::milliseconds(10));
465 ++(*numTimeouts.contextualLock());
469 // Sleep while holding the lock.
471 // This will block other threads from acquiring the write lock to add
472 // their thread index to v, but it won't block threads that have entered
473 // the for loop and are trying to acquire a read lock.
475 // For lock types that give preference to readers rather than writers,
476 // this will tend to serialize all threads on the wlock() above.
477 randomSleep(std::chrono::milliseconds(5), std::chrono::milliseconds(15));
478 auto found = std::find(lv->begin(), lv->end(), threadIdx);
479 CHECK(found != lv->end());
484 static const size_t numThreads = 100;
485 runParallel(numThreads, worker);
487 std::vector<int> result;
490 EXPECT_EQ(numThreads, result.size());
491 sort(result.begin(), result.end());
493 for (size_t i = 0; i < numThreads; ++i) {
494 EXPECT_EQ(i, result[i]);
496 // We generally expect a small number of timeouts here.
497 // For locks that give readers preference over writers this should usually
498 // be 0. With locks that give writers preference we do see a small-ish
499 // number of read timeouts.
500 LOG(INFO) << "testTimedShared: " << *numTimeouts.contextualRLock()
504 // Testing the deprecated TIMED_SYNCHRONIZED API
505 template <class Mutex> void testTimedSynchronized() {
506 folly::Synchronized<std::vector<int>, Mutex> v;
507 folly::Synchronized<uint64_t, Mutex> numTimeouts;
509 auto worker = [&](size_t threadIdx) {
511 v->push_back(2 * threadIdx);
513 // Aaand test the TIMED_SYNCHRONIZED macro
515 TIMED_SYNCHRONIZED(5, lv, v) {
517 // Sleep for a random time to ensure we trigger timeouts
520 std::chrono::milliseconds(5), std::chrono::milliseconds(15));
521 lv->push_back(2 * threadIdx + 1);
525 ++(*numTimeouts.contextualLock());
529 static const size_t numThreads = 100;
530 runParallel(numThreads, worker);
532 std::vector<int> result;
535 EXPECT_EQ(2 * numThreads, result.size());
536 sort(result.begin(), result.end());
538 for (size_t i = 0; i < 2 * numThreads; ++i) {
539 EXPECT_EQ(i, result[i]);
541 // We generally expect a large number of number timeouts here.
542 // I'm not adding a check for it since it's theoretically possible that
543 // we might get 0 timeouts depending on the CPU scheduling if our threads
544 // don't get to run very often.
545 LOG(INFO) << "testTimedSynchronized: " << *numTimeouts.contextualRLock()
549 // Testing the deprecated TIMED_SYNCHRONIZED_CONST API
550 template <class Mutex> void testTimedSynchronizedWithConst() {
551 folly::Synchronized<std::vector<int>, Mutex> v;
552 folly::Synchronized<uint64_t, Mutex> numTimeouts;
554 auto worker = [&](size_t threadIdx) {
556 v->push_back(threadIdx);
558 // Test TIMED_SYNCHRONIZED_CONST
560 TIMED_SYNCHRONIZED_CONST(10, lv, v) {
562 // Sleep while holding the lock.
564 // This will block other threads from acquiring the write lock to add
565 // their thread index to v, but it won't block threads that have
566 // entered the for loop and are trying to acquire a read lock.
568 // For lock types that give preference to readers rather than writers,
569 // this will tend to serialize all threads on the wlock() above.
571 std::chrono::milliseconds(5), std::chrono::milliseconds(15));
572 auto found = std::find(lv->begin(), lv->end(), threadIdx);
573 CHECK(found != lv->end());
576 ++(*numTimeouts.contextualLock());
582 static const size_t numThreads = 100;
583 runParallel(numThreads, worker);
585 std::vector<int> result;
588 EXPECT_EQ(numThreads, result.size());
589 sort(result.begin(), result.end());
591 for (size_t i = 0; i < numThreads; ++i) {
592 EXPECT_EQ(i, result[i]);
594 // We generally expect a small number of timeouts here.
595 // For locks that give readers preference over writers this should usually
596 // be 0. With locks that give writers preference we do see a small-ish
597 // number of read timeouts.
598 LOG(INFO) << "testTimedSynchronizedWithConst: "
599 << *numTimeouts.contextualRLock() << " timeouts";
602 template <class Mutex> void testConstCopy() {
603 std::vector<int> input = {1, 2, 3};
604 const folly::Synchronized<std::vector<int>, Mutex> v(input);
606 std::vector<int> result;
609 EXPECT_EQ(input, result);
612 EXPECT_EQ(input, result);
615 struct NotCopiableNotMovable {
616 NotCopiableNotMovable(int, const char*) {}
617 NotCopiableNotMovable(const NotCopiableNotMovable&) = delete;
618 NotCopiableNotMovable& operator=(const NotCopiableNotMovable&) = delete;
619 NotCopiableNotMovable(NotCopiableNotMovable&&) = delete;
620 NotCopiableNotMovable& operator=(NotCopiableNotMovable&&) = delete;
623 template <class Mutex> void testInPlaceConstruction() {
624 // This won't compile without construct_in_place
625 folly::Synchronized<NotCopiableNotMovable> a(
626 folly::construct_in_place, 5, "a"