|
| 1 | +/** |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + * */ |
| 24 | +#ifndef UNIFIEDCACHE_INFRA_SPSC_RING_QUEUE_H |
| 25 | +#define UNIFIEDCACHE_INFRA_SPSC_RING_QUEUE_H |
| 26 | + |
| 27 | +#include <atomic> |
| 28 | +#include <climits> |
| 29 | +#include <cstddef> |
| 30 | +#include <functional> |
| 31 | +#include <memory> |
| 32 | +#include <thread> |
| 33 | + |
| 34 | +namespace UC { |
| 35 | + |
| 36 | +template <typename T> |
| 37 | +class SpscRingQueue { |
| 38 | + alignas(64) std::atomic<size_t> head_ = 0; |
| 39 | + alignas(64) std::atomic<size_t> tail_ = 0; |
| 40 | + bool pow2_{false}; |
| 41 | + size_t mask_{0}; |
| 42 | + size_t capacity_{0}; |
| 43 | + std::unique_ptr<T[]> buffer_; |
| 44 | + |
| 45 | + size_t Mod(size_t n) { return pow2_ ? (n & mask_) : (n % capacity_); } |
| 46 | + |
| 47 | +public: |
| 48 | + void Setup(size_t capacity) |
| 49 | + { |
| 50 | + capacity_ = capacity; |
| 51 | + mask_ = capacity_ - 1; |
| 52 | + pow2_ = (capacity_ & mask_) == 0; |
| 53 | + buffer_ = std::make_unique<T[]>(capacity_); |
| 54 | + } |
| 55 | + |
| 56 | + void Push(T&& value) |
| 57 | + { |
| 58 | + while (true) { |
| 59 | + const size_t currentHead = head_.load(std::memory_order_relaxed); |
| 60 | + const size_t nextHead = Mod(currentHead + 1); |
| 61 | + if (nextHead != tail_.load(std::memory_order_acquire)) { |
| 62 | + buffer_[currentHead] = std::move(value); |
| 63 | + head_.store(nextHead, std::memory_order_release); |
| 64 | + return; |
| 65 | + } |
| 66 | + std::this_thread::yield(); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + bool TryPush(T&& value) |
| 71 | + { |
| 72 | + const size_t currentHead = head_.load(std::memory_order_relaxed); |
| 73 | + const size_t nextHead = Mod(currentHead + 1); |
| 74 | + const size_t currentTail = tail_.load(std::memory_order_acquire); |
| 75 | + if (nextHead == currentTail) { return false; } |
| 76 | + buffer_[currentHead] = std::move(value); |
| 77 | + head_.store(nextHead, std::memory_order_release); |
| 78 | + return true; |
| 79 | + } |
| 80 | + |
| 81 | + bool TryPop(T& value) |
| 82 | + { |
| 83 | + const size_t currentHead = head_.load(std::memory_order_acquire); |
| 84 | + const size_t currentTail = tail_.load(std::memory_order_relaxed); |
| 85 | + if (currentTail == currentHead) { return false; } |
| 86 | + value = std::move(buffer_[currentTail]); |
| 87 | + tail_.store(Mod(currentTail + 1), std::memory_order_release); |
| 88 | + return true; |
| 89 | + } |
| 90 | + |
| 91 | + template <typename ConsumerHandler, typename... Args> |
| 92 | + void ConsumerLoop(const std::atomic_bool& stop, ConsumerHandler&& handler, Args&&... args) |
| 93 | + { |
| 94 | + constexpr size_t kSpinLimit = 16; |
| 95 | + constexpr size_t kTaskBatch = 64; |
| 96 | + size_t spinCount = 0; |
| 97 | + size_t taskCount = 0; |
| 98 | + T task; |
| 99 | + while (!stop.load(std::memory_order_relaxed)) { |
| 100 | + if (TryPop(task)) { |
| 101 | + spinCount = 0; |
| 102 | + std::invoke(handler, std::forward<Args>(args)..., std::move(task)); |
| 103 | + if (++taskCount % kTaskBatch == 0) { |
| 104 | + if (stop.load(std::memory_order_acquire)) { break; } |
| 105 | + } |
| 106 | + continue; |
| 107 | + } |
| 108 | + if (++spinCount < kSpinLimit) { |
| 109 | + std::this_thread::yield(); |
| 110 | + } else { |
| 111 | + if (stop.load(std::memory_order_acquire)) { break; } |
| 112 | + std::this_thread::sleep_for(std::chrono::microseconds(100)); |
| 113 | + spinCount = 0; |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | +}; |
| 118 | + |
| 119 | +} // namespace UC |
| 120 | + |
| 121 | +#endif |
0 commit comments