2 * Copyright 2017 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 <folly/Foreach.h>
20 #include <folly/Random.h>
21 #include <folly/Synchronized.h>
22 #include <folly/portability/GTest.h>
23 #include <glog/logging.h>
25 #include <condition_variable>
33 namespace sync_tests {
35 inline std::mt19937& getRNG() {
36 static const auto seed = folly::randomNumberSeed();
37 static std::mt19937 rng(seed);
41 void randomSleep(std::chrono::milliseconds min, std::chrono::milliseconds max) {
42 std::uniform_int_distribution<> range(min.count(), max.count());
43 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;
111 const auto& constObj = obj;
113 obj.wlock()->resize(1000);
115 folly::Synchronized<std::vector<int>, Mutex> obj2{*obj.wlock()};
116 EXPECT_EQ(1000, obj2.rlock()->size());
119 auto lockedObj = obj.wlock();
120 lockedObj->push_back(10);
121 EXPECT_EQ(1001, lockedObj->size());
122 EXPECT_EQ(10, lockedObj->back());
123 EXPECT_EQ(1000, obj2.wlock()->size());
124 EXPECT_EQ(1000, obj2.rlock()->size());
127 auto unlocker = lockedObj.scopedUnlock();
128 EXPECT_EQ(1001, obj.wlock()->size());
133 auto lockedObj = obj.rlock();
134 EXPECT_EQ(1001, lockedObj->size());
135 EXPECT_EQ(1001, obj.rlock()->size());
137 auto unlocker = lockedObj.scopedUnlock();
138 EXPECT_EQ(1001, obj.wlock()->size());
142 obj.wlock()->front() = 2;
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;
163 const auto& constObj = obj;
165 obj.lock()->resize(1000);
167 folly::Synchronized<std::vector<int>, Mutex> obj2{*obj.lock()};
168 EXPECT_EQ(1000, obj2.lock()->size());
171 auto lockedObj = obj.lock();
172 lockedObj->push_back(10);
173 EXPECT_EQ(1001, lockedObj->size());
174 EXPECT_EQ(10, lockedObj->back());
175 EXPECT_EQ(1000, obj2.lock()->size());
178 auto unlocker = lockedObj.scopedUnlock();
179 EXPECT_EQ(1001, obj.lock()->size());
183 auto lockedObj = constObj.lock();
184 EXPECT_EQ(1001, lockedObj->size());
185 EXPECT_EQ(10, lockedObj->back());
186 EXPECT_EQ(1000, obj2.lock()->size());
189 obj.lock()->front() = 2;
191 EXPECT_EQ(1001, obj.lock()->size());
192 EXPECT_EQ(2, obj.lock()->front());
193 EXPECT_EQ(2, obj.contextualLock()->front());
194 EXPECT_EQ(10, obj.lock()->back());
195 EXPECT_EQ(1000, obj2.lock()->size());
198 template <class Mutex>
200 testBasicImpl<Mutex>();
203 // testWithLock() version for shared lock types
204 template <class Mutex>
205 typename std::enable_if<folly::LockTraits<Mutex>::is_shared>::type
207 folly::Synchronized<std::vector<int>, Mutex> obj;
208 const auto& constObj = obj;
210 // Test withWLock() and withRLock()
211 obj.withWLock([](std::vector<int>& lockedObj) {
212 lockedObj.resize(1000);
213 lockedObj.push_back(10);
214 lockedObj.push_back(11);
216 obj.withWLock([](const std::vector<int>& lockedObj) {
217 EXPECT_EQ(1002, lockedObj.size());
219 constObj.withWLock([](const std::vector<int>& lockedObj) {
220 EXPECT_EQ(1002, lockedObj.size());
221 EXPECT_EQ(11, lockedObj.back());
223 obj.withRLock([](const std::vector<int>& lockedObj) {
224 EXPECT_EQ(1002, lockedObj.size());
225 EXPECT_EQ(11, lockedObj.back());
227 constObj.withRLock([](const std::vector<int>& lockedObj) {
228 EXPECT_EQ(1002, lockedObj.size());
231 #if __cpp_generic_lambdas >= 201304
232 obj.withWLock([](auto& lockedObj) { lockedObj.push_back(12); });
234 [](const auto& lockedObj) { EXPECT_EQ(1003, lockedObj.size()); });
235 constObj.withWLock([](const auto& lockedObj) {
236 EXPECT_EQ(1003, lockedObj.size());
237 EXPECT_EQ(12, lockedObj.back());
239 obj.withRLock([](const auto& lockedObj) {
240 EXPECT_EQ(1003, lockedObj.size());
241 EXPECT_EQ(12, lockedObj.back());
244 [](const auto& lockedObj) { EXPECT_EQ(1003, lockedObj.size()); });
245 obj.withWLock([](auto& lockedObj) { lockedObj.pop_back(); });
248 // Test withWLockPtr() and withRLockPtr()
249 using SynchType = folly::Synchronized<std::vector<int>, Mutex>;
250 #if __cpp_generic_lambdas >= 201304
251 obj.withWLockPtr([](auto&& lockedObj) { lockedObj->push_back(13); });
252 obj.withRLockPtr([](auto&& lockedObj) {
253 EXPECT_EQ(1003, lockedObj->size());
254 EXPECT_EQ(13, lockedObj->back());
256 constObj.withRLockPtr([](auto&& lockedObj) {
257 EXPECT_EQ(1003, lockedObj->size());
258 EXPECT_EQ(13, lockedObj->back());
260 obj.withWLockPtr([&](auto&& lockedObj) {
261 lockedObj->push_back(14);
263 auto unlocker = lockedObj.scopedUnlock();
264 obj.wlock()->push_back(15);
266 EXPECT_EQ(15, lockedObj->back());
268 constObj.withWLockPtr([](auto&& lockedObj) {
269 EXPECT_EQ(1005, lockedObj->size());
270 EXPECT_EQ(15, lockedObj->back());
273 obj.withWLockPtr([](typename SynchType::LockedPtr&& lockedObj) {
274 lockedObj->push_back(13);
275 lockedObj->push_back(14);
276 lockedObj->push_back(15);
280 obj.withWLockPtr([](typename SynchType::LockedPtr&& lockedObj) {
281 lockedObj->push_back(16);
282 EXPECT_EQ(1006, lockedObj->size());
284 constObj.withWLockPtr([](typename SynchType::ConstWLockedPtr&& lockedObj) {
285 EXPECT_EQ(1006, lockedObj->size());
286 EXPECT_EQ(16, lockedObj->back());
288 obj.withRLockPtr([](typename SynchType::ConstLockedPtr&& lockedObj) {
289 EXPECT_EQ(1006, lockedObj->size());
290 EXPECT_EQ(16, lockedObj->back());
292 constObj.withRLockPtr([](typename SynchType::ConstLockedPtr&& lockedObj) {
293 EXPECT_EQ(1006, lockedObj->size());
294 EXPECT_EQ(16, lockedObj->back());
298 // testWithLock() version for non-shared lock types
299 template <class Mutex>
300 typename std::enable_if<!folly::LockTraits<Mutex>::is_shared>::type
302 folly::Synchronized<std::vector<int>, Mutex> obj;
305 obj.withLock([](std::vector<int>& lockedObj) {
306 lockedObj.resize(1000);
307 lockedObj.push_back(10);
308 lockedObj.push_back(11);
310 obj.withLock([](const std::vector<int>& lockedObj) {
311 EXPECT_EQ(1002, lockedObj.size());
314 #if __cpp_generic_lambdas >= 201304
315 obj.withLock([](auto& lockedObj) { lockedObj.push_back(12); });
317 [](const auto& lockedObj) { EXPECT_EQ(1003, lockedObj.size()); });
318 obj.withLock([](auto& lockedObj) { lockedObj.pop_back(); });
321 // Test withLockPtr()
322 using SynchType = folly::Synchronized<std::vector<int>, Mutex>;
323 #if __cpp_generic_lambdas >= 201304
324 obj.withLockPtr([](auto&& lockedObj) { lockedObj->push_back(13); });
325 obj.withLockPtr([](auto&& lockedObj) {
326 EXPECT_EQ(1003, lockedObj->size());
327 EXPECT_EQ(13, lockedObj->back());
329 obj.withLockPtr([&](auto&& lockedObj) {
330 lockedObj->push_back(14);
332 auto unlocker = lockedObj.scopedUnlock();
333 obj.lock()->push_back(15);
335 EXPECT_EQ(1005, lockedObj->size());
336 EXPECT_EQ(15, lockedObj->back());
339 obj.withLockPtr([](typename SynchType::LockedPtr&& lockedObj) {
340 lockedObj->push_back(13);
341 lockedObj->push_back(14);
342 lockedObj->push_back(15);
346 obj.withLockPtr([](typename SynchType::LockedPtr&& lockedObj) {
347 lockedObj->push_back(16);
348 EXPECT_EQ(1006, lockedObj->size());
350 const auto& constObj = obj;
351 constObj.withLockPtr([](typename SynchType::ConstLockedPtr&& lockedObj) {
352 EXPECT_EQ(1006, lockedObj->size());
353 EXPECT_EQ(16, lockedObj->back());
357 template <class Mutex>
358 void testUnlockCommon() {
359 folly::Synchronized<int, Mutex> value{7};
360 const auto& cv = value;
363 auto lv = value.contextualLock();
367 EXPECT_TRUE(lv.isNull());
370 auto rlv = cv.contextualLock();
373 EXPECT_TRUE(rlv.isNull());
376 auto rlv2 = cv.contextualRLock();
380 lv = value.contextualLock();
385 EXPECT_EQ(9, *value.contextualRLock());
388 // testUnlock() version for shared lock types
389 template <class Mutex>
390 typename std::enable_if<folly::LockTraits<Mutex>::is_shared>::type
392 folly::Synchronized<int, Mutex> value{10};
394 auto lv = value.wlock();
399 EXPECT_TRUE(lv.isNull());
401 auto rlv = value.rlock();
405 EXPECT_TRUE(rlv.isNull());
407 auto lv2 = value.wlock();
413 EXPECT_TRUE(lv2.isNull());
414 EXPECT_FALSE(lv.isNull());
418 testUnlockCommon<Mutex>();
421 // testUnlock() version for non-shared lock types
422 template <class Mutex>
423 typename std::enable_if<!folly::LockTraits<Mutex>::is_shared>::type
425 folly::Synchronized<int, Mutex> value{10};
427 auto lv = value.lock();
431 EXPECT_TRUE(lv.isNull());
434 auto lv2 = value.lock();
438 EXPECT_TRUE(lv2.isNull());
446 EXPECT_TRUE(lv.isNull());
448 EXPECT_FALSE(lv2.isNull());
452 testUnlockCommon<Mutex>();
455 // Testing the deprecated SYNCHRONIZED and SYNCHRONIZED_CONST APIs
456 template <class Mutex>
457 void testDeprecated() {
458 folly::Synchronized<std::vector<int>, Mutex> obj;
463 EXPECT_EQ(1000, obj2->size());
467 EXPECT_EQ(1001, obj.size());
468 EXPECT_EQ(10, obj.back());
469 EXPECT_EQ(1000, obj2->size());
472 SYNCHRONIZED_CONST (obj) {
473 EXPECT_EQ(1001, obj.size());
476 SYNCHRONIZED (lockedObj, *&obj) {
477 lockedObj.front() = 2;
480 EXPECT_EQ(1001, obj->size());
481 EXPECT_EQ(10, obj->back());
482 EXPECT_EQ(1000, obj2->size());
484 EXPECT_EQ(FB_ARG_2_OR_1(1, 2), 2);
485 EXPECT_EQ(FB_ARG_2_OR_1(1), 1);
488 template <class Mutex> void testConcurrency() {
489 folly::Synchronized<std::vector<int>, Mutex> v;
490 static const size_t numThreads = 100;
491 // Note: I initially tried using itersPerThread = 1000,
492 // which works fine for most lock types, but std::shared_timed_mutex
493 // appears to be extraordinarily slow. It could take around 30 seconds
494 // to run this test with 1000 iterations per thread using shared_timed_mutex.
495 static const size_t itersPerThread = 100;
497 auto pushNumbers = [&](size_t threadIdx) {
499 for (size_t n = 0; n < itersPerThread; ++n) {
500 v.contextualLock()->push_back((itersPerThread * threadIdx) + n);
501 std::this_thread::yield();
504 runParallel(numThreads, pushNumbers);
506 std::vector<int> result;
509 EXPECT_EQ(numThreads * itersPerThread, result.size());
510 sort(result.begin(), result.end());
512 for (size_t i = 0; i < itersPerThread * numThreads; ++i) {
513 EXPECT_EQ(i, result[i]);
517 template <class Mutex>
518 void testAcquireLocked() {
519 folly::Synchronized<std::vector<int>, Mutex> v;
520 folly::Synchronized<std::map<int, int>, Mutex> m;
522 auto dualLockWorker = [&](size_t threadIdx) {
523 // Note: this will be less awkward with C++ 17's structured
524 // binding functionality, which will make it easier to use the returned
527 auto ret = acquireLocked(v, m);
528 std::get<0>(ret)->push_back(threadIdx);
529 (*std::get<1>(ret))[threadIdx] = threadIdx + 1;
531 auto ret = acquireLocked(m, v);
532 std::get<1>(ret)->push_back(threadIdx);
533 (*std::get<0>(ret))[threadIdx] = threadIdx + 1;
536 static const size_t numThreads = 100;
537 runParallel(numThreads, dualLockWorker);
539 std::vector<int> result;
542 EXPECT_EQ(numThreads, result.size());
543 sort(result.begin(), result.end());
545 for (size_t i = 0; i < numThreads; ++i) {
546 EXPECT_EQ(i, result[i]);
550 template <class Mutex>
551 void testAcquireLockedWithConst() {
552 folly::Synchronized<std::vector<int>, Mutex> v;
553 folly::Synchronized<std::map<int, int>, Mutex> m;
555 auto dualLockWorker = [&](size_t threadIdx) {
558 auto ret = acquireLocked(v, cm);
559 (void)std::get<1>(ret)->size();
560 std::get<0>(ret)->push_back(threadIdx);
562 auto ret = acquireLocked(cm, v);
563 (void)std::get<0>(ret)->size();
564 std::get<1>(ret)->push_back(threadIdx);
567 static const size_t numThreads = 100;
568 runParallel(numThreads, dualLockWorker);
570 std::vector<int> result;
573 EXPECT_EQ(numThreads, result.size());
574 sort(result.begin(), result.end());
576 for (size_t i = 0; i < numThreads; ++i) {
577 EXPECT_EQ(i, result[i]);
581 // Testing the deprecated SYNCHRONIZED_DUAL API
582 template <class Mutex> void testDualLocking() {
583 folly::Synchronized<std::vector<int>, Mutex> v;
584 folly::Synchronized<std::map<int, int>, Mutex> m;
586 auto dualLockWorker = [&](size_t threadIdx) {
588 SYNCHRONIZED_DUAL(lv, v, lm, m) {
589 lv.push_back(threadIdx);
590 lm[threadIdx] = threadIdx + 1;
593 SYNCHRONIZED_DUAL(lm, m, lv, v) {
594 lv.push_back(threadIdx);
595 lm[threadIdx] = threadIdx + 1;
599 static const size_t numThreads = 100;
600 runParallel(numThreads, dualLockWorker);
602 std::vector<int> result;
605 EXPECT_EQ(numThreads, result.size());
606 sort(result.begin(), result.end());
608 for (size_t i = 0; i < numThreads; ++i) {
609 EXPECT_EQ(i, result[i]);
613 // Testing the deprecated SYNCHRONIZED_DUAL API
614 template <class Mutex> void testDualLockingWithConst() {
615 folly::Synchronized<std::vector<int>, Mutex> v;
616 folly::Synchronized<std::map<int, int>, Mutex> m;
618 auto dualLockWorker = [&](size_t threadIdx) {
621 SYNCHRONIZED_DUAL(lv, v, lm, cm) {
623 lv.push_back(threadIdx);
626 SYNCHRONIZED_DUAL(lm, cm, lv, v) {
628 lv.push_back(threadIdx);
632 static const size_t numThreads = 100;
633 runParallel(numThreads, dualLockWorker);
635 std::vector<int> result;
638 EXPECT_EQ(numThreads, result.size());
639 sort(result.begin(), result.end());
641 for (size_t i = 0; i < numThreads; ++i) {
642 EXPECT_EQ(i, result[i]);
646 template <class Mutex>
648 folly::Synchronized<std::vector<int>, Mutex> v;
649 folly::Synchronized<uint64_t, Mutex> numTimeouts;
651 auto worker = [&](size_t threadIdx) {
652 // Test directly using operator-> on the lock result
653 v.contextualLock()->push_back(2 * threadIdx);
655 // Test using lock with a timeout
657 auto lv = v.contextualLock(std::chrono::milliseconds(5));
659 ++(*numTimeouts.contextualLock());
663 // Sleep for a random time to ensure we trigger timeouts
665 randomSleep(std::chrono::milliseconds(5), std::chrono::milliseconds(15));
666 lv->push_back(2 * threadIdx + 1);
671 static const size_t numThreads = 100;
672 runParallel(numThreads, worker);
674 std::vector<int> result;
677 EXPECT_EQ(2 * numThreads, result.size());
678 sort(result.begin(), result.end());
680 for (size_t i = 0; i < 2 * numThreads; ++i) {
681 EXPECT_EQ(i, result[i]);
683 // We generally expect a large number of number timeouts here.
684 // I'm not adding a check for it since it's theoretically possible that
685 // we might get 0 timeouts depending on the CPU scheduling if our threads
686 // don't get to run very often.
687 LOG(INFO) << "testTimed: " << *numTimeouts.contextualRLock() << " timeouts";
689 // Make sure we can lock with various timeout duration units
691 auto lv = v.contextualLock(std::chrono::milliseconds(5));
692 EXPECT_TRUE(bool(lv));
693 EXPECT_FALSE(lv.isNull());
694 auto lv2 = v.contextualLock(std::chrono::microseconds(5));
695 // We may or may not acquire lv2 successfully, depending on whether
696 // or not this is a recursive mutex type.
699 auto lv = v.contextualLock(std::chrono::seconds(1));
700 EXPECT_TRUE(bool(lv));
704 template <class Mutex>
705 void testTimedShared() {
706 folly::Synchronized<std::vector<int>, Mutex> v;
707 folly::Synchronized<uint64_t, Mutex> numTimeouts;
709 auto worker = [&](size_t threadIdx) {
710 // Test directly using operator-> on the lock result
711 v.wlock()->push_back(threadIdx);
713 // Test lock() with a timeout
715 auto lv = v.rlock(std::chrono::milliseconds(10));
717 ++(*numTimeouts.contextualLock());
721 // Sleep while holding the lock.
723 // This will block other threads from acquiring the write lock to add
724 // their thread index to v, but it won't block threads that have entered
725 // the for loop and are trying to acquire a read lock.
727 // For lock types that give preference to readers rather than writers,
728 // this will tend to serialize all threads on the wlock() above.
729 randomSleep(std::chrono::milliseconds(5), std::chrono::milliseconds(15));
730 auto found = std::find(lv->begin(), lv->end(), threadIdx);
731 CHECK(found != lv->end());
736 static const size_t numThreads = 100;
737 runParallel(numThreads, worker);
739 std::vector<int> result;
742 EXPECT_EQ(numThreads, result.size());
743 sort(result.begin(), result.end());
745 for (size_t i = 0; i < numThreads; ++i) {
746 EXPECT_EQ(i, result[i]);
748 // We generally expect a small number of timeouts here.
749 // For locks that give readers preference over writers this should usually
750 // be 0. With locks that give writers preference we do see a small-ish
751 // number of read timeouts.
752 LOG(INFO) << "testTimedShared: " << *numTimeouts.contextualRLock()
756 // Testing the deprecated TIMED_SYNCHRONIZED API
757 template <class Mutex> void testTimedSynchronized() {
758 folly::Synchronized<std::vector<int>, Mutex> v;
759 folly::Synchronized<uint64_t, Mutex> numTimeouts;
761 auto worker = [&](size_t threadIdx) {
763 v->push_back(2 * threadIdx);
765 // Aaand test the TIMED_SYNCHRONIZED macro
767 TIMED_SYNCHRONIZED(5, lv, v) {
769 // Sleep for a random time to ensure we trigger timeouts
772 std::chrono::milliseconds(5), std::chrono::milliseconds(15));
773 lv->push_back(2 * threadIdx + 1);
777 ++(*numTimeouts.contextualLock());
781 static const size_t numThreads = 100;
782 runParallel(numThreads, worker);
784 std::vector<int> result;
787 EXPECT_EQ(2 * numThreads, result.size());
788 sort(result.begin(), result.end());
790 for (size_t i = 0; i < 2 * numThreads; ++i) {
791 EXPECT_EQ(i, result[i]);
793 // We generally expect a large number of number timeouts here.
794 // I'm not adding a check for it since it's theoretically possible that
795 // we might get 0 timeouts depending on the CPU scheduling if our threads
796 // don't get to run very often.
797 LOG(INFO) << "testTimedSynchronized: " << *numTimeouts.contextualRLock()
801 // Testing the deprecated TIMED_SYNCHRONIZED_CONST API
802 template <class Mutex> void testTimedSynchronizedWithConst() {
803 folly::Synchronized<std::vector<int>, Mutex> v;
804 folly::Synchronized<uint64_t, Mutex> numTimeouts;
806 auto worker = [&](size_t threadIdx) {
808 v->push_back(threadIdx);
810 // Test TIMED_SYNCHRONIZED_CONST
812 TIMED_SYNCHRONIZED_CONST(10, lv, v) {
814 // Sleep while holding the lock.
816 // This will block other threads from acquiring the write lock to add
817 // their thread index to v, but it won't block threads that have
818 // entered the for loop and are trying to acquire a read lock.
820 // For lock types that give preference to readers rather than writers,
821 // this will tend to serialize all threads on the wlock() above.
823 std::chrono::milliseconds(5), std::chrono::milliseconds(15));
824 auto found = std::find(lv->begin(), lv->end(), threadIdx);
825 CHECK(found != lv->end());
828 ++(*numTimeouts.contextualLock());
834 static const size_t numThreads = 100;
835 runParallel(numThreads, worker);
837 std::vector<int> result;
840 EXPECT_EQ(numThreads, result.size());
841 sort(result.begin(), result.end());
843 for (size_t i = 0; i < numThreads; ++i) {
844 EXPECT_EQ(i, result[i]);
846 // We generally expect a small number of timeouts here.
847 // For locks that give readers preference over writers this should usually
848 // be 0. With locks that give writers preference we do see a small-ish
849 // number of read timeouts.
850 LOG(INFO) << "testTimedSynchronizedWithConst: "
851 << *numTimeouts.contextualRLock() << " timeouts";
854 template <class Mutex> void testConstCopy() {
855 std::vector<int> input = {1, 2, 3};
856 const folly::Synchronized<std::vector<int>, Mutex> v(input);
858 std::vector<int> result;
861 EXPECT_EQ(input, result);
864 EXPECT_EQ(input, result);
867 struct NotCopiableNotMovable {
868 NotCopiableNotMovable(int, const char*) {}
869 NotCopiableNotMovable(const NotCopiableNotMovable&) = delete;
870 NotCopiableNotMovable& operator=(const NotCopiableNotMovable&) = delete;
871 NotCopiableNotMovable(NotCopiableNotMovable&&) = delete;
872 NotCopiableNotMovable& operator=(NotCopiableNotMovable&&) = delete;
875 template <class Mutex> void testInPlaceConstruction() {
876 // This won't compile without in_place
877 folly::Synchronized<NotCopiableNotMovable> a(folly::in_place, 5, "a");