Move canvas async work into app runtime

This commit is contained in:
2026-06-16 08:33:16 +02:00
parent 3e4eb89499
commit 52f0d32612
9 changed files with 151 additions and 119 deletions

View File

@@ -8,11 +8,16 @@ AppRuntime::AppRuntime()
{
prepared_file_worker_main(stop_token);
})
, canvas_async_worker_([this](std::stop_token stop_token)
{
canvas_async_worker_main(stop_token);
})
{
}
AppRuntime::~AppRuntime()
{
canvas_async_worker_stop();
prepared_file_worker_stop();
}
@@ -47,6 +52,17 @@ void AppRuntime::prepared_file_task(std::function<void()> task)
prepared_file_cv_.notify_one();
}
void AppRuntime::canvas_async_task(std::function<void()> task)
{
{
std::lock_guard<std::mutex> lock(canvas_async_task_mutex_);
if (!canvas_async_running_)
return;
canvas_async_tasklist_.push_back(std::move(task));
}
canvas_async_cv_.notify_one();
}
void AppRuntime::prepared_file_worker_main(std::stop_token stop_token)
{
for (;;)
@@ -78,6 +94,37 @@ void AppRuntime::prepared_file_worker_main(std::stop_token stop_token)
}
}
void AppRuntime::canvas_async_worker_main(std::stop_token stop_token)
{
for (;;)
{
std::function<void()> task;
{
std::unique_lock<std::mutex> lock(canvas_async_task_mutex_);
canvas_async_cv_.wait(lock, [this, &stop_token]
{
return stop_token.stop_requested() || !canvas_async_running_ || !canvas_async_tasklist_.empty();
});
if ((stop_token.stop_requested() || !canvas_async_running_) && canvas_async_tasklist_.empty())
break;
task = std::move(canvas_async_tasklist_.front());
canvas_async_tasklist_.pop_front();
}
if (task)
{
try
{
task();
}
catch (...)
{
LOG("canvas async worker task failed");
}
}
}
}
void AppRuntime::prepared_file_worker_stop()
{
{
@@ -92,6 +139,20 @@ void AppRuntime::prepared_file_worker_stop()
}
}
void AppRuntime::canvas_async_worker_stop()
{
{
std::lock_guard<std::mutex> lock(canvas_async_task_mutex_);
canvas_async_running_ = false;
}
canvas_async_cv_.notify_all();
if (canvas_async_worker_.joinable())
{
canvas_async_worker_.request_stop();
canvas_async_worker_.join();
}
}
void AppRuntime::render_thread_tick(App& app)
{
static uint32_t count = 0;