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/Baton.h>
20 #include <folly/portability/GTest.h>
21 #include <folly/test/DeterministicSchedule.h>
26 typedef DeterministicSchedule DSched;
28 template <template <typename> class Atom, bool SinglePoster, bool Blocking>
29 void run_basic_test() {
30 Baton<Atom, SinglePoster, Blocking> b;
35 template <template <typename> class Atom, bool SinglePoster, bool Blocking>
36 void run_pingpong_test(int numRounds) {
37 using B = Baton<Atom, SinglePoster, Blocking>;
40 B& b = batons[16]; // to get it on a different cache line
41 auto thr = DSched::thread([&] {
42 for (int i = 0; i < numRounds; ++i) {
48 for (int i = 0; i < numRounds; ++i) {
56 template <template <typename> class Atom, typename Clock, bool SinglePoster>
57 void run_basic_timed_wait_tests() {
58 Baton<Atom, SinglePoster> b;
60 // tests if early delivery works fine
61 EXPECT_TRUE(b.timed_wait(Clock::now()));
64 template <template <typename> class Atom, typename Clock, bool SinglePoster>
65 void run_timed_wait_tmo_tests() {
66 Baton<Atom, SinglePoster> b;
68 auto thr = DSched::thread([&] {
69 bool rv = b.timed_wait(Clock::now() + std::chrono::milliseconds(1));
70 // main thread is guaranteed to not post until timeout occurs
76 template <template <typename> class Atom, typename Clock, bool SinglePoster>
77 void run_timed_wait_regular_test() {
78 Baton<Atom, SinglePoster> b;
80 auto thr = DSched::thread([&] {
81 // To wait forever we'd like to use time_point<Clock>::max, but
82 // std::condition_variable does math to convert the timeout to
83 // system_clock without handling overflow.
84 auto farFuture = Clock::now() + std::chrono::hours(1000);
85 bool rv = b.timed_wait(farFuture);
86 if (!std::is_same<Atom<int>, DeterministicAtomic<int>>::value) {
87 // DeterministicAtomic ignores actual times, so doesn't guarantee
93 if (!std::is_same<Atom<int>, DeterministicAtomic<int>>::value) {
94 // If we are using std::atomic (or EmulatedFutexAtomic) then
95 // a sleep here guarantees to a large extent that 'thr' will
96 // execute wait before we post it, thus testing late delivery. For
97 // DeterministicAtomic, we just rely on DeterministicSchedule to do
98 // the scheduling. The test won't fail if we lose the race, we just
99 // don't get coverage.
100 std::this_thread::sleep_for(std::chrono::milliseconds(2));
107 template <template <typename> class Atom, bool SinglePoster, bool Blocking>
108 void run_try_wait_tests() {
109 Baton<Atom, SinglePoster, Blocking> b;
110 EXPECT_FALSE(b.try_wait());
112 EXPECT_TRUE(b.try_wait());
115 template <template <typename> class Atom, bool SinglePoster, bool Blocking>
116 void run_multi_producer_tests() {
117 constexpr int NPROD = 5;
118 Baton<Atom, SinglePoster, Blocking> local_ping[NPROD];
119 Baton<Atom, SinglePoster, Blocking> local_pong[NPROD];
120 Baton<Atom, /* SingleProducer = */ false, Blocking> global;
121 Baton<Atom, SinglePoster, Blocking> shutdown;
123 std::thread prod[NPROD];
124 for (int i = 0; i < NPROD; ++i) {
125 prod[i] = DSched::thread([&, i] {
126 if (!std::is_same<Atom<int>, DeterministicAtomic<int>>::value) {
127 // If we are using std::atomic (or EmulatedFutexAtomic) then
128 // a variable sleep here will make it more likely that
129 // global.post()-s will span more than one global.wait() by
130 // the consumer thread and for the latter to block (if the
131 // global baton is blocking). For DeterministicAtomic, we just
132 // rely on DeterministicSchedule to do the scheduling. The
133 // test won't fail if we lose the race, we just don't get
135 for (int j = 0; j < i; ++j) {
136 std::this_thread::sleep_for(std::chrono::microseconds(1));
139 local_ping[i].post();
141 local_pong[i].wait();
145 auto cons = DSched::thread([&] {
149 if (shutdown.try_wait()) {
152 for (int i = 0; i < NPROD; ++i) {
153 if (local_ping.try_wait()) {
161 for (auto& t : prod) {
170 } // namespace test {
171 } // namespace folly {