1/*
2 * Copyright 2014-present Facebook, Inc.
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#include <folly/synchronization/Baton.h>
18
19#include <thread>
20
21#include <folly/portability/GTest.h>
22#include <folly/synchronization/test/BatonTestHelpers.h>
23#include <folly/test/DeterministicSchedule.h>
24
25using namespace folly;
26using namespace folly::test;
27using folly::detail::EmulatedFutexAtomic;
28using std::chrono::steady_clock;
29using std::chrono::system_clock;
30
31/// Basic test
32
33TEST(Baton, basic_blocking) {
34 run_basic_test<true, std::atomic>();
35 run_basic_test<true, EmulatedFutexAtomic>();
36 run_basic_test<true, DeterministicAtomic>();
37}
38
39TEST(Baton, basic_nonblocking) {
40 run_basic_test<false, std::atomic>();
41 run_basic_test<false, EmulatedFutexAtomic>();
42 run_basic_test<false, DeterministicAtomic>();
43}
44
45/// Ping pong tests
46
47TEST(Baton, pingpong_blocking) {
48 DSched sched(DSched::uniform(0));
49
50 run_pingpong_test<true, DeterministicAtomic>(1000);
51}
52
53TEST(Baton, pingpong_nonblocking) {
54 DSched sched(DSched::uniform(0));
55
56 run_pingpong_test<false, DeterministicAtomic>(1000);
57}
58
59// Timed wait basic system clock tests
60
61TEST(Baton, timed_wait_basic_system_clock_blocking) {
62 run_basic_timed_wait_tests<true, std::atomic, system_clock>();
63 run_basic_timed_wait_tests<true, EmulatedFutexAtomic, system_clock>();
64 run_basic_timed_wait_tests<true, DeterministicAtomic, system_clock>();
65}
66
67TEST(Baton, timed_wait_basic_system_clock_nonblocking) {
68 run_basic_timed_wait_tests<false, std::atomic, system_clock>();
69 run_basic_timed_wait_tests<false, EmulatedFutexAtomic, system_clock>();
70 run_basic_timed_wait_tests<false, DeterministicAtomic, system_clock>();
71}
72
73// Timed wait timeout system clock tests
74
75TEST(Baton, timed_wait_timeout_system_clock_blocking) {
76 DSched sched(DSched::uniform(0));
77 run_timed_wait_tmo_tests<true, std::atomic, system_clock>();
78 run_timed_wait_tmo_tests<true, EmulatedFutexAtomic, system_clock>();
79 run_timed_wait_tmo_tests<true, DeterministicAtomic, system_clock>();
80}
81
82TEST(Baton, timed_wait_timeout_system_clock_nonblocking) {
83 DSched sched(DSched::uniform(0));
84 run_timed_wait_tmo_tests<false, std::atomic, system_clock>();
85 run_timed_wait_tmo_tests<false, EmulatedFutexAtomic, system_clock>();
86 run_timed_wait_tmo_tests<false, DeterministicAtomic, system_clock>();
87}
88
89// Timed wait regular system clock tests
90
91TEST(Baton, timed_wait_system_clock_blocking) {
92 DSched sched(DSched::uniform(0));
93 run_timed_wait_regular_test<true, std::atomic, system_clock>();
94 run_timed_wait_regular_test<true, EmulatedFutexAtomic, system_clock>();
95 run_timed_wait_regular_test<true, DeterministicAtomic, system_clock>();
96}
97
98TEST(Baton, timed_wait_system_clock_nonblocking) {
99 DSched sched(DSched::uniform(0));
100 run_timed_wait_regular_test<false, std::atomic, system_clock>();
101 run_timed_wait_regular_test<false, EmulatedFutexAtomic, system_clock>();
102 run_timed_wait_regular_test<false, DeterministicAtomic, system_clock>();
103}
104
105// Timed wait basic steady clock tests
106
107TEST(Baton, timed_wait_basic_steady_clock_blocking) {
108 DSched sched(DSched::uniform(0));
109 run_basic_timed_wait_tests<true, std::atomic, steady_clock>();
110 run_basic_timed_wait_tests<true, EmulatedFutexAtomic, steady_clock>();
111 run_basic_timed_wait_tests<true, DeterministicAtomic, steady_clock>();
112}
113
114TEST(Baton, timed_wait_basic_steady_clock_nonblocking) {
115 DSched sched(DSched::uniform(0));
116 run_basic_timed_wait_tests<false, std::atomic, steady_clock>();
117 run_basic_timed_wait_tests<false, EmulatedFutexAtomic, steady_clock>();
118 run_basic_timed_wait_tests<false, DeterministicAtomic, steady_clock>();
119}
120
121// Timed wait timeout steady clock tests
122
123TEST(Baton, timed_wait_timeout_steady_clock_blocking) {
124 DSched sched(DSched::uniform(0));
125 run_timed_wait_tmo_tests<true, std::atomic, steady_clock>();
126 run_timed_wait_tmo_tests<true, EmulatedFutexAtomic, steady_clock>();
127 run_timed_wait_tmo_tests<true, DeterministicAtomic, steady_clock>();
128}
129
130TEST(Baton, timed_wait_timeout_steady_clock_nonblocking) {
131 DSched sched(DSched::uniform(0));
132 run_timed_wait_tmo_tests<false, std::atomic, steady_clock>();
133 run_timed_wait_tmo_tests<false, EmulatedFutexAtomic, steady_clock>();
134 run_timed_wait_tmo_tests<false, DeterministicAtomic, steady_clock>();
135}
136
137// Timed wait regular steady clock tests
138
139TEST(Baton, timed_wait_steady_clock_blocking) {
140 DSched sched(DSched::uniform(0));
141 run_timed_wait_regular_test<true, std::atomic, steady_clock>();
142 run_timed_wait_regular_test<true, EmulatedFutexAtomic, steady_clock>();
143 run_timed_wait_regular_test<true, DeterministicAtomic, steady_clock>();
144}
145
146TEST(Baton, timed_wait_steady_clock_nonblocking) {
147 DSched sched(DSched::uniform(0));
148 run_timed_wait_regular_test<false, std::atomic, steady_clock>();
149 run_timed_wait_regular_test<false, EmulatedFutexAtomic, steady_clock>();
150 run_timed_wait_regular_test<false, DeterministicAtomic, steady_clock>();
151}
152
153/// Try wait tests
154
155TEST(Baton, try_wait_blocking) {
156 run_try_wait_tests<true, std::atomic>();
157 run_try_wait_tests<true, EmulatedFutexAtomic>();
158 run_try_wait_tests<true, DeterministicAtomic>();
159}
160
161TEST(Baton, try_wait_nonblocking) {
162 run_try_wait_tests<false, std::atomic>();
163 run_try_wait_tests<false, EmulatedFutexAtomic>();
164 run_try_wait_tests<false, DeterministicAtomic>();
165}
166