Own grid workers and thin Apple platform bridge
This commit is contained in:
@@ -5,6 +5,14 @@
|
||||
#include "app_core/document_platform_io.h"
|
||||
#include "app_core/document_sharing.h"
|
||||
#include "platform_api/platform_services.h"
|
||||
|
||||
#include <condition_variable>
|
||||
#include <deque>
|
||||
#include <functional>
|
||||
#include <mutex>
|
||||
#include <stop_token>
|
||||
#include <thread>
|
||||
|
||||
#ifdef __LINUX__
|
||||
#include <GLFW/glfw3.h>
|
||||
#include "platform_linux/linux_platform_services.h"
|
||||
@@ -14,6 +22,79 @@
|
||||
|
||||
namespace {
|
||||
|
||||
class LegacyPreparedFileWorker final {
|
||||
public:
|
||||
LegacyPreparedFileWorker()
|
||||
: worker_([this](std::stop_token stop_token) {
|
||||
run(stop_token);
|
||||
})
|
||||
{
|
||||
}
|
||||
|
||||
~LegacyPreparedFileWorker()
|
||||
{
|
||||
shutdown();
|
||||
}
|
||||
|
||||
void post(std::function<void()> task)
|
||||
{
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (stopping_)
|
||||
return;
|
||||
tasks_.push_back(std::move(task));
|
||||
}
|
||||
cv_.notify_one();
|
||||
}
|
||||
|
||||
private:
|
||||
void shutdown()
|
||||
{
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
stopping_ = true;
|
||||
}
|
||||
cv_.notify_all();
|
||||
}
|
||||
|
||||
void run(std::stop_token stop_token)
|
||||
{
|
||||
for (;;) {
|
||||
std::function<void()> task;
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
cv_.wait(lock, [&] {
|
||||
return stopping_ || stop_token.stop_requested() || !tasks_.empty();
|
||||
});
|
||||
if ((stopping_ || stop_token.stop_requested()) && tasks_.empty())
|
||||
break;
|
||||
task = std::move(tasks_.front());
|
||||
tasks_.pop_front();
|
||||
}
|
||||
|
||||
if (task) {
|
||||
try {
|
||||
task();
|
||||
} catch (...) {
|
||||
LOG("prepared file worker task failed");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::mutex mutex_;
|
||||
std::condition_variable cv_;
|
||||
std::deque<std::function<void()>> tasks_;
|
||||
bool stopping_ = false;
|
||||
std::jthread worker_;
|
||||
};
|
||||
|
||||
LegacyPreparedFileWorker& prepared_file_worker()
|
||||
{
|
||||
static LegacyPreparedFileWorker worker;
|
||||
return worker;
|
||||
}
|
||||
|
||||
[[nodiscard]] GLint rgba8_internal_format() noexcept
|
||||
{
|
||||
return static_cast<GLint>(pp::renderer::gl::rgba8_internal_format());
|
||||
@@ -201,15 +282,31 @@ void App::pick_file_save(const std::string& type, const std::string& default_nam
|
||||
}
|
||||
|
||||
LOG("App::pick_file_save %s", target.path.c_str());
|
||||
auto write_and_save = [=] {
|
||||
writer(target.path);
|
||||
save_prepared_file(target.path, target.suggested_name, callback);
|
||||
};
|
||||
if (target.write_on_background_thread) {
|
||||
auto* app = this;
|
||||
prepared_file_worker().post([
|
||||
app,
|
||||
writer = std::move(writer),
|
||||
callback = std::move(callback),
|
||||
path = target.path,
|
||||
suggested_name = target.suggested_name
|
||||
]() mutable {
|
||||
writer(path);
|
||||
app->ui_task([app,
|
||||
path = std::move(path),
|
||||
suggested_name = std::move(suggested_name),
|
||||
callback = std::move(callback)]() mutable {
|
||||
app->save_prepared_file(
|
||||
std::move(path),
|
||||
std::move(suggested_name),
|
||||
std::move(callback));
|
||||
});
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (target.write_on_background_thread)
|
||||
std::thread(write_and_save).detach();
|
||||
else
|
||||
write_and_save();
|
||||
writer(target.path);
|
||||
save_prepared_file(target.path, target.suggested_name, std::move(callback));
|
||||
}
|
||||
|
||||
void App::pick_file_save(std::vector<std::string> types, std::function<void(std::string)> callback)
|
||||
|
||||
Reference in New Issue
Block a user