1  
//
1  
//
2  
// Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
2  
// Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
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_SIGNAL_SET_HPP
10  
#ifndef BOOST_COROSIO_SIGNAL_SET_HPP
11  
#define BOOST_COROSIO_SIGNAL_SET_HPP
11  
#define BOOST_COROSIO_SIGNAL_SET_HPP
12  

12  

13 -
#include <boost/corosio/detail/except.hpp>
 
14  
#include <boost/corosio/detail/config.hpp>
13  
#include <boost/corosio/detail/config.hpp>
15  
#include <boost/corosio/io_object.hpp>
14  
#include <boost/corosio/io_object.hpp>
16  
#include <boost/capy/io_result.hpp>
15  
#include <boost/capy/io_result.hpp>
17  
#include <boost/capy/error.hpp>
16  
#include <boost/capy/error.hpp>
18  
#include <boost/capy/ex/executor_ref.hpp>
17  
#include <boost/capy/ex/executor_ref.hpp>
19  
#include <boost/capy/ex/execution_context.hpp>
18  
#include <boost/capy/ex/execution_context.hpp>
20  
#include <boost/capy/ex/io_env.hpp>
19  
#include <boost/capy/ex/io_env.hpp>
21  
#include <boost/capy/concept/executor.hpp>
20  
#include <boost/capy/concept/executor.hpp>
22  
#include <system_error>
21  
#include <system_error>
23  

22  

24  
#include <concepts>
23  
#include <concepts>
25  
#include <coroutine>
24  
#include <coroutine>
26  
#include <stop_token>
25  
#include <stop_token>
27  
#include <system_error>
26  
#include <system_error>
28  

27  

29  
/*
28  
/*
30  
    Signal Set Public API
29  
    Signal Set Public API
31  
    =====================
30  
    =====================
32  

31  

33  
    This header provides the public interface for asynchronous signal handling.
32  
    This header provides the public interface for asynchronous signal handling.
34  
    The implementation is split across platform-specific files:
33  
    The implementation is split across platform-specific files:
35  
      - posix/signals.cpp: Uses sigaction() for robust signal handling
34  
      - posix/signals.cpp: Uses sigaction() for robust signal handling
36  
      - iocp/signals.cpp: Uses C runtime signal() (Windows lacks sigaction)
35  
      - iocp/signals.cpp: Uses C runtime signal() (Windows lacks sigaction)
37  

36  

38  
    Key design decisions:
37  
    Key design decisions:
39  

38  

40  
    1. Abstract flag values: The flags_t enum uses arbitrary bit positions
39  
    1. Abstract flag values: The flags_t enum uses arbitrary bit positions
41  
       (not SA_RESTART, etc.) to avoid including <signal.h> in public headers.
40  
       (not SA_RESTART, etc.) to avoid including <signal.h> in public headers.
42  
       The POSIX implementation maps these to actual SA_* constants internally.
41  
       The POSIX implementation maps these to actual SA_* constants internally.
43  

42  

44  
    2. Flag conflict detection: When multiple signal_sets register for the
43  
    2. Flag conflict detection: When multiple signal_sets register for the
45  
       same signal, they must use compatible flags. The first registration
44  
       same signal, they must use compatible flags. The first registration
46  
       establishes the flags; subsequent registrations must match or use
45  
       establishes the flags; subsequent registrations must match or use
47  
       dont_care.
46  
       dont_care.
48  

47  

49  
    3. Polymorphic implementation: implementation is an abstract base that
48  
    3. Polymorphic implementation: implementation is an abstract base that
50  
       platform-specific implementations (posix_signal_impl, win_signal_impl)
49  
       platform-specific implementations (posix_signal_impl, win_signal_impl)
51  
       derive from. This allows the public API to be platform-agnostic.
50  
       derive from. This allows the public API to be platform-agnostic.
52  

51  

53  
    4. The inline add(int) overload avoids a virtual call for the common case
52  
    4. The inline add(int) overload avoids a virtual call for the common case
54  
       of adding signals without flags (delegates to add(int, none)).
53  
       of adding signals without flags (delegates to add(int, none)).
55  
*/
54  
*/
56  

55  

57  
namespace boost::corosio {
56  
namespace boost::corosio {
58  

57  

59  
/** An asynchronous signal set for coroutine I/O.
58  
/** An asynchronous signal set for coroutine I/O.
60  

59  

61  
    This class provides the ability to perform an asynchronous wait
60  
    This class provides the ability to perform an asynchronous wait
62  
    for one or more signals to occur. The signal set registers for
61  
    for one or more signals to occur. The signal set registers for
63  
    signals using sigaction() on POSIX systems or the C runtime
62  
    signals using sigaction() on POSIX systems or the C runtime
64  
    signal() function on Windows.
63  
    signal() function on Windows.
65  

64  

66  
    @par Thread Safety
65  
    @par Thread Safety
67  
    Distinct objects: Safe.@n
66  
    Distinct objects: Safe.@n
68  
    Shared objects: Unsafe. A signal_set must not have concurrent
67  
    Shared objects: Unsafe. A signal_set must not have concurrent
69  
    wait operations.
68  
    wait operations.
70  

69  

71  
    @par Semantics
70  
    @par Semantics
72  
    Wraps platform signal handling (sigaction on POSIX, C runtime
71  
    Wraps platform signal handling (sigaction on POSIX, C runtime
73  
    signal() on Windows). Operations dispatch to OS signal APIs
72  
    signal() on Windows). Operations dispatch to OS signal APIs
74  
    via the io_context reactor.
73  
    via the io_context reactor.
75  

74  

76  
    @par Supported Signals
75  
    @par Supported Signals
77  
    On Windows, the following signals are supported:
76  
    On Windows, the following signals are supported:
78  
    SIGINT, SIGTERM, SIGABRT, SIGFPE, SIGILL, SIGSEGV.
77  
    SIGINT, SIGTERM, SIGABRT, SIGFPE, SIGILL, SIGSEGV.
79  

78  

80  
    @par Example
79  
    @par Example
81  
    @code
80  
    @code
82  
    signal_set signals(ctx, SIGINT, SIGTERM);
81  
    signal_set signals(ctx, SIGINT, SIGTERM);
83  
    auto [ec, signum] = co_await signals.wait();
82  
    auto [ec, signum] = co_await signals.wait();
84  
    if (ec == capy::cond::canceled)
83  
    if (ec == capy::cond::canceled)
85  
    {
84  
    {
86  
        // Operation was cancelled via stop_token or cancel()
85  
        // Operation was cancelled via stop_token or cancel()
87  
    }
86  
    }
88  
    else if (!ec)
87  
    else if (!ec)
89  
    {
88  
    {
90  
        std::cout << "Received signal " << signum << std::endl;
89  
        std::cout << "Received signal " << signum << std::endl;
91  
    }
90  
    }
92  
    @endcode
91  
    @endcode
93  
*/
92  
*/
94  
class BOOST_COROSIO_DECL signal_set : public io_object
93  
class BOOST_COROSIO_DECL signal_set : public io_object
95  
{
94  
{
96  
public:
95  
public:
97  
    /** Flags for signal registration.
96  
    /** Flags for signal registration.
98  

97  

99  
        These flags control the behavior of signal handling. Multiple
98  
        These flags control the behavior of signal handling. Multiple
100  
        flags can be combined using the bitwise OR operator.
99  
        flags can be combined using the bitwise OR operator.
101  

100  

102  
        @note Flags only have effect on POSIX systems. On Windows,
101  
        @note Flags only have effect on POSIX systems. On Windows,
103  
        only `none` and `dont_care` are supported; other flags return
102  
        only `none` and `dont_care` are supported; other flags return
104  
        `operation_not_supported`.
103  
        `operation_not_supported`.
105  
    */
104  
    */
106  
    enum flags_t : unsigned
105  
    enum flags_t : unsigned
107  
    {
106  
    {
108  
        /// Use existing flags if signal is already registered.
107  
        /// Use existing flags if signal is already registered.
109  
        /// When adding a signal that's already registered by another
108  
        /// When adding a signal that's already registered by another
110  
        /// signal_set, this flag indicates acceptance of whatever
109  
        /// signal_set, this flag indicates acceptance of whatever
111  
        /// flags were used for the existing registration.
110  
        /// flags were used for the existing registration.
112  
        dont_care = 1u << 16,
111  
        dont_care = 1u << 16,
113  

112  

114  
        /// No special flags.
113  
        /// No special flags.
115  
        none = 0,
114  
        none = 0,
116  

115  

117  
        /// Restart interrupted system calls.
116  
        /// Restart interrupted system calls.
118  
        /// Equivalent to SA_RESTART on POSIX systems.
117  
        /// Equivalent to SA_RESTART on POSIX systems.
119  
        restart = 1u << 0,
118  
        restart = 1u << 0,
120  

119  

121  
        /// Don't generate SIGCHLD when children stop.
120  
        /// Don't generate SIGCHLD when children stop.
122  
        /// Equivalent to SA_NOCLDSTOP on POSIX systems.
121  
        /// Equivalent to SA_NOCLDSTOP on POSIX systems.
123  
        no_child_stop = 1u << 1,
122  
        no_child_stop = 1u << 1,
124  

123  

125  
        /// Don't create zombie processes on child termination.
124  
        /// Don't create zombie processes on child termination.
126  
        /// Equivalent to SA_NOCLDWAIT on POSIX systems.
125  
        /// Equivalent to SA_NOCLDWAIT on POSIX systems.
127  
        no_child_wait = 1u << 2,
126  
        no_child_wait = 1u << 2,
128  

127  

129  
        /// Don't block the signal while its handler runs.
128  
        /// Don't block the signal while its handler runs.
130  
        /// Equivalent to SA_NODEFER on POSIX systems.
129  
        /// Equivalent to SA_NODEFER on POSIX systems.
131  
        no_defer = 1u << 3,
130  
        no_defer = 1u << 3,
132  

131  

133  
        /// Reset handler to SIG_DFL after one invocation.
132  
        /// Reset handler to SIG_DFL after one invocation.
134  
        /// Equivalent to SA_RESETHAND on POSIX systems.
133  
        /// Equivalent to SA_RESETHAND on POSIX systems.
135  
        reset_handler = 1u << 4
134  
        reset_handler = 1u << 4
136  
    };
135  
    };
137  

136  

138  
    /// Combine two flag values.
137  
    /// Combine two flag values.
139  
    friend constexpr flags_t operator|(flags_t a, flags_t b) noexcept
138  
    friend constexpr flags_t operator|(flags_t a, flags_t b) noexcept
140  
    {
139  
    {
141  
        return static_cast<flags_t>(
140  
        return static_cast<flags_t>(
142  
            static_cast<unsigned>(a) | static_cast<unsigned>(b));
141  
            static_cast<unsigned>(a) | static_cast<unsigned>(b));
143  
    }
142  
    }
144  

143  

145  
    /// Mask two flag values.
144  
    /// Mask two flag values.
146  
    friend constexpr flags_t operator&(flags_t a, flags_t b) noexcept
145  
    friend constexpr flags_t operator&(flags_t a, flags_t b) noexcept
147  
    {
146  
    {
148  
        return static_cast<flags_t>(
147  
        return static_cast<flags_t>(
149  
            static_cast<unsigned>(a) & static_cast<unsigned>(b));
148  
            static_cast<unsigned>(a) & static_cast<unsigned>(b));
150  
    }
149  
    }
151  

150  

152  
    /// Compound assignment OR.
151  
    /// Compound assignment OR.
153  
    friend constexpr flags_t& operator|=(flags_t& a, flags_t b) noexcept
152  
    friend constexpr flags_t& operator|=(flags_t& a, flags_t b) noexcept
154  
    {
153  
    {
155  
        return a = a | b;
154  
        return a = a | b;
156  
    }
155  
    }
157  

156  

158  
    /// Compound assignment AND.
157  
    /// Compound assignment AND.
159  
    friend constexpr flags_t& operator&=(flags_t& a, flags_t b) noexcept
158  
    friend constexpr flags_t& operator&=(flags_t& a, flags_t b) noexcept
160  
    {
159  
    {
161  
        return a = a & b;
160  
        return a = a & b;
162  
    }
161  
    }
163  

162  

164  
    /// Bitwise NOT (complement).
163  
    /// Bitwise NOT (complement).
165  
    friend constexpr flags_t operator~(flags_t a) noexcept
164  
    friend constexpr flags_t operator~(flags_t a) noexcept
166  
    {
165  
    {
167  
        return static_cast<flags_t>(~static_cast<unsigned>(a));
166  
        return static_cast<flags_t>(~static_cast<unsigned>(a));
168  
    }
167  
    }
169  

168  

170  
private:
169  
private:
171  
    struct wait_awaitable
170  
    struct wait_awaitable
172  
    {
171  
    {
173  
        signal_set& s_;
172  
        signal_set& s_;
174  
        std::stop_token token_;
173  
        std::stop_token token_;
175  
        mutable std::error_code ec_;
174  
        mutable std::error_code ec_;
176  
        mutable int signal_number_ = 0;
175  
        mutable int signal_number_ = 0;
177  

176  

178  
        explicit wait_awaitable(signal_set& s) noexcept : s_(s) {}
177  
        explicit wait_awaitable(signal_set& s) noexcept : s_(s) {}
179  

178  

180  
        bool await_ready() const noexcept
179  
        bool await_ready() const noexcept
181  
        {
180  
        {
182  
            return token_.stop_requested();
181  
            return token_.stop_requested();
183  
        }
182  
        }
184  

183  

185  
        capy::io_result<int> await_resume() const noexcept
184  
        capy::io_result<int> await_resume() const noexcept
186  
        {
185  
        {
187  
            if (token_.stop_requested())
186  
            if (token_.stop_requested())
188  
                return {capy::error::canceled};
187  
                return {capy::error::canceled};
189  
            return {ec_, signal_number_};
188  
            return {ec_, signal_number_};
190  
        }
189  
        }
191  

190  

192 -
        auto await_suspend(
191 +
        auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
193 -
            std::coroutine_handle<> h,
192 +
            -> std::coroutine_handle<>
194 -
            capy::io_env const* env) -> std::coroutine_handle<>
 
195  
        {
193  
        {
196  
            token_ = env->stop_token;
194  
            token_ = env->stop_token;
197 -
            return s_.get().wait(h, env->executor, token_, &ec_, &signal_number_);
195 +
            return s_.get().wait(
 
196 +
                h, env->executor, token_, &ec_, &signal_number_);
198  
        }
197  
        }
199  
    };
198  
    };
200  

199  

201  
public:
200  
public:
202  
    struct implementation : io_object::implementation
201  
    struct implementation : io_object::implementation
203  
    {
202  
    {
204  
        virtual std::coroutine_handle<> wait(
203  
        virtual std::coroutine_handle<> wait(
205  
            std::coroutine_handle<>,
204  
            std::coroutine_handle<>,
206  
            capy::executor_ref,
205  
            capy::executor_ref,
207  
            std::stop_token,
206  
            std::stop_token,
208  
            std::error_code*,
207  
            std::error_code*,
209  
            int*) = 0;
208  
            int*) = 0;
210  

209  

211  
        virtual std::error_code add(int signal_number, flags_t flags) = 0;
210  
        virtual std::error_code add(int signal_number, flags_t flags) = 0;
212  
        virtual std::error_code remove(int signal_number) = 0;
211  
        virtual std::error_code remove(int signal_number) = 0;
213  
        virtual std::error_code clear() = 0;
212  
        virtual std::error_code clear() = 0;
214  
        virtual void cancel() = 0;
213  
        virtual void cancel() = 0;
215  
    };
214  
    };
216  

215  

217  
    /** Destructor.
216  
    /** Destructor.
218  

217  

219  
        Cancels any pending operations and releases signal resources.
218  
        Cancels any pending operations and releases signal resources.
220  
    */
219  
    */
221 -
    ~signal_set();
220 +
    ~signal_set() override;
222  

221  

223  
    /** Construct an empty signal set.
222  
    /** Construct an empty signal set.
224  

223  

225  
        @param ctx The execution context that will own this signal set.
224  
        @param ctx The execution context that will own this signal set.
226  
    */
225  
    */
227  
    explicit signal_set(capy::execution_context& ctx);
226  
    explicit signal_set(capy::execution_context& ctx);
228  

227  

229  
    /** Construct a signal set with initial signals.
228  
    /** Construct a signal set with initial signals.
230  

229  

231  
        @param ctx The execution context that will own this signal set.
230  
        @param ctx The execution context that will own this signal set.
232  
        @param signal First signal number to add.
231  
        @param signal First signal number to add.
233  
        @param signals Additional signal numbers to add.
232  
        @param signals Additional signal numbers to add.
234  

233  

235  
        @throws std::system_error Thrown on failure.
234  
        @throws std::system_error Thrown on failure.
236  
    */
235  
    */
237  
    template<std::convertible_to<int>... Signals>
236  
    template<std::convertible_to<int>... Signals>
238 -
    signal_set(
237 +
    signal_set(capy::execution_context& ctx, int signal, Signals... signals)
239 -
        capy::execution_context& ctx,
 
240 -
        int signal,
 
241 -
        Signals... signals)
 
242  
        : signal_set(ctx)
238  
        : signal_set(ctx)
243  
    {
239  
    {
244  
        auto check = [](std::error_code ec) {
240  
        auto check = [](std::error_code ec) {
245 -
            if( ec )
241 +
            if (ec)
246  
                throw std::system_error(ec);
242  
                throw std::system_error(ec);
247  
        };
243  
        };
248  
        check(add(signal));
244  
        check(add(signal));
249  
        (check(add(signals)), ...);
245  
        (check(add(signals)), ...);
250  
    }
246  
    }
251  

247  

252  
    /** Move constructor.
248  
    /** Move constructor.
253  

249  

254  
        Transfers ownership of the signal set resources.
250  
        Transfers ownership of the signal set resources.
255  

251  

256  
        @param other The signal set to move from.
252  
        @param other The signal set to move from.
257  
    */
253  
    */
258  
    signal_set(signal_set&& other) noexcept;
254  
    signal_set(signal_set&& other) noexcept;
259  

255  

260  
    /** Move assignment operator.
256  
    /** Move assignment operator.
261  

257  

262 -
        The source and destination must share the same execution context.
 
263 -

 
264  
        Closes any existing signal set and transfers ownership.
258  
        Closes any existing signal set and transfers ownership.
265  
        @param other The signal set to move from.
259  
        @param other The signal set to move from.
266  

260  

267 -

 
268 -
        @throws std::logic_error if the signal sets have different
 
269 -
            execution contexts.
 
270  
        @return Reference to this signal set.
261  
        @return Reference to this signal set.
271  
    */
262  
    */
272 -
    signal_set& operator=(signal_set&& other);
263 +
    signal_set& operator=(signal_set&& other) noexcept;
273  

264  

274  
    signal_set(signal_set const&) = delete;
265  
    signal_set(signal_set const&) = delete;
275  
    signal_set& operator=(signal_set const&) = delete;
266  
    signal_set& operator=(signal_set const&) = delete;
276  

267  

277  
    /** Add a signal to the signal set.
268  
    /** Add a signal to the signal set.
278  

269  

279  
        This function adds the specified signal to the set with the
270  
        This function adds the specified signal to the set with the
280  
        specified flags. It has no effect if the signal is already
271  
        specified flags. It has no effect if the signal is already
281  
        in the set with the same flags.
272  
        in the set with the same flags.
282  

273  

283  
        If the signal is already registered globally (by another
274  
        If the signal is already registered globally (by another
284  
        signal_set) and the flags differ, an error is returned
275  
        signal_set) and the flags differ, an error is returned
285  
        unless one of them has the `dont_care` flag.
276  
        unless one of them has the `dont_care` flag.
286  

277  

287  
        @param signal_number The signal to be added to the set.
278  
        @param signal_number The signal to be added to the set.
288  
        @param flags The flags to apply when registering the signal.
279  
        @param flags The flags to apply when registering the signal.
289  
            On POSIX systems, these map to sigaction() flags.
280  
            On POSIX systems, these map to sigaction() flags.
290  
            On Windows, flags are accepted but ignored.
281  
            On Windows, flags are accepted but ignored.
291  

282  

292  
        @return Success, or an error if the signal could not be added.
283  
        @return Success, or an error if the signal could not be added.
293  
            Returns `errc::invalid_argument` if the signal is already
284  
            Returns `errc::invalid_argument` if the signal is already
294  
            registered with different flags.
285  
            registered with different flags.
295  
    */
286  
    */
296  
    std::error_code add(int signal_number, flags_t flags);
287  
    std::error_code add(int signal_number, flags_t flags);
297  

288  

298  
    /** Add a signal to the signal set with default flags.
289  
    /** Add a signal to the signal set with default flags.
299  

290  

300  
        This is equivalent to calling `add(signal_number, none)`.
291  
        This is equivalent to calling `add(signal_number, none)`.
301  

292  

302  
        @param signal_number The signal to be added to the set.
293  
        @param signal_number The signal to be added to the set.
303  

294  

304  
        @return Success, or an error if the signal could not be added.
295  
        @return Success, or an error if the signal could not be added.
305  
    */
296  
    */
306  
    std::error_code add(int signal_number)
297  
    std::error_code add(int signal_number)
307  
    {
298  
    {
308  
        return add(signal_number, none);
299  
        return add(signal_number, none);
309  
    }
300  
    }
310  

301  

311  
    /** Remove a signal from the signal set.
302  
    /** Remove a signal from the signal set.
312  

303  

313  
        This function removes the specified signal from the set. It has
304  
        This function removes the specified signal from the set. It has
314  
        no effect if the signal is not in the set.
305  
        no effect if the signal is not in the set.
315  

306  

316  
        @param signal_number The signal to be removed from the set.
307  
        @param signal_number The signal to be removed from the set.
317  

308  

318  
        @return Success, or an error if the signal could not be removed.
309  
        @return Success, or an error if the signal could not be removed.
319  
    */
310  
    */
320  
    std::error_code remove(int signal_number);
311  
    std::error_code remove(int signal_number);
321  

312  

322  
    /** Remove all signals from the signal set.
313  
    /** Remove all signals from the signal set.
323  

314  

324  
        This function removes all signals from the set. It has no effect
315  
        This function removes all signals from the set. It has no effect
325  
        if the set is already empty.
316  
        if the set is already empty.
326  

317  

327  
        @return Success, or an error if resetting any signal handler fails.
318  
        @return Success, or an error if resetting any signal handler fails.
328  
    */
319  
    */
329  
    std::error_code clear();
320  
    std::error_code clear();
330  

321  

331  
    /** Cancel all operations associated with the signal set.
322  
    /** Cancel all operations associated with the signal set.
332  

323  

333  
        This function forces the completion of any pending asynchronous
324  
        This function forces the completion of any pending asynchronous
334  
        wait operations against the signal set. The handler for each
325  
        wait operations against the signal set. The handler for each
335  
        cancelled operation will be invoked with an error code that
326  
        cancelled operation will be invoked with an error code that
336  
        compares equal to `capy::cond::canceled`.
327  
        compares equal to `capy::cond::canceled`.
337  

328  

338  
        Cancellation does not alter the set of registered signals.
329  
        Cancellation does not alter the set of registered signals.
339  
    */
330  
    */
340  
    void cancel();
331  
    void cancel();
341  

332  

342  
    /** Wait for a signal to be delivered.
333  
    /** Wait for a signal to be delivered.
343  

334  

344  
        The operation supports cancellation via `std::stop_token` through
335  
        The operation supports cancellation via `std::stop_token` through
345  
        the affine awaitable protocol. If the associated stop token is
336  
        the affine awaitable protocol. If the associated stop token is
346  
        triggered, the operation completes immediately with an error
337  
        triggered, the operation completes immediately with an error
347  
        that compares equal to `capy::cond::canceled`.
338  
        that compares equal to `capy::cond::canceled`.
348  

339  

349  
        @par Example
340  
        @par Example
350  
        @code
341  
        @code
351  
        signal_set signals(ctx, SIGINT);
342  
        signal_set signals(ctx, SIGINT);
352  
        auto [ec, signum] = co_await signals.wait();
343  
        auto [ec, signum] = co_await signals.wait();
353  
        if (ec == capy::cond::canceled)
344  
        if (ec == capy::cond::canceled)
354  
        {
345  
        {
355  
            // Cancelled via stop_token or cancel()
346  
            // Cancelled via stop_token or cancel()
356  
            co_return;
347  
            co_return;
357  
        }
348  
        }
358  
        if (ec)
349  
        if (ec)
359  
        {
350  
        {
360  
            // Handle other errors
351  
            // Handle other errors
361  
            co_return;
352  
            co_return;
362  
        }
353  
        }
363  
        // Process signal
354  
        // Process signal
364  
        std::cout << "Received signal " << signum << std::endl;
355  
        std::cout << "Received signal " << signum << std::endl;
365  
        @endcode
356  
        @endcode
366  

357  

367  
        @return An awaitable that completes with `io_result<int>`.
358  
        @return An awaitable that completes with `io_result<int>`.
368  
            Returns the signal number when a signal is delivered,
359  
            Returns the signal number when a signal is delivered,
369  
            or an error code on failure. Compare against error conditions
360  
            or an error code on failure. Compare against error conditions
370  
            (e.g., `ec == capy::cond::canceled`) rather than error codes.
361  
            (e.g., `ec == capy::cond::canceled`) rather than error codes.
371  
    */
362  
    */
372  
    auto wait()
363  
    auto wait()
373  
    {
364  
    {
374  
        return wait_awaitable(*this);
365  
        return wait_awaitable(*this);
375  
    }
366  
    }
376  

367  

377  
private:
368  
private:
378  
    implementation& get() const noexcept
369  
    implementation& get() const noexcept
379  
    {
370  
    {
380  
        return *static_cast<implementation*>(h_.get());
371  
        return *static_cast<implementation*>(h_.get());
381  
    }
372  
    }
382  
};
373  
};
383  

374  

384  
} // namespace boost::corosio
375  
} // namespace boost::corosio
385  

376  

386  
#endif
377  
#endif