include/boost/corosio/detail/conditionally_enabled_event.hpp

100.0% Lines (23/23) 100.0% List of functions (7/7) 62.5% Branches (5/8)
conditionally_enabled_event.hpp
f(x) Functions (7)
Line Branch TLA Hits Source Code
1 //
2 // Copyright (c) 2026 Michael Vandeberg
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/cppalliance/corosio
8 //
9
10 #ifndef BOOST_COROSIO_DETAIL_CONDITIONALLY_ENABLED_EVENT_HPP
11 #define BOOST_COROSIO_DETAIL_CONDITIONALLY_ENABLED_EVENT_HPP
12
13 #include <boost/corosio/detail/conditionally_enabled_mutex.hpp>
14
15 #include <chrono>
16 #include <condition_variable>
17
18 namespace boost::corosio::detail {
19
20 /* Condition variable wrapper that becomes a no-op when disabled.
21
22 When enabled, notify/wait delegate to an underlying
23 std::condition_variable. When disabled, all operations
24 are no-ops. The wait paths are unreachable in
25 single-threaded mode because the task sentinel prevents
26 the empty-queue state in do_one().
27 */
28 class conditionally_enabled_event
29 {
30 std::condition_variable cond_;
31 bool enabled_;
32
33 public:
34 4602x explicit conditionally_enabled_event(bool enabled = true) noexcept
35 2301x : enabled_(enabled)
36 2301x {
37 4602x }
38
39 conditionally_enabled_event(conditionally_enabled_event const&) = delete;
40 conditionally_enabled_event&
41 operator=(conditionally_enabled_event const&) = delete;
42
43 2291x void set_enabled(bool v) noexcept
44 {
45 2291x enabled_ = v;
46 2291x }
47
48 14x void notify_one()
49 {
50
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14x if (enabled_)
51 14x cond_.notify_one();
52 14x }
53
54 6253x void notify_all()
55 {
56
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6241 times.
6253x if (enabled_)
57 6241x cond_.notify_all();
58 6253x }
59
60 12x void wait(conditionally_enabled_mutex::scoped_lock& lock)
61 {
62
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12x if (enabled_)
63 12x cond_.wait(lock.underlying());
64 12x }
65
66 template<class Rep, class Period>
67 4x void wait_for(
68 conditionally_enabled_mutex::scoped_lock& lock,
69 std::chrono::duration<Rep, Period> const& d)
70 {
71
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4x if (enabled_)
72 4x cond_.wait_for(lock.underlying(), d);
73 4x }
74 };
75
76 } // namespace boost::corosio::detail
77
78 #endif // BOOST_COROSIO_DETAIL_CONDITIONALLY_ENABLED_EVENT_HPP
79