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()));
46 std::this_thread::sleep_for(duration);
50 * Run a functon simultaneously in a number of different threads.
52 * The function will be passed the index number of the thread it is running in.
53 * This function makes an attempt to synchronize the start of the threads as
54 * best as possible. It waits for all threads to be allocated and started
55 * before invoking the function.
57 template <class Function>
58 void runParallel(size_t numThreads, const Function& function) {
59 std::vector<std::thread> threads;
60 threads.reserve(numThreads);
62 // Variables used to synchronize all threads to try and start them
63 // as close to the same time as possible
64 folly::Synchronized<size_t, std::mutex> threadsReady(0);
65 std::condition_variable readyCV;
66 folly::Synchronized<bool, std::mutex> go(false);
67 std::condition_variable goCV;
69 auto worker = [&](size_t threadIndex) {
70 // Signal that we are ready
71 ++(*threadsReady.lock());
74 // Wait until we are given the signal to start
75 // The purpose of this is to try and make sure all threads start
76 // as close to the same time as possible.
78 auto lockedGo = go.lock();
79 goCV.wait(lockedGo.getUniqueLock(), [&] { return *lockedGo; });
82 function(threadIndex);
85 // Start all of the threads
86 for (size_t threadIndex = 0; threadIndex < numThreads; ++threadIndex) {
87 threads.emplace_back([threadIndex, &worker]() { worker(threadIndex); });
90 // Wait for all threads to become ready
92 auto readyLocked = threadsReady.lock();
93 readyCV.wait(readyLocked.getUniqueLock(), [&] {
94 return *readyLocked == numThreads;
97 // Now signal the threads that they can go
101 // Wait for all threads to finish
102 for (auto& thread : threads) {
107 // testBasic() version for shared lock types
108 template <class Mutex>
109 typename std::enable_if<folly::LockTraits<Mutex>::is_shared>::type
111 folly::Synchronized<std::vector<int>, Mutex> obj;
112 const auto& constObj = obj;
114 obj.wlock()->resize(1000);
116 folly::Synchronized<std::vector<int>, Mutex> obj2{*obj.wlock()};
117 EXPECT_EQ(1000, obj2.rlock()->size());
120 auto lockedObj = obj.wlock();
121 lockedObj->push_back(10);
122 EXPECT_EQ(1001, lockedObj->size());
123 EXPECT_EQ(10, lockedObj->back());
124 EXPECT_EQ(1000, obj2.wlock()->size());
125 EXPECT_EQ(1000, obj2.rlock()->size());
128 auto unlocker = lockedObj.scopedUnlock();
129 EXPECT_EQ(1001, obj.wlock()->size());
134 auto lockedObj = obj.rlock();
135 EXPECT_EQ(1001, lockedObj->size());
136 EXPECT_EQ(1001, obj.rlock()->size());
138 auto unlocker = lockedObj.scopedUnlock();
139 EXPECT_EQ(1001, obj.wlock()->size());
143 obj.wlock()->front() = 2;
146 // contextualLock() on a const reference should grab a shared lock
147 auto lockedObj = constObj.contextualLock();
148 EXPECT_EQ(2, lockedObj->front());
149 EXPECT_EQ(2, constObj.rlock()->front());
150 EXPECT_EQ(2, obj.rlock()->front());
153 EXPECT_EQ(1001, obj.rlock()->size());
154 EXPECT_EQ(2, obj.rlock()->front());
155 EXPECT_EQ(10, obj.rlock()->back());
156 EXPECT_EQ(1000, obj2.rlock()->size());
159 // testBasic() version for non-shared lock types
160 template <class Mutex>
161 typename std::enable_if<!folly::LockTraits<Mutex>::is_shared>::type
163 folly::Synchronized<std::vector<int>, Mutex> obj;
164 const auto& constObj = obj;
166 obj.lock()->resize(1000);
168 folly::Synchronized<std::vector<int>, Mutex> obj2{*obj.lock()};
169 EXPECT_EQ(1000, obj2.lock()->size());
172 auto lockedObj = obj.lock();
173 lockedObj->push_back(10);
174 EXPECT_EQ(1001, lockedObj->size());
175 EXPECT_EQ(10, lockedObj->back());
176 EXPECT_EQ(1000, obj2.lock()->size());
179 auto unlocker = lockedObj.scopedUnlock();
180 EXPECT_EQ(1001, obj.lock()->size());
184 auto lockedObj = constObj.lock();
185 EXPECT_EQ(1001, lockedObj->size());
186 EXPECT_EQ(10, lockedObj->back());
187 EXPECT_EQ(1000, obj2.lock()->size());
190 obj.lock()->front() = 2;
192 EXPECT_EQ(1001, obj.lock()->size());
193 EXPECT_EQ(2, obj.lock()->front());
194 EXPECT_EQ(2, obj.contextualLock()->front());
195 EXPECT_EQ(10, obj.lock()->back());
196 EXPECT_EQ(1000, obj2.lock()->size());
199 template <class Mutex>
201 testBasicImpl<Mutex>();
204 // testWithLock() version for shared lock types
205 template <class Mutex>
206 typename std::enable_if<folly::LockTraits<Mutex>::is_shared>::type
208 folly::Synchronized<std::vector<int>, Mutex> obj;
209 const auto& constObj = obj;
211 // Test withWLock() and withRLock()
212 obj.withWLock([](std::vector<int>& lockedObj) {
213 lockedObj.resize(1000);
214 lockedObj.push_back(10);
215 lockedObj.push_back(11);
217 obj.withWLock([](const std::vector<int>& lockedObj) {
218 EXPECT_EQ(1002, lockedObj.size());
220 constObj.withWLock([](const std::vector<int>& lockedObj) {
221 EXPECT_EQ(1002, lockedObj.size());
222 EXPECT_EQ(11, lockedObj.back());
224 obj.withRLock([](const std::vector<int>& lockedObj) {
225 EXPECT_EQ(1002, lockedObj.size());
226 EXPECT_EQ(11, lockedObj.back());
228 constObj.withRLock([](const std::vector<int>& lockedObj) {
229 EXPECT_EQ(1002, lockedObj.size());
232 #if __cpp_generic_lambdas >= 201304
233 obj.withWLock([](auto& lockedObj) { lockedObj.push_back(12); });
235 [](const auto& lockedObj) { EXPECT_EQ(1003, lockedObj.size()); });
236 constObj.withWLock([](const auto& lockedObj) {
237 EXPECT_EQ(1003, lockedObj.size());
238 EXPECT_EQ(12, lockedObj.back());
240 obj.withRLock([](const auto& lockedObj) {
241 EXPECT_EQ(1003, lockedObj.size());
242 EXPECT_EQ(12, lockedObj.back());
245 [](const auto& lockedObj) { EXPECT_EQ(1003, lockedObj.size()); });
246 obj.withWLock([](auto& lockedObj) { lockedObj.pop_back(); });
249 // Test withWLockPtr() and withRLockPtr()
250 using SynchType = folly::Synchronized<std::vector<int>, Mutex>;
251 #if __cpp_generic_lambdas >= 201304
252 obj.withWLockPtr([](auto&& lockedObj) { lockedObj->push_back(13); });
253 obj.withRLockPtr([](auto&& lockedObj) {
254 EXPECT_EQ(1003, lockedObj->size());
255 EXPECT_EQ(13, lockedObj->back());
257 constObj.withRLockPtr([](auto&& lockedObj) {
258 EXPECT_EQ(1003, lockedObj->size());
259 EXPECT_EQ(13, lockedObj->back());
261 obj.withWLockPtr([&](auto&& lockedObj) {
262 lockedObj->push_back(14);
264 auto unlocker = lockedObj.scopedUnlock();
265 obj.wlock()->push_back(15);
267 EXPECT_EQ(15, lockedObj->back());
269 constObj.withWLockPtr([](auto&& lockedObj) {
270 EXPECT_EQ(1005, lockedObj->size());
271 EXPECT_EQ(15, lockedObj->back());
274 obj.withWLockPtr([](typename SynchType::LockedPtr&& lockedObj) {
275 lockedObj->push_back(13);
276 lockedObj->push_back(14);
277 lockedObj->push_back(15);
281 obj.withWLockPtr([](typename SynchType::LockedPtr&& lockedObj) {
282 lockedObj->push_back(16);
283 EXPECT_EQ(1006, lockedObj->size());
285 constObj.withWLockPtr([](typename SynchType::ConstWLockedPtr&& lockedObj) {
286 EXPECT_EQ(1006, lockedObj->size());
287 EXPECT_EQ(16, lockedObj->back());
289 obj.withRLockPtr([](typename SynchType::ConstLockedPtr&& lockedObj) {
290 EXPECT_EQ(1006, lockedObj->size());
291 EXPECT_EQ(16, lockedObj->back());
293 constObj.withRLockPtr([](typename SynchType::ConstLockedPtr&& lockedObj) {
294 EXPECT_EQ(1006, lockedObj->size());
295 EXPECT_EQ(16, lockedObj->back());
299 // testWithLock() version for non-shared lock types
300 template <class Mutex>
301 typename std::enable_if<!folly::LockTraits<Mutex>::is_shared>::type
303 folly::Synchronized<std::vector<int>, Mutex> obj;
306 obj.withLock([](std::vector<int>& lockedObj) {
307 lockedObj.resize(1000);
308 lockedObj.push_back(10);
309 lockedObj.push_back(11);
311 obj.withLock([](const std::vector<int>& lockedObj) {
312 EXPECT_EQ(1002, lockedObj.size());
315 #if __cpp_generic_lambdas >= 201304
316 obj.withLock([](auto& lockedObj) { lockedObj.push_back(12); });
318 [](const auto& lockedObj) { EXPECT_EQ(1003, lockedObj.size()); });
319 obj.withLock([](auto& lockedObj) { lockedObj.pop_back(); });
322 // Test withLockPtr()
323 using SynchType = folly::Synchronized<std::vector<int>, Mutex>;
324 #if __cpp_generic_lambdas >= 201304
325 obj.withLockPtr([](auto&& lockedObj) { lockedObj->push_back(13); });
326 obj.withLockPtr([](auto&& lockedObj) {
327 EXPECT_EQ(1003, lockedObj->size());
328 EXPECT_EQ(13, lockedObj->back());
330 obj.withLockPtr([&](auto&& lockedObj) {
331 lockedObj->push_back(14);
333 auto unlocker = lockedObj.scopedUnlock();
334 obj.lock()->push_back(15);
336 EXPECT_EQ(1005, lockedObj->size());
337 EXPECT_EQ(15, lockedObj->back());
340 obj.withLockPtr([](typename SynchType::LockedPtr&& lockedObj) {
341 lockedObj->push_back(13);
342 lockedObj->push_back(14);
343 lockedObj->push_back(15);
347 obj.withLockPtr([](typename SynchType::LockedPtr&& lockedObj) {
348 lockedObj->push_back(16);
349 EXPECT_EQ(1006, lockedObj->size());
351 const auto& constObj = obj;
352 constObj.withLockPtr([](typename SynchType::ConstLockedPtr&& lockedObj) {
353 EXPECT_EQ(1006, lockedObj->size());
354 EXPECT_EQ(16, lockedObj->back());
358 template <class Mutex>
359 void testUnlockCommon() {
360 folly::Synchronized<int, Mutex> value{7};
361 const auto& cv = value;
364 auto lv = value.contextualLock();
368 EXPECT_TRUE(lv.isNull());
371 auto rlv = cv.contextualLock();
374 EXPECT_TRUE(rlv.isNull());
377 auto rlv2 = cv.contextualRLock();
381 lv = value.contextualLock();
386 EXPECT_EQ(9, *value.contextualRLock());
389 // testUnlock() version for shared lock types
390 template <class Mutex>
391 typename std::enable_if<folly::LockTraits<Mutex>::is_shared>::type
393 folly::Synchronized<int, Mutex> value{10};
395 auto lv = value.wlock();
400 EXPECT_TRUE(lv.isNull());
402 auto rlv = value.rlock();
406 EXPECT_TRUE(rlv.isNull());
408 auto lv2 = value.wlock();
414 EXPECT_TRUE(lv2.isNull());
415 EXPECT_FALSE(lv.isNull());
419 testUnlockCommon<Mutex>();
422 // testUnlock() version for non-shared lock types
423 template <class Mutex>
424 typename std::enable_if<!folly::LockTraits<Mutex>::is_shared>::type
426 folly::Synchronized<int, Mutex> value{10};
428 auto lv = value.lock();
432 EXPECT_TRUE(lv.isNull());
435 auto lv2 = value.lock();
439 EXPECT_TRUE(lv2.isNull());
447 EXPECT_TRUE(lv.isNull());
449 EXPECT_FALSE(lv2.isNull());
453 testUnlockCommon<Mutex>();
456 // Testing the deprecated SYNCHRONIZED and SYNCHRONIZED_CONST APIs
457 template <class Mutex>
458 void testDeprecated() {
459 folly::Synchronized<std::vector<int>, Mutex> obj;
464 EXPECT_EQ(1000, obj2->size());
468 EXPECT_EQ(1001, obj.size());
469 EXPECT_EQ(10, obj.back());
470 EXPECT_EQ(1000, obj2->size());
472 UNSYNCHRONIZED(obj) {
473 EXPECT_EQ(1001, obj->size());
477 SYNCHRONIZED_CONST (obj) {
478 EXPECT_EQ(1001, obj.size());
479 UNSYNCHRONIZED(obj) {
480 EXPECT_EQ(1001, obj->size());
484 SYNCHRONIZED (lockedObj, *&obj) {
485 lockedObj.front() = 2;
488 EXPECT_EQ(1001, obj->size());
489 EXPECT_EQ(10, obj->back());
490 EXPECT_EQ(1000, obj2->size());
492 EXPECT_EQ(FB_ARG_2_OR_1(1, 2), 2);
493 EXPECT_EQ(FB_ARG_2_OR_1(1), 1);
496 template <class Mutex> void testConcurrency() {
497 folly::Synchronized<std::vector<int>, Mutex> v;
498 static const size_t numThreads = 100;
499 // Note: I initially tried using itersPerThread = 1000,
500 // which works fine for most lock types, but std::shared_timed_mutex
501 // appears to be extraordinarily slow. It could take around 30 seconds
502 // to run this test with 1000 iterations per thread using shared_timed_mutex.
503 static const size_t itersPerThread = 100;
505 auto pushNumbers = [&](size_t threadIdx) {
507 for (size_t n = 0; n < itersPerThread; ++n) {
508 v.contextualLock()->push_back((itersPerThread * threadIdx) + n);
512 runParallel(numThreads, pushNumbers);
514 std::vector<int> result;
517 EXPECT_EQ(numThreads * itersPerThread, result.size());
518 sort(result.begin(), result.end());
520 for (size_t i = 0; i < itersPerThread * numThreads; ++i) {
521 EXPECT_EQ(i, result[i]);
525 template <class Mutex>
526 void testAcquireLocked() {
527 folly::Synchronized<std::vector<int>, Mutex> v;
528 folly::Synchronized<std::map<int, int>, Mutex> m;
530 auto dualLockWorker = [&](size_t threadIdx) {
531 // Note: this will be less awkward with C++ 17's structured
532 // binding functionality, which will make it easier to use the returned
535 auto ret = acquireLocked(v, m);
536 std::get<0>(ret)->push_back(threadIdx);
537 (*std::get<1>(ret))[threadIdx] = threadIdx + 1;
539 auto ret = acquireLocked(m, v);
540 std::get<1>(ret)->push_back(threadIdx);
541 (*std::get<0>(ret))[threadIdx] = threadIdx + 1;
544 static const size_t numThreads = 100;
545 runParallel(numThreads, dualLockWorker);
547 std::vector<int> result;
550 EXPECT_EQ(numThreads, result.size());
551 sort(result.begin(), result.end());
553 for (size_t i = 0; i < numThreads; ++i) {
554 EXPECT_EQ(i, result[i]);
558 template <class Mutex>
559 void testAcquireLockedWithConst() {
560 folly::Synchronized<std::vector<int>, Mutex> v;
561 folly::Synchronized<std::map<int, int>, Mutex> m;
563 auto dualLockWorker = [&](size_t threadIdx) {
566 auto ret = acquireLocked(v, cm);
567 (void)std::get<1>(ret)->size();
568 std::get<0>(ret)->push_back(threadIdx);
570 auto ret = acquireLocked(cm, v);
571 (void)std::get<0>(ret)->size();
572 std::get<1>(ret)->push_back(threadIdx);
575 static const size_t numThreads = 100;
576 runParallel(numThreads, dualLockWorker);
578 std::vector<int> result;
581 EXPECT_EQ(numThreads, result.size());
582 sort(result.begin(), result.end());
584 for (size_t i = 0; i < numThreads; ++i) {
585 EXPECT_EQ(i, result[i]);
589 // Testing the deprecated SYNCHRONIZED_DUAL API
590 template <class Mutex> void testDualLocking() {
591 folly::Synchronized<std::vector<int>, Mutex> v;
592 folly::Synchronized<std::map<int, int>, Mutex> m;
594 auto dualLockWorker = [&](size_t threadIdx) {
596 SYNCHRONIZED_DUAL(lv, v, lm, m) {
597 lv.push_back(threadIdx);
598 lm[threadIdx] = threadIdx + 1;
601 SYNCHRONIZED_DUAL(lm, m, lv, v) {
602 lv.push_back(threadIdx);
603 lm[threadIdx] = threadIdx + 1;
607 static const size_t numThreads = 100;
608 runParallel(numThreads, dualLockWorker);
610 std::vector<int> result;
613 EXPECT_EQ(numThreads, result.size());
614 sort(result.begin(), result.end());
616 for (size_t i = 0; i < numThreads; ++i) {
617 EXPECT_EQ(i, result[i]);
621 // Testing the deprecated SYNCHRONIZED_DUAL API
622 template <class Mutex> void testDualLockingWithConst() {
623 folly::Synchronized<std::vector<int>, Mutex> v;
624 folly::Synchronized<std::map<int, int>, Mutex> m;
626 auto dualLockWorker = [&](size_t threadIdx) {
629 SYNCHRONIZED_DUAL(lv, v, lm, cm) {
631 lv.push_back(threadIdx);
634 SYNCHRONIZED_DUAL(lm, cm, lv, v) {
636 lv.push_back(threadIdx);
640 static const size_t numThreads = 100;
641 runParallel(numThreads, dualLockWorker);
643 std::vector<int> result;
646 EXPECT_EQ(numThreads, result.size());
647 sort(result.begin(), result.end());
649 for (size_t i = 0; i < numThreads; ++i) {
650 EXPECT_EQ(i, result[i]);
654 template <class Mutex>
656 folly::Synchronized<std::vector<int>, Mutex> v;
657 folly::Synchronized<uint64_t, Mutex> numTimeouts;
659 auto worker = [&](size_t threadIdx) {
660 // Test directly using operator-> on the lock result
661 v.contextualLock()->push_back(2 * threadIdx);
663 // Test using lock with a timeout
665 auto lv = v.contextualLock(std::chrono::milliseconds(5));
667 ++(*numTimeouts.contextualLock());
671 // Sleep for a random time to ensure we trigger timeouts
673 randomSleep(std::chrono::milliseconds(5), std::chrono::milliseconds(15));
674 lv->push_back(2 * threadIdx + 1);
679 static const size_t numThreads = 100;
680 runParallel(numThreads, worker);
682 std::vector<int> result;
685 EXPECT_EQ(2 * numThreads, result.size());
686 sort(result.begin(), result.end());
688 for (size_t i = 0; i < 2 * numThreads; ++i) {
689 EXPECT_EQ(i, result[i]);
691 // We generally expect a large number of number timeouts here.
692 // I'm not adding a check for it since it's theoretically possible that
693 // we might get 0 timeouts depending on the CPU scheduling if our threads
694 // don't get to run very often.
695 LOG(INFO) << "testTimed: " << *numTimeouts.contextualRLock() << " timeouts";
697 // Make sure we can lock with various timeout duration units
699 auto lv = v.contextualLock(std::chrono::milliseconds(5));
700 EXPECT_TRUE(bool(lv));
701 EXPECT_FALSE(lv.isNull());
702 auto lv2 = v.contextualLock(std::chrono::microseconds(5));
703 // We may or may not acquire lv2 successfully, depending on whether
704 // or not this is a recursive mutex type.
707 auto lv = v.contextualLock(std::chrono::seconds(1));
708 EXPECT_TRUE(bool(lv));
712 template <class Mutex>
713 void testTimedShared() {
714 folly::Synchronized<std::vector<int>, Mutex> v;
715 folly::Synchronized<uint64_t, Mutex> numTimeouts;
717 auto worker = [&](size_t threadIdx) {
718 // Test directly using operator-> on the lock result
719 v.wlock()->push_back(threadIdx);
721 // Test lock() with a timeout
723 auto lv = v.rlock(std::chrono::milliseconds(10));
725 ++(*numTimeouts.contextualLock());
729 // Sleep while holding the lock.
731 // This will block other threads from acquiring the write lock to add
732 // their thread index to v, but it won't block threads that have entered
733 // the for loop and are trying to acquire a read lock.
735 // For lock types that give preference to readers rather than writers,
736 // this will tend to serialize all threads on the wlock() above.
737 randomSleep(std::chrono::milliseconds(5), std::chrono::milliseconds(15));
738 auto found = std::find(lv->begin(), lv->end(), threadIdx);
739 CHECK(found != lv->end());
744 static const size_t numThreads = 100;
745 runParallel(numThreads, worker);
747 std::vector<int> result;
750 EXPECT_EQ(numThreads, result.size());
751 sort(result.begin(), result.end());
753 for (size_t i = 0; i < numThreads; ++i) {
754 EXPECT_EQ(i, result[i]);
756 // We generally expect a small number of timeouts here.
757 // For locks that give readers preference over writers this should usually
758 // be 0. With locks that give writers preference we do see a small-ish
759 // number of read timeouts.
760 LOG(INFO) << "testTimedShared: " << *numTimeouts.contextualRLock()
764 // Testing the deprecated TIMED_SYNCHRONIZED API
765 template <class Mutex> void testTimedSynchronized() {
766 folly::Synchronized<std::vector<int>, Mutex> v;
767 folly::Synchronized<uint64_t, Mutex> numTimeouts;
769 auto worker = [&](size_t threadIdx) {
771 v->push_back(2 * threadIdx);
773 // Aaand test the TIMED_SYNCHRONIZED macro
775 TIMED_SYNCHRONIZED(5, lv, v) {
777 // Sleep for a random time to ensure we trigger timeouts
780 std::chrono::milliseconds(5), std::chrono::milliseconds(15));
781 lv->push_back(2 * threadIdx + 1);
785 ++(*numTimeouts.contextualLock());
789 static const size_t numThreads = 100;
790 runParallel(numThreads, worker);
792 std::vector<int> result;
795 EXPECT_EQ(2 * numThreads, result.size());
796 sort(result.begin(), result.end());
798 for (size_t i = 0; i < 2 * numThreads; ++i) {
799 EXPECT_EQ(i, result[i]);
801 // We generally expect a large number of number timeouts here.
802 // I'm not adding a check for it since it's theoretically possible that
803 // we might get 0 timeouts depending on the CPU scheduling if our threads
804 // don't get to run very often.
805 LOG(INFO) << "testTimedSynchronized: " << *numTimeouts.contextualRLock()
809 // Testing the deprecated TIMED_SYNCHRONIZED_CONST API
810 template <class Mutex> void testTimedSynchronizedWithConst() {
811 folly::Synchronized<std::vector<int>, Mutex> v;
812 folly::Synchronized<uint64_t, Mutex> numTimeouts;
814 auto worker = [&](size_t threadIdx) {
816 v->push_back(threadIdx);
818 // Test TIMED_SYNCHRONIZED_CONST
820 TIMED_SYNCHRONIZED_CONST(10, lv, v) {
822 // Sleep while holding the lock.
824 // This will block other threads from acquiring the write lock to add
825 // their thread index to v, but it won't block threads that have
826 // entered the for loop and are trying to acquire a read lock.
828 // For lock types that give preference to readers rather than writers,
829 // this will tend to serialize all threads on the wlock() above.
831 std::chrono::milliseconds(5), std::chrono::milliseconds(15));
832 auto found = std::find(lv->begin(), lv->end(), threadIdx);
833 CHECK(found != lv->end());
836 ++(*numTimeouts.contextualLock());
842 static const size_t numThreads = 100;
843 runParallel(numThreads, worker);
845 std::vector<int> result;
848 EXPECT_EQ(numThreads, result.size());
849 sort(result.begin(), result.end());
851 for (size_t i = 0; i < numThreads; ++i) {
852 EXPECT_EQ(i, result[i]);
854 // We generally expect a small number of timeouts here.
855 // For locks that give readers preference over writers this should usually
856 // be 0. With locks that give writers preference we do see a small-ish
857 // number of read timeouts.
858 LOG(INFO) << "testTimedSynchronizedWithConst: "
859 << *numTimeouts.contextualRLock() << " timeouts";
862 template <class Mutex> void testConstCopy() {
863 std::vector<int> input = {1, 2, 3};
864 const folly::Synchronized<std::vector<int>, Mutex> v(input);
866 std::vector<int> result;
869 EXPECT_EQ(input, result);
872 EXPECT_EQ(input, result);
875 struct NotCopiableNotMovable {
876 NotCopiableNotMovable(int, const char*) {}
877 NotCopiableNotMovable(const NotCopiableNotMovable&) = delete;
878 NotCopiableNotMovable& operator=(const NotCopiableNotMovable&) = delete;
879 NotCopiableNotMovable(NotCopiableNotMovable&&) = delete;
880 NotCopiableNotMovable& operator=(NotCopiableNotMovable&&) = delete;
883 template <class Mutex> void testInPlaceConstruction() {
884 // This won't compile without construct_in_place
885 folly::Synchronized<NotCopiableNotMovable> a(
886 folly::construct_in_place, 5, "a"