Move Windows main task queue into platform services
This commit is contained in:
62
src/main.cpp
62
src/main.cpp
@@ -17,7 +17,6 @@
|
||||
#include "../resource.h"
|
||||
|
||||
#include <WbemCli.h>
|
||||
#include <deque>
|
||||
#include "wacom.h"
|
||||
#include "abr.h"
|
||||
|
||||
@@ -30,68 +29,17 @@ namespace pp::platform::windows {
|
||||
RetainedState& retained_state();
|
||||
}
|
||||
using pp::platform::windows::retained_state;
|
||||
namespace {
|
||||
|
||||
struct RetainedMainTaskQueue final
|
||||
{
|
||||
std::deque<std::packaged_task<void()>> tasklist;
|
||||
std::mutex task_mutex;
|
||||
};
|
||||
|
||||
RetainedMainTaskQueue& retained_main_task_queue()
|
||||
{
|
||||
static RetainedMainTaskQueue queue;
|
||||
return queue;
|
||||
}
|
||||
|
||||
template <typename Callable>
|
||||
void enqueue_main_task(Callable&& task)
|
||||
{
|
||||
auto& queue = retained_main_task_queue();
|
||||
std::lock_guard<std::mutex> lock(queue.task_mutex);
|
||||
queue.tasklist.emplace_back(std::forward<Callable>(task));
|
||||
}
|
||||
|
||||
void enqueue_main_task_bridge(std::packaged_task<void()> task)
|
||||
{
|
||||
auto& queue = retained_main_task_queue();
|
||||
std::lock_guard<std::mutex> lock(queue.task_mutex);
|
||||
queue.tasklist.emplace_back(std::move(task));
|
||||
}
|
||||
|
||||
void drain_main_tasks()
|
||||
{
|
||||
std::deque<std::packaged_task<void()>> working_list;
|
||||
{
|
||||
auto& queue = retained_main_task_queue();
|
||||
std::lock_guard<std::mutex> lock(queue.task_mutex);
|
||||
working_list = std::move(queue.tasklist);
|
||||
}
|
||||
|
||||
while (!working_list.empty())
|
||||
{
|
||||
working_list.front()();
|
||||
working_list.pop_front();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
HWND pp_windows_main_window_handle()
|
||||
{
|
||||
return retained_state().hWnd;
|
||||
}
|
||||
|
||||
void pp_windows_enqueue_main_task(std::packaged_task<void()> task)
|
||||
{
|
||||
enqueue_main_task_bridge(std::move(task));
|
||||
}
|
||||
|
||||
void destroy_window()
|
||||
{
|
||||
enqueue_main_task([hWnd = retained_state().hWnd] {
|
||||
pp::platform::windows::enqueue_main_thread_task(std::packaged_task<void()>([hWnd = retained_state().hWnd] {
|
||||
pp::platform::windows::request_window_close(hWnd);
|
||||
});
|
||||
}));
|
||||
}
|
||||
|
||||
void async_lock()
|
||||
@@ -122,9 +70,9 @@ void win32_update_stylus(float dt)
|
||||
void win32_update_fps(int frames)
|
||||
{
|
||||
auto& state = retained_state();
|
||||
enqueue_main_task([hWnd = state.hWnd, window_title = state.window_title, &vr = state.vr, frames] {
|
||||
pp::platform::windows::enqueue_main_thread_task(std::packaged_task<void()>([hWnd = state.hWnd, window_title = state.window_title, &vr = state.vr, frames] {
|
||||
pp::platform::windows::update_window_fps(hWnd, window_title, vr, frames);
|
||||
});
|
||||
}));
|
||||
}
|
||||
|
||||
int read_WMI_info()
|
||||
@@ -487,7 +435,7 @@ int main(int argc, char** argv)
|
||||
}
|
||||
|
||||
// list of tasks for the main thread
|
||||
drain_main_tasks();
|
||||
pp::platform::windows::drain_main_thread_tasks();
|
||||
}
|
||||
// Clean up
|
||||
WacomTablet::I.terminate();
|
||||
|
||||
@@ -22,11 +22,28 @@ void win32_save_window_state();
|
||||
bool win32_vr_start();
|
||||
void win32_vr_stop();
|
||||
HWND pp_windows_main_window_handle();
|
||||
void pp_windows_enqueue_main_task(std::packaged_task<void()> task);
|
||||
|
||||
namespace pp::platform::windows {
|
||||
namespace {
|
||||
|
||||
struct RetainedMainTaskQueue final {
|
||||
std::deque<std::packaged_task<void()>> tasklist;
|
||||
std::mutex task_mutex;
|
||||
};
|
||||
|
||||
[[nodiscard]] RetainedMainTaskQueue& retained_main_task_queue()
|
||||
{
|
||||
static RetainedMainTaskQueue queue;
|
||||
return queue;
|
||||
}
|
||||
|
||||
void enqueue_main_task(std::packaged_task<void()> task)
|
||||
{
|
||||
auto& queue = retained_main_task_queue();
|
||||
std::lock_guard<std::mutex> lock(queue.task_mutex);
|
||||
queue.tasklist.emplace_back(std::move(task));
|
||||
}
|
||||
|
||||
struct RetainedWin32AsyncRenderContextState final {
|
||||
HDC hdc{};
|
||||
HGLRC hrc{};
|
||||
@@ -96,6 +113,27 @@ void swap_async_render_context()
|
||||
{
|
||||
SwapBuffers(retained_win32_async_render_context_state().hdc);
|
||||
}
|
||||
|
||||
void enqueue_main_thread_task(std::packaged_task<void()> task)
|
||||
{
|
||||
enqueue_main_task(std::move(task));
|
||||
}
|
||||
|
||||
void drain_main_thread_tasks()
|
||||
{
|
||||
std::deque<std::packaged_task<void()>> working_list;
|
||||
{
|
||||
auto& queue = retained_main_task_queue();
|
||||
std::lock_guard<std::mutex> lock(queue.task_mutex);
|
||||
working_list = std::move(queue.tasklist);
|
||||
}
|
||||
|
||||
while (!working_list.empty())
|
||||
{
|
||||
working_list.front()();
|
||||
working_list.pop_front();
|
||||
}
|
||||
}
|
||||
} // namespace pp::platform::windows
|
||||
|
||||
namespace {
|
||||
@@ -159,7 +197,7 @@ void handle_gl_callback(
|
||||
|
||||
void show_cursor(bool visible)
|
||||
{
|
||||
pp_windows_enqueue_main_task(std::packaged_task<void()>([=] {
|
||||
pp::platform::windows::enqueue_main_thread_task(std::packaged_task<void()>([=] {
|
||||
if (visible)
|
||||
while (ShowCursor(true) < 0);
|
||||
else
|
||||
|
||||
@@ -2,8 +2,12 @@
|
||||
|
||||
#include "platform_api/platform_services.h"
|
||||
|
||||
#include <future>
|
||||
|
||||
namespace pp::platform::windows {
|
||||
|
||||
[[nodiscard]] PlatformServices& platform_services();
|
||||
void enqueue_main_thread_task(std::packaged_task<void()> task);
|
||||
void drain_main_thread_tasks();
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user