89.68% Lines (304/339)
96.77% Functions (30/31)
| TLA | Baseline | Branch | ||||||
|---|---|---|---|---|---|---|---|---|
| Line | Hits | Code | Line | Hits | Code | |||
| 1 | // | 1 | // | |||||
| 2 | // Copyright (c) 2026 Steve Gerbino | 2 | // Copyright (c) 2026 Steve Gerbino | |||||
| 3 | // Copyright (c) 2026 Michael Vandeberg | 3 | // Copyright (c) 2026 Michael Vandeberg | |||||
| 4 | // | 4 | // | |||||
| 5 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | 5 | // 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) | 6 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |||||
| 7 | // | 7 | // | |||||
| 8 | // Official repository: https://github.com/cppalliance/corosio | 8 | // Official repository: https://github.com/cppalliance/corosio | |||||
| 9 | // | 9 | // | |||||
| 10 | 10 | |||||||
| 11 | #ifndef BOOST_COROSIO_NATIVE_DETAIL_POSIX_POSIX_SIGNAL_SERVICE_HPP | 11 | #ifndef BOOST_COROSIO_NATIVE_DETAIL_POSIX_POSIX_SIGNAL_SERVICE_HPP | |||||
| 12 | #define BOOST_COROSIO_NATIVE_DETAIL_POSIX_POSIX_SIGNAL_SERVICE_HPP | 12 | #define BOOST_COROSIO_NATIVE_DETAIL_POSIX_POSIX_SIGNAL_SERVICE_HPP | |||||
| 13 | 13 | |||||||
| 14 | #include <boost/corosio/detail/platform.hpp> | 14 | #include <boost/corosio/detail/platform.hpp> | |||||
| 15 | 15 | |||||||
| 16 | #if BOOST_COROSIO_POSIX | 16 | #if BOOST_COROSIO_POSIX | |||||
| 17 | 17 | |||||||
| 18 | #include <boost/corosio/native/detail/posix/posix_signal.hpp> | 18 | #include <boost/corosio/native/detail/posix/posix_signal.hpp> | |||||
| 19 | 19 | |||||||
| 20 | #include <boost/corosio/detail/config.hpp> | 20 | #include <boost/corosio/detail/config.hpp> | |||||
| 21 | #include <boost/capy/ex/execution_context.hpp> | 21 | #include <boost/capy/ex/execution_context.hpp> | |||||
| 22 | #include <boost/corosio/detail/scheduler.hpp> | 22 | #include <boost/corosio/detail/scheduler.hpp> | |||||
| 23 | #include <boost/capy/error.hpp> | 23 | #include <boost/capy/error.hpp> | |||||
| 24 | 24 | |||||||
| 25 | #include <mutex> | 25 | #include <mutex> | |||||
| 26 | 26 | |||||||
| 27 | #include <errno.h> | 27 | #include <errno.h> | |||||
| 28 | #include <fcntl.h> | 28 | #include <fcntl.h> | |||||
| 29 | #include <signal.h> | 29 | #include <signal.h> | |||||
| 30 | #include <unistd.h> | 30 | #include <unistd.h> | |||||
| 31 | 31 | |||||||
| 32 | /* | 32 | /* | |||||
| 33 | POSIX Signal Service | 33 | POSIX Signal Service | |||||
| 34 | ==================== | 34 | ==================== | |||||
| 35 | 35 | |||||||
| 36 | Concrete signal service implementation for POSIX backends. Manages signal | 36 | Concrete signal service implementation for POSIX backends. Manages signal | |||||
| 37 | registrations via sigaction() and dispatches completions through the | 37 | registrations via sigaction() and dispatches completions through the | |||||
| 38 | scheduler. One instance per execution_context, created by | 38 | scheduler. One instance per execution_context, created by | |||||
| 39 | get_signal_service(). | 39 | get_signal_service(). | |||||
| 40 | 40 | |||||||
| 41 | See the block comment further down for the full architecture overview. | 41 | See the block comment further down for the full architecture overview. | |||||
| 42 | */ | 42 | */ | |||||
| 43 | 43 | |||||||
| 44 | /* | 44 | /* | |||||
| 45 | POSIX Signal Implementation | 45 | POSIX Signal Implementation | |||||
| 46 | =========================== | 46 | =========================== | |||||
| 47 | 47 | |||||||
| 48 | This file implements signal handling for POSIX systems using sigaction(). | 48 | This file implements signal handling for POSIX systems using sigaction(). | |||||
| 49 | The implementation supports signal flags (SA_RESTART, etc.) and integrates | 49 | The implementation supports signal flags (SA_RESTART, etc.) and integrates | |||||
| 50 | with any POSIX-compatible scheduler via the abstract scheduler interface. | 50 | with any POSIX-compatible scheduler via the abstract scheduler interface. | |||||
| 51 | 51 | |||||||
| 52 | Architecture Overview | 52 | Architecture Overview | |||||
| 53 | --------------------- | 53 | --------------------- | |||||
| 54 | 54 | |||||||
| 55 | Three layers manage signal registrations: | 55 | Three layers manage signal registrations: | |||||
| 56 | 56 | |||||||
| 57 | 1. signal_state (global singleton) | 57 | 1. signal_state (global singleton) | |||||
| 58 | - Tracks the global service list and per-signal registration counts | 58 | - Tracks the global service list and per-signal registration counts | |||||
| 59 | - Stores the flags used for first registration of each signal (for | 59 | - Stores the flags used for first registration of each signal (for | |||||
| 60 | conflict detection when multiple signal_sets register same signal) | 60 | conflict detection when multiple signal_sets register same signal) | |||||
| 61 | - Owns the mutex that protects signal handler installation/removal | 61 | - Owns the mutex that protects signal handler installation/removal | |||||
| 62 | 62 | |||||||
| 63 | 2. posix_signal_service (one per execution_context) | 63 | 2. posix_signal_service (one per execution_context) | |||||
| 64 | - Maintains registrations_[] table indexed by signal number | 64 | - Maintains registrations_[] table indexed by signal number | |||||
| 65 | - Each slot is a doubly-linked list of signal_registrations for that signal | 65 | - Each slot is a doubly-linked list of signal_registrations for that signal | |||||
| 66 | - Also maintains impl_list_ of all posix_signal objects it owns | 66 | - Also maintains impl_list_ of all posix_signal objects it owns | |||||
| 67 | 67 | |||||||
| 68 | 3. posix_signal (one per signal_set) | 68 | 3. posix_signal (one per signal_set) | |||||
| 69 | - Owns a singly-linked list (sorted by signal number) of signal_registrations | 69 | - Owns a singly-linked list (sorted by signal number) of signal_registrations | |||||
| 70 | - Contains the pending_op_ used for wait operations | 70 | - Contains the pending_op_ used for wait operations | |||||
| 71 | 71 | |||||||
| 72 | Signal Delivery Flow | 72 | Signal Delivery Flow | |||||
| 73 | -------------------- | 73 | -------------------- | |||||
| 74 | 74 | |||||||
| 75 | Delivery uses the self-pipe trick so the signal handler itself performs | 75 | Delivery uses the self-pipe trick so the signal handler itself performs | |||||
| 76 | only async-signal-safe work (mirrors Boost.Asio): | 76 | only async-signal-safe work (mirrors Boost.Asio): | |||||
| 77 | 77 | |||||||
| 78 | 1. Signal arrives -> corosio_posix_signal_handler(). The handler only | 78 | 1. Signal arrives -> corosio_posix_signal_handler(). The handler only | |||||
| 79 | write()s the signal number to the global self-pipe (write_fd) and | 79 | write()s the signal number to the global self-pipe (write_fd) and | |||||
| 80 | restores errno. No locks, no allocation, no scheduler dispatch. | 80 | restores errno. No locks, no allocation, no scheduler dispatch. | |||||
| 81 | 81 | |||||||
| 82 | 2. The read end of the pipe is watched by one backend's event loop | 82 | 2. The read end of the pipe is watched by one backend's event loop | |||||
| 83 | (registered via scheduler::register_signal_reader on the first | 83 | (registered via scheduler::register_signal_reader on the first | |||||
| 84 | registration). When it becomes readable the backend drains it | 84 | registration). When it becomes readable the backend drains it | |||||
| 85 | (drain_signal_pipe) and calls deliver_signal() in normal context. | 85 | (drain_signal_pipe) and calls deliver_signal() in normal context. | |||||
| 86 | 86 | |||||||
| 87 | 3. deliver_signal() iterates all posix_signal_service services: | 87 | 3. deliver_signal() iterates all posix_signal_service services: | |||||
| 88 | - If a signal_set is waiting (impl->waiting_ == true), post the signal_op | 88 | - If a signal_set is waiting (impl->waiting_ == true), post the signal_op | |||||
| 89 | to the scheduler for immediate completion | 89 | to the scheduler for immediate completion | |||||
| 90 | - Otherwise, increment reg->undelivered to queue the signal | 90 | - Otherwise, increment reg->undelivered to queue the signal | |||||
| 91 | 91 | |||||||
| 92 | 4. When wait() is called via start_wait(): | 92 | 4. When wait() is called via start_wait(): | |||||
| 93 | - First check for queued signals (undelivered > 0); if found, post | 93 | - First check for queued signals (undelivered > 0); if found, post | |||||
| 94 | immediate completion without blocking | 94 | immediate completion without blocking | |||||
| 95 | - Otherwise, set waiting_ = true and call work_started() to keep | 95 | - Otherwise, set waiting_ = true and call work_started() to keep | |||||
| 96 | the io_context alive | 96 | the io_context alive | |||||
| 97 | 97 | |||||||
| 98 | Locking Protocol | 98 | Locking Protocol | |||||
| 99 | ---------------- | 99 | ---------------- | |||||
| 100 | 100 | |||||||
| 101 | Two mutex levels exist (MUST acquire in this order to avoid deadlock): | 101 | Two mutex levels exist (MUST acquire in this order to avoid deadlock): | |||||
| 102 | 1. signal_state::mutex - protects handler registration and service list | 102 | 1. signal_state::mutex - protects handler registration and service list | |||||
| 103 | 2. posix_signal_service::mutex_ - protects per-service registration tables | 103 | 2. posix_signal_service::mutex_ - protects per-service registration tables | |||||
| 104 | 104 | |||||||
| 105 | Async-Signal-Safety | 105 | Async-Signal-Safety | |||||
| 106 | ------------------- | 106 | ------------------- | |||||
| 107 | 107 | |||||||
| 108 | The C signal handler (corosio_posix_signal_handler) performs only | 108 | The C signal handler (corosio_posix_signal_handler) performs only | |||||
| 109 | async-signal-safe operations: it reads the single global write_fd and | 109 | async-signal-safe operations: it reads the single global write_fd and | |||||
| 110 | calls write(), saving/restoring errno. It never locks a mutex, allocates | 110 | calls write(), saving/restoring errno. It never locks a mutex, allocates | |||||
| 111 | memory, or dispatches through the scheduler. All of that happens in | 111 | memory, or dispatches through the scheduler. All of that happens in | |||||
| 112 | deliver_signal(), which runs in normal thread context from the backend | 112 | deliver_signal(), which runs in normal thread context from the backend | |||||
| 113 | event loop after draining the self-pipe. There is therefore no | 113 | event loop after draining the self-pipe. There is therefore no | |||||
| 114 | self-deadlock risk if a signal arrives while a thread holds state->mutex | 114 | self-deadlock risk if a signal arrives while a thread holds state->mutex | |||||
| 115 | or service->mutex_. | 115 | or service->mutex_. | |||||
| 116 | 116 | |||||||
| 117 | Flag Handling | 117 | Flag Handling | |||||
| 118 | ------------- | 118 | ------------- | |||||
| 119 | 119 | |||||||
| 120 | - Flags are abstract values in the public API (signal_set::flags_t) | 120 | - Flags are abstract values in the public API (signal_set::flags_t) | |||||
| 121 | - flags_supported() validates that requested flags are available on | 121 | - flags_supported() validates that requested flags are available on | |||||
| 122 | this platform; returns false if SA_NOCLDWAIT is unavailable and | 122 | this platform; returns false if SA_NOCLDWAIT is unavailable and | |||||
| 123 | no_child_wait is requested | 123 | no_child_wait is requested | |||||
| 124 | - to_sigaction_flags() maps validated flags to actual SA_* constants | 124 | - to_sigaction_flags() maps validated flags to actual SA_* constants | |||||
| 125 | - First registration of a signal establishes the flags; subsequent | 125 | - First registration of a signal establishes the flags; subsequent | |||||
| 126 | registrations must be compatible (same flags or dont_care) | 126 | registrations must be compatible (same flags or dont_care) | |||||
| 127 | - Requesting unavailable flags returns operation_not_supported | 127 | - Requesting unavailable flags returns operation_not_supported | |||||
| 128 | 128 | |||||||
| 129 | Work Tracking | 129 | Work Tracking | |||||
| 130 | ------------- | 130 | ------------- | |||||
| 131 | 131 | |||||||
| 132 | When waiting for a signal: | 132 | When waiting for a signal: | |||||
| 133 | - start_wait() calls sched_->work_started() to prevent io_context::run() | 133 | - start_wait() calls sched_->work_started() to prevent io_context::run() | |||||
| 134 | from returning while we wait | 134 | from returning while we wait | |||||
| 135 | - signal_op::svc is set to point to the service | 135 | - signal_op::svc is set to point to the service | |||||
| 136 | - signal_op::operator()() calls work_finished() after resuming the coroutine | 136 | - signal_op::operator()() calls work_finished() after resuming the coroutine | |||||
| 137 | 137 | |||||||
| 138 | If a signal was already queued (undelivered > 0), no work tracking is needed | 138 | If a signal was already queued (undelivered > 0), no work tracking is needed | |||||
| 139 | because completion is posted immediately. | 139 | because completion is posted immediately. | |||||
| 140 | */ | 140 | */ | |||||
| 141 | 141 | |||||||
| 142 | namespace boost::corosio { | 142 | namespace boost::corosio { | |||||
| 143 | 143 | |||||||
| 144 | namespace detail { | 144 | namespace detail { | |||||
| 145 | 145 | |||||||
| 146 | /** Signal service for POSIX backends. | 146 | /** Signal service for POSIX backends. | |||||
| 147 | 147 | |||||||
| 148 | Manages signal registrations via sigaction() and dispatches signal | 148 | Manages signal registrations via sigaction() and dispatches signal | |||||
| 149 | completions through the scheduler. One instance per execution_context. | 149 | completions through the scheduler. One instance per execution_context. | |||||
| 150 | */ | 150 | */ | |||||
| 151 | class BOOST_COROSIO_DECL posix_signal_service final | 151 | class BOOST_COROSIO_DECL posix_signal_service final | |||||
| 152 | : public capy::execution_context::service | 152 | : public capy::execution_context::service | |||||
| 153 | , public io_object::io_service | 153 | , public io_object::io_service | |||||
| 154 | { | 154 | { | |||||
| 155 | public: | 155 | public: | |||||
| 156 | using key_type = posix_signal_service; | 156 | using key_type = posix_signal_service; | |||||
| 157 | 157 | |||||||
| 158 | posix_signal_service(capy::execution_context& ctx, scheduler& sched); | 158 | posix_signal_service(capy::execution_context& ctx, scheduler& sched); | |||||
| 159 | ~posix_signal_service() override; | 159 | ~posix_signal_service() override; | |||||
| 160 | 160 | |||||||
| 161 | posix_signal_service(posix_signal_service const&) = delete; | 161 | posix_signal_service(posix_signal_service const&) = delete; | |||||
| 162 | posix_signal_service& operator=(posix_signal_service const&) = delete; | 162 | posix_signal_service& operator=(posix_signal_service const&) = delete; | |||||
| 163 | 163 | |||||||
| 164 | io_object::implementation* construct() override; | 164 | io_object::implementation* construct() override; | |||||
| 165 | 165 | |||||||
| HITCBC | 166 | 125 | void destroy(io_object::implementation* p) override | 166 | 125 | void destroy(io_object::implementation* p) override | ||
| 167 | { | 167 | { | |||||
| HITCBC | 168 | 125 | auto& impl = static_cast<posix_signal&>(*p); | 168 | 125 | auto& impl = static_cast<posix_signal&>(*p); | ||
| HITCBC | 169 | 125 | [[maybe_unused]] auto n = impl.clear(); | 169 | 125 | [[maybe_unused]] auto n = impl.clear(); | ||
| HITCBC | 170 | 125 | impl.cancel(); | 170 | 125 | impl.cancel(); | ||
| HITCBC | 171 | 125 | destroy_impl(impl); | 171 | 125 | destroy_impl(impl); | ||
| HITCBC | 172 | 125 | } | 172 | 125 | } | ||
| 173 | 173 | |||||||
| 174 | void shutdown() override; | 174 | void shutdown() override; | |||||
| 175 | 175 | |||||||
| 176 | void destroy_impl(posix_signal& impl); | 176 | void destroy_impl(posix_signal& impl); | |||||
| 177 | 177 | |||||||
| 178 | std::error_code add_signal( | 178 | std::error_code add_signal( | |||||
| 179 | posix_signal& impl, int signal_number, signal_set::flags_t flags); | 179 | posix_signal& impl, int signal_number, signal_set::flags_t flags); | |||||
| 180 | 180 | |||||||
| 181 | std::error_code remove_signal(posix_signal& impl, int signal_number); | 181 | std::error_code remove_signal(posix_signal& impl, int signal_number); | |||||
| 182 | 182 | |||||||
| 183 | std::error_code clear_signals(posix_signal& impl); | 183 | std::error_code clear_signals(posix_signal& impl); | |||||
| 184 | 184 | |||||||
| 185 | void cancel_wait(posix_signal& impl); | 185 | void cancel_wait(posix_signal& impl); | |||||
| 186 | void start_wait(posix_signal& impl, signal_op* op); | 186 | void start_wait(posix_signal& impl, signal_op* op); | |||||
| 187 | 187 | |||||||
| 188 | static void deliver_signal(int signal_number); | 188 | static void deliver_signal(int signal_number); | |||||
| 189 | 189 | |||||||
| 190 | void work_started() noexcept; | 190 | void work_started() noexcept; | |||||
| 191 | void work_finished() noexcept; | 191 | void work_finished() noexcept; | |||||
| 192 | void post(signal_op* op); | 192 | void post(signal_op* op); | |||||
| 193 | 193 | |||||||
| 194 | private: | 194 | private: | |||||
| 195 | static void add_service(posix_signal_service* service); | 195 | static void add_service(posix_signal_service* service); | |||||
| 196 | static void remove_service(posix_signal_service* service); | 196 | static void remove_service(posix_signal_service* service); | |||||
| 197 | 197 | |||||||
| 198 | scheduler* sched_; | 198 | scheduler* sched_; | |||||
| 199 | std::mutex mutex_; | 199 | std::mutex mutex_; | |||||
| 200 | 200 | |||||||
| 201 | // Registers the signal self-pipe's read end with sched_ exactly once per | 201 | // Registers the signal self-pipe's read end with sched_ exactly once per | |||||
| 202 | // service, so every io_context that waits on a signal can drain the pipe. | 202 | // service, so every io_context that waits on a signal can drain the pipe. | |||||
| 203 | // A once_flag (not a bool under mutex_) because registration must run | 203 | // A once_flag (not a bool under mutex_) because registration must run | |||||
| 204 | // without holding mutex_ or the signal-state mutex — see add_signal. | 204 | // without holding mutex_ or the signal-state mutex — see add_signal. | |||||
| 205 | - | std::once_flag reader_once_; | 205 | + | std::mutex reader_mutex_; | |||
| 206 | + | bool reader_registered_ = false; | ||||||
| 206 | 207 | |||||||
| 207 | intrusive_list<posix_signal> impl_list_; | 208 | intrusive_list<posix_signal> impl_list_; | |||||
| 208 | 209 | |||||||
| 209 | // Per-signal registration table | 210 | // Per-signal registration table | |||||
| 210 | signal_registration* registrations_[max_signal_number]; | 211 | signal_registration* registrations_[max_signal_number]; | |||||
| 211 | 212 | |||||||
| 212 | // Registration counts for each signal | 213 | // Registration counts for each signal | |||||
| 213 | std::size_t registration_count_[max_signal_number]; | 214 | std::size_t registration_count_[max_signal_number]; | |||||
| 214 | 215 | |||||||
| 215 | // Linked list of all posix_signal_service services for signal delivery | 216 | // Linked list of all posix_signal_service services for signal delivery | |||||
| 216 | posix_signal_service* next_ = nullptr; | 217 | posix_signal_service* next_ = nullptr; | |||||
| 217 | posix_signal_service* prev_ = nullptr; | 218 | posix_signal_service* prev_ = nullptr; | |||||
| 218 | }; | 219 | }; | |||||
| 219 | 220 | |||||||
| 220 | /** Get or create the signal service for the given context. | 221 | /** Get or create the signal service for the given context. | |||||
| 221 | 222 | |||||||
| 222 | This function is called by the concrete scheduler during initialization | 223 | This function is called by the concrete scheduler during initialization | |||||
| 223 | to create the signal service with a reference to itself. | 224 | to create the signal service with a reference to itself. | |||||
| 224 | 225 | |||||||
| 225 | @param ctx Reference to the owning execution_context. | 226 | @param ctx Reference to the owning execution_context. | |||||
| 226 | @param sched Reference to the scheduler for posting completions. | 227 | @param sched Reference to the scheduler for posting completions. | |||||
| 227 | @return Reference to the signal service. | 228 | @return Reference to the signal service. | |||||
| 228 | */ | 229 | */ | |||||
| 229 | posix_signal_service& | 230 | posix_signal_service& | |||||
| 230 | get_signal_service(capy::execution_context& ctx, scheduler& sched); | 231 | get_signal_service(capy::execution_context& ctx, scheduler& sched); | |||||
| 231 | 232 | |||||||
| 232 | } // namespace detail | 233 | } // namespace detail | |||||
| 233 | 234 | |||||||
| 234 | } // namespace boost::corosio | 235 | } // namespace boost::corosio | |||||
| 235 | 236 | |||||||
| 236 | // --------------------------------------------------------------------------- | 237 | // --------------------------------------------------------------------------- | |||||
| 237 | // Inline implementation | 238 | // Inline implementation | |||||
| 238 | // --------------------------------------------------------------------------- | 239 | // --------------------------------------------------------------------------- | |||||
| 239 | 240 | |||||||
| 240 | namespace boost::corosio { | 241 | namespace boost::corosio { | |||||
| 241 | 242 | |||||||
| 242 | namespace detail { | 243 | namespace detail { | |||||
| 243 | 244 | |||||||
| 244 | namespace posix_signal_detail { | 245 | namespace posix_signal_detail { | |||||
| 245 | 246 | |||||||
| 246 | struct signal_state | 247 | struct signal_state | |||||
| 247 | { | 248 | { | |||||
| 248 | std::mutex mutex; | 249 | std::mutex mutex; | |||||
| 249 | posix_signal_service* service_list = nullptr; | 250 | posix_signal_service* service_list = nullptr; | |||||
| 250 | std::size_t registration_count[max_signal_number] = {}; | 251 | std::size_t registration_count[max_signal_number] = {}; | |||||
| 251 | signal_set::flags_t registered_flags[max_signal_number] = {}; | 252 | signal_set::flags_t registered_flags[max_signal_number] = {}; | |||||
| 252 | 253 | |||||||
| 253 | // Self-pipe used to defer signal delivery out of handler context. | 254 | // Self-pipe used to defer signal delivery out of handler context. | |||||
| 254 | // The C handler writes the signal number to write_fd (async-signal- | 255 | // The C handler writes the signal number to write_fd (async-signal- | |||||
| 255 | // safe); a backend event loop drains read_fd and calls deliver_signal() | 256 | // safe); a backend event loop drains read_fd and calls deliver_signal() | |||||
| 256 | // in normal context. Created once (on the first signal registration) and | 257 | // in normal context. Created once (on the first signal registration) and | |||||
| 257 | // kept for the process lifetime. Each posix_signal_service registers the | 258 | // kept for the process lifetime. Each posix_signal_service registers the | |||||
| 258 | // read end with its own scheduler (see reader_once_) so every running | 259 | // read end with its own scheduler (see reader_once_) so every running | |||||
| 259 | // io_context can drain it; multiple readers on one pipe are safe because | 260 | // io_context can drain it; multiple readers on one pipe are safe because | |||||
| 260 | // each signal is a fixed sizeof(int) record read atomically. | 261 | // each signal is a fixed sizeof(int) record read atomically. | |||||
| 261 | int read_fd = -1; | 262 | int read_fd = -1; | |||||
| 262 | int write_fd = -1; | 263 | int write_fd = -1; | |||||
| 263 | }; | 264 | }; | |||||
| 264 | 265 | |||||||
| 265 | BOOST_COROSIO_DECL signal_state* get_signal_state(); | 266 | BOOST_COROSIO_DECL signal_state* get_signal_state(); | |||||
| 266 | 267 | |||||||
| 267 | // Check if requested flags are supported on this platform. | 268 | // Check if requested flags are supported on this platform. | |||||
| 268 | // Returns true if all flags are supported, false otherwise. | 269 | // Returns true if all flags are supported, false otherwise. | |||||
| 269 | inline bool | 270 | inline bool | |||||
| HITCBC | 270 | 138 | flags_supported([[maybe_unused]] signal_set::flags_t flags) | 271 | 138 | flags_supported([[maybe_unused]] signal_set::flags_t flags) | ||
| 271 | { | 272 | { | |||||
| 272 | #ifndef SA_NOCLDWAIT | 273 | #ifndef SA_NOCLDWAIT | |||||
| 273 | if (flags & signal_set::no_child_wait) | 274 | if (flags & signal_set::no_child_wait) | |||||
| 274 | return false; | 275 | return false; | |||||
| 275 | #endif | 276 | #endif | |||||
| HITCBC | 276 | 138 | return true; | 277 | 138 | return true; | ||
| 277 | } | 278 | } | |||||
| 278 | 279 | |||||||
| 279 | // Map abstract flags to sigaction() flags. | 280 | // Map abstract flags to sigaction() flags. | |||||
| 280 | // Caller must ensure flags_supported() returns true first. | 281 | // Caller must ensure flags_supported() returns true first. | |||||
| 281 | inline int | 282 | inline int | |||||
| HITCBC | 282 | 112 | to_sigaction_flags(signal_set::flags_t flags) | 283 | 113 | to_sigaction_flags(signal_set::flags_t flags) | ||
| 283 | { | 284 | { | |||||
| HITCBC | 284 | 112 | int sa_flags = 0; | 285 | 113 | int sa_flags = 0; | ||
| HITCBC | 285 | 112 | if (flags & signal_set::restart) | 286 | 113 | if (flags & signal_set::restart) | ||
| HITCBC | 286 | 20 | sa_flags |= SA_RESTART; | 287 | 21 | sa_flags |= SA_RESTART; | ||
| HITCBC | 287 | 112 | if (flags & signal_set::no_child_stop) | 288 | 113 | if (flags & signal_set::no_child_stop) | ||
| HITGBC | 288 | ✗ | sa_flags |= SA_NOCLDSTOP; | 289 | 1 | sa_flags |= SA_NOCLDSTOP; | ||
| 289 | #ifdef SA_NOCLDWAIT | 290 | #ifdef SA_NOCLDWAIT | |||||
| HITCBC | 290 | 112 | if (flags & signal_set::no_child_wait) | 291 | 113 | if (flags & signal_set::no_child_wait) | ||
| MISUBC | 291 | ✗ | sa_flags |= SA_NOCLDWAIT; | 292 | ✗ | sa_flags |= SA_NOCLDWAIT; | ||
| 292 | #endif | 293 | #endif | |||||
| HITCBC | 293 | 112 | if (flags & signal_set::no_defer) | 294 | 113 | if (flags & signal_set::no_defer) | ||
| HITCBC | 294 | 2 | sa_flags |= SA_NODEFER; | 295 | 2 | sa_flags |= SA_NODEFER; | ||
| HITCBC | 295 | 112 | if (flags & signal_set::reset_handler) | 296 | 113 | if (flags & signal_set::reset_handler) | ||
| MISUBC | 296 | ✗ | sa_flags |= SA_RESETHAND; | 297 | ✗ | sa_flags |= SA_RESETHAND; | ||
| HITCBC | 297 | 112 | return sa_flags; | 298 | 113 | return sa_flags; | ||
| 298 | } | 299 | } | |||||
| 299 | 300 | |||||||
| 300 | // Check if two flag values are compatible | 301 | // Check if two flag values are compatible | |||||
| 301 | inline bool | 302 | inline bool | |||||
| HITCBC | 302 | 26 | flags_compatible(signal_set::flags_t existing, signal_set::flags_t requested) | 303 | 25 | flags_compatible(signal_set::flags_t existing, signal_set::flags_t requested) | ||
| 303 | { | 304 | { | |||||
| 304 | // dont_care is always compatible | 305 | // dont_care is always compatible | |||||
| HITCBC | 305 | 50 | if ((existing & signal_set::dont_care) || | 306 | 48 | if ((existing & signal_set::dont_care) || | ||
| HITCBC | 306 | 24 | (requested & signal_set::dont_care)) | 307 | 23 | (requested & signal_set::dont_care)) | ||
| HITCBC | 307 | 7 | return true; | 308 | 7 | return true; | ||
| 308 | 309 | |||||||
| 309 | // Mask out dont_care bit for comparison | 310 | // Mask out dont_care bit for comparison | |||||
| HITCBC | 310 | 19 | constexpr auto mask = ~signal_set::dont_care; | 311 | 18 | constexpr auto mask = ~signal_set::dont_care; | ||
| HITCBC | 311 | 19 | return (existing & mask) == (requested & mask); | 312 | 18 | return (existing & mask) == (requested & mask); | ||
| 312 | } | 313 | } | |||||
| 313 | 314 | |||||||
| 314 | // Lazily create the global signal self-pipe. Idempotent; call under | 315 | // Lazily create the global signal self-pipe. Idempotent; call under | |||||
| 315 | // state->mutex before installing the first signal handler so write_fd is | 316 | // state->mutex before installing the first signal handler so write_fd is | |||||
| 316 | // valid by the time the handler can fire. Both ends are non-blocking and | 317 | // valid by the time the handler can fire. Both ends are non-blocking and | |||||
| 317 | // close-on-exec (mirrors the reactor self-pipe setup in select_scheduler). | 318 | // close-on-exec (mirrors the reactor self-pipe setup in select_scheduler). | |||||
| 318 | // Returns false and leaves the fds at -1 if creation fails. | 319 | // Returns false and leaves the fds at -1 if creation fails. | |||||
| 319 | inline bool | 320 | inline bool | |||||
| HITCBC | 320 | 138 | open_signal_pipe(signal_state* state) | 321 | 138 | open_signal_pipe(signal_state* state) | ||
| 321 | { | 322 | { | |||||
| HITCBC | 322 | 138 | if (state->read_fd >= 0) | 323 | 138 | if (state->read_fd >= 0) | ||
| HITCBC | 323 | 135 | return true; | 324 | 135 | return true; | ||
| 324 | 325 | |||||||
| 325 | int fds[2]; | 326 | int fds[2]; | |||||
| HITCBC | 326 | 3 | if (::pipe(fds) < 0) | 327 | 3 | if (::pipe(fds) < 0) | ||
| MISUBC | 327 | ✗ | return false; | 328 | ✗ | return false; | ||
| 328 | 329 | |||||||
| HITCBC | 329 | 9 | for (int i = 0; i < 2; ++i) | 330 | 9 | for (int i = 0; i < 2; ++i) | ||
| 330 | { | 331 | { | |||||
| HITCBC | 331 | 6 | int fl = ::fcntl(fds[i], F_GETFL, 0); | 332 | 6 | int fl = ::fcntl(fds[i], F_GETFL, 0); | ||
| HITCBC | 332 | 12 | if (fl == -1 || ::fcntl(fds[i], F_SETFL, fl | O_NONBLOCK) == -1 || | 333 | 12 | if (fl == -1 || ::fcntl(fds[i], F_SETFL, fl | O_NONBLOCK) == -1 || | ||
| HITCBC | 333 | 6 | ::fcntl(fds[i], F_SETFD, FD_CLOEXEC) == -1) | 334 | 6 | ::fcntl(fds[i], F_SETFD, FD_CLOEXEC) == -1) | ||
| 334 | { | 335 | { | |||||
| MISUBC | 335 | ✗ | ::close(fds[0]); | 336 | ✗ | ::close(fds[0]); | ||
| MISUBC | 336 | ✗ | ::close(fds[1]); | 337 | ✗ | ::close(fds[1]); | ||
| MISUBC | 337 | ✗ | return false; | 338 | ✗ | return false; | ||
| 338 | } | 339 | } | |||||
| 339 | } | 340 | } | |||||
| 340 | 341 | |||||||
| HITCBC | 341 | 3 | state->read_fd = fds[0]; | 342 | 3 | state->read_fd = fds[0]; | ||
| HITCBC | 342 | 3 | state->write_fd = fds[1]; | 343 | 3 | state->write_fd = fds[1]; | ||
| HITCBC | 343 | 3 | return true; | 344 | 3 | return true; | ||
| 344 | } | 345 | } | |||||
| 345 | 346 | |||||||
| 346 | // C signal handler. Async-signal-safe: it touches only the single global | 347 | // C signal handler. Async-signal-safe: it touches only the single global | |||||
| 347 | // write_fd (an int set before any handler is installed) and calls write(), | 348 | // write_fd (an int set before any handler is installed) and calls write(), | |||||
| 348 | // which POSIX lists as async-signal-safe. errno is saved and restored so an | 349 | // which POSIX lists as async-signal-safe. errno is saved and restored so an | |||||
| 349 | // interrupted foreground syscall is unaffected. A full pipe (write returns | 350 | // interrupted foreground syscall is unaffected. A full pipe (write returns | |||||
| 350 | // EAGAIN) or a short write is intentionally dropped — the reactor still | 351 | // EAGAIN) or a short write is intentionally dropped — the reactor still | |||||
| 351 | // coalesces because deliver_signal reports the signal to every waiting set. | 352 | // coalesces because deliver_signal reports the signal to every waiting set. | |||||
| 352 | inline void | 353 | inline void | |||||
| HITCBC | 353 | 302 | corosio_posix_signal_handler(int signal_number) | 354 | 302 | corosio_posix_signal_handler(int signal_number) | ||
| 354 | { | 355 | { | |||||
| HITCBC | 355 | 302 | int saved_errno = errno; | 356 | 302 | int saved_errno = errno; | ||
| HITCBC | 356 | 302 | signal_state* state = get_signal_state(); | 357 | 302 | signal_state* state = get_signal_state(); | ||
| 357 | [[maybe_unused]] ssize_t r = | 358 | [[maybe_unused]] ssize_t r = | |||||
| HITCBC | 358 | 302 | ::write(state->write_fd, &signal_number, sizeof(int)); | 359 | 302 | ::write(state->write_fd, &signal_number, sizeof(int)); | ||
| HITCBC | 359 | 302 | errno = saved_errno; | 360 | 302 | errno = saved_errno; | ||
| 360 | // With sigaction(), the handler persists automatically (unlike some | 361 | // With sigaction(), the handler persists automatically (unlike some | |||||
| 361 | // signal() implementations that reset to SIG_DFL). | 362 | // signal() implementations that reset to SIG_DFL). | |||||
| HITCBC | 362 | 302 | } | 363 | 302 | } | ||
| 363 | 364 | |||||||
| 364 | // Drain the signal self-pipe and deliver each pending signal. Runs in normal | 365 | // Drain the signal self-pipe and deliver each pending signal. Runs in normal | |||||
| 365 | // thread context from the backend event loop, so deliver_signal()'s mutex | 366 | // thread context from the backend event loop, so deliver_signal()'s mutex | |||||
| 366 | // locking and scheduler post are safe here. Reads until EAGAIN (edge- | 367 | // locking and scheduler post are safe here. Reads until EAGAIN (edge- | |||||
| 367 | // triggered backends require a full drain per readiness event). | 368 | // triggered backends require a full drain per readiness event). | |||||
| 368 | inline void | 369 | inline void | |||||
| HITCBC | 369 | 302 | drain_signal_pipe() | 370 | 302 | drain_signal_pipe() | ||
| 370 | { | 371 | { | |||||
| HITCBC | 371 | 302 | signal_state* state = get_signal_state(); | 372 | 302 | signal_state* state = get_signal_state(); | ||
| 372 | int signal_number; | 373 | int signal_number; | |||||
| HITCBC | 373 | 604 | while (::read(state->read_fd, &signal_number, sizeof(int)) == | 374 | 604 | while (::read(state->read_fd, &signal_number, sizeof(int)) == | ||
| 374 | static_cast<ssize_t>(sizeof(int))) | 375 | static_cast<ssize_t>(sizeof(int))) | |||||
| 375 | { | 376 | { | |||||
| HITCBC | 376 | 302 | posix_signal_service::deliver_signal(signal_number); | 377 | 302 | posix_signal_service::deliver_signal(signal_number); | ||
| 377 | } | 378 | } | |||||
| HITCBC | 378 | 302 | } | 379 | 302 | } | ||
| 379 | 380 | |||||||
| 380 | } // namespace posix_signal_detail | 381 | } // namespace posix_signal_detail | |||||
| 381 | 382 | |||||||
| 382 | // signal_op implementation | 383 | // signal_op implementation | |||||
| 383 | 384 | |||||||
| 384 | inline void | 385 | inline void | |||||
| HITCBC | 385 | 304 | signal_op::operator()() | 386 | 304 | signal_op::operator()() | ||
| 386 | { | 387 | { | |||||
| HITCBC | 387 | 304 | if (ec_out) | 388 | 304 | if (ec_out) | ||
| HITCBC | 388 | 304 | *ec_out = {}; | 389 | 304 | *ec_out = {}; | ||
| HITCBC | 389 | 304 | if (signal_out) | 390 | 304 | if (signal_out) | ||
| HITCBC | 390 | 304 | *signal_out = signal_number; | 391 | 304 | *signal_out = signal_number; | ||
| 391 | 392 | |||||||
| 392 | // Capture svc before resuming (coro may destroy us) | 393 | // Capture svc before resuming (coro may destroy us) | |||||
| HITCBC | 393 | 304 | auto* service = svc; | 394 | 304 | auto* service = svc; | ||
| HITCBC | 394 | 304 | svc = nullptr; | 395 | 304 | svc = nullptr; | ||
| 395 | 396 | |||||||
| HITCBC | 396 | 304 | cont.h = h; | 397 | 304 | cont.h = h; | ||
| HITCBC | 397 | 304 | d.post(cont); | 398 | 304 | d.post(cont); | ||
| 398 | 399 | |||||||
| 399 | // Balance the work_started() from start_wait | 400 | // Balance the work_started() from start_wait | |||||
| HITCBC | 400 | 304 | if (service) | 401 | 304 | if (service) | ||
| HITCBC | 401 | 304 | service->work_finished(); | 402 | 304 | service->work_finished(); | ||
| HITCBC | 402 | 304 | } | 403 | 304 | } | ||
| 403 | 404 | |||||||
| 404 | inline void | 405 | inline void | |||||
| MISUBC | 405 | ✗ | signal_op::destroy() | 406 | ✗ | signal_op::destroy() | ||
| 406 | { | 407 | { | |||||
| 407 | // No-op: signal_op is embedded in posix_signal | 408 | // No-op: signal_op is embedded in posix_signal | |||||
| MISUBC | 408 | ✗ | } | 409 | ✗ | } | ||
| 409 | 410 | |||||||
| 410 | // posix_signal implementation | 411 | // posix_signal implementation | |||||
| 411 | 412 | |||||||
| HITCBC | 412 | 125 | inline posix_signal::posix_signal(posix_signal_service& svc) noexcept | 413 | 125 | inline posix_signal::posix_signal(posix_signal_service& svc) noexcept | ||
| HITCBC | 413 | 125 | : svc_(svc) | 414 | 125 | : svc_(svc) | ||
| 414 | { | 415 | { | |||||
| HITCBC | 415 | 125 | } | 416 | 125 | } | ||
| 416 | 417 | |||||||
| 417 | inline std::coroutine_handle<> | 418 | inline std::coroutine_handle<> | |||||
| HITCBC | 418 | 313 | posix_signal::wait( | 419 | 313 | posix_signal::wait( | ||
| 419 | std::coroutine_handle<> h, | 420 | std::coroutine_handle<> h, | |||||
| 420 | capy::executor_ref d, | 421 | capy::executor_ref d, | |||||
| 421 | std::stop_token token, | 422 | std::stop_token token, | |||||
| 422 | std::error_code* ec, | 423 | std::error_code* ec, | |||||
| 423 | int* signal_out) | 424 | int* signal_out) | |||||
| 424 | { | 425 | { | |||||
| HITCBC | 425 | 313 | pending_op_.h = h; | 426 | 313 | pending_op_.h = h; | ||
| HITCBC | 426 | 313 | pending_op_.d = d; | 427 | 313 | pending_op_.d = d; | ||
| HITCBC | 427 | 313 | pending_op_.ec_out = ec; | 428 | 313 | pending_op_.ec_out = ec; | ||
| HITCBC | 428 | 313 | pending_op_.signal_out = signal_out; | 429 | 313 | pending_op_.signal_out = signal_out; | ||
| HITCBC | 429 | 313 | pending_op_.signal_number = 0; | 430 | 313 | pending_op_.signal_number = 0; | ||
| 430 | 431 | |||||||
| HITCBC | 431 | 313 | if (token.stop_requested()) | 432 | 313 | if (token.stop_requested()) | ||
| 432 | { | 433 | { | |||||
| HITCBC | 433 | 2 | if (ec) | 434 | 2 | if (ec) | ||
| HITCBC | 434 | 2 | *ec = make_error_code(capy::error::canceled); | 435 | 2 | *ec = make_error_code(capy::error::canceled); | ||
| HITCBC | 435 | 2 | if (signal_out) | 436 | 2 | if (signal_out) | ||
| HITCBC | 436 | 2 | *signal_out = 0; | 437 | 2 | *signal_out = 0; | ||
| HITCBC | 437 | 2 | pending_op_.cont.h = h; | 438 | 2 | pending_op_.cont.h = h; | ||
| HITCBC | 438 | 2 | d.post(pending_op_.cont); | 439 | 2 | d.post(pending_op_.cont); | ||
| 439 | // completion is always posted to scheduler queue, never inline. | 440 | // completion is always posted to scheduler queue, never inline. | |||||
| HITCBC | 440 | 2 | return std::noop_coroutine(); | 441 | 2 | return std::noop_coroutine(); | ||
| 441 | } | 442 | } | |||||
| 442 | 443 | |||||||
| HITCBC | 443 | 311 | svc_.start_wait(*this, &pending_op_); | 444 | 311 | svc_.start_wait(*this, &pending_op_); | ||
| 444 | // completion is always posted to scheduler queue, never inline. | 445 | // completion is always posted to scheduler queue, never inline. | |||||
| HITCBC | 445 | 311 | return std::noop_coroutine(); | 446 | 311 | return std::noop_coroutine(); | ||
| 446 | } | 447 | } | |||||
| 447 | 448 | |||||||
| 448 | inline std::error_code | 449 | inline std::error_code | |||||
| HITCBC | 449 | 142 | posix_signal::add(int signal_number, signal_set::flags_t flags) | 450 | 142 | posix_signal::add(int signal_number, signal_set::flags_t flags) | ||
| 450 | { | 451 | { | |||||
| HITCBC | 451 | 142 | return svc_.add_signal(*this, signal_number, flags); | 452 | 142 | return svc_.add_signal(*this, signal_number, flags); | ||
| 452 | } | 453 | } | |||||
| 453 | 454 | |||||||
| 454 | inline std::error_code | 455 | inline std::error_code | |||||
| HITCBC | 455 | 8 | posix_signal::remove(int signal_number) | 456 | 8 | posix_signal::remove(int signal_number) | ||
| 456 | { | 457 | { | |||||
| HITCBC | 457 | 8 | return svc_.remove_signal(*this, signal_number); | 458 | 8 | return svc_.remove_signal(*this, signal_number); | ||
| 458 | } | 459 | } | |||||
| 459 | 460 | |||||||
| 460 | inline std::error_code | 461 | inline std::error_code | |||||
| HITCBC | 461 | 131 | posix_signal::clear() | 462 | 130 | posix_signal::clear() | ||
| 462 | { | 463 | { | |||||
| HITCBC | 463 | 131 | return svc_.clear_signals(*this); | 464 | 130 | return svc_.clear_signals(*this); | ||
| 464 | } | 465 | } | |||||
| 465 | 466 | |||||||
| 466 | inline void | 467 | inline void | |||||
| HITCBC | 467 | - | 140 | posix_signal::cancel() | 468 | + | 140 | posix_signal::cancel() noexcept |
| 468 | { | 469 | { | |||||
| HITCBC | 469 | 140 | svc_.cancel_wait(*this); | 470 | 140 | svc_.cancel_wait(*this); | ||
| HITCBC | 470 | 140 | } | 471 | 140 | } | ||
| 471 | 472 | |||||||
| 472 | // posix_signal_service implementation | 473 | // posix_signal_service implementation | |||||
| 473 | 474 | |||||||
| HITCBC | 474 | 1533 | inline posix_signal_service::posix_signal_service( | 475 | 1603 | inline posix_signal_service::posix_signal_service( | ||
| HITCBC | 475 | 1533 | capy::execution_context&, scheduler& sched) | 476 | 1603 | capy::execution_context&, scheduler& sched) | ||
| HITCBC | 476 | 1533 | : sched_(&sched) | 477 | 1603 | : sched_(&sched) | ||
| 477 | { | 478 | { | |||||
| HITCBC | 478 | 99645 | for (int i = 0; i < max_signal_number; ++i) | 479 | 104195 | for (int i = 0; i < max_signal_number; ++i) | ||
| 479 | { | 480 | { | |||||
| HITCBC | 480 | 98112 | registrations_[i] = nullptr; | 481 | 102592 | registrations_[i] = nullptr; | ||
| HITCBC | 481 | 98112 | registration_count_[i] = 0; | 482 | 102592 | registration_count_[i] = 0; | ||
| 482 | } | 483 | } | |||||
| HITCBC | 483 | 1533 | add_service(this); | 484 | 1603 | add_service(this); | ||
| HITCBC | 484 | 1533 | } | 485 | 1603 | } | ||
| 485 | 486 | |||||||
| HITCBC | 486 | 3066 | inline posix_signal_service::~posix_signal_service() | 487 | 3206 | inline posix_signal_service::~posix_signal_service() | ||
| 487 | { | 488 | { | |||||
| HITCBC | 488 | 1533 | remove_service(this); | 489 | 1603 | remove_service(this); | ||
| HITCBC | 489 | 3066 | } | 490 | 3206 | } | ||
| 490 | 491 | |||||||
| 491 | inline void | 492 | inline void | |||||
| HITCBC | 492 | 1533 | posix_signal_service::shutdown() | 493 | 1603 | posix_signal_service::shutdown() | ||
| 493 | { | 494 | { | |||||
| HITCBC | 494 | 1533 | std::lock_guard lock(mutex_); | 495 | 1603 | std::lock_guard lock(mutex_); | ||
| 495 | 496 | |||||||
| HITCBC | 496 | 1533 | for (auto* impl = impl_list_.pop_front(); impl != nullptr; | 497 | 1603 | for (auto* impl = impl_list_.pop_front(); impl != nullptr; | ||
| MISUBC | 497 | ✗ | impl = impl_list_.pop_front()) | 498 | ✗ | impl = impl_list_.pop_front()) | ||
| 498 | { | 499 | { | |||||
| MISUBC | 499 | ✗ | while (auto* reg = impl->signals_) | 500 | ✗ | while (auto* reg = impl->signals_) | ||
| 500 | { | 501 | { | |||||
| MISUBC | 501 | ✗ | impl->signals_ = reg->next_in_set; | 502 | ✗ | impl->signals_ = reg->next_in_set; | ||
| MISUBC | 502 | ✗ | delete reg; | 503 | ✗ | delete reg; | ||
| MISUBC | 503 | ✗ | } | 504 | ✗ | } | ||
| MISUBC | 504 | ✗ | delete impl; | 505 | ✗ | delete impl; | ||
| 505 | } | 506 | } | |||||
| HITCBC | 506 | 1533 | } | 507 | 1603 | } | ||
| 507 | 508 | |||||||
| 508 | inline io_object::implementation* | 509 | inline io_object::implementation* | |||||
| HITCBC | 509 | 125 | posix_signal_service::construct() | 510 | 125 | posix_signal_service::construct() | ||
| 510 | { | 511 | { | |||||
| HITCBC | 511 | 125 | auto* impl = new posix_signal(*this); | 512 | 125 | auto* impl = new posix_signal(*this); | ||
| 512 | 513 | |||||||
| 513 | { | 514 | { | |||||
| HITCBC | 514 | 125 | std::lock_guard lock(mutex_); | 515 | 125 | std::lock_guard lock(mutex_); | ||
| HITCBC | 515 | 125 | impl_list_.push_back(impl); | 516 | 125 | impl_list_.push_back(impl); | ||
| HITCBC | 516 | 125 | } | 517 | 125 | } | ||
| 517 | 518 | |||||||
| HITCBC | 518 | 125 | return impl; | 519 | 125 | return impl; | ||
| 519 | } | 520 | } | |||||
| 520 | 521 | |||||||
| 521 | inline void | 522 | inline void | |||||
| HITCBC | 522 | 125 | posix_signal_service::destroy_impl(posix_signal& impl) | 523 | 125 | posix_signal_service::destroy_impl(posix_signal& impl) | ||
| 523 | { | 524 | { | |||||
| 524 | { | 525 | { | |||||
| HITCBC | 525 | 125 | std::lock_guard lock(mutex_); | 526 | 125 | std::lock_guard lock(mutex_); | ||
| HITCBC | 526 | 125 | impl_list_.remove(&impl); | 527 | 125 | impl_list_.remove(&impl); | ||
| HITCBC | 527 | 125 | } | 528 | 125 | } | ||
| 528 | 529 | |||||||
| HITCBC | 529 | 125 | delete &impl; | 530 | 125 | delete &impl; | ||
| HITCBC | 530 | 125 | } | 531 | 125 | } | ||
| 531 | 532 | |||||||
| 532 | inline std::error_code | 533 | inline std::error_code | |||||
| HITCBC | 533 | 142 | posix_signal_service::add_signal( | 534 | 142 | posix_signal_service::add_signal( | ||
| 534 | posix_signal& impl, int signal_number, signal_set::flags_t flags) | 535 | posix_signal& impl, int signal_number, signal_set::flags_t flags) | |||||
| 535 | { | 536 | { | |||||
| HITCBC | 536 | 142 | if (signal_number < 0 || signal_number >= max_signal_number) | 537 | 142 | if (signal_number < 0 || signal_number >= max_signal_number) | ||
| HITCBC | 537 | 4 | return make_error_code(std::errc::invalid_argument); | 538 | 4 | return make_error_code(std::errc::invalid_argument); | ||
| 538 | 539 | |||||||
| 539 | // Validate that requested flags are supported on this platform | 540 | // Validate that requested flags are supported on this platform | |||||
| 540 | // (e.g., SA_NOCLDWAIT may not be available on all POSIX systems) | 541 | // (e.g., SA_NOCLDWAIT may not be available on all POSIX systems) | |||||
| HITCBC | 541 | 138 | if (!posix_signal_detail::flags_supported(flags)) | 542 | 138 | if (!posix_signal_detail::flags_supported(flags)) | ||
| MISUBC | 542 | ✗ | return make_error_code(std::errc::operation_not_supported); | 543 | ✗ | return make_error_code(std::errc::operation_not_supported); | ||
| 543 | 544 | |||||||
| 544 | posix_signal_detail::signal_state* state = | 545 | posix_signal_detail::signal_state* state = | |||||
| HITCBC | 545 | 138 | posix_signal_detail::get_signal_state(); | 546 | 138 | posix_signal_detail::get_signal_state(); | ||
| 546 | 547 | |||||||
| 547 | // Ensure the global self-pipe exists and this service's scheduler is | 548 | // Ensure the global self-pipe exists and this service's scheduler is | |||||
| 548 | // watching its read end, BEFORE taking the registration locks. The | 549 | // watching its read end, BEFORE taking the registration locks. The | |||||
| 549 | // reactor drain path locks the descriptor mutex and then the signal-state | 550 | // reactor drain path locks the descriptor mutex and then the signal-state | |||||
| 550 | // and service mutexes; register_signal_reader locks the descriptor mutex | 551 | // and service mutexes; register_signal_reader locks the descriptor mutex | |||||
| 551 | // (via register_descriptor), so it must run holding neither of those or | 552 | // (via register_descriptor), so it must run holding neither of those or | |||||
| 552 | // the lock order would invert (a real deadlock, caught by TSan). call_once | 553 | // the lock order would invert (a real deadlock, caught by TSan). call_once | |||||
| 553 | // makes the once-per-service registration safe when two signal_sets on | 554 | // makes the once-per-service registration safe when two signal_sets on | |||||
| 554 | // this context race add() from different threads. | 555 | // this context race add() from different threads. | |||||
| 555 | { | 556 | { | |||||
| HITCBC | 556 | 138 | std::lock_guard state_lock(state->mutex); | 557 | 138 | std::lock_guard state_lock(state->mutex); | ||
| HITCBC | 557 | 138 | if (!posix_signal_detail::open_signal_pipe(state)) | 558 | 138 | if (!posix_signal_detail::open_signal_pipe(state)) | ||
| MISUBC | 558 | ✗ | return make_error_code(std::errc::io_error); | 559 | ✗ | return make_error_code(std::errc::io_error); | ||
| HITCBC | 559 | 138 | } | 560 | 138 | } | ||
| ECB | 560 | - | 138 | std::call_once(reader_once_, [this, state] { | 561 | + | { | |
| ECB | 561 | - | 92 | sched_->register_signal_reader(state->read_fd); | 562 | + | // Success-latched so a failed environmental registration | |
| ECB | 562 | - | 92 | }); | 563 | + | // (epoll_ctl ENOMEM/ENOSPC) is retried by the next add() | |
| 564 | + | // instead of being lost; the code travels the return channel. | ||||||
| HITGNC | 565 | + | 138 | std::lock_guard reg_lock(reader_mutex_); | ||||
| HITGNC | 566 | + | 138 | if (!reader_registered_) | ||||
| 567 | + | { | ||||||
| HITGNC | 568 | + | 92 | if (auto ec = sched_->register_signal_reader(state->read_fd)) | ||||
| MISUNC | 569 | + | ✗ | return ec; | ||||
| HITGNC | 570 | + | 92 | reader_registered_ = true; | ||||
| 571 | + | } | ||||||
| HITGNC | 572 | + | 138 | } | ||||
| 563 | 573 | |||||||
| HITCBC | 564 | 138 | std::lock_guard state_lock(state->mutex); | 574 | 138 | std::lock_guard state_lock(state->mutex); | ||
| HITCBC | 565 | 138 | std::lock_guard lock(mutex_); | 575 | 138 | std::lock_guard lock(mutex_); | ||
| 566 | 576 | |||||||
| 567 | // Find insertion point (list is sorted by signal number) | 577 | // Find insertion point (list is sorted by signal number) | |||||
| HITCBC | 568 | 138 | signal_registration** insertion_point = &impl.signals_; | 578 | 138 | signal_registration** insertion_point = &impl.signals_; | ||
| HITCBC | 569 | 138 | signal_registration* reg = impl.signals_; | 579 | 138 | signal_registration* reg = impl.signals_; | ||
| HITCBC | 570 | 158 | while (reg && reg->signal_number < signal_number) | 580 | 159 | while (reg && reg->signal_number < signal_number) | ||
| 571 | { | 581 | { | |||||
| HITCBC | 572 | 20 | insertion_point = ®->next_in_set; | 582 | 21 | insertion_point = ®->next_in_set; | ||
| HITCBC | 573 | 20 | reg = reg->next_in_set; | 583 | 21 | reg = reg->next_in_set; | ||
| 574 | } | 584 | } | |||||
| 575 | 585 | |||||||
| 576 | // Already registered in this set - check flag compatibility | 586 | // Already registered in this set - check flag compatibility | |||||
| 577 | // (same signal_set adding same signal twice with different flags) | 587 | // (same signal_set adding same signal twice with different flags) | |||||
| HITCBC | 578 | 138 | if (reg && reg->signal_number == signal_number) | 588 | 138 | if (reg && reg->signal_number == signal_number) | ||
| 579 | { | 589 | { | |||||
| HITCBC | 580 | 14 | if (!posix_signal_detail::flags_compatible(reg->flags, flags)) | 590 | 13 | if (!posix_signal_detail::flags_compatible(reg->flags, flags)) | ||
| HITCBC | 581 | 5 | return make_error_code(std::errc::invalid_argument); | 591 | 4 | return make_error_code(std::errc::invalid_argument); | ||
| HITCBC | 582 | 9 | return {}; | 592 | 9 | return {}; | ||
| 583 | } | 593 | } | |||||
| 584 | 594 | |||||||
| 585 | // Check flag compatibility with global registration | 595 | // Check flag compatibility with global registration | |||||
| 586 | // (different signal_set already registered this signal with different flags) | 596 | // (different signal_set already registered this signal with different flags) | |||||
| HITCBC | 587 | 124 | if (state->registration_count[signal_number] > 0) | 597 | 125 | if (state->registration_count[signal_number] > 0) | ||
| 588 | { | 598 | { | |||||
| HITCBC | 589 | 12 | if (!posix_signal_detail::flags_compatible( | 599 | 12 | if (!posix_signal_detail::flags_compatible( | ||
| 590 | state->registered_flags[signal_number], flags)) | 600 | state->registered_flags[signal_number], flags)) | |||||
| HITCBC | 591 | 2 | return make_error_code(std::errc::invalid_argument); | 601 | 2 | return make_error_code(std::errc::invalid_argument); | ||
| 592 | } | 602 | } | |||||
| 593 | 603 | |||||||
| HITCBC | 594 | 122 | auto* new_reg = new signal_registration; | 604 | 123 | auto* new_reg = new signal_registration; | ||
| HITCBC | 595 | 122 | new_reg->signal_number = signal_number; | 605 | 123 | new_reg->signal_number = signal_number; | ||
| HITCBC | 596 | 122 | new_reg->flags = flags; | 606 | 123 | new_reg->flags = flags; | ||
| HITCBC | 597 | 122 | new_reg->owner = &impl; | 607 | 123 | new_reg->owner = &impl; | ||
| HITCBC | 598 | 122 | new_reg->undelivered = 0; | 608 | 123 | new_reg->undelivered = 0; | ||
| 599 | 609 | |||||||
| 600 | // Install signal handler on first global registration | 610 | // Install signal handler on first global registration | |||||
| HITCBC | 601 | 122 | if (state->registration_count[signal_number] == 0) | 611 | 123 | if (state->registration_count[signal_number] == 0) | ||
| 602 | { | 612 | { | |||||
| HITCBC | 603 | 112 | struct sigaction sa = {}; | 613 | 113 | struct sigaction sa = {}; | ||
| HITCBC | 604 | 112 | sa.sa_handler = posix_signal_detail::corosio_posix_signal_handler; | 614 | 113 | sa.sa_handler = posix_signal_detail::corosio_posix_signal_handler; | ||
| HITCBC | 605 | 112 | sigemptyset(&sa.sa_mask); | 615 | 113 | sigemptyset(&sa.sa_mask); | ||
| HITCBC | 606 | 112 | sa.sa_flags = posix_signal_detail::to_sigaction_flags(flags); | 616 | 113 | sa.sa_flags = posix_signal_detail::to_sigaction_flags(flags); | ||
| 607 | 617 | |||||||
| HITCBC | 608 | 112 | if (::sigaction(signal_number, &sa, nullptr) < 0) | 618 | 113 | if (::sigaction(signal_number, &sa, nullptr) < 0) | ||
| 609 | { | 619 | { | |||||
| MISUBC | 610 | ✗ | delete new_reg; | 620 | ✗ | delete new_reg; | ||
| MISUBC | 611 | ✗ | return make_error_code(std::errc::invalid_argument); | 621 | ✗ | return make_error_code(std::errc::invalid_argument); | ||
| 612 | } | 622 | } | |||||
| 613 | 623 | |||||||
| 614 | // Store the flags used for first registration | 624 | // Store the flags used for first registration | |||||
| HITCBC | 615 | 112 | state->registered_flags[signal_number] = flags; | 625 | 113 | state->registered_flags[signal_number] = flags; | ||
| 616 | } | 626 | } | |||||
| 617 | 627 | |||||||
| HITCBC | 618 | 122 | new_reg->next_in_set = reg; | 628 | 123 | new_reg->next_in_set = reg; | ||
| HITCBC | 619 | 122 | *insertion_point = new_reg; | 629 | 123 | *insertion_point = new_reg; | ||
| 620 | 630 | |||||||
| HITCBC | 621 | 122 | new_reg->next_in_table = registrations_[signal_number]; | 631 | 123 | new_reg->next_in_table = registrations_[signal_number]; | ||
| HITCBC | 622 | 122 | new_reg->prev_in_table = nullptr; | 632 | 123 | new_reg->prev_in_table = nullptr; | ||
| HITCBC | 623 | 122 | if (registrations_[signal_number]) | 633 | 123 | if (registrations_[signal_number]) | ||
| HITCBC | 624 | 10 | registrations_[signal_number]->prev_in_table = new_reg; | 634 | 10 | registrations_[signal_number]->prev_in_table = new_reg; | ||
| HITCBC | 625 | 122 | registrations_[signal_number] = new_reg; | 635 | 123 | registrations_[signal_number] = new_reg; | ||
| 626 | 636 | |||||||
| HITCBC | 627 | 122 | ++state->registration_count[signal_number]; | 637 | 123 | ++state->registration_count[signal_number]; | ||
| HITCBC | 628 | 122 | ++registration_count_[signal_number]; | 638 | 123 | ++registration_count_[signal_number]; | ||
| 629 | 639 | |||||||
| HITCBC | 630 | 122 | return {}; | 640 | 123 | return {}; | ||
| HITCBC | 631 | 138 | } | 641 | 138 | } | ||
| 632 | 642 | |||||||
| 633 | inline std::error_code | 643 | inline std::error_code | |||||
| HITCBC | 634 | 8 | posix_signal_service::remove_signal(posix_signal& impl, int signal_number) | 644 | 8 | posix_signal_service::remove_signal(posix_signal& impl, int signal_number) | ||
| 635 | { | 645 | { | |||||
| HITCBC | 636 | 8 | if (signal_number < 0 || signal_number >= max_signal_number) | 646 | 8 | if (signal_number < 0 || signal_number >= max_signal_number) | ||
| HITCBC | 637 | 2 | return make_error_code(std::errc::invalid_argument); | 647 | 2 | return make_error_code(std::errc::invalid_argument); | ||
| 638 | 648 | |||||||
| 639 | posix_signal_detail::signal_state* state = | 649 | posix_signal_detail::signal_state* state = | |||||
| HITCBC | 640 | 6 | posix_signal_detail::get_signal_state(); | 650 | 6 | posix_signal_detail::get_signal_state(); | ||
| HITCBC | 641 | 6 | std::lock_guard state_lock(state->mutex); | 651 | 6 | std::lock_guard state_lock(state->mutex); | ||
| HITCBC | 642 | 6 | std::lock_guard lock(mutex_); | 652 | 6 | std::lock_guard lock(mutex_); | ||
| 643 | 653 | |||||||
| HITCBC | 644 | 6 | signal_registration** deletion_point = &impl.signals_; | 654 | 6 | signal_registration** deletion_point = &impl.signals_; | ||
| HITCBC | 645 | 6 | signal_registration* reg = impl.signals_; | 655 | 6 | signal_registration* reg = impl.signals_; | ||
| HITCBC | 646 | 6 | while (reg && reg->signal_number < signal_number) | 656 | 6 | while (reg && reg->signal_number < signal_number) | ||
| 647 | { | 657 | { | |||||
| MISUBC | 648 | ✗ | deletion_point = ®->next_in_set; | 658 | ✗ | deletion_point = ®->next_in_set; | ||
| MISUBC | 649 | ✗ | reg = reg->next_in_set; | 659 | ✗ | reg = reg->next_in_set; | ||
| 650 | } | 660 | } | |||||
| 651 | 661 | |||||||
| HITCBC | 652 | 6 | if (!reg || reg->signal_number != signal_number) | 662 | 6 | if (!reg || reg->signal_number != signal_number) | ||
| HITCBC | 653 | 3 | return {}; | 663 | 3 | return {}; | ||
| 654 | 664 | |||||||
| 655 | // Restore default handler on last global unregistration | 665 | // Restore default handler on last global unregistration | |||||
| HITCBC | 656 | 3 | if (state->registration_count[signal_number] == 1) | 666 | 3 | if (state->registration_count[signal_number] == 1) | ||
| 657 | { | 667 | { | |||||
| HITCBC | 658 | 3 | struct sigaction sa = {}; | 668 | 3 | struct sigaction sa = {}; | ||
| HITCBC | 659 | 3 | sa.sa_handler = SIG_DFL; | 669 | 3 | sa.sa_handler = SIG_DFL; | ||
| HITCBC | 660 | 3 | sigemptyset(&sa.sa_mask); | 670 | 3 | sigemptyset(&sa.sa_mask); | ||
| HITCBC | 661 | 3 | sa.sa_flags = 0; | 671 | 3 | sa.sa_flags = 0; | ||
| 662 | 672 | |||||||
| HITCBC | 663 | 3 | if (::sigaction(signal_number, &sa, nullptr) < 0) | 673 | 3 | if (::sigaction(signal_number, &sa, nullptr) < 0) | ||
| MISUBC | 664 | ✗ | return make_error_code(std::errc::invalid_argument); | 674 | ✗ | return make_error_code(std::errc::invalid_argument); | ||
| 665 | 675 | |||||||
| 666 | // Clear stored flags | 676 | // Clear stored flags | |||||
| HITCBC | 667 | 3 | state->registered_flags[signal_number] = signal_set::none; | 677 | 3 | state->registered_flags[signal_number] = signal_set::none; | ||
| 668 | } | 678 | } | |||||
| 669 | 679 | |||||||
| HITCBC | 670 | 3 | *deletion_point = reg->next_in_set; | 680 | 3 | *deletion_point = reg->next_in_set; | ||
| 671 | 681 | |||||||
| HITCBC | 672 | 3 | if (registrations_[signal_number] == reg) | 682 | 3 | if (registrations_[signal_number] == reg) | ||
| HITCBC | 673 | 3 | registrations_[signal_number] = reg->next_in_table; | 683 | 3 | registrations_[signal_number] = reg->next_in_table; | ||
| HITCBC | 674 | 3 | if (reg->prev_in_table) | 684 | 3 | if (reg->prev_in_table) | ||
| MISUBC | 675 | ✗ | reg->prev_in_table->next_in_table = reg->next_in_table; | 685 | ✗ | reg->prev_in_table->next_in_table = reg->next_in_table; | ||
| HITCBC | 676 | 3 | if (reg->next_in_table) | 686 | 3 | if (reg->next_in_table) | ||
| MISUBC | 677 | ✗ | reg->next_in_table->prev_in_table = reg->prev_in_table; | 687 | ✗ | reg->next_in_table->prev_in_table = reg->prev_in_table; | ||
| 678 | 688 | |||||||
| HITCBC | 679 | 3 | --state->registration_count[signal_number]; | 689 | 3 | --state->registration_count[signal_number]; | ||
| HITCBC | 680 | 3 | --registration_count_[signal_number]; | 690 | 3 | --registration_count_[signal_number]; | ||
| 681 | 691 | |||||||
| HITCBC | 682 | 3 | delete reg; | 692 | 3 | delete reg; | ||
| HITCBC | 683 | 3 | return {}; | 693 | 3 | return {}; | ||
| HITCBC | 684 | 6 | } | 694 | 6 | } | ||
| 685 | 695 | |||||||
| 686 | inline std::error_code | 696 | inline std::error_code | |||||
| HITCBC | 687 | 131 | posix_signal_service::clear_signals(posix_signal& impl) | 697 | 130 | posix_signal_service::clear_signals(posix_signal& impl) | ||
| 688 | { | 698 | { | |||||
| 689 | posix_signal_detail::signal_state* state = | 699 | posix_signal_detail::signal_state* state = | |||||
| HITCBC | 690 | 131 | posix_signal_detail::get_signal_state(); | 700 | 130 | posix_signal_detail::get_signal_state(); | ||
| HITCBC | 691 | 131 | std::lock_guard state_lock(state->mutex); | 701 | 130 | std::lock_guard state_lock(state->mutex); | ||
| HITCBC | 692 | 131 | std::lock_guard lock(mutex_); | 702 | 130 | std::lock_guard lock(mutex_); | ||
| 693 | 703 | |||||||
| HITCBC | 694 | 131 | std::error_code first_error; | 704 | 130 | std::error_code first_error; | ||
| 695 | 705 | |||||||
| HITCBC | 696 | 250 | while (signal_registration* reg = impl.signals_) | 706 | 250 | while (signal_registration* reg = impl.signals_) | ||
| 697 | { | 707 | { | |||||
| HITCBC | 698 | 119 | int signal_number = reg->signal_number; | 708 | 120 | int signal_number = reg->signal_number; | ||
| 699 | 709 | |||||||
| HITCBC | 700 | 119 | if (state->registration_count[signal_number] == 1) | 710 | 120 | if (state->registration_count[signal_number] == 1) | ||
| 701 | { | 711 | { | |||||
| HITCBC | 702 | 109 | struct sigaction sa = {}; | 712 | 110 | struct sigaction sa = {}; | ||
| HITCBC | 703 | 109 | sa.sa_handler = SIG_DFL; | 713 | 110 | sa.sa_handler = SIG_DFL; | ||
| HITCBC | 704 | 109 | sigemptyset(&sa.sa_mask); | 714 | 110 | sigemptyset(&sa.sa_mask); | ||
| HITCBC | 705 | 109 | sa.sa_flags = 0; | 715 | 110 | sa.sa_flags = 0; | ||
| 706 | 716 | |||||||
| HITCBC | 707 | 109 | if (::sigaction(signal_number, &sa, nullptr) < 0 && !first_error) | 717 | 110 | if (::sigaction(signal_number, &sa, nullptr) < 0 && !first_error) | ||
| MISUBC | 708 | ✗ | first_error = make_error_code(std::errc::invalid_argument); | 718 | ✗ | first_error = make_error_code(std::errc::invalid_argument); | ||
| 709 | 719 | |||||||
| 710 | // Clear stored flags | 720 | // Clear stored flags | |||||
| HITCBC | 711 | 109 | state->registered_flags[signal_number] = signal_set::none; | 721 | 110 | state->registered_flags[signal_number] = signal_set::none; | ||
| 712 | } | 722 | } | |||||
| 713 | 723 | |||||||
| HITCBC | 714 | 119 | impl.signals_ = reg->next_in_set; | 724 | 120 | impl.signals_ = reg->next_in_set; | ||
| 715 | 725 | |||||||
| HITCBC | 716 | 119 | if (registrations_[signal_number] == reg) | 726 | 120 | if (registrations_[signal_number] == reg) | ||
| HITCBC | 717 | 119 | registrations_[signal_number] = reg->next_in_table; | 727 | 120 | registrations_[signal_number] = reg->next_in_table; | ||
| HITCBC | 718 | 119 | if (reg->prev_in_table) | 728 | 120 | if (reg->prev_in_table) | ||
| MISUBC | 719 | ✗ | reg->prev_in_table->next_in_table = reg->next_in_table; | 729 | ✗ | reg->prev_in_table->next_in_table = reg->next_in_table; | ||
| HITCBC | 720 | 119 | if (reg->next_in_table) | 730 | 120 | if (reg->next_in_table) | ||
| HITCBC | 721 | 10 | reg->next_in_table->prev_in_table = reg->prev_in_table; | 731 | 10 | reg->next_in_table->prev_in_table = reg->prev_in_table; | ||
| 722 | 732 | |||||||
| HITCBC | 723 | 119 | --state->registration_count[signal_number]; | 733 | 120 | --state->registration_count[signal_number]; | ||
| HITCBC | 724 | 119 | --registration_count_[signal_number]; | 734 | 120 | --registration_count_[signal_number]; | ||
| 725 | 735 | |||||||
| HITCBC | 726 | 119 | delete reg; | 736 | 120 | delete reg; | ||
| HITCBC | 727 | 119 | } | 737 | 120 | } | ||
| 728 | 738 | |||||||
| HITCBC | 729 | 131 | if (first_error) | 739 | 130 | if (first_error) | ||
| MISUBC | 730 | ✗ | return first_error; | 740 | ✗ | return first_error; | ||
| HITCBC | 731 | 131 | return {}; | 741 | 130 | return {}; | ||
| HITCBC | 732 | 131 | } | 742 | 130 | } | ||
| 733 | 743 | |||||||
| 734 | inline void | 744 | inline void | |||||
| HITCBC | 735 | 140 | posix_signal_service::cancel_wait(posix_signal& impl) | 745 | 140 | posix_signal_service::cancel_wait(posix_signal& impl) | ||
| 736 | { | 746 | { | |||||
| HITCBC | 737 | 140 | bool was_waiting = false; | 747 | 140 | bool was_waiting = false; | ||
| HITCBC | 738 | 140 | signal_op* op = nullptr; | 748 | 140 | signal_op* op = nullptr; | ||
| 739 | 749 | |||||||
| 740 | { | 750 | { | |||||
| HITCBC | 741 | 140 | std::lock_guard lock(mutex_); | 751 | 140 | std::lock_guard lock(mutex_); | ||
| HITCBC | 742 | 140 | impl.cancelled_ = true; | 752 | 140 | impl.cancelled_ = true; | ||
| HITCBC | 743 | 140 | if (impl.waiting_) | 753 | 140 | if (impl.waiting_) | ||
| 744 | { | 754 | { | |||||
| HITCBC | 745 | 5 | was_waiting = true; | 755 | 5 | was_waiting = true; | ||
| HITCBC | 746 | 5 | impl.waiting_ = false; | 756 | 5 | impl.waiting_ = false; | ||
| HITCBC | 747 | 5 | op = &impl.pending_op_; | 757 | 5 | op = &impl.pending_op_; | ||
| 748 | } | 758 | } | |||||
| HITCBC | 749 | 140 | } | 759 | 140 | } | ||
| 750 | 760 | |||||||
| HITCBC | 751 | 140 | if (was_waiting) | 761 | 140 | if (was_waiting) | ||
| 752 | { | 762 | { | |||||
| HITCBC | 753 | 5 | if (op->ec_out) | 763 | 5 | if (op->ec_out) | ||
| HITCBC | 754 | 5 | *op->ec_out = make_error_code(capy::error::canceled); | 764 | 5 | *op->ec_out = make_error_code(capy::error::canceled); | ||
| HITCBC | 755 | 5 | if (op->signal_out) | 765 | 5 | if (op->signal_out) | ||
| HITCBC | 756 | 5 | *op->signal_out = 0; | 766 | 5 | *op->signal_out = 0; | ||
| HITCBC | 757 | 5 | op->cont.h = op->h; | 767 | 5 | op->cont.h = op->h; | ||
| HITCBC | 758 | 5 | op->d.post(op->cont); | 768 | 5 | op->d.post(op->cont); | ||
| HITCBC | 759 | 5 | sched_->work_finished(); | 769 | 5 | sched_->work_finished(); | ||
| 760 | } | 770 | } | |||||
| HITCBC | 761 | 140 | } | 771 | 140 | } | ||
| 762 | 772 | |||||||
| 763 | inline void | 773 | inline void | |||||
| HITCBC | 764 | 311 | posix_signal_service::start_wait(posix_signal& impl, signal_op* op) | 774 | 311 | posix_signal_service::start_wait(posix_signal& impl, signal_op* op) | ||
| 765 | { | 775 | { | |||||
| 766 | { | 776 | { | |||||
| HITCBC | 767 | 311 | std::lock_guard lock(mutex_); | 777 | 311 | std::lock_guard lock(mutex_); | ||
| 768 | 778 | |||||||
| 769 | // Check if cancel() was called before this wait started | 779 | // Check if cancel() was called before this wait started | |||||
| HITCBC | 770 | 311 | if (impl.cancelled_) | 780 | 311 | if (impl.cancelled_) | ||
| 771 | { | 781 | { | |||||
| HITCBC | 772 | 2 | impl.cancelled_ = false; | 782 | 2 | impl.cancelled_ = false; | ||
| HITCBC | 773 | 2 | if (op->ec_out) | 783 | 2 | if (op->ec_out) | ||
| HITCBC | 774 | 2 | *op->ec_out = make_error_code(capy::error::canceled); | 784 | 2 | *op->ec_out = make_error_code(capy::error::canceled); | ||
| HITCBC | 775 | 2 | if (op->signal_out) | 785 | 2 | if (op->signal_out) | ||
| HITCBC | 776 | 2 | *op->signal_out = 0; | 786 | 2 | *op->signal_out = 0; | ||
| HITCBC | 777 | 2 | op->cont.h = op->h; | 787 | 2 | op->cont.h = op->h; | ||
| HITCBC | 778 | 2 | op->d.post(op->cont); | 788 | 2 | op->d.post(op->cont); | ||
| HITCBC | 779 | 2 | return; | 789 | 2 | return; | ||
| 780 | } | 790 | } | |||||
| 781 | 791 | |||||||
| 782 | // Check for queued signals first (signal arrived before wait started) | 792 | // Check for queued signals first (signal arrived before wait started) | |||||
| HITCBC | 783 | 309 | signal_registration* reg = impl.signals_; | 793 | 309 | signal_registration* reg = impl.signals_; | ||
| HITCBC | 784 | 622 | while (reg) | 794 | 622 | while (reg) | ||
| 785 | { | 795 | { | |||||
| HITCBC | 786 | 313 | if (reg->undelivered > 0) | 796 | 313 | if (reg->undelivered > 0) | ||
| 787 | { | 797 | { | |||||
| MISUBC | 788 | ✗ | --reg->undelivered; | 798 | ✗ | --reg->undelivered; | ||
| MISUBC | 789 | ✗ | op->signal_number = reg->signal_number; | 799 | ✗ | op->signal_number = reg->signal_number; | ||
| 790 | // svc=nullptr: no work_finished needed since we never called work_started | 800 | // svc=nullptr: no work_finished needed since we never called work_started | |||||
| MISUBC | 791 | ✗ | op->svc = nullptr; | 801 | ✗ | op->svc = nullptr; | ||
| MISUBC | 792 | ✗ | sched_->post(op); | 802 | ✗ | sched_->post(op); | ||
| MISUBC | 793 | ✗ | return; | 803 | ✗ | return; | ||
| 794 | } | 804 | } | |||||
| HITCBC | 795 | 313 | reg = reg->next_in_set; | 805 | 313 | reg = reg->next_in_set; | ||
| 796 | } | 806 | } | |||||
| 797 | 807 | |||||||
| 798 | // No queued signals - wait for delivery | 808 | // No queued signals - wait for delivery | |||||
| HITCBC | 799 | 309 | impl.waiting_ = true; | 809 | 309 | impl.waiting_ = true; | ||
| 800 | // svc=this: signal_op::operator() will call work_finished() to balance this | 810 | // svc=this: signal_op::operator() will call work_finished() to balance this | |||||
| HITCBC | 801 | 309 | op->svc = this; | 811 | 309 | op->svc = this; | ||
| HITCBC | 802 | 309 | sched_->work_started(); | 812 | 309 | sched_->work_started(); | ||
| HITCBC | 803 | 311 | } | 813 | 311 | } | ||
| 804 | } | 814 | } | |||||
| 805 | 815 | |||||||
| 806 | inline void | 816 | inline void | |||||
| HITCBC | 807 | 302 | posix_signal_service::deliver_signal(int signal_number) | 817 | 302 | posix_signal_service::deliver_signal(int signal_number) | ||
| 808 | { | 818 | { | |||||
| HITCBC | 809 | 302 | if (signal_number < 0 || signal_number >= max_signal_number) | 819 | 302 | if (signal_number < 0 || signal_number >= max_signal_number) | ||
| MISUBC | 810 | ✗ | return; | 820 | ✗ | return; | ||
| 811 | 821 | |||||||
| 812 | posix_signal_detail::signal_state* state = | 822 | posix_signal_detail::signal_state* state = | |||||
| HITCBC | 813 | 302 | posix_signal_detail::get_signal_state(); | 823 | 302 | posix_signal_detail::get_signal_state(); | ||
| HITCBC | 814 | 302 | std::lock_guard lock(state->mutex); | 824 | 302 | std::lock_guard lock(state->mutex); | ||
| 815 | 825 | |||||||
| HITCBC | 816 | 302 | posix_signal_service* service = state->service_list; | 826 | 302 | posix_signal_service* service = state->service_list; | ||
| HITCBC | 817 | 604 | while (service) | 827 | 604 | while (service) | ||
| 818 | { | 828 | { | |||||
| HITCBC | 819 | 302 | std::lock_guard svc_lock(service->mutex_); | 829 | 302 | std::lock_guard svc_lock(service->mutex_); | ||
| 820 | 830 | |||||||
| HITCBC | 821 | 302 | signal_registration* reg = service->registrations_[signal_number]; | 831 | 302 | signal_registration* reg = service->registrations_[signal_number]; | ||
| HITCBC | 822 | 606 | while (reg) | 832 | 606 | while (reg) | ||
| 823 | { | 833 | { | |||||
| HITCBC | 824 | 304 | posix_signal* impl = static_cast<posix_signal*>(reg->owner); | 834 | 304 | posix_signal* impl = static_cast<posix_signal*>(reg->owner); | ||
| 825 | 835 | |||||||
| HITCBC | 826 | 304 | if (impl->waiting_) | 836 | 304 | if (impl->waiting_) | ||
| 827 | { | 837 | { | |||||
| HITCBC | 828 | 304 | impl->waiting_ = false; | 838 | 304 | impl->waiting_ = false; | ||
| HITCBC | 829 | 304 | impl->pending_op_.signal_number = signal_number; | 839 | 304 | impl->pending_op_.signal_number = signal_number; | ||
| HITCBC | 830 | 304 | service->post(&impl->pending_op_); | 840 | 304 | service->post(&impl->pending_op_); | ||
| 831 | } | 841 | } | |||||
| 832 | else | 842 | else | |||||
| 833 | { | 843 | { | |||||
| MISUBC | 834 | ✗ | ++reg->undelivered; | 844 | ✗ | ++reg->undelivered; | ||
| 835 | } | 845 | } | |||||
| 836 | 846 | |||||||
| HITCBC | 837 | 304 | reg = reg->next_in_table; | 847 | 304 | reg = reg->next_in_table; | ||
| 838 | } | 848 | } | |||||
| 839 | 849 | |||||||
| HITCBC | 840 | 302 | service = service->next_; | 850 | 302 | service = service->next_; | ||
| HITCBC | 841 | 302 | } | 851 | 302 | } | ||
| HITCBC | 842 | 302 | } | 852 | 302 | } | ||
| 843 | 853 | |||||||
| 844 | inline void | 854 | inline void | |||||
| 845 | posix_signal_service::work_started() noexcept | 855 | posix_signal_service::work_started() noexcept | |||||
| 846 | { | 856 | { | |||||
| 847 | sched_->work_started(); | 857 | sched_->work_started(); | |||||
| 848 | } | 858 | } | |||||
| 849 | 859 | |||||||
| 850 | inline void | 860 | inline void | |||||
| HITCBC | 851 | 304 | posix_signal_service::work_finished() noexcept | 861 | 304 | posix_signal_service::work_finished() noexcept | ||
| 852 | { | 862 | { | |||||
| HITCBC | 853 | 304 | sched_->work_finished(); | 863 | 304 | sched_->work_finished(); | ||
| HITCBC | 854 | 304 | } | 864 | 304 | } | ||
| 855 | 865 | |||||||
| 856 | inline void | 866 | inline void | |||||
| HITCBC | 857 | 304 | posix_signal_service::post(signal_op* op) | 867 | 304 | posix_signal_service::post(signal_op* op) | ||
| 858 | { | 868 | { | |||||
| HITCBC | 859 | 304 | sched_->post(op); | 869 | 304 | sched_->post(op); | ||
| HITCBC | 860 | 304 | } | 870 | 304 | } | ||
| 861 | 871 | |||||||
| 862 | inline void | 872 | inline void | |||||
| HITCBC | 863 | 1533 | posix_signal_service::add_service(posix_signal_service* service) | 873 | 1603 | posix_signal_service::add_service(posix_signal_service* service) | ||
| 864 | { | 874 | { | |||||
| 865 | posix_signal_detail::signal_state* state = | 875 | posix_signal_detail::signal_state* state = | |||||
| HITCBC | 866 | 1533 | posix_signal_detail::get_signal_state(); | 876 | 1603 | posix_signal_detail::get_signal_state(); | ||
| HITCBC | 867 | 1533 | std::lock_guard lock(state->mutex); | 877 | 1603 | std::lock_guard lock(state->mutex); | ||
| 868 | 878 | |||||||
| HITCBC | 869 | 1533 | service->next_ = state->service_list; | 879 | 1603 | service->next_ = state->service_list; | ||
| HITCBC | 870 | 1533 | service->prev_ = nullptr; | 880 | 1603 | service->prev_ = nullptr; | ||
| HITCBC | 871 | 1533 | if (state->service_list) | 881 | 1603 | if (state->service_list) | ||
| HITCBC | 872 | 5 | state->service_list->prev_ = service; | 882 | 5 | state->service_list->prev_ = service; | ||
| HITCBC | 873 | 1533 | state->service_list = service; | 883 | 1603 | state->service_list = service; | ||
| HITCBC | 874 | 1533 | } | 884 | 1603 | } | ||
| 875 | 885 | |||||||
| 876 | inline void | 886 | inline void | |||||
| HITCBC | 877 | 1533 | posix_signal_service::remove_service(posix_signal_service* service) | 887 | 1603 | posix_signal_service::remove_service(posix_signal_service* service) | ||
| 878 | { | 888 | { | |||||
| 879 | posix_signal_detail::signal_state* state = | 889 | posix_signal_detail::signal_state* state = | |||||
| HITCBC | 880 | 1533 | posix_signal_detail::get_signal_state(); | 890 | 1603 | posix_signal_detail::get_signal_state(); | ||
| HITCBC | 881 | 1533 | std::lock_guard lock(state->mutex); | 891 | 1603 | std::lock_guard lock(state->mutex); | ||
| 882 | 892 | |||||||
| HITCBC | 883 | 1533 | if (service->next_ || service->prev_ || state->service_list == service) | 893 | 1603 | if (service->next_ || service->prev_ || state->service_list == service) | ||
| 884 | { | 894 | { | |||||
| HITCBC | 885 | 1533 | if (state->service_list == service) | 895 | 1603 | if (state->service_list == service) | ||
| HITCBC | 886 | 1533 | state->service_list = service->next_; | 896 | 1603 | state->service_list = service->next_; | ||
| HITCBC | 887 | 1533 | if (service->prev_) | 897 | 1603 | if (service->prev_) | ||
| MISUBC | 888 | ✗ | service->prev_->next_ = service->next_; | 898 | ✗ | service->prev_->next_ = service->next_; | ||
| HITCBC | 889 | 1533 | if (service->next_) | 899 | 1603 | if (service->next_) | ||
| HITCBC | 890 | 5 | service->next_->prev_ = service->prev_; | 900 | 5 | service->next_->prev_ = service->prev_; | ||
| HITCBC | 891 | 1533 | service->next_ = nullptr; | 901 | 1603 | service->next_ = nullptr; | ||
| HITCBC | 892 | 1533 | service->prev_ = nullptr; | 902 | 1603 | service->prev_ = nullptr; | ||
| 893 | } | 903 | } | |||||
| HITCBC | 894 | 1533 | } | 904 | 1603 | } | ||
| 895 | 905 | |||||||
| 896 | // get_signal_service - factory function | 906 | // get_signal_service - factory function | |||||
| 897 | 907 | |||||||
| 898 | inline posix_signal_service& | 908 | inline posix_signal_service& | |||||
| HITCBC | 899 | 1533 | get_signal_service(capy::execution_context& ctx, scheduler& sched) | 909 | 1603 | get_signal_service(capy::execution_context& ctx, scheduler& sched) | ||
| 900 | { | 910 | { | |||||
| HITCBC | 901 | 1533 | return ctx.make_service<posix_signal_service>(sched); | 911 | 1603 | return ctx.make_service<posix_signal_service>(sched); | ||
| 902 | } | 912 | } | |||||
| 903 | 913 | |||||||
| 904 | } // namespace detail | 914 | } // namespace detail | |||||
| 905 | } // namespace boost::corosio | 915 | } // namespace boost::corosio | |||||
| 906 | 916 | |||||||
| 907 | #endif // BOOST_COROSIO_POSIX | 917 | #endif // BOOST_COROSIO_POSIX | |||||
| 908 | 918 | |||||||
| 909 | #endif // BOOST_COROSIO_NATIVE_DETAIL_POSIX_POSIX_SIGNAL_SERVICE_HPP | 919 | #endif // BOOST_COROSIO_NATIVE_DETAIL_POSIX_POSIX_SIGNAL_SERVICE_HPP | |||||