100.00% Lines (13/13) 100.00% Functions (7/7)
TLA Baseline Branch
Line Hits Code Line Hits Code
1   // 1   //
2 - // Copyright (c) 2026 Michael Vandeberg  
3   // Copyright (c) 2026 Steve Gerbino 2   // Copyright (c) 2026 Steve Gerbino
4   // 3   //
5   // Distributed under the Boost Software License, Version 1.0. (See accompanying 4   // Distributed under the Boost Software License, Version 1.0. (See accompanying
6   // 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)
7   // 6   //
8   // Official repository: https://github.com/cppalliance/corosio 7   // Official repository: https://github.com/cppalliance/corosio
9   // 8   //
10   9  
11   #ifndef BOOST_COROSIO_IO_IO_SIGNAL_SET_HPP 10   #ifndef BOOST_COROSIO_IO_IO_SIGNAL_SET_HPP
12   #define BOOST_COROSIO_IO_IO_SIGNAL_SET_HPP 11   #define BOOST_COROSIO_IO_IO_SIGNAL_SET_HPP
13   12  
14   #include <boost/corosio/detail/config.hpp> 13   #include <boost/corosio/detail/config.hpp>
15   #include <boost/corosio/detail/op_base.hpp> 14   #include <boost/corosio/detail/op_base.hpp>
16   #include <boost/corosio/io/io_object.hpp> 15   #include <boost/corosio/io/io_object.hpp>
17   #include <boost/capy/results.hpp> 16   #include <boost/capy/results.hpp>
18   #include <boost/capy/error.hpp> 17   #include <boost/capy/error.hpp>
19   #include <boost/capy/ex/executor_ref.hpp> 18   #include <boost/capy/ex/executor_ref.hpp>
20   #include <boost/capy/ex/await_context.hpp> 19   #include <boost/capy/ex/await_context.hpp>
21   20  
22   #include <boost/capy/continuation.hpp> 21   #include <boost/capy/continuation.hpp>
23   22  
24   #include <coroutine> 23   #include <coroutine>
25   #include <stop_token> 24   #include <stop_token>
26   #include <system_error> 25   #include <system_error>
27   26  
28   namespace boost::corosio { 27   namespace boost::corosio {
29   28  
30   /** Delivers a registered signal to the waiting coroutine. 29   /** Delivers a registered signal to the waiting coroutine.
31   30  
32   Provides the common signal set interface: `wait` and `cancel`. 31   Provides the common signal set interface: `wait` and `cancel`.
33   Concrete classes like @ref signal_set add signal registration 32   Concrete classes like @ref signal_set add signal registration
34   (add, remove, clear) and platform-specific flags. 33   (add, remove, clear) and platform-specific flags.
35   34  
36   @par Thread Safety 35   @par Thread Safety
37   Distinct objects: Safe. 36   Distinct objects: Safe.
38   Shared objects: Unsafe. 37   Shared objects: Unsafe.
39   38  
40 - @see @ref signal_set, @ref io_object 39 + @see signal_set, io_object
41   */ 40   */
42   class BOOST_COROSIO_DECL io_signal_set : public io_object 41   class BOOST_COROSIO_DECL io_signal_set : public io_object
43   { 42   {
44   struct wait_awaitable : detail::value_op_base<wait_awaitable, int> 43   struct wait_awaitable : detail::value_op_base<wait_awaitable, int>
45   { 44   {
46   private: 45   private:
47   friend io_signal_set; 46   friend io_signal_set;
48   friend detail::value_op_base<wait_awaitable, int>; 47   friend detail::value_op_base<wait_awaitable, int>;
49   48  
50   io_signal_set& s_; 49   io_signal_set& s_;
51   50  
HITCBC 52   206031 explicit wait_awaitable(io_signal_set& s) noexcept : s_(s) {} 51   205964 explicit wait_awaitable(io_signal_set& s) noexcept : s_(s) {}
53   52  
54   std::coroutine_handle<> 53   std::coroutine_handle<>
HITCBC 55   205987 dispatch(capy::continuation& cont, capy::executor_ref ex) const 54   205925 dispatch(capy::continuation& cont, capy::executor_ref ex) const
56   { 55   {
HITCBC 57   205987 return s_.get().wait(cont, ex, token_, &ec_, &value_); 56   205925 return s_.get().wait(cont, ex, token_, &ec_, &value_);
58   } 57   }
59   }; 58   };
60   59  
61   public: 60   public:
62   /** Define backend hooks for signal set wait and cancel. 61   /** Define backend hooks for signal set wait and cancel.
63   62  
64   Platform backends derive from this to implement 63   Platform backends derive from this to implement
65   signal delivery notification. 64   signal delivery notification.
66   */ 65   */
67   struct implementation : io_object::implementation 66   struct implementation : io_object::implementation
68   { 67   {
69   /** Initiate an asynchronous wait for a signal. 68   /** Initiate an asynchronous wait for a signal.
70   69  
71   @param cont Continuation to resume on completion; it lives in 70   @param cont Continuation to resume on completion; it lives in
72   the awaiting frame until then. 71   the awaiting frame until then.
73   @param ex Executor for dispatching the completion. 72   @param ex Executor for dispatching the completion.
74   @param token Stop token for cancellation. 73   @param token Stop token for cancellation.
75   @param ec Output error code. 74   @param ec Output error code.
76   @param signo Output signal number. 75   @param signo Output signal number.
77   76  
78   @return Coroutine handle to resume immediately. 77   @return Coroutine handle to resume immediately.
79   */ 78   */
80   virtual std::coroutine_handle<> wait( 79   virtual std::coroutine_handle<> wait(
81   capy::continuation& cont, 80   capy::continuation& cont,
82   capy::executor_ref ex, 81   capy::executor_ref ex,
83   std::stop_token token, 82   std::stop_token token,
84   std::error_code* ec, 83   std::error_code* ec,
85   int* signo) = 0; 84   int* signo) = 0;
86   85  
87   /** Cancel all pending wait operations. 86   /** Cancel all pending wait operations.
88   87  
89   Cancelled waiters complete with an error that 88   Cancelled waiters complete with an error that
90   compares equal to `capy::cond::canceled`. 89   compares equal to `capy::cond::canceled`.
91   */ 90   */
92   virtual void cancel() noexcept = 0; 91   virtual void cancel() noexcept = 0;
93   }; 92   };
94   93  
95   /** Cancel all operations associated with the signal set. 94   /** Cancel all operations associated with the signal set.
96   95  
97   Forces the completion of any pending asynchronous wait 96   Forces the completion of any pending asynchronous wait
98   operations. Each cancelled operation completes with an error 97   operations. Each cancelled operation completes with an error
99   code that compares equal to `capy::cond::canceled`. 98   code that compares equal to `capy::cond::canceled`.
100   99  
101   Cancellation does not alter the set of registered signals. 100   Cancellation does not alter the set of registered signals.
102   */ 101   */
HITCBC 103   200021 void cancel() noexcept 102   200021 void cancel() noexcept
104   { 103   {
HITCBC 105   200021 do_cancel(); 104   200021 do_cancel();
HITCBC 106   200021 } 105   200021 }
107   106  
108   /** Wait for a signal to be delivered. 107   /** Wait for a signal to be delivered.
109   108  
110   The operation supports cancellation via `std::stop_token` through 109   The operation supports cancellation via `std::stop_token` through
111   the affine awaitable protocol. If the associated stop token is 110   the affine awaitable protocol. If the associated stop token is
112   triggered, the operation completes immediately with an error 111   triggered, the operation completes immediately with an error
113   that compares equal to `capy::cond::canceled`. 112   that compares equal to `capy::cond::canceled`.
114   113  
115   This signal set must outlive the returned awaitable. 114   This signal set must outlive the returned awaitable.
116   115  
117   @return An awaitable that completes with `results<int>`. 116   @return An awaitable that completes with `results<int>`.
118   Returns the signal number when a signal is delivered, 117   Returns the signal number when a signal is delivered,
119   or an error code on failure. 118   or an error code on failure.
120   */ 119   */
HITCBC 121   206031 [[nodiscard]] auto wait() 120   205964 [[nodiscard]] auto wait()
122   { 121   {
HITCBC 123   206031 return wait_awaitable(*this); 122   205964 return wait_awaitable(*this);
124   } 123   }
125   124  
126   protected: 125   protected:
127   /** Dispatch cancel to the concrete implementation. */ 126   /** Dispatch cancel to the concrete implementation. */
128   virtual void do_cancel() noexcept = 0; 127   virtual void do_cancel() noexcept = 0;
129   128  
130   /** Adopt an existing handle. 129   /** Adopt an existing handle.
131   130  
132   @param h The handle the signal set takes ownership of. 131   @param h The handle the signal set takes ownership of.
133   */ 132   */
HITCBC 134   3482 explicit io_signal_set(handle h) noexcept : io_object(std::move(h)) {} 133   3478 explicit io_signal_set(handle h) noexcept : io_object(std::move(h)) {}
135   134  
136   /// Move construct. 135   /// Move construct.
HITCBC 137   6 io_signal_set(io_signal_set&& other) noexcept : io_object(std::move(other)) 136   4 io_signal_set(io_signal_set&& other) noexcept : io_object(std::move(other))
138   { 137   {
HITCBC 139   6 } 138   4 }
140   139  
141   /// Move assign. 140   /// Move assign.
142   io_signal_set& operator=(io_signal_set&& other) noexcept 141   io_signal_set& operator=(io_signal_set&& other) noexcept
143   { 142   {
144   if (this != &other) 143   if (this != &other)
145   h_ = std::move(other.h_); 144   h_ = std::move(other.h_);
146   return *this; 145   return *this;
147   } 146   }
148   147  
149   /// Copy construction is disabled; the handle is uniquely owned. 148   /// Copy construction is disabled; the handle is uniquely owned.
150   io_signal_set(io_signal_set const&) = delete; 149   io_signal_set(io_signal_set const&) = delete;
151   /// Copy assignment is disabled; the handle is uniquely owned. 150   /// Copy assignment is disabled; the handle is uniquely owned.
152   io_signal_set& operator=(io_signal_set const&) = delete; 151   io_signal_set& operator=(io_signal_set const&) = delete;
153   152  
154   private: 153   private:
HITCBC 155   205987 implementation& get() const noexcept 154   205925 implementation& get() const noexcept
156   { 155   {
HITCBC 157   205987 return *static_cast<implementation*>(h_.get()); 156   205925 return *static_cast<implementation*>(h_.get());
158   } 157   }
159   }; 158   };
160   159  
161   } // namespace boost::corosio 160   } // namespace boost::corosio
162   161  
163   #endif 162   #endif