include/boost/corosio/detail/conditionally_enabled_event.hpp
52.2% Lines (12/23)
57.1% List of functions (4/7)
25.0% Branches (2/8)
Functions (7)
Function
Calls
Lines
Branches
Blocks
boost::corosio::detail::conditionally_enabled_event::~conditionally_enabled_event()
:28
1358x
100.0%
–
100.0%
boost::corosio::detail::conditionally_enabled_event::conditionally_enabled_event(bool)
:34
1358x
100.0%
–
100.0%
boost::corosio::detail::conditionally_enabled_event::set_enabled(bool)
:42
0
0.0%
–
0.0%
boost::corosio::detail::conditionally_enabled_event::notify_one()
:47
0
0.0%
0.0%
0.0%
boost::corosio::detail::conditionally_enabled_event::notify_all()
:53
3289x
100.0%
50.0%
100.0%
boost::corosio::detail::conditionally_enabled_event::wait(boost::corosio::detail::conditionally_enabled_mutex::scoped_lock&)
:59
3x
100.0%
50.0%
100.0%
void boost::corosio::detail::conditionally_enabled_event::wait_for<long long, std::__1::ratio<1l, 1000000l>>(boost::corosio::detail::conditionally_enabled_mutex::scoped_lock&, std::__1::chrono::duration<long long, std::__1::ratio<1l, 1000000l>> const&)
:66
0
0.0%
0.0%
0.0%
| 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 | 1358x | explicit conditionally_enabled_event(bool enabled = true) noexcept | ||
| 35 | 679x | : enabled_(enabled) | ||
| 36 | 679x | { | ||
| 37 | 1358x | } | ||
| 38 | ||||
| 39 | conditionally_enabled_event(conditionally_enabled_event const&) = delete; | |||
| 40 | conditionally_enabled_event& operator=(conditionally_enabled_event const&) = delete; | |||
| 41 | ||||
| 42 | ✗ | void set_enabled(bool v) noexcept | ||
| 43 | { | |||
| 44 | ✗ | enabled_ = v; | ||
| 45 | ✗ | } | ||
| 46 | ||||
| 47 | ✗ | void notify_one() | ||
| 48 | { | |||
| 49 | ✗ | if (enabled_) | ||
| 50 | ✗ | cond_.notify_one(); | ||
| 51 | ✗ | } | ||
| 52 | ||||
| 53 | 3289x | void notify_all() | ||
| 54 | { | |||
| 55 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3289 times.
|
3289x | if (enabled_) | |
| 56 | 3289x | cond_.notify_all(); | ||
| 57 | 3289x | } | ||
| 58 | ||||
| 59 | 3x | void wait(conditionally_enabled_mutex::scoped_lock& lock) | ||
| 60 | { | |||
| 61 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3x | if (enabled_) | |
| 62 | 3x | cond_.wait(lock.underlying()); | ||
| 63 | 3x | } | ||
| 64 | ||||
| 65 | template<class Rep, class Period> | |||
| 66 | ✗ | void wait_for( | ||
| 67 | conditionally_enabled_mutex::scoped_lock& lock, | |||
| 68 | std::chrono::duration<Rep, Period> const& d) | |||
| 69 | { | |||
| 70 | ✗ | if (enabled_) | ||
| 71 | ✗ | cond_.wait_for(lock.underlying(), d); | ||
| 72 | ✗ | } | ||
| 73 | }; | |||
| 74 | ||||
| 75 | } // namespace boost::corosio::detail | |||
| 76 | ||||
| 77 | #endif // BOOST_COROSIO_DETAIL_CONDITIONALLY_ENABLED_EVENT_HPP | |||
| 78 |