123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- /*
- * Copyright 2017-present Facebook, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- #include <folly/synchronization/SaturatingSemaphore.h>
- #include <folly/portability/GTest.h>
- #include <folly/test/DeterministicSchedule.h>
- /// Test helper functions
- using folly::SaturatingSemaphore;
- using DSched = folly::test::DeterministicSchedule;
- template <bool MayBlock, template <typename> class Atom = std::atomic>
- void run_basic_test() {
- SaturatingSemaphore<MayBlock, Atom> f;
- ASSERT_FALSE(f.ready());
- ASSERT_FALSE(f.try_wait());
- ASSERT_FALSE(f.try_wait_until(
- std::chrono::steady_clock::now() + std::chrono::microseconds(1)));
- ASSERT_FALSE(f.try_wait_until(
- std::chrono::steady_clock::now() + std::chrono::microseconds(1),
- f.wait_options().spin_max(std::chrono::microseconds(1))));
- f.post();
- f.post();
- f.wait();
- f.wait(f.wait_options().spin_max(std::chrono::nanoseconds(100)));
- ASSERT_TRUE(f.ready());
- ASSERT_TRUE(f.try_wait());
- ASSERT_TRUE(f.try_wait_until(
- std::chrono::steady_clock::now() + std::chrono::microseconds(1)));
- f.wait();
- f.reset();
- ASSERT_FALSE(f.try_wait());
- }
- template <bool MayBlock, template <typename> class Atom = std::atomic>
- void run_pingpong_test(int numRounds) {
- using WF = SaturatingSemaphore<MayBlock, Atom>;
- std::array<WF, 17> flags;
- WF& a = flags[0];
- WF& b = flags[16]; // different cache line
- auto thr = DSched::thread([&] {
- for (int i = 0; i < numRounds; ++i) {
- a.try_wait();
- a.wait();
- a.reset();
- b.post();
- }
- });
- for (int i = 0; i < numRounds; ++i) {
- a.post();
- b.try_wait();
- b.wait();
- b.reset();
- }
- DSched::join(thr);
- }
- template <bool MayBlock, template <typename> class Atom = std::atomic>
- void run_multi_poster_multi_waiter_test(int np, int nw) {
- SaturatingSemaphore<MayBlock, Atom> f;
- std::atomic<int> posted{0};
- std::atomic<int> waited{0};
- std::atomic<bool> go_post{false};
- std::atomic<bool> go_wait{false};
- std::vector<std::thread> prod(np);
- std::vector<std::thread> cons(nw);
- for (int i = 0; i < np; ++i) {
- prod[i] = DSched::thread([&] {
- while (!go_post.load()) {
- /* spin */;
- }
- f.post();
- posted.fetch_add(1);
- });
- }
- for (int i = 0; i < nw; ++i) {
- cons[i] = DSched::thread([&] {
- ASSERT_FALSE(f.ready());
- ASSERT_FALSE(f.try_wait());
- ASSERT_FALSE(f.try_wait_for(std::chrono::microseconds(1)));
- ASSERT_FALSE(f.try_wait_until(
- std::chrono::steady_clock::now() + std::chrono::microseconds(1)));
- ASSERT_FALSE(f.try_wait_until(
- std::chrono::steady_clock::now() + std::chrono::microseconds(1),
- f.wait_options().spin_max(std::chrono::microseconds(0))));
- waited.fetch_add(1);
- while (!go_wait.load()) {
- /* spin */;
- }
- ASSERT_TRUE(f.ready());
- ASSERT_TRUE(f.try_wait());
- ASSERT_TRUE(f.try_wait_for(std::chrono::microseconds(1)));
- ASSERT_TRUE(f.try_wait_until(
- std::chrono::steady_clock::now() + std::chrono::microseconds(1)));
- ASSERT_TRUE(f.try_wait_until(
- std::chrono::steady_clock::now() + std::chrono::microseconds(1),
- f.wait_options().spin_max(std::chrono::microseconds(0))));
- f.wait();
- });
- }
- while (waited.load() < nw) {
- /* spin */;
- }
- go_post.store(true);
- while (posted.load() < np) {
- /* spin */;
- }
- go_wait.store(true);
- for (auto& t : prod) {
- DSched::join(t);
- }
- for (auto& t : cons) {
- DSched::join(t);
- }
- }
- /// Tests
- TEST(SaturatingSemaphore, basic_spin_only) {
- run_basic_test<false>();
- }
- TEST(SaturatingSemaphore, basic_may_block) {
- run_basic_test<true>();
- }
- TEST(SaturatingSemaphore, pingpong_spin_only) {
- run_pingpong_test<false>(1000);
- }
- TEST(SaturatingSemaphore, pingpong_may_block) {
- run_pingpong_test<true>(1000);
- }
- TEST(SaturatingSemaphore, multi_poster_multi_waiter_spin_only) {
- run_multi_poster_multi_waiter_test<false>(1, 1);
- run_multi_poster_multi_waiter_test<false>(1, 10);
- run_multi_poster_multi_waiter_test<false>(10, 1);
- run_multi_poster_multi_waiter_test<false>(10, 10);
- }
- TEST(SaturatingSemaphore, multi_poster_multi_waiter_may_block) {
- run_multi_poster_multi_waiter_test<true>(1, 1);
- run_multi_poster_multi_waiter_test<true>(1, 10);
- run_multi_poster_multi_waiter_test<true>(10, 1);
- run_multi_poster_multi_waiter_test<true>(10, 10);
- }
|