Move prepared-file work into app runtime
This commit is contained in:
@@ -3,6 +3,19 @@
|
||||
|
||||
#include "app.h"
|
||||
|
||||
AppRuntime::AppRuntime()
|
||||
: prepared_file_worker_([this](std::stop_token stop_token)
|
||||
{
|
||||
prepared_file_worker_main(stop_token);
|
||||
})
|
||||
{
|
||||
}
|
||||
|
||||
AppRuntime::~AppRuntime()
|
||||
{
|
||||
prepared_file_worker_stop();
|
||||
}
|
||||
|
||||
bool AppRuntime::is_render_thread() const noexcept
|
||||
{
|
||||
return std::this_thread::get_id() == render_thread_id_;
|
||||
@@ -23,6 +36,62 @@ void AppRuntime::notify_ui_worker() noexcept
|
||||
ui_cv_.notify_all();
|
||||
}
|
||||
|
||||
void AppRuntime::prepared_file_task(std::function<void()> task)
|
||||
{
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(prepared_file_task_mutex_);
|
||||
if (!prepared_file_running_)
|
||||
return;
|
||||
prepared_file_tasklist_.push_back(std::move(task));
|
||||
}
|
||||
prepared_file_cv_.notify_one();
|
||||
}
|
||||
|
||||
void AppRuntime::prepared_file_worker_main(std::stop_token stop_token)
|
||||
{
|
||||
for (;;)
|
||||
{
|
||||
std::function<void()> task;
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(prepared_file_task_mutex_);
|
||||
prepared_file_cv_.wait(lock, [this, &stop_token]
|
||||
{
|
||||
return stop_token.stop_requested() || !prepared_file_running_ || !prepared_file_tasklist_.empty();
|
||||
});
|
||||
if ((stop_token.stop_requested() || !prepared_file_running_) && prepared_file_tasklist_.empty())
|
||||
break;
|
||||
task = std::move(prepared_file_tasklist_.front());
|
||||
prepared_file_tasklist_.pop_front();
|
||||
}
|
||||
|
||||
if (task)
|
||||
{
|
||||
try
|
||||
{
|
||||
task();
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
LOG("prepared file worker task failed");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AppRuntime::prepared_file_worker_stop()
|
||||
{
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(prepared_file_task_mutex_);
|
||||
prepared_file_running_ = false;
|
||||
}
|
||||
prepared_file_cv_.notify_all();
|
||||
if (prepared_file_worker_.joinable())
|
||||
{
|
||||
prepared_file_worker_.request_stop();
|
||||
prepared_file_worker_.join();
|
||||
}
|
||||
}
|
||||
|
||||
void AppRuntime::render_thread_tick(App& app)
|
||||
{
|
||||
static uint32_t count = 0;
|
||||
|
||||
Reference in New Issue
Block a user