Use boost::compat::latch

This commit is contained in:
Peter Dimov
2023-06-26 10:32:52 +03:00
parent 01b2369700
commit 6bcf457b46
9 changed files with 15 additions and 120 deletions

View File

@ -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;

View File

@ -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 <boost/compat/latch.hpp>
#include <boost/container_hash/hash.hpp>
#include <boost/core/span.hpp>
#include <boost/unordered/unordered_flat_map.hpp>
@ -370,7 +369,7 @@ std::vector<boost::span<T> > split(
template <class T, class F> void thread_runner(std::vector<T>& values, F f)
{
boost::latch latch(static_cast<std::ptrdiff_t>(num_threads));
boost::compat::latch latch(static_cast<std::ptrdiff_t>(num_threads));
std::vector<std::thread> threads;
auto subslices = split<T>(values, num_threads);

View File

@ -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 <boost/compat/latch.hpp>
#include <boost/container_hash/hash.hpp>
#include <boost/core/span.hpp>
#include <boost/unordered/unordered_flat_map.hpp>
@ -339,7 +338,7 @@ std::vector<boost::span<T> > split(
template <class T, class F> void thread_runner(std::vector<T>& values, F f)
{
boost::latch latch(static_cast<std::ptrdiff_t>(num_threads));
boost::compat::latch latch(static_cast<std::ptrdiff_t>(num_threads));
std::vector<std::thread> threads;
auto subslices = split<T>(values, num_threads);

View File

@ -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 <boost/assert.hpp>
#include <climits>
#include <condition_variable>
#include <cstddef>
#include <mutex>
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<std::mutex> lk(m_);
count_down_and_notify(lk, n);
}
bool try_wait() const noexcept
{
std::unique_lock<std::mutex> lk(m_);
return is_ready();
}
void wait() const
{
std::unique_lock<std::mutex> lk(m_);
wait_impl(lk);
}
void arrive_and_wait(std::ptrdiff_t n = 1)
{
std::unique_lock<std::mutex> 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<std::mutex>& 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<std::mutex>& lk) const
{
cv_.wait(lk, [this] { return this->is_ready(); });
}
};
} // namespace boost
#endif // BOOST_UNORDERED_TEST_CFOA_LATCH_HPP

View File

@ -4,8 +4,7 @@
#define BOOST_ENABLE_ASSERT_HANDLER
#include "latch.hpp"
#include <boost/compat/latch.hpp>
#include <boost/core/lightweight_test.hpp>
#include <thread>
@ -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();

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -520,7 +520,7 @@ namespace {
X x;
std::thread t1, t2;
boost::latch l(2);
boost::compat::latch l(2);
std::vector<std::string> strs(values.size());
t1 = std::thread([&l, &values, &x, &strs] {