mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2026-03-16 18:03:03 +00:00
feat: add bind for loading fastfiles to ModMan
This commit is contained in:
77
src/ModMan/Utils/DispatchableThread.cpp
Normal file
77
src/ModMan/Utils/DispatchableThread.cpp
Normal file
@@ -0,0 +1,77 @@
|
||||
#include "DispatchableThread.h"
|
||||
|
||||
DispatchableThread::DispatchableThread()
|
||||
: m_terminate(false)
|
||||
{
|
||||
}
|
||||
|
||||
DispatchableThread::~DispatchableThread()
|
||||
{
|
||||
Terminate();
|
||||
}
|
||||
|
||||
void DispatchableThread::Start()
|
||||
{
|
||||
m_terminate = false;
|
||||
m_thread = std::thread(
|
||||
[&]
|
||||
{
|
||||
ThreadLoop();
|
||||
});
|
||||
}
|
||||
|
||||
void DispatchableThread::Terminate()
|
||||
{
|
||||
std::unique_lock lock(m_cb_mutex);
|
||||
|
||||
if (!m_terminate)
|
||||
{
|
||||
m_terminate = true;
|
||||
m_cv.notify_all();
|
||||
lock.unlock();
|
||||
m_thread.join();
|
||||
}
|
||||
else
|
||||
{
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
void DispatchableThread::Dispatch(cb_t cb)
|
||||
{
|
||||
std::lock_guard lock(m_cb_mutex);
|
||||
|
||||
m_cb_list.emplace_back(std::move(cb));
|
||||
m_cv.notify_one();
|
||||
}
|
||||
|
||||
std::optional<DispatchableThread::cb_t> DispatchableThread::NextCallback()
|
||||
{
|
||||
if (m_terminate || m_cb_list.empty())
|
||||
return std::nullopt;
|
||||
|
||||
auto cb = std::move(m_cb_list.front());
|
||||
m_cb_list.pop_front();
|
||||
|
||||
return cb;
|
||||
}
|
||||
|
||||
void DispatchableThread::ThreadLoop()
|
||||
{
|
||||
while (!m_terminate)
|
||||
{
|
||||
std::unique_lock lock(m_cb_mutex);
|
||||
m_cv.wait(lock,
|
||||
[&]
|
||||
{
|
||||
return !m_cb_list.empty() || m_terminate;
|
||||
});
|
||||
|
||||
auto maybeCb = NextCallback();
|
||||
|
||||
lock.unlock();
|
||||
|
||||
if (maybeCb)
|
||||
(*maybeCb)();
|
||||
}
|
||||
}
|
||||
37
src/ModMan/Utils/DispatchableThread.h
Normal file
37
src/ModMan/Utils/DispatchableThread.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
|
||||
#include <condition_variable>
|
||||
#include <deque>
|
||||
#include <functional>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
#include <thread>
|
||||
|
||||
class DispatchableThread
|
||||
{
|
||||
public:
|
||||
using cb_t = std::function<void()>;
|
||||
|
||||
DispatchableThread();
|
||||
~DispatchableThread();
|
||||
DispatchableThread(const DispatchableThread& other) = delete;
|
||||
DispatchableThread(DispatchableThread&& other) noexcept = default;
|
||||
DispatchableThread& operator=(const DispatchableThread& other) = delete;
|
||||
DispatchableThread& operator=(DispatchableThread&& other) noexcept = default;
|
||||
|
||||
void Start();
|
||||
void Terminate();
|
||||
|
||||
void Dispatch(cb_t cb);
|
||||
|
||||
private:
|
||||
std::optional<cb_t> NextCallback();
|
||||
void ThreadLoop();
|
||||
|
||||
std::mutex m_cb_mutex;
|
||||
std::deque<cb_t> m_cb_list;
|
||||
|
||||
std::condition_variable m_cv;
|
||||
std::thread m_thread;
|
||||
bool m_terminate;
|
||||
};
|
||||
Reference in New Issue
Block a user