mirror of
https://git.h3cjp.net/H3cJP/yuzu.git
synced 2024-11-15 07:03:08 +00:00
common/thread_worker: Use unique function
This commit is contained in:
parent
2c8d337418
commit
bf5b5c1bf4
|
@ -8,36 +8,30 @@
|
||||||
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) {
|
||||||
for (std::size_t i = 0; i < num_workers; ++i)
|
const auto lambda = [this, thread_name{std::string{name}}] {
|
||||||
threads.emplace_back([this, thread_name{std::string{name}}] {
|
|
||||||
Common::SetCurrentThreadName(thread_name.c_str());
|
Common::SetCurrentThreadName(thread_name.c_str());
|
||||||
|
|
||||||
// Wait for first request
|
while (!stop) {
|
||||||
|
UniqueFunction<void> task;
|
||||||
{
|
{
|
||||||
std::unique_lock lock{queue_mutex};
|
std::unique_lock lock{queue_mutex};
|
||||||
condition.wait(lock, [this] { return stop || !requests.empty(); });
|
if (requests.empty()) {
|
||||||
|
wait_condition.notify_all();
|
||||||
}
|
}
|
||||||
|
|
||||||
while (true) {
|
|
||||||
std::function<void()> task;
|
|
||||||
|
|
||||||
{
|
|
||||||
std::unique_lock lock{queue_mutex};
|
|
||||||
condition.wait(lock, [this] { return stop || !requests.empty(); });
|
condition.wait(lock, [this] { return stop || !requests.empty(); });
|
||||||
if (stop || requests.empty()) {
|
if (stop || requests.empty()) {
|
||||||
return;
|
break;
|
||||||
}
|
}
|
||||||
task = std::move(requests.front());
|
task = std::move(requests.front());
|
||||||
requests.pop();
|
requests.pop();
|
||||||
|
|
||||||
if (requests.empty()) {
|
|
||||||
wait_condition.notify_one();
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
task();
|
task();
|
||||||
}
|
}
|
||||||
});
|
wait_condition.notify_all();
|
||||||
|
};
|
||||||
|
for (size_t i = 0; i < num_workers; ++i) {
|
||||||
|
threads.emplace_back(lambda);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ThreadWorker::~ThreadWorker() {
|
ThreadWorker::~ThreadWorker() {
|
||||||
|
@ -51,10 +45,10 @@ ThreadWorker::~ThreadWorker() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ThreadWorker::QueueWork(std::function<void()>&& work) {
|
void ThreadWorker::QueueWork(UniqueFunction<void> work) {
|
||||||
{
|
{
|
||||||
std::unique_lock lock{queue_mutex};
|
std::unique_lock lock{queue_mutex};
|
||||||
requests.emplace(work);
|
requests.emplace(std::move(work));
|
||||||
}
|
}
|
||||||
condition.notify_one();
|
condition.notify_one();
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,18 +11,20 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <queue>
|
#include <queue>
|
||||||
|
|
||||||
|
#include "common/unique_function.h"
|
||||||
|
|
||||||
namespace Common {
|
namespace Common {
|
||||||
|
|
||||||
class ThreadWorker final {
|
class ThreadWorker final {
|
||||||
public:
|
public:
|
||||||
explicit ThreadWorker(std::size_t num_workers, const std::string& name);
|
explicit ThreadWorker(std::size_t num_workers, const std::string& name);
|
||||||
~ThreadWorker();
|
~ThreadWorker();
|
||||||
void QueueWork(std::function<void()>&& work);
|
void QueueWork(UniqueFunction<void> work);
|
||||||
void WaitForRequests();
|
void WaitForRequests();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<std::thread> threads;
|
std::vector<std::thread> threads;
|
||||||
std::queue<std::function<void()>> requests;
|
std::queue<UniqueFunction<void>> requests;
|
||||||
std::mutex queue_mutex;
|
std::mutex queue_mutex;
|
||||||
std::condition_variable condition;
|
std::condition_variable condition;
|
||||||
std::condition_variable wait_condition;
|
std::condition_variable wait_condition;
|
||||||
|
|
Loading…
Reference in a new issue