100.00% Lines (48/48) 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   3418439 ready_is_continuation(std::uintptr_t e) noexcept 34   3535734 ready_is_continuation(std::uintptr_t e) noexcept
35   { 35   {
HITCBC 36   3418439 return (e & ready_cont_bit) != 0; 36   3535734 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   3187076 ready_as_op(std::uintptr_t e) noexcept 41   3302141 ready_as_op(std::uintptr_t e) noexcept
42   { 42   {
HITCBC 43   3187076 return std::bit_cast<scheduler_op*>(e & ~ready_cont_bit); 43   3302141 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   96094 ready_as_cont(std::uintptr_t e) noexcept 48   96565 ready_as_cont(std::uintptr_t e) noexcept
49   { 49   {
HITCBC 50   96094 return std::bit_cast<capy::continuation*>(e & ~ready_cont_bit); 50   96565 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")
HITCBC 79   817875 static std::uintptr_t next_of(std::uintptr_t e) noexcept 79   845824 static std::uintptr_t next_of(std::uintptr_t e) noexcept
80   { 80   {
HITCBC 81   817875 if (ready_is_continuation(e)) 81   845824 if (ready_is_continuation(e))
HITCBC 82   24037 return std::bit_cast<std::uintptr_t>(ready_as_cont(e)->reserved); 82   24154 return std::bit_cast<std::uintptr_t>(ready_as_cont(e)->reserved);
HITCBC 83   793838 return ready_as_op(e)->q_next_; 83   821670 return ready_as_op(e)->q_next_;
84   } 84   }
85   85  
HITCBC 86   1331303 static void set_next(std::uintptr_t e, std::uintptr_t nxt) noexcept 86   1376609 static void set_next(std::uintptr_t e, std::uintptr_t nxt) noexcept
87   { 87   {
HITCBC 88   1331303 if (ready_is_continuation(e)) 88   1376609 if (ready_is_continuation(e))
HITCBC 89   48020 ready_as_cont(e)->reserved = std::bit_cast<void*>(nxt); 89   48257 ready_as_cont(e)->reserved = std::bit_cast<void*>(nxt);
90   else 90   else
HITCBC 91   1283283 ready_as_op(e)->q_next_ = nxt; 91   1328352 ready_as_op(e)->q_next_ = nxt;
HITCBC 92   1331303 } 92   1376609 }
93   BOOST_COROSIO_GCC_WARNING_POP 93   BOOST_COROSIO_GCC_WARNING_POP
94   94  
HITCBC 95   817875 void push_entry(std::uintptr_t e) noexcept 95   845824 void push_entry(std::uintptr_t e) noexcept
96   { 96   {
HITCBC 97   817875 set_next(e, 0); 97   845824 set_next(e, 0);
HITCBC 98   817875 if (tail_) 98   845824 if (tail_)
HITCBC 99   376767 set_next(tail_, e); 99   390064 set_next(tail_, e);
100   else 100   else
HITCBC 101   441108 head_ = e; 101   455760 head_ = e;
HITCBC 102   817875 tail_ = e; 102   845824 tail_ = e;
HITCBC 103   817875 } 103   845824 }
104   104  
105   public: 105   public:
HITCBC 106   4096 ready_queue() = default; 106   4101 ready_queue() = default;
107   107  
108   ready_queue(ready_queue&& o) noexcept : head_(o.head_), tail_(o.tail_) 108   ready_queue(ready_queue&& o) noexcept : head_(o.head_), tail_(o.tail_)
109   { 109   {
110   o.head_ = 0; 110   o.head_ = 0;
111   o.tail_ = 0; 111   o.tail_ = 0;
112   } 112   }
113   113  
114   ready_queue(ready_queue const&) = delete; 114   ready_queue(ready_queue const&) = delete;
115   ready_queue& operator=(ready_queue const&) = delete; 115   ready_queue& operator=(ready_queue const&) = delete;
116   ready_queue& operator=(ready_queue&&) = delete; 116   ready_queue& operator=(ready_queue&&) = delete;
117   117  
118   /// Return true if the queue holds no entries. 118   /// Return true if the queue holds no entries.
HITCBC 119   2080766 bool empty() const noexcept 119   2150275 bool empty() const noexcept
120   { 120   {
HITCBC 121   2080766 return head_ == 0; 121   2150275 return head_ == 0;
122   } 122   }
123   123  
124   /// Append a scheduler_op to the back of the queue. 124   /// Append a scheduler_op to the back of the queue.
HITCBC 125   793838 void push(scheduler_op* op) noexcept 125   821670 void push(scheduler_op* op) noexcept
126   { 126   {
HITCBC 127   793838 push_entry(std::bit_cast<std::uintptr_t>(op)); 127   821670 push_entry(std::bit_cast<std::uintptr_t>(op));
HITCBC 128   793838 } 128   821670 }
129   129  
130   /// Append a continuation to the back of the queue. 130   /// Append a continuation to the back of the queue.
HITCBC 131   24037 void push(capy::continuation& c) noexcept 131   24154 void push(capy::continuation& c) noexcept
132   { 132   {
HITCBC 133   24037 push_entry(std::bit_cast<std::uintptr_t>(&c) | ready_cont_bit); 133   24154 push_entry(std::bit_cast<std::uintptr_t>(&c) | ready_cont_bit);
HITCBC 134   24037 } 134   24154 }
135   135  
136   /// Move all entries of @p other to the back in O(1); @p other is emptied. 136   /// Move all entries of @p other to the back in O(1); @p other is emptied.
HITCBC 137   459569 void splice(ready_queue& other) noexcept 137   473233 void splice(ready_queue& other) noexcept
138   { 138   {
HITCBC 139   459569 if (other.empty()) 139   473233 if (other.empty())
HITCBC 140   34656 return; 140   33612 return;
HITCBC 141   424913 if (tail_) 141   439621 if (tail_)
HITCBC 142   136661 set_next(tail_, other.head_); 142   140721 set_next(tail_, other.head_);
143   else 143   else
HITCBC 144   288252 head_ = other.head_; 144   298900 head_ = other.head_;
HITCBC 145   424913 tail_ = other.tail_; 145   439621 tail_ = other.tail_;
HITCBC 146   424913 other.head_ = 0; 146   439621 other.head_ = 0;
HITCBC 147   424913 other.tail_ = 0; 147   439621 other.tail_ = 0;
148   } 148   }
149   149  
150   /// Remove and return the front entry as a tagged value, or 0 when empty. 150   /// Remove and return the front entry as a tagged value, or 0 when empty.
HITCBC 151   1136100 std::uintptr_t pop() noexcept 151   1178381 std::uintptr_t pop() noexcept
152   { 152   {
HITCBC 153   1136100 auto e = head_; 153   1178381 auto e = head_;
HITCBC 154   1136100 if (!e) 154   1178381 if (!e)
HITCBC 155   318225 return 0; 155   332557 return 0;
HITCBC 156   817875 head_ = next_of(e); 156   845824 head_ = next_of(e);
HITCBC 157   817875 if (!head_) 157   845824 if (!head_)
HITCBC 158   304447 tail_ = 0; 158   315039 tail_ = 0;
HITCBC 159   817875 return e; 159   845824 return e;
160   } 160   }
161   }; 161   };
162   162  
163   } // namespace boost::corosio::detail 163   } // namespace boost::corosio::detail
164   164  
165   #endif 165   #endif