CoopSync for TBB 0.1.0
Cooperative synchronization primitives for TBB resumable tasks
Loading...
Searching...
No Matches
latch.hpp
1// SPDX-FileCopyrightText: 2026 CERN
2//
3// SPDX-License-Identifier: Apache-2.0
4
5#pragma once
6
7#include <atomic>
8#include <cstddef>
9#include <limits>
10
11#include "coopsync_tbb/detail/macros.hpp"
12#include "coopsync_tbb/detail/wait_queue.hpp"
13
14namespace coopsync_tbb {
20class COOPSYNC_TBB_EXPORT latch {
21 public:
26 explicit latch(std::ptrdiff_t expected);
27
29 latch(const latch&) = delete;
30
32 latch& operator=(const latch&) = delete;
33
35 latch(latch&&) = delete;
36
38 latch& operator=(latch&&) = delete;
39
45
48 static constexpr std::ptrdiff_t max() noexcept;
49
53 COOPSYNC_TBB_NODISCARD bool try_wait() const noexcept;
54
59 void count_down(std::ptrdiff_t update = 1);
60
65 void wait();
66
71 void arrive_and_wait(std::ptrdiff_t update = 1);
72
73 private:
74 std::atomic<std::ptrdiff_t> m_counter;
75 detail::wait_queue m_waiters;
76};
77
78inline constexpr std::ptrdiff_t latch::max() noexcept {
79 return std::numeric_limits<std::ptrdiff_t>::max();
80}
81
82} // namespace coopsync_tbb
latch(std::ptrdiff_t expected)
Constructs a latch with the specified initial count. The count must be non-negative.
void count_down(std::ptrdiff_t update=1)
Decrements the latch counter by the specified update. If the counter reaches zero,...
latch & operator=(const latch &)=delete
Latch is not copy-assignable.
latch & operator=(latch &&)=delete
Latch is not move-assignable.
void wait()
Suspends the calling task until the latch counter reaches zero. If the counter is already zero,...
bool try_wait() const noexcept
Checks if the latch has reached zero without suspending the calling task.
~latch()
Destroys the latch.
static constexpr std::ptrdiff_t max() noexcept
Returns the maximum value for the latch counter.
Definition latch.hpp:78
latch(const latch &)=delete
Latch is not copy-constructible.
latch(latch &&)=delete
Latch is not move-constructible.
void arrive_and_wait(std::ptrdiff_t update=1)
Decrements the latch counter by the specified update and suspends until the counter reaches zero....
Main namespace of the library, containing all public APIs.
Definition wait_for.hpp:11