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
|
// Copyright 2017 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include <atomic>
#include <functional>
#include <mutex>
#include <string>
#include <thread>
#include "Common/Event.h"
#include "Common/SPSCQueue.h"
#include "Common/Thread.h"
// A thread that executes the given function for every item placed into its queue.
namespace Common
{
namespace detail
{
template <typename T, bool IsSingleProducer>
class WorkQueueThreadBase final
{
public:
WorkQueueThreadBase() = default;
WorkQueueThreadBase(std::string name, std::function<void(T)> function)
{
Reset(std::move(name), std::move(function));
}
~WorkQueueThreadBase() { Shutdown(); }
// Shuts the current work thread down (if any) and starts a new thread with the given function
// Note: Some consumers of this API push items to the queue before starting the thread.
void Reset(std::string name, std::function<void(T)> function)
{
auto lg = GetLockGuard();
Shutdown();
m_run_thread.store(true, std::memory_order_relaxed);
m_thread = std::thread(std::bind_front(&WorkQueueThreadBase::ThreadLoop, this), std::move(name),
std::move(function));
}
// Adds an item to the work queue
template <typename... Args>
void EmplaceItem(Args&&... args)
{
auto lg = GetLockGuard();
m_items.Emplace(std::forward<Args>(args)...);
m_event.Set();
}
void Push(T&& item) { EmplaceItem(std::move(item)); }
void Push(const T& item) { EmplaceItem(item); }
// Empties the queue, skipping all work.
// Blocks until the current work is cancelled.
void Cancel()
{
auto lg = GetLockGuard();
if (IsRunning())
{
m_skip_work.store(true, std::memory_order_relaxed);
WaitForCompletion();
m_skip_work.store(false, std::memory_order_relaxed);
}
else
{
m_items.Clear();
}
}
// Tells the worker thread to stop when its queue is empty.
// Blocks until the worker thread exits. Does nothing if thread isn't running.
void Shutdown() { StopThread(true); }
// Tells the worker thread to stop immediately, potentially leaving work in the queue.
// Blocks until the worker thread exits. Does nothing if thread isn't running.
void Stop() { StopThread(false); }
// Stops the worker thread ASAP and empties the queue.
void StopAndCancel()
{
auto lg = GetLockGuard();
Stop();
Cancel();
}
// Blocks until all items in the queue have been processed (or cancelled)
// Does nothing if thread isn't running.
void WaitForCompletion()
{
auto lg = GetLockGuard();
if (IsRunning())
m_items.WaitForEmpty();
}
private:
void StopThread(bool wait_for_completion)
{
auto lg = GetLockGuard();
if (wait_for_completion)
WaitForCompletion();
if (m_run_thread.exchange(false, std::memory_order_relaxed))
{
m_event.Set();
m_thread.join();
}
}
auto GetLockGuard()
{
struct DummyLockGuard
{
// Silences unused variable warning.
~DummyLockGuard() { void(); }
};
if constexpr (IsSingleProducer)
return DummyLockGuard{};
else
return std::lock_guard{m_mutex};
}
bool IsRunning() { return m_thread.joinable(); }
void ThreadLoop(const std::string& thread_name, const std::function<void(T)>& function)
{
Common::SetCurrentThreadName(thread_name.c_str());
while (m_run_thread.load(std::memory_order_relaxed))
{
if (m_items.Empty())
{
m_event.Wait();
continue;
}
if (m_skip_work.load(std::memory_order_relaxed))
{
m_items.Clear();
continue;
}
function(std::move(m_items.Front()));
m_items.Pop();
}
}
std::thread m_thread;
Common::WaitableSPSCQueue<T> m_items;
Common::Event m_event;
std::atomic_bool m_skip_work = false;
std::atomic_bool m_run_thread = false;
using DummyMutex = std::type_identity<void>;
using ProducerMutex = std::conditional_t<IsSingleProducer, DummyMutex, std::recursive_mutex>;
ProducerMutex m_mutex;
};
} // namespace detail
// Multiple threads may use the public interface.
template <typename T>
using WorkQueueThread = detail::WorkQueueThreadBase<T, false>;
// A "Single Producer" WorkQueueThread.
// It uses no mutex but only one thread can safely manipulate the queue.
template <typename T>
using WorkQueueThreadSP = detail::WorkQueueThreadBase<T, true>;
} // namespace Common
|