summaryrefslogtreecommitdiffstats
path: root/src/common/bounded_threadsafe_queue.h
blob: e0342753968bf94a769fbde8615156fb0e2759d7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <atomic>
#include <condition_variable>
#include <cstddef>
#include <mutex>
#include <new>

#include "common/polyfill_thread.h"

namespace Common {

namespace detail {
constexpr size_t DefaultCapacity = 0x1000;
} // namespace detail

template <typename T, size_t Capacity = detail::DefaultCapacity>
class SPSCQueue {
    static_assert((Capacity & (Capacity - 1)) == 0, "Capacity must be a power of two.");

public:
    void Push(T&& t) {
        const size_t write_index = m_write_index.load();

        // Wait until we have free slots to write to.
        while ((write_index - m_read_index.load()) == Capacity) {
            std::this_thread::yield();
        }

        // Determine the position to write to.
        const size_t pos = write_index % Capacity;

        // Push into the queue.
        m_data[pos] = std::move(t);

        // Increment the write index.
        ++m_write_index;

        // Notify the consumer that we have pushed into the queue.
        std::scoped_lock lock{cv_mutex};
        cv.notify_one();
    }

    template <typename... Args>
    void Push(Args&&... args) {
        const size_t write_index = m_write_index.load();

        // Wait until we have free slots to write to.
        while ((write_index - m_read_index.load()) == Capacity) {
            std::this_thread::yield();
        }

        // Determine the position to write to.
        const size_t pos = write_index % Capacity;

        // Emplace into the queue.
        std::construct_at(std::addressof(m_data[pos]), std::forward<Args>(args)...);

        // Increment the write index.
        ++m_write_index;

        // Notify the consumer that we have pushed into the queue.
        std::scoped_lock lock{cv_mutex};
        cv.notify_one();
    }

    bool TryPop(T& t) {
        return Pop(t);
    }

    void PopWait(T& t, std::stop_token stop_token) {
        Wait(stop_token);
        Pop(t);
    }

    T PopWait(std::stop_token stop_token) {
        Wait(stop_token);
        T t;
        Pop(t);
        return t;
    }

    void Clear() {
        while (!Empty()) {
            Pop();
        }
    }

    bool Empty() const {
        return m_read_index.load() == m_write_index.load();
    }

    size_t Size() const {
        return m_write_index.load() - m_read_index.load();
    }

private:
    void Pop() {
        const size_t read_index = m_read_index.load();

        // Check if the queue is empty.
        if (read_index == m_write_index.load()) {
            return;
        }

        // Determine the position to read from.
        const size_t pos = read_index % Capacity;

        // Pop the data off the queue, deleting it.
        std::destroy_at(std::addressof(m_data[pos]));

        // Increment the read index.
        ++m_read_index;
    }

    bool Pop(T& t) {
        const size_t read_index = m_read_index.load();

        // Check if the queue is empty.
        if (read_index == m_write_index.load()) {
            return false;
        }

        // Determine the position to read from.
        const size_t pos = read_index % Capacity;

        // Pop the data off the queue, moving it.
        t = std::move(m_data[pos]);

        // Increment the read index.
        ++m_read_index;

        return true;
    }

    void Wait(std::stop_token stop_token) {
        std::unique_lock lock{cv_mutex};
        Common::CondvarWait(cv, lock, stop_token, [this] { return !Empty(); });
    }

    alignas(128) std::atomic_size_t m_read_index{0};
    alignas(128) std::atomic_size_t m_write_index{0};

    std::array<T, Capacity> m_data;

    std::condition_variable_any cv;
    std::mutex cv_mutex;
};

template <typename T, size_t Capacity = detail::DefaultCapacity>
class MPSCQueue {
public:
    void Push(T&& t) {
        std::scoped_lock lock{write_mutex};
        spsc_queue.Push(std::move(t));
    }

    template <typename... Args>
    void Push(Args&&... args) {
        std::scoped_lock lock{write_mutex};
        spsc_queue.Push(std::forward<Args>(args)...);
    }

    bool TryPop(T& t) {
        return spsc_queue.TryPop(t);
    }

    void PopWait(T& t, std::stop_token stop_token) {
        spsc_queue.PopWait(t, stop_token);
    }

    T PopWait(std::stop_token stop_token) {
        return spsc_queue.PopWait(stop_token);
    }

    void Clear() {
        spsc_queue.Clear();
    }

    bool Empty() {
        return spsc_queue.Empty();
    }

    size_t Size() {
        return spsc_queue.Size();
    }

private:
    SPSCQueue<T, Capacity> spsc_queue;
    std::mutex write_mutex;
};

template <typename T, size_t Capacity = detail::DefaultCapacity>
class MPMCQueue {
public:
    void Push(T&& t) {
        std::scoped_lock lock{write_mutex};
        spsc_queue.Push(std::move(t));
    }

    template <typename... Args>
    void Push(Args&&... args) {
        std::scoped_lock lock{write_mutex};
        spsc_queue.Push(std::forward<Args>(args)...);
    }

    bool TryPop(T& t) {
        std::scoped_lock lock{read_mutex};
        return spsc_queue.TryPop(t);
    }

    void PopWait(T& t, std::stop_token stop_token) {
        std::scoped_lock lock{read_mutex};
        spsc_queue.PopWait(t, stop_token);
    }

    T PopWait(std::stop_token stop_token) {
        std::scoped_lock lock{read_mutex};
        return spsc_queue.PopWait(stop_token);
    }

    void Clear() {
        std::scoped_lock lock{read_mutex};
        spsc_queue.Clear();
    }

    bool Empty() {
        std::scoped_lock lock{read_mutex};
        return spsc_queue.Empty();
    }

    size_t Size() {
        std::scoped_lock lock{read_mutex};
        return spsc_queue.Size();
    }

private:
    SPSCQueue<T, Capacity> spsc_queue;
    std::mutex write_mutex;
    std::mutex read_mutex;
};

} // namespace Common