WorkQueueThread: provide name and function at same time

This commit is contained in:
Scott Mansell 2023-02-04 15:56:27 +13:00
parent 6594532f10
commit 7c4fcc30a3
11 changed files with 16 additions and 15 deletions

View File

@ -39,7 +39,7 @@ void CubebStream::StateCallback(cubeb_stream* stream, void* user_data, cubeb_sta
CubebStream::CubebStream() CubebStream::CubebStream()
#ifdef _WIN32 #ifdef _WIN32
: m_work_queue([](const std::function<void()>& func) { func(); }) : m_work_queue("Cubeb Worker", [](const std::function<void()>& func) { func(); })
{ {
Common::Event sync_event; Common::Event sync_event;
m_work_queue.EmplaceItem([this, &sync_event] { m_work_queue.EmplaceItem([this, &sync_event] {

View File

@ -20,19 +20,20 @@ template <typename T>
class WorkQueueThread class WorkQueueThread
{ {
public: public:
WorkQueueThread(std::string name) : m_thread_name(name){}; WorkQueueThread() = default;
WorkQueueThread(std::function<void(T)> function, std::string name) : m_thread_name(name) WorkQueueThread(const std::string name, std::function<void(T)> function)
{ {
Reset(std::move(function)); Reset(std::move(name), std::move(function));
} }
~WorkQueueThread() { Shutdown(); } ~WorkQueueThread() { Shutdown(); }
// Shuts the current work thread down (if any) and starts a new thread with the given function // 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. // Note: Some consumers of this API push items to the queue before starting the thread.
void Reset(std::function<void(T)> function) void Reset(const std::string& name, std::function<void(T)> function)
{ {
Shutdown(); Shutdown();
std::lock_guard lg(m_lock); std::lock_guard lg(m_lock);
m_thread_name = std::move(name);
m_shutdown = false; m_shutdown = false;
m_function = std::move(function); m_function = std::move(function);
m_thread = std::thread(&WorkQueueThread::ThreadLoop, this); m_thread = std::thread(&WorkQueueThread::ThreadLoop, this);

View File

@ -200,7 +200,7 @@ CEXIMic::CEXIMic(int index)
: slot(index) : slot(index)
#ifdef _WIN32 #ifdef _WIN32
, ,
m_work_queue([](const std::function<void()>& func) { func(); }, "Mic Worker") m_work_queue("Mic Worker", [](const std::function<void()>& func) { func(); })
#endif #endif
{ {
m_position = 0; m_position = 0;

View File

@ -102,7 +102,7 @@ private:
int samples_avail; int samples_avail;
#ifdef _WIN32 #ifdef _WIN32
Common::WorkQueueThread<std::function<void()>> m_work_queue{"Mic Worker"}; Common::WorkQueueThread<std::function<void()>> m_work_queue;
bool m_coinit_success = false; bool m_coinit_success = false;
bool m_should_couninit = false; bool m_should_couninit = false;
#endif #endif

View File

@ -65,7 +65,7 @@ enum SOResultCode : s32
NetIPTopDevice::NetIPTopDevice(Kernel& ios, const std::string& device_name) NetIPTopDevice::NetIPTopDevice(Kernel& ios, const std::string& device_name)
: Device(ios, device_name) : Device(ios, device_name)
{ {
m_work_queue.Reset([this](AsyncTask task) { m_work_queue.Reset("Network Worker", [this](AsyncTask task) {
const IPCReply reply = task.handler(); const IPCReply reply = task.handler();
{ {
std::lock_guard lg(m_async_reply_lock); std::lock_guard lg(m_async_reply_lock);

View File

@ -120,7 +120,7 @@ private:
IPCReply HandleICMPPingRequest(const IOCtlVRequest& request); IPCReply HandleICMPPingRequest(const IOCtlVRequest& request);
Common::SocketContext m_socket_context; Common::SocketContext m_socket_context;
Common::WorkQueueThread<AsyncTask> m_work_queue{"Network Worker"}; Common::WorkQueueThread<AsyncTask> m_work_queue;
std::mutex m_async_reply_lock; std::mutex m_async_reply_lock;
std::queue<AsyncReply> m_async_replies; std::queue<AsyncReply> m_async_replies;
}; };

View File

@ -150,7 +150,7 @@ s32 NWC24MakeUserID(u64* nwc24_id, u32 hollywood_id, u16 id_ctr, HardwareModel h
NetKDRequestDevice::NetKDRequestDevice(Kernel& ios, const std::string& device_name) NetKDRequestDevice::NetKDRequestDevice(Kernel& ios, const std::string& device_name)
: Device(ios, device_name), config{ios.GetFS()}, m_dl_list{ios.GetFS()} : Device(ios, device_name), config{ios.GetFS()}, m_dl_list{ios.GetFS()}
{ {
m_work_queue.Reset([this](AsyncTask task) { m_work_queue.Reset("WiiConnect24 Worker", [this](AsyncTask task) {
const IPCReply reply = task.handler(); const IPCReply reply = task.handler();
{ {
std::lock_guard lg(m_async_reply_lock); std::lock_guard lg(m_async_reply_lock);

View File

@ -54,7 +54,7 @@ private:
NWC24::NWC24Config config; NWC24::NWC24Config config;
NWC24::NWC24Dl m_dl_list; NWC24::NWC24Dl m_dl_list;
Common::WorkQueueThread<AsyncTask> m_work_queue{"WiiConnect24 Worker"}; Common::WorkQueueThread<AsyncTask> m_work_queue;
std::mutex m_async_reply_lock; std::mutex m_async_reply_lock;
std::queue<AsyncReply> m_async_replies; std::queue<AsyncReply> m_async_replies;
// TODO: Maybe move away from Common::HttpRequest? // TODO: Maybe move away from Common::HttpRequest?

View File

@ -85,7 +85,7 @@ struct CompressAndDumpState_args
static std::mutex s_save_thread_mutex; static std::mutex s_save_thread_mutex;
// Queue for compressing and writing savestates to disk. // Queue for compressing and writing savestates to disk.
static Common::WorkQueueThread<CompressAndDumpState_args> s_save_thread("Savestate Worker"); static Common::WorkQueueThread<CompressAndDumpState_args> s_save_thread;
// Keeps track of savestate writes that are currently happening, so we don't load a state while // Keeps track of savestate writes that are currently happening, so we don't load a state while
// another one is still saving. This is particularly important so if you save to a slot and then // another one is still saving. This is particularly important so if you save to a slot and then
@ -724,7 +724,7 @@ void Init()
if (lzo_init() != LZO_E_OK) if (lzo_init() != LZO_E_OK)
PanicAlertFmtT("Internal LZO Error - lzo_init() failed"); PanicAlertFmtT("Internal LZO Error - lzo_init() failed");
s_save_thread.Reset([](CompressAndDumpState_args args) { s_save_thread.Reset("Savestate Worker", [](CompressAndDumpState_args args) {
CompressAndDumpState(args); CompressAndDumpState(args);
{ {

View File

@ -55,7 +55,7 @@ GameTracker::GameTracker(QObject* parent) : QFileSystemWatcher(parent)
m_load_thread.EmplaceItem(Command{CommandType::UpdateMetadata, {}}); m_load_thread.EmplaceItem(Command{CommandType::UpdateMetadata, {}});
}); });
m_load_thread.Reset([this](Command command) { m_load_thread.Reset("GameList Tracker", [this](Command command) {
switch (command.type) switch (command.type)
{ {
case CommandType::LoadCache: case CommandType::LoadCache:

View File

@ -87,7 +87,7 @@ private:
// game path -> directories that track it // game path -> directories that track it
QMap<QString, QSet<QString>> m_tracked_files; QMap<QString, QSet<QString>> m_tracked_files;
QVector<QString> m_tracked_paths; QVector<QString> m_tracked_paths;
Common::WorkQueueThread<Command> m_load_thread{"GameList Tracker"}; Common::WorkQueueThread<Command> m_load_thread;
UICommon::GameFileCache m_cache; UICommon::GameFileCache m_cache;
Common::Event m_cache_loaded_event; Common::Event m_cache_loaded_event;
Common::Event m_initial_games_emitted_event; Common::Event m_initial_games_emitted_event;