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