Pop a task from the bottom of this thread's queue
| 122 | |
| 123 | // Pop a task from the bottom of this thread's queue |
| 124 | task_run_handle pop() |
| 125 | { |
| 126 | std::size_t b = bottom.load(std::memory_order_relaxed); |
| 127 | |
| 128 | // Early exit if queue is empty |
| 129 | std::size_t t = top.load(std::memory_order_relaxed); |
| 130 | if (to_signed(b - t) <= 0) |
| 131 | return task_run_handle(); |
| 132 | |
| 133 | // Make sure bottom is stored before top is read |
| 134 | bottom.store(--b, std::memory_order_relaxed); |
| 135 | std::atomic_thread_fence(std::memory_order_seq_cst); |
| 136 | t = top.load(std::memory_order_relaxed); |
| 137 | |
| 138 | // If the queue is empty, restore bottom and exit |
| 139 | if (to_signed(b - t) < 0) { |
| 140 | bottom.store(b + 1, std::memory_order_relaxed); |
| 141 | return task_run_handle(); |
| 142 | } |
| 143 | |
| 144 | // Fetch the element from the queue |
| 145 | circular_array* a = array.load(std::memory_order_relaxed); |
| 146 | void* x = a->get(b); |
| 147 | |
| 148 | // If this was the last element in the queue, check for races |
| 149 | if (b == t) { |
| 150 | if (!top.compare_exchange_strong(t, t + 1, std::memory_order_seq_cst, std::memory_order_relaxed)) { |
| 151 | bottom.store(b + 1, std::memory_order_relaxed); |
| 152 | return task_run_handle(); |
| 153 | } |
| 154 | bottom.store(b + 1, std::memory_order_relaxed); |
| 155 | } |
| 156 | return task_run_handle::from_void_ptr(x); |
| 157 | } |
| 158 | |
| 159 | // Steal a task from the top of this thread's queue |
| 160 | task_run_handle steal() |
nothing calls this directly
no test coverage detected