The open source OpenXR runtime
1/*
2Copyright (c) 2020 Erik Rigtorp <erik@rigtorp.se>
3
4Permission is hereby granted, free of charge, to any person obtaining a copy
5of this software and associated documentation files (the "Software"), to deal
6in the Software without restriction, including without limitation the rights
7to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8copies of the Software, and to permit persons to whom the Software is
9furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall be included in all
12copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20SOFTWARE.
21 */
22
23#pragma once
24
25#include <atomic>
26#include <cassert>
27#include <cstddef>
28#include <stdexcept>
29#include <type_traits> // std::enable_if, std::is_*_constructible
30
31#include "../common/TracyAlloc.hpp"
32
33#if defined (_MSC_VER)
34#pragma warning(push)
35#pragma warning(disable:4324)
36#endif
37
38namespace tracy {
39
40template <typename T> class SPSCQueue {
41public:
42 explicit SPSCQueue(const size_t capacity)
43 : capacity_(capacity) {
44 capacity_++; // Needs one slack element
45 slots_ = (T*)tracy_malloc(sizeof(T) * (capacity_ + 2 * kPadding));
46
47 static_assert(alignof(SPSCQueue<T>) == kCacheLineSize, "");
48 static_assert(sizeof(SPSCQueue<T>) >= 3 * kCacheLineSize, "");
49 assert(reinterpret_cast<char *>(&readIdx_) -
50 reinterpret_cast<char *>(&writeIdx_) >=
51 static_cast<std::ptrdiff_t>(kCacheLineSize));
52 }
53
54 ~SPSCQueue() {
55 while (front()) {
56 pop();
57 }
58 tracy_free(slots_);
59 }
60
61 // non-copyable and non-movable
62 SPSCQueue(const SPSCQueue &) = delete;
63 SPSCQueue &operator=(const SPSCQueue &) = delete;
64
65 template <typename... Args>
66 void emplace(Args &&...args) noexcept(
67 std::is_nothrow_constructible<T, Args &&...>::value) {
68 static_assert(std::is_constructible<T, Args &&...>::value,
69 "T must be constructible with Args&&...");
70 auto const writeIdx = writeIdx_.load(std::memory_order_relaxed);
71 auto nextWriteIdx = writeIdx + 1;
72 if (nextWriteIdx == capacity_) {
73 nextWriteIdx = 0;
74 }
75 while (nextWriteIdx == readIdxCache_) {
76 readIdxCache_ = readIdx_.load(std::memory_order_acquire);
77 }
78 new (&slots_[writeIdx + kPadding]) T(std::forward<Args>(args)...);
79 writeIdx_.store(nextWriteIdx, std::memory_order_release);
80 }
81
82 T *front() noexcept {
83 auto const readIdx = readIdx_.load(std::memory_order_relaxed);
84 if (readIdx == writeIdxCache_) {
85 writeIdxCache_ = writeIdx_.load(std::memory_order_acquire);
86 if (writeIdxCache_ == readIdx) {
87 return nullptr;
88 }
89 }
90 return &slots_[readIdx + kPadding];
91 }
92
93 void pop() noexcept {
94 static_assert(std::is_nothrow_destructible<T>::value,
95 "T must be nothrow destructible");
96 auto const readIdx = readIdx_.load(std::memory_order_relaxed);
97 assert(writeIdx_.load(std::memory_order_acquire) != readIdx);
98 slots_[readIdx + kPadding].~T();
99 auto nextReadIdx = readIdx + 1;
100 if (nextReadIdx == capacity_) {
101 nextReadIdx = 0;
102 }
103 readIdx_.store(nextReadIdx, std::memory_order_release);
104 }
105
106 size_t size() const noexcept {
107 std::ptrdiff_t diff = writeIdx_.load(std::memory_order_acquire) -
108 readIdx_.load(std::memory_order_acquire);
109 if (diff < 0) {
110 diff += capacity_;
111 }
112 return static_cast<size_t>(diff);
113 }
114
115 bool empty() const noexcept {
116 return writeIdx_.load(std::memory_order_acquire) ==
117 readIdx_.load(std::memory_order_acquire);
118 }
119
120 size_t capacity() const noexcept { return capacity_ - 1; }
121
122private:
123 static constexpr size_t kCacheLineSize = 64;
124
125 // Padding to avoid false sharing between slots_ and adjacent allocations
126 static constexpr size_t kPadding = (kCacheLineSize - 1) / sizeof(T) + 1;
127
128private:
129 size_t capacity_;
130 T *slots_;
131
132 // Align to cache line size in order to avoid false sharing
133 // readIdxCache_ and writeIdxCache_ is used to reduce the amount of cache
134 // coherency traffic
135 alignas(kCacheLineSize) std::atomic<size_t> writeIdx_ = {0};
136 alignas(kCacheLineSize) size_t readIdxCache_ = 0;
137 alignas(kCacheLineSize) std::atomic<size_t> readIdx_ = {0};
138 alignas(kCacheLineSize) size_t writeIdxCache_ = 0;
139
140 // Padding to avoid adjacent allocations to share cache line with
141 // writeIdxCache_
142 char padding_[kCacheLineSize - sizeof(SPSCQueue<T>::writeIdxCache_)];
143};
144} // namespace rigtorp
145
146#if defined (_MSC_VER)
147#pragma warning(pop)
148#endif