mirror of
https://git.h3cjp.net/H3cJP/citra.git
synced 2024-12-12 13:55:14 +00:00
common/thread_worker: Fix data race
This commit is contained in:
parent
bf5b5c1bf4
commit
a10e112e64
|
@ -8,9 +8,17 @@
|
||||||
namespace Common {
|
namespace Common {
|
||||||
|
|
||||||
ThreadWorker::ThreadWorker(std::size_t num_workers, const std::string& name) {
|
ThreadWorker::ThreadWorker(std::size_t num_workers, const std::string& name) {
|
||||||
|
workers_queued.store(static_cast<u64>(num_workers), std::memory_order_release);
|
||||||
const auto lambda = [this, thread_name{std::string{name}}] {
|
const auto lambda = [this, thread_name{std::string{name}}] {
|
||||||
Common::SetCurrentThreadName(thread_name.c_str());
|
Common::SetCurrentThreadName(thread_name.c_str());
|
||||||
|
|
||||||
|
// TODO(Blinkhawk): Change the design, this is very prone to data races
|
||||||
|
// Wait for first request
|
||||||
|
{
|
||||||
|
std::unique_lock lock{queue_mutex};
|
||||||
|
condition.wait(lock, [this] { return stop || !requests.empty(); });
|
||||||
|
}
|
||||||
|
|
||||||
while (!stop) {
|
while (!stop) {
|
||||||
UniqueFunction<void> task;
|
UniqueFunction<void> task;
|
||||||
{
|
{
|
||||||
|
@ -26,7 +34,9 @@ ThreadWorker::ThreadWorker(std::size_t num_workers, const std::string& name) {
|
||||||
requests.pop();
|
requests.pop();
|
||||||
}
|
}
|
||||||
task();
|
task();
|
||||||
|
work_done++;
|
||||||
}
|
}
|
||||||
|
workers_stopped++;
|
||||||
wait_condition.notify_all();
|
wait_condition.notify_all();
|
||||||
};
|
};
|
||||||
for (size_t i = 0; i < num_workers; ++i) {
|
for (size_t i = 0; i < num_workers; ++i) {
|
||||||
|
@ -49,13 +59,15 @@ void ThreadWorker::QueueWork(UniqueFunction<void> work) {
|
||||||
{
|
{
|
||||||
std::unique_lock lock{queue_mutex};
|
std::unique_lock lock{queue_mutex};
|
||||||
requests.emplace(std::move(work));
|
requests.emplace(std::move(work));
|
||||||
|
work_scheduled++;
|
||||||
}
|
}
|
||||||
condition.notify_one();
|
condition.notify_one();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ThreadWorker::WaitForRequests() {
|
void ThreadWorker::WaitForRequests() {
|
||||||
std::unique_lock lock{queue_mutex};
|
std::unique_lock lock{queue_mutex};
|
||||||
wait_condition.wait(lock, [this] { return stop || requests.empty(); });
|
wait_condition.wait(
|
||||||
|
lock, [this] { return workers_stopped >= workers_queued || work_done >= work_scheduled; });
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace Common
|
} // namespace Common
|
||||||
|
|
|
@ -11,6 +11,7 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <queue>
|
#include <queue>
|
||||||
|
|
||||||
|
#include "common/common_types.h"
|
||||||
#include "common/unique_function.h"
|
#include "common/unique_function.h"
|
||||||
|
|
||||||
namespace Common {
|
namespace Common {
|
||||||
|
@ -29,6 +30,10 @@ private:
|
||||||
std::condition_variable condition;
|
std::condition_variable condition;
|
||||||
std::condition_variable wait_condition;
|
std::condition_variable wait_condition;
|
||||||
std::atomic_bool stop{};
|
std::atomic_bool stop{};
|
||||||
|
std::atomic<u64> work_scheduled{};
|
||||||
|
std::atomic<u64> work_done{};
|
||||||
|
std::atomic<u64> workers_stopped{};
|
||||||
|
std::atomic<u64> workers_queued{};
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Common
|
} // namespace Common
|
||||||
|
|
Loading…
Reference in a new issue