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   2118 explicit conditionally_enabled_event(bool enabled = true) noexcept 34   2107 explicit conditionally_enabled_event(bool enabled = true) noexcept
HITCBC 35   2118 : enabled_(enabled) 35   2107 : enabled_(enabled)
36   { 36   {
HITCBC 37   2118 } 37   2107 }
38   38  
39   conditionally_enabled_event(conditionally_enabled_event const&) = delete; 39   conditionally_enabled_event(conditionally_enabled_event const&) = delete;
40   conditionally_enabled_event& operator=(conditionally_enabled_event const&) = delete; 40   conditionally_enabled_event& operator=(conditionally_enabled_event const&) = delete;
41   41  
HITCBC 42   2106 void set_enabled(bool v) noexcept 42   2095 void set_enabled(bool v) noexcept
43   { 43   {
HITCBC 44   2106 enabled_ = v; 44   2095 enabled_ = v;
HITCBC 45   2106 } 45   2095 }
46   46  
HITCBC 47   68 void notify_one() 47   43 void notify_one()
48   { 48   {
HITCBC 49   68 if (enabled_) 49   43 if (enabled_)
HITCBC 50   68 cond_.notify_one(); 50   43 cond_.notify_one();
HITCBC 51   68 } 51   43 }
52   52  
HITCBC 53   3859 void notify_all() 53   3838 void notify_all()
54   { 54   {
HITCBC 55   3859 if (enabled_) 55   3838 if (enabled_)
HITCBC 56   3847 cond_.notify_all(); 56   3826 cond_.notify_all();
HITCBC 57   3859 } 57   3838 }
58   58  
HITCBC 59   10 void wait(conditionally_enabled_mutex::scoped_lock& lock) 59   8 void wait(conditionally_enabled_mutex::scoped_lock& lock)
60   { 60   {
HITCBC 61   10 if (enabled_) 61   8 if (enabled_)
HITCBC 62   10 cond_.wait(lock.underlying()); 62   8 cond_.wait(lock.underlying());
HITCBC 63   10 } 63   8 }
64   64  
65   template<class Rep, class Period> 65   template<class Rep, class Period>
HITCBC 66   49 void wait_for( 66   34 void wait_for(
67   conditionally_enabled_mutex::scoped_lock& lock, 67   conditionally_enabled_mutex::scoped_lock& lock,
68   std::chrono::duration<Rep, Period> const& d) 68   std::chrono::duration<Rep, Period> const& d)
69   { 69   {
HITCBC 70   49 if (enabled_) 70   34 if (enabled_)
HITCBC 71   49 cond_.wait_for(lock.underlying(), d); 71   34 cond_.wait_for(lock.underlying(), d);
HITCBC 72   49 } 72   34 }
73   }; 73   };
74   74  
75   } // namespace boost::corosio::detail 75   } // namespace boost::corosio::detail
76   76  
77   #endif // BOOST_COROSIO_DETAIL_CONDITIONALLY_ENABLED_EVENT_HPP 77   #endif // BOOST_COROSIO_DETAIL_CONDITIONALLY_ENABLED_EVENT_HPP