Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
main_thread_dispatcher.hpp
Go to the documentation of this file.
1#pragma once
2
4#include <cstdint>
5#include <functional>
6
7namespace pulp::events {
8
9// Process-wide bridge to the native UI/main message loop.
10//
11// Platform owners register a backend while their native event loop is alive.
12// Callers can then marshal work to that loop without depending on AppKit,
13// UIKit, SDL, or another platform-specific queue API.
14//
15// unregister_backend() waits for any in-flight backend callbacks selected by
16// this dispatcher to finish before returning. If a backend unregisters itself
17// from inside one of its callbacks, the registration is removed immediately
18// and unregister waits for any other threads already inside that backend's
19// callbacks before allowing the current callback to finish. A backend that
20// accepts work is still responsible for owning whatever state is needed until
21// that work runs.
23public:
24 using Token = std::uint64_t;
25
26 struct Backend {
27 std::function<bool(Task)> post;
28 std::function<bool()> is_main_thread;
29 // Optional: post `task` to the main thread after `delay_ms`
30 // milliseconds. Lets callers run a paced main-thread poll without
31 // busy-reposting. A backend that cannot schedule may leave this empty;
32 // `call_async_after` then falls back to an immediate `post`.
33 std::function<bool(Task, int /*delay_ms*/)> post_after;
34 };
35
36 // Registers a backend and makes it active. If another live backend was
37 // already active, it is restored when the newer token unregisters.
39
40 // Removes a live registration. Returns false for unknown or zero tokens.
41 static bool unregister_backend(Token token);
42
43 static bool has_backend();
44 static bool is_main_thread();
45
46 // Posts work to the active main-thread backend. Returns false if no backend
47 // accepts the task. Task exceptions are swallowed so they cannot escape into
48 // native event queues.
49 static bool call_async(Task task);
50
51 // Posts work to the active main-thread backend after `delay_ms`
52 // milliseconds. Falls back to an immediate `call_async` if the backend has
53 // no `post_after`. Returns false if no backend accepts the task. Task
54 // exceptions are swallowed. Used to drive a paced (non-busy) main-thread
55 // poll loop.
56 static bool call_async_after(Task task, int delay_ms);
57
58 // Runs work on the active main thread, blocking the caller when needed.
59 // Returns false if no backend accepts the task. Task exceptions propagate
60 // back to the caller.
61 static bool call_sync(Task task);
62};
63
64} // namespace pulp::events
Definition main_thread_dispatcher.hpp:22
std::uint64_t Token
Definition main_thread_dispatcher.hpp:24
static bool call_sync(Task task)
static bool unregister_backend(Token token)
static Token register_backend(Backend backend)
static bool call_async_after(Task task, int delay_ms)
static bool call_async(Task task)
Definition child_process_manager.hpp:18
std::function< void()> Task
Definition event_loop.hpp:18
Definition main_thread_dispatcher.hpp:26
std::function< bool()> is_main_thread
Definition main_thread_dispatcher.hpp:28
std::function< bool(Task)> post
Definition main_thread_dispatcher.hpp:27
std::function< bool(Task, int)> post_after
Definition main_thread_dispatcher.hpp:33