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.
17 #include <folly/synchronization/Baton.h>
21 #include <folly/portability/GTest.h>
22 #include <folly/synchronization/test/BatonTestHelpers.h>
23 #include <folly/test/DeterministicSchedule.h>
25 using namespace folly;
26 using namespace folly::test;
27 using folly::detail::EmulatedFutexAtomic;
31 TEST(Baton, basic_blocking) {
32 run_basic_test<true, std::atomic>();
33 run_basic_test<true, EmulatedFutexAtomic>();
34 run_basic_test<true, DeterministicAtomic>();
37 TEST(Baton, basic_nonblocking) {
38 run_basic_test<false, std::atomic>();
39 run_basic_test<false, EmulatedFutexAtomic>();
40 run_basic_test<false, DeterministicAtomic>();
45 TEST(Baton, pingpong_blocking) {
46 DSched sched(DSched::uniform(0));
48 run_pingpong_test<true, DeterministicAtomic>(1000);
51 TEST(Baton, pingpong_nonblocking) {
52 DSched sched(DSched::uniform(0));
54 run_pingpong_test<false, DeterministicAtomic>(1000);
57 /// Timed wait tests - Nonblocking Baton does not support try_wait_until()
59 // Timed wait basic system clock tests
61 TEST(Baton, timed_wait_basic_system_clock) {
62 run_basic_timed_wait_tests<std::atomic, std::chrono::system_clock>();
63 run_basic_timed_wait_tests<EmulatedFutexAtomic, std::chrono::system_clock>();
64 run_basic_timed_wait_tests<DeterministicAtomic, std::chrono::system_clock>();
67 // Timed wait timeout system clock tests
69 TEST(Baton, timed_wait_timeout_system_clock) {
70 run_timed_wait_tmo_tests<std::atomic, std::chrono::system_clock>();
71 run_timed_wait_tmo_tests<EmulatedFutexAtomic, std::chrono::system_clock>();
72 run_timed_wait_tmo_tests<DeterministicAtomic, std::chrono::system_clock>();
75 // Timed wait regular system clock tests
77 TEST(Baton, timed_wait_system_clock) {
78 run_timed_wait_regular_test<std::atomic, std::chrono::system_clock>();
79 run_timed_wait_regular_test<EmulatedFutexAtomic, std::chrono::system_clock>();
80 run_timed_wait_regular_test<DeterministicAtomic, std::chrono::system_clock>();
83 // Timed wait basic steady clock tests
85 TEST(Baton, timed_wait_basic_steady_clock) {
86 run_basic_timed_wait_tests<std::atomic, std::chrono::steady_clock>();
87 run_basic_timed_wait_tests<EmulatedFutexAtomic, std::chrono::steady_clock>();
88 run_basic_timed_wait_tests<DeterministicAtomic, std::chrono::steady_clock>();
91 // Timed wait timeout steady clock tests
93 TEST(Baton, timed_wait_timeout_steady_clock) {
94 run_timed_wait_tmo_tests<std::atomic, std::chrono::steady_clock>();
95 run_timed_wait_tmo_tests<EmulatedFutexAtomic, std::chrono::steady_clock>();
96 run_timed_wait_tmo_tests<DeterministicAtomic, std::chrono::steady_clock>();
99 // Timed wait regular steady clock tests
101 TEST(Baton, timed_wait_steady_clock) {
102 run_timed_wait_regular_test<std::atomic, std::chrono::steady_clock>();
103 run_timed_wait_regular_test<EmulatedFutexAtomic, std::chrono::steady_clock>();
104 run_timed_wait_regular_test<DeterministicAtomic, std::chrono::steady_clock>();
109 TEST(Baton, try_wait_blocking) {
110 run_try_wait_tests<true, std::atomic>();
111 run_try_wait_tests<true, EmulatedFutexAtomic>();
112 run_try_wait_tests<true, DeterministicAtomic>();
115 TEST(Baton, try_wait_nonblocking) {
116 run_try_wait_tests<false, std::atomic>();
117 run_try_wait_tests<false, EmulatedFutexAtomic>();
118 run_try_wait_tests<false, DeterministicAtomic>();