diff options
| author | 2021-04-06 04:23:02 +0200 | |
|---|---|---|
| committer | 2021-07-08 19:03:26 -0300 | |
| commit | a10e112e6436b30c9eb5ca2a82c94f83205bbc34 (patch) | |
| tree | c7f0a0fdbcd0d9db3e059af4491dac6a74eb6d8f /src/common/thread_worker.cpp | |
| parent | common/thread_worker: Use unique function (diff) | |
| download | yuzu-a10e112e6436b30c9eb5ca2a82c94f83205bbc34.tar.gz yuzu-a10e112e6436b30c9eb5ca2a82c94f83205bbc34.tar.xz yuzu-a10e112e6436b30c9eb5ca2a82c94f83205bbc34.zip | |
common/thread_worker: Fix data race
Diffstat (limited to 'src/common/thread_worker.cpp')
| -rw-r--r-- | src/common/thread_worker.cpp | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/src/common/thread_worker.cpp b/src/common/thread_worker.cpp index f4d8bb0f0..fd130dfb4 100644 --- a/src/common/thread_worker.cpp +++ b/src/common/thread_worker.cpp | |||
| @@ -8,9 +8,17 @@ | |||
| 8 | namespace Common { | 8 | namespace Common { |
| 9 | 9 | ||
| 10 | ThreadWorker::ThreadWorker(std::size_t num_workers, const std::string& name) { | 10 | ThreadWorker::ThreadWorker(std::size_t num_workers, const std::string& name) { |
| 11 | workers_queued.store(static_cast<u64>(num_workers), std::memory_order_release); | ||
| 11 | const auto lambda = [this, thread_name{std::string{name}}] { | 12 | const auto lambda = [this, thread_name{std::string{name}}] { |
| 12 | Common::SetCurrentThreadName(thread_name.c_str()); | 13 | Common::SetCurrentThreadName(thread_name.c_str()); |
| 13 | 14 | ||
| 15 | // TODO(Blinkhawk): Change the design, this is very prone to data races | ||
| 16 | // Wait for first request | ||
| 17 | { | ||
| 18 | std::unique_lock lock{queue_mutex}; | ||
| 19 | condition.wait(lock, [this] { return stop || !requests.empty(); }); | ||
| 20 | } | ||
| 21 | |||
| 14 | while (!stop) { | 22 | while (!stop) { |
| 15 | UniqueFunction<void> task; | 23 | UniqueFunction<void> task; |
| 16 | { | 24 | { |
| @@ -26,7 +34,9 @@ ThreadWorker::ThreadWorker(std::size_t num_workers, const std::string& name) { | |||
| 26 | requests.pop(); | 34 | requests.pop(); |
| 27 | } | 35 | } |
| 28 | task(); | 36 | task(); |
| 37 | work_done++; | ||
| 29 | } | 38 | } |
| 39 | workers_stopped++; | ||
| 30 | wait_condition.notify_all(); | 40 | wait_condition.notify_all(); |
| 31 | }; | 41 | }; |
| 32 | for (size_t i = 0; i < num_workers; ++i) { | 42 | for (size_t i = 0; i < num_workers; ++i) { |
| @@ -49,13 +59,15 @@ void ThreadWorker::QueueWork(UniqueFunction<void> work) { | |||
| 49 | { | 59 | { |
| 50 | std::unique_lock lock{queue_mutex}; | 60 | std::unique_lock lock{queue_mutex}; |
| 51 | requests.emplace(std::move(work)); | 61 | requests.emplace(std::move(work)); |
| 62 | work_scheduled++; | ||
| 52 | } | 63 | } |
| 53 | condition.notify_one(); | 64 | condition.notify_one(); |
| 54 | } | 65 | } |
| 55 | 66 | ||
| 56 | void ThreadWorker::WaitForRequests() { | 67 | void ThreadWorker::WaitForRequests() { |
| 57 | std::unique_lock lock{queue_mutex}; | 68 | std::unique_lock lock{queue_mutex}; |
| 58 | wait_condition.wait(lock, [this] { return stop || requests.empty(); }); | 69 | wait_condition.wait( |
| 70 | lock, [this] { return workers_stopped >= workers_queued || work_done >= work_scheduled; }); | ||
| 59 | } | 71 | } |
| 60 | 72 | ||
| 61 | } // namespace Common | 73 | } // namespace Common |