ion 0.12.0
Atomic concurrency support library
Loading...
Searching...
No Matches
barrier.hpp
1/**
2 * @file
3 * @copyright
4 * SPDX-FileCopyrightText: 2022-2024 European Southern Observatory (ESO)
5
6 * SPDX-License-Identifier: LGPL-3.0-only
7 */
8#ifndef ION_DETAIL_BARRIER_HPP
9#define ION_DETAIL_BARRIER_HPP
10
11#include <ion/detail/macros.hpp>
12
13namespace ion::detail {
14
15struct NoOp {};
16
17template <class CompletionFunction>
18class BarrierBase;
19template <>
20class BarrierBase<NoOp> {
21public:
22 BarrierBase(NoOp&& f) {
23 }
24};
25
26template <class CompletionFunction>
27class BarrierBase {
28public:
29 static_assert(std::is_nothrow_invocable_v<CompletionFunction&>, "");
30 BarrierBase(CompletionFunction&& f) : m_completion(std::forward<CompletionFunction>(f)) {
31 }
32
33protected:
34 ION_FORCE_INLINE inline void Complete() noexcept {
35 m_completion();
36 };
37
38private:
39 CompletionFunction m_completion;
40};
41
42template <class CompletionFunction>
43// NOLINTNEXTLINE(readability-identifier-naming)
44constexpr bool IsNoOp =
45 std::is_same_v<detail::NoOp, std::remove_cv_t<std::remove_reference_t<CompletionFunction>>>;
46
47template <class F>
48ION_FORCE_INLINE inline void Invoke(F&& f) {
49 if constexpr (!IsNoOp<F>) {
50 f();
51 }
52}
53
54} // namespace ion::detail
55#endif // ION_DETAIL_BARRIER_HPP