100.00% Lines (47/47)
100.00% Functions (12/12)
| TLA | Baseline | Branch | ||||||
|---|---|---|---|---|---|---|---|---|
| Line | Hits | Code | Line | Hits | Code | |||
| 1 | // | 1 | // | |||||
| 2 | // Copyright (c) 2026 Steve Gerbino | 2 | // Copyright (c) 2026 Steve Gerbino | |||||
| 3 | // | 3 | // | |||||
| 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |||||
| 5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | 5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |||||
| 6 | // | 6 | // | |||||
| 7 | // Official repository: https://github.com/cppalliance/corosio | 7 | // Official repository: https://github.com/cppalliance/corosio | |||||
| 8 | // | 8 | // | |||||
| 9 | 9 | |||||||
| 10 | #ifndef BOOST_COROSIO_DETAIL_READY_QUEUE_HPP | 10 | #ifndef BOOST_COROSIO_DETAIL_READY_QUEUE_HPP | |||||
| 11 | #define BOOST_COROSIO_DETAIL_READY_QUEUE_HPP | 11 | #define BOOST_COROSIO_DETAIL_READY_QUEUE_HPP | |||||
| 12 | 12 | |||||||
| 13 | #include <boost/corosio/detail/scheduler_op.hpp> | 13 | #include <boost/corosio/detail/scheduler_op.hpp> | |||||
| 14 | #include <boost/capy/continuation.hpp> | 14 | #include <boost/capy/continuation.hpp> | |||||
| 15 | 15 | |||||||
| 16 | #include <bit> | 16 | #include <bit> | |||||
| 17 | #include <cstdint> | 17 | #include <cstdint> | |||||
| 18 | 18 | |||||||
| 19 | namespace boost::corosio::detail { | 19 | namespace boost::corosio::detail { | |||||
| 20 | 20 | |||||||
| 21 | // A queue entry is a tagged pointer: low bit selects the node kind, the rest | 21 | // A queue entry is a tagged pointer: low bit selects the node kind, the rest | |||||
| 22 | // is the address. We steal a LOW bit (guaranteed zero by alignment), never a | 22 | // is the address. We steal a LOW bit (guaranteed zero by alignment), never a | |||||
| 23 | // high bit (which would depend on a fragile platform canonical-address | 23 | // high bit (which would depend on a fragile platform canonical-address | |||||
| 24 | // assumption). Both node types are >= 8-aligned, so the low 3 bits are free. | 24 | // assumption). Both node types are >= 8-aligned, so the low 3 bits are free. | |||||
| 25 | static_assert(alignof(scheduler_op) >= 2); | 25 | static_assert(alignof(scheduler_op) >= 2); | |||||
| 26 | static_assert(alignof(capy::continuation) >= 2); | 26 | static_assert(alignof(capy::continuation) >= 2); | |||||
| 27 | static_assert(sizeof(void*) == sizeof(std::uintptr_t)); | 27 | static_assert(sizeof(void*) == sizeof(std::uintptr_t)); | |||||
| 28 | static_assert(sizeof(capy::continuation::reserved) >= sizeof(void*)); | 28 | static_assert(sizeof(capy::continuation::reserved) >= sizeof(void*)); | |||||
| 29 | 29 | |||||||
| 30 | inline constexpr std::uintptr_t ready_cont_bit = 1; | 30 | inline constexpr std::uintptr_t ready_cont_bit = 1; | |||||
| 31 | 31 | |||||||
| 32 | /// Return true if a queue entry refers to a continuation (vs a scheduler_op). | 32 | /// Return true if a queue entry refers to a continuation (vs a scheduler_op). | |||||
| 33 | inline bool | 33 | inline bool | |||||
| HITCBC | 34 | 2630912 | ready_is_continuation(std::uintptr_t e) noexcept | 34 | 1073570 | ready_is_continuation(std::uintptr_t e) noexcept | ||
| 35 | { | 35 | { | |||||
| HITCBC | 36 | 2630912 | return (e & ready_cont_bit) != 0; | 36 | 1073570 | return (e & ready_cont_bit) != 0; | ||
| 37 | } | 37 | } | |||||
| 38 | 38 | |||||||
| 39 | /// Recover the scheduler_op from an op-tagged entry. | 39 | /// Recover the scheduler_op from an op-tagged entry. | |||||
| 40 | inline scheduler_op* | 40 | inline scheduler_op* | |||||
| HITCBC | 41 | 2465881 | ready_as_op(std::uintptr_t e) noexcept | 41 | 976830 | ready_as_op(std::uintptr_t e) noexcept | ||
| 42 | { | 42 | { | |||||
| HITCBC | 43 | 2465881 | return std::bit_cast<scheduler_op*>(e & ~ready_cont_bit); | 43 | 976830 | return std::bit_cast<scheduler_op*>(e & ~ready_cont_bit); | ||
| 44 | } | 44 | } | |||||
| 45 | 45 | |||||||
| 46 | /// Recover the continuation from a continuation-tagged entry. | 46 | /// Recover the continuation from a continuation-tagged entry. | |||||
| 47 | inline capy::continuation* | 47 | inline capy::continuation* | |||||
| HITCBC | 48 | 55113 | ready_as_cont(std::uintptr_t e) noexcept | 48 | 48906 | ready_as_cont(std::uintptr_t e) noexcept | ||
| 49 | { | 49 | { | |||||
| HITCBC | 50 | 55113 | return std::bit_cast<capy::continuation*>(e & ~ready_cont_bit); | 50 | 48906 | return std::bit_cast<capy::continuation*>(e & ~ready_cont_bit); | ||
| 51 | } | 51 | } | |||||
| 52 | 52 | |||||||
| 53 | /** A unified intrusive FIFO of scheduler_ops and continuations. | 53 | /** A unified intrusive FIFO of scheduler_ops and continuations. | |||||
| 54 | 54 | |||||||
| 55 | Carries both completion handlers (`scheduler_op`, dispatched via | 55 | Carries both completion handlers (`scheduler_op`, dispatched via | |||||
| 56 | `(*op)()`) and posted coroutine resumptions (`capy::continuation`, | 56 | `(*op)()`) and posted coroutine resumptions (`capy::continuation`, | |||||
| 57 | dispatched via `h.resume()`) in one ordered queue, with no per-entry | 57 | dispatched via `h.resume()`) in one ordered queue, with no per-entry | |||||
| 58 | allocation. The next-link lives in the node: `scheduler_op::q_next_` | 58 | allocation. The next-link lives in the node: `scheduler_op::q_next_` | |||||
| 59 | for ops, `capy::continuation::reserved` for continuations. | 59 | for ops, `capy::continuation::reserved` for continuations. | |||||
| 60 | 60 | |||||||
| 61 | @par Thread Safety | 61 | @par Thread Safety | |||||
| 62 | Not thread-safe; external synchronization required (the schedulers | 62 | Not thread-safe; external synchronization required (the schedulers | |||||
| 63 | hold their dispatch mutex while touching it). | 63 | hold their dispatch mutex while touching it). | |||||
| 64 | */ | 64 | */ | |||||
| 65 | class ready_queue | 65 | class ready_queue | |||||
| 66 | { | 66 | { | |||||
| 67 | std::uintptr_t head_ = 0; // tagged first entry, 0 when empty | 67 | std::uintptr_t head_ = 0; // tagged first entry, 0 when empty | |||||
| 68 | std::uintptr_t tail_ = 0; // tagged last entry, 0 when empty | 68 | std::uintptr_t tail_ = 0; // tagged last entry, 0 when empty | |||||
| 69 | 69 | |||||||
| 70 | // Read a node's next-link by value. A continuation's link lives in its | 70 | // Read a node's next-link by value. A continuation's link lives in its | |||||
| 71 | // void* `reserved` slot; bit_cast keeps us from forming a uintptr_t | 71 | // void* `reserved` slot; bit_cast keeps us from forming a uintptr_t | |||||
| 72 | // lvalue over that void* object (which would violate strict aliasing). | 72 | // lvalue over that void* object (which would violate strict aliasing). | |||||
| 73 | // | 73 | // | |||||
| 74 | // GCC 12/13 false-positive: when inlining proves an entry refers to a | 74 | // GCC 12/13 false-positive: when inlining proves an entry refers to a | |||||
| 75 | // continuation, -Warray-bounds still diagnoses the untaken scheduler_op | 75 | // continuation, -Warray-bounds still diagnoses the untaken scheduler_op | |||||
| 76 | // branch against the smaller object. Fixed in GCC 14. | 76 | // branch against the smaller object. Fixed in GCC 14. | |||||
| 77 | BOOST_COROSIO_GCC_WARNING_PUSH | 77 | BOOST_COROSIO_GCC_WARNING_PUSH | |||||
| 78 | BOOST_COROSIO_GCC_WARNING_DISABLE("-Warray-bounds") | 78 | BOOST_COROSIO_GCC_WARNING_DISABLE("-Warray-bounds") | |||||
| 79 | static std::uintptr_t | 79 | static std::uintptr_t | |||||
| HITCBC | 80 | 627432 | next_of(std::uintptr_t e) noexcept | 80 | 257703 | next_of(std::uintptr_t e) noexcept | ||
| 81 | { | 81 | { | |||||
| HITCBC | 82 | 627432 | if (ready_is_continuation(e)) | 82 | 257703 | if (ready_is_continuation(e)) | ||
| HITCBC | 83 | 13780 | return std::bit_cast<std::uintptr_t>(ready_as_cont(e)->reserved); | 83 | 12230 | return std::bit_cast<std::uintptr_t>(ready_as_cont(e)->reserved); | ||
| HITCBC | 84 | 613652 | return ready_as_op(e)->q_next_; | 84 | 245473 | return ready_as_op(e)->q_next_; | ||
| 85 | } | 85 | } | |||||
| 86 | 86 | |||||||
| 87 | static void | 87 | static void | |||||
| HITCBC | 88 | 1032386 | set_next(std::uintptr_t e, std::uintptr_t nxt) noexcept | 88 | 421967 | set_next(std::uintptr_t e, std::uintptr_t nxt) noexcept | ||
| 89 | { | 89 | { | |||||
| HITCBC | 90 | 1032386 | if (ready_is_continuation(e)) | 90 | 421967 | if (ready_is_continuation(e)) | ||
| HITCBC | 91 | 27553 | ready_as_cont(e)->reserved = std::bit_cast<void*>(nxt); | 91 | 24446 | ready_as_cont(e)->reserved = std::bit_cast<void*>(nxt); | ||
| 92 | else | 92 | else | |||||
| HITCBC | 93 | 1004833 | ready_as_op(e)->q_next_ = nxt; | 93 | 397521 | ready_as_op(e)->q_next_ = nxt; | ||
| HITCBC | 94 | 1032386 | } | 94 | 421967 | } | ||
| 95 | BOOST_COROSIO_GCC_WARNING_POP | 95 | BOOST_COROSIO_GCC_WARNING_POP | |||||
| 96 | 96 | |||||||
| 97 | void | 97 | void | |||||
| HITCBC | 98 | 627432 | push_entry(std::uintptr_t e) noexcept | 98 | 257703 | push_entry(std::uintptr_t e) noexcept | ||
| 99 | { | 99 | { | |||||
| HITCBC | 100 | 627432 | set_next(e, 0); | 100 | 257703 | set_next(e, 0); | ||
| HITCBC | 101 | 627432 | if (tail_) | 101 | 257703 | if (tail_) | ||
| HITCBC | 102 | 279051 | set_next(tail_, e); | 102 | 118932 | set_next(tail_, e); | ||
| 103 | else | 103 | else | |||||
| HITCBC | 104 | 348381 | head_ = e; | 104 | 138771 | head_ = e; | ||
| HITCBC | 105 | 627432 | tail_ = e; | 105 | 257703 | tail_ = e; | ||
| HITCBC | 106 | 627432 | } | 106 | 257703 | } | ||
| 107 | 107 | |||||||
| 108 | public: | 108 | public: | |||||
| HITCBC | 109 | 2147 | ready_queue() = default; | 109 | 2153 | ready_queue() = default; | ||
| 110 | 110 | |||||||
| 111 | ready_queue(ready_queue&& o) noexcept | 111 | ready_queue(ready_queue&& o) noexcept | |||||
| 112 | : head_(o.head_) | 112 | : head_(o.head_) | |||||
| 113 | , tail_(o.tail_) | 113 | , tail_(o.tail_) | |||||
| 114 | { | 114 | { | |||||
| 115 | o.head_ = 0; | 115 | o.head_ = 0; | |||||
| 116 | o.tail_ = 0; | 116 | o.tail_ = 0; | |||||
| 117 | } | 117 | } | |||||
| 118 | 118 | |||||||
| 119 | ready_queue(ready_queue const&) = delete; | 119 | ready_queue(ready_queue const&) = delete; | |||||
| 120 | ready_queue& operator=(ready_queue const&) = delete; | 120 | ready_queue& operator=(ready_queue const&) = delete; | |||||
| 121 | ready_queue& operator=(ready_queue&&) = delete; | 121 | ready_queue& operator=(ready_queue&&) = delete; | |||||
| 122 | 122 | |||||||
| 123 | /// Return true if the queue holds no entries. | 123 | /// Return true if the queue holds no entries. | |||||
| HITCBC | 124 | 1811641 | bool empty() const noexcept { return head_ == 0; } | 124 | 734069 | bool empty() const noexcept { return head_ == 0; } | ||
| 125 | 125 | |||||||
| 126 | /// Append a scheduler_op to the back of the queue. | 126 | /// Append a scheduler_op to the back of the queue. | |||||
| HITCBC | 127 | 613652 | void push(scheduler_op* op) noexcept | 127 | 245473 | void push(scheduler_op* op) noexcept | ||
| 128 | { | 128 | { | |||||
| HITCBC | 129 | 613652 | push_entry(std::bit_cast<std::uintptr_t>(op)); | 129 | 245473 | push_entry(std::bit_cast<std::uintptr_t>(op)); | ||
| HITCBC | 130 | 613652 | } | 130 | 245473 | } | ||
| 131 | 131 | |||||||
| 132 | /// Append a continuation to the back of the queue. | 132 | /// Append a continuation to the back of the queue. | |||||
| HITCBC | 133 | 13780 | void push(capy::continuation& c) noexcept | 133 | 12230 | void push(capy::continuation& c) noexcept | ||
| 134 | { | 134 | { | |||||
| HITCBC | 135 | 13780 | push_entry(std::bit_cast<std::uintptr_t>(&c) | ready_cont_bit); | 135 | 12230 | push_entry(std::bit_cast<std::uintptr_t>(&c) | ready_cont_bit); | ||
| HITCBC | 136 | 13780 | } | 136 | 12230 | } | ||
| 137 | 137 | |||||||
| 138 | /// Move all entries of @p other to the back in O(1); @p other is emptied. | 138 | /// Move all entries of @p other to the back in O(1); @p other is emptied. | |||||
| HITCBC | 139 | 360114 | void splice(ready_queue& other) noexcept | 139 | 143077 | void splice(ready_queue& other) noexcept | ||
| 140 | { | 140 | { | |||||
| HITCBC | 141 | 360114 | if (other.empty()) | 141 | 143077 | if (other.empty()) | ||
| HITCBC | 142 | 28747 | return; | 142 | 14787 | return; | ||
| HITCBC | 143 | 331367 | if (tail_) | 143 | 128290 | if (tail_) | ||
| HITCBC | 144 | 125903 | set_next(tail_, other.head_); | 144 | 45332 | set_next(tail_, other.head_); | ||
| 145 | else | 145 | else | |||||
| HITCBC | 146 | 205464 | head_ = other.head_; | 146 | 82958 | head_ = other.head_; | ||
| HITCBC | 147 | 331367 | tail_ = other.tail_; | 147 | 128290 | tail_ = other.tail_; | ||
| HITCBC | 148 | 331367 | other.head_ = 0; | 148 | 128290 | other.head_ = 0; | ||
| HITCBC | 149 | 331367 | other.tail_ = 0; | 149 | 128290 | other.tail_ = 0; | ||
| 150 | } | 150 | } | |||||
| 151 | 151 | |||||||
| 152 | /// Remove and return the front entry as a tagged value, or 0 when empty. | 152 | /// Remove and return the front entry as a tagged value, or 0 when empty. | |||||
| HITCBC | 153 | 862405 | std::uintptr_t pop() noexcept | 153 | 347295 | std::uintptr_t pop() noexcept | ||
| 154 | { | 154 | { | |||||
| HITCBC | 155 | 862405 | auto e = head_; | 155 | 347295 | auto e = head_; | ||
| HITCBC | 156 | 862405 | if (!e) | 156 | 347295 | if (!e) | ||
| HITCBC | 157 | 234973 | return 0; | 157 | 89592 | return 0; | ||
| HITCBC | 158 | 627432 | head_ = next_of(e); | 158 | 257703 | head_ = next_of(e); | ||
| HITCBC | 159 | 627432 | if (!head_) | 159 | 257703 | if (!head_) | ||
| HITCBC | 160 | 222478 | tail_ = 0; | 160 | 93439 | tail_ = 0; | ||
| HITCBC | 161 | 627432 | return e; | 161 | 257703 | return e; | ||
| 162 | } | 162 | } | |||||
| 163 | }; | 163 | }; | |||||
| 164 | 164 | |||||||
| 165 | } // namespace boost::corosio::detail | 165 | } // namespace boost::corosio::detail | |||||
| 166 | 166 | |||||||
| 167 | #endif | 167 | #endif | |||||