diff --git a/test/cfoa/assign_tests.cpp b/test/cfoa/assign_tests.cpp index aa17e0ac..24675d87 100644 --- a/test/cfoa/assign_tests.cpp +++ b/test/cfoa/assign_tests.cpp @@ -787,7 +787,7 @@ namespace { std::thread t1, t2, t3; - boost::latch start_latch(2), end_latch(2); + boost::compat::latch start_latch(2), end_latch(2); auto v1 = make_random_values(1024 * 16, [&] { return gen(rg); }); auto v2 = v1; diff --git a/test/cfoa/exception_helpers.hpp b/test/cfoa/exception_helpers.hpp index 691936f6..87528558 100644 --- a/test/cfoa/exception_helpers.hpp +++ b/test/cfoa/exception_helpers.hpp @@ -2,11 +2,10 @@ // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -#include "latch.hpp" - #include "../helpers/generators.hpp" #include "../helpers/test.hpp" +#include #include #include #include @@ -370,7 +369,7 @@ std::vector > split( template void thread_runner(std::vector& values, F f) { - boost::latch latch(static_cast(num_threads)); + boost::compat::latch latch(static_cast(num_threads)); std::vector threads; auto subslices = split(values, num_threads); diff --git a/test/cfoa/helpers.hpp b/test/cfoa/helpers.hpp index 517326bf..02ecfa05 100644 --- a/test/cfoa/helpers.hpp +++ b/test/cfoa/helpers.hpp @@ -5,11 +5,10 @@ #ifndef BOOST_UNORDERED_TEST_CFOA_HELPERS_HPP #define BOOST_UNORDERED_TEST_CFOA_HELPERS_HPP -#include "latch.hpp" - #include "../helpers/generators.hpp" #include "../helpers/test.hpp" +#include #include #include #include @@ -339,7 +338,7 @@ std::vector > split( template void thread_runner(std::vector& values, F f) { - boost::latch latch(static_cast(num_threads)); + boost::compat::latch latch(static_cast(num_threads)); std::vector threads; auto subslices = split(values, num_threads); diff --git a/test/cfoa/latch.hpp b/test/cfoa/latch.hpp deleted file mode 100644 index bee42119..00000000 --- a/test/cfoa/latch.hpp +++ /dev/null @@ -1,87 +0,0 @@ -// Copyright (C) 2023 Christian Mazakas -// Distributed under the Boost Software License, Version 1.0. (See accompanying -// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - -#ifndef BOOST_UNORDERED_TEST_CFOA_LATCH_HPP -#define BOOST_UNORDERED_TEST_CFOA_LATCH_HPP - -#include - -#include -#include -#include -#include - -namespace boost { - class latch - { - private: - std::ptrdiff_t n_; - mutable std::mutex m_; - mutable std::condition_variable cv_; - - public: - explicit latch(std::ptrdiff_t expected) : n_{expected}, m_{}, cv_{} - { - BOOST_ASSERT(n_ >= 0); - BOOST_ASSERT(n_ <= max()); - } - - latch(latch const&) = delete; - latch& operator=(latch const&) = delete; - - ~latch() = default; - - void count_down(std::ptrdiff_t n = 1) - { - std::unique_lock lk(m_); - count_down_and_notify(lk, n); - } - - bool try_wait() const noexcept - { - std::unique_lock lk(m_); - return is_ready(); - } - - void wait() const - { - std::unique_lock lk(m_); - wait_impl(lk); - } - - void arrive_and_wait(std::ptrdiff_t n = 1) - { - std::unique_lock lk(m_); - bool should_wait = count_down_and_notify(lk, n); - if (should_wait) { - wait_impl(lk); - } - } - - static constexpr std::ptrdiff_t max() noexcept { return INT_MAX; } - - private: - bool is_ready() const { return n_ == 0; } - - bool count_down_and_notify( - std::unique_lock& lk, std::ptrdiff_t n) - { - n_ -= n; - if (n_ == 0) { - lk.unlock(); - cv_.notify_all(); - return false; - } - - return true; - } - - void wait_impl(std::unique_lock& lk) const - { - cv_.wait(lk, [this] { return this->is_ready(); }); - } - }; -} // namespace boost - -#endif // BOOST_UNORDERED_TEST_CFOA_LATCH_HPP diff --git a/test/cfoa/latch_tests.cpp b/test/cfoa/latch_tests.cpp index 0c7e9ece..1ce9d65f 100644 --- a/test/cfoa/latch_tests.cpp +++ b/test/cfoa/latch_tests.cpp @@ -4,8 +4,7 @@ #define BOOST_ENABLE_ASSERT_HANDLER -#include "latch.hpp" - +#include #include #include @@ -28,13 +27,11 @@ namespace boost { } // namespace boost namespace { - void test_max() { BOOST_TEST_EQ(boost::latch::max(), INT_MAX); } - void test_constructor() { { auto const f = [] { - boost::latch l(-1); + boost::compat::latch l(-1); (void)l; }; BOOST_TEST_THROWS(f(), exception); @@ -43,38 +40,26 @@ namespace { { std::ptrdiff_t n = 0; - boost::latch l(n); + boost::compat::latch l(n); BOOST_TEST(l.try_wait()); } { std::ptrdiff_t n = 16; - boost::latch l(n); + boost::compat::latch l(n); BOOST_TEST_NOT(l.try_wait()); l.count_down(16); BOOST_TEST(l.try_wait()); } - -#if PTRDIFF_MAX > INT_MAX - { - auto const f = [] { - std::ptrdiff_t n = INT_MAX; - n += 10; - boost::latch l(n); - (void)l; - }; - BOOST_TEST_THROWS(f(), exception); - } -#endif } void test_count_down_and_wait() { constexpr std::ptrdiff_t n = 1024; - boost::latch l(2 * n); + boost::compat::latch l(2 * n); bool bs[] = {false, false}; @@ -116,7 +101,7 @@ namespace { { std::ptrdiff_t const n = 16; - boost::latch l(2 * n); + boost::compat::latch l(2 * n); int xs[n] = {0}; @@ -146,7 +131,6 @@ namespace { int main() { - test_max(); test_constructor(); test_count_down_and_wait(); test_arrive_and_wait(); diff --git a/test/cfoa/merge_tests.cpp b/test/cfoa/merge_tests.cpp index 92770ecb..60d35065 100644 --- a/test/cfoa/merge_tests.cpp +++ b/test/cfoa/merge_tests.cpp @@ -103,7 +103,7 @@ namespace { map2_type x2(2 * vals1.size(), allocator_type(3)); std::thread t1, t2, t3; - boost::latch l(2); + boost::compat::latch l(2); std::mutex m; std::condition_variable cv; diff --git a/test/cfoa/rehash_tests.cpp b/test/cfoa/rehash_tests.cpp index 1a3092aa..51810f54 100644 --- a/test/cfoa/rehash_tests.cpp +++ b/test/cfoa/rehash_tests.cpp @@ -79,7 +79,7 @@ namespace { map_type x(0, hasher(1), key_equal(2), allocator_type(3)); std::thread t1, t2, t3; - boost::latch l(2); + boost::compat::latch l(2); std::mutex m; std::condition_variable cv; diff --git a/test/cfoa/swap_tests.cpp b/test/cfoa/swap_tests.cpp index 3ab29791..264dc460 100644 --- a/test/cfoa/swap_tests.cpp +++ b/test/cfoa/swap_tests.cpp @@ -190,7 +190,7 @@ namespace { map_type x2(vals2.size(), hasher(2), key_equal(1), allocator_type(3)); std::thread t1, t2, t3; - boost::latch l(2); + boost::compat::latch l(2); std::mutex m; std::condition_variable cv; diff --git a/test/cfoa/visit_tests.cpp b/test/cfoa/visit_tests.cpp index fae4deae..fff8f69d 100644 --- a/test/cfoa/visit_tests.cpp +++ b/test/cfoa/visit_tests.cpp @@ -520,7 +520,7 @@ namespace { X x; std::thread t1, t2; - boost::latch l(2); + boost::compat::latch l(2); std::vector strs(values.size()); t1 = std::thread([&l, &values, &x, &strs] {