Merge app startup and shutdown into create and destroy

This commit is contained in:
Peter Li 2026-09-18 21:10:34 -07:00
parent 894e3dee59
commit 6d9a0acf9a
3 changed files with 64 additions and 56 deletions

View File

@ -145,18 +145,22 @@ Set `uhdk_app_desc.use_application_thread` before `uhdk_app_create`:
| `0` (default) | OS main thread | Pumped by `uhdk_app_poll_events` | | `0` (default) | OS main thread | Pumped by `uhdk_app_poll_events` |
| `1` | One separate application thread | OS main runs `uhdk_app_run_native` | | `1` | One separate application thread | OS main runs `uhdk_app_run_native` |
With `0`, main calls `create`, `start`, runs its poll/update/render loop, then With `0`, main calls `create`, runs its poll/update/render loop, then calls
calls `stop` and `destroy`. With `1`, main calls `create` and `start`, creates a `destroy`. With `1`, main calls `create`, creates a thread, and calls `run_native`.
thread, and calls `run_native`. The application thread calls `attach`, runs its The application thread calls `attach`, runs its loop and cleans up, then calls
loop and cleans up, then calls `detach`. Main joins it after `run_native` returns, `detach`. Main joins it after `run_native` returns and destroys the app. If thread
calls `stop`, then `destroy`. If creating the thread fails, stop and destroy the creation fails, destroy the app without entering `run_native`.
started app without entering `run_native`. Start is attempted once; failure leaves
an app destroyable. Attachment is once per app, with no thread migration.
The convenience `uhdk_app_run(app, entry, user)` starts/stops services and invokes Create initializes native services and I/O. Failure unwinds initialization and
entry with application ownership already established. It honors the flag, creating sets the output handle to null; no destroy is needed. Destroy initiates shutdown,
and joining the application thread when enabled. Its callback owns its loop and drains teardown and joins internal I/O workers before releasing storage. It must
must not attach/detach/stop. Create and destroy still belong to the caller. run on OS main, outside GL brackets, after any application thread is detached and
joined. Attachment is once per app, with no thread migration.
The convenience `uhdk_app_run(app, entry, user)` invokes entry with application
ownership established. It honors the flag, creating and joining the application
thread when enabled. Its callback owns its loop and must not attach/detach/destroy.
The caller must destroy the app after run returns, including on run failure.
The caller decides when to poll, update, and render; there are no application tick The caller decides when to poll, update, and render; there are no application tick
APIs. Use `UHDK_PROFILE_BEGIN`/`UHDK_PROFILE_END` for application phases. Frame stats APIs. Use `UHDK_PROFILE_BEGIN`/`UHDK_PROFILE_END` for application phases. Frame stats
@ -172,10 +176,10 @@ effort and never frees storage underneath an active OS call. Final shutdown may
wait for that call, while normal native dispatch and polling remain independent. wait for that call, while normal native dispatch and polling remain independent.
Accept shutdown with `uhdk_app_quit`; drain terminal request results until Accept shutdown with `uhdk_app_quit`; drain terminal request results until
`STOPPED`, clean up GL resources inside frame brackets, then detach/stop. Native `STOPPED`, clean up GL resources inside frame brackets, then detach/destroy. Native
services remain alive for this cleanup. Early detach/stop abandons undelivered services remain alive for this cleanup. Early detach/destroy abandons undelivered
results but retires internal requests before destruction. A fatal native-loop results but retires internal requests before destruction. A fatal native-loop
failure is persistent and wakes waiting polls; detach/join/stop still apply. failure is persistent and wakes waiting polls; detach/join/destroy still apply.
## Borrowed event payloads ## Borrowed event payloads

View File

@ -38,7 +38,7 @@
* application thread, or app destruction (whichever comes first). Copy spans * application thread, or app destruction (whichever comes first). Copy spans
* or retain files/menus to keep them. Every such poll resets storage, including empty/error polls. * or retain files/menus to keep them. Every such poll resets storage, including empty/error polls.
* Rendering does not reset storage. Each poll returns an immutable batch. * Rendering does not reset storage. Each poll returns an immutable batch.
* - create/start/run/run_native/stop/destroy belong to OS main. With * - create/run/run_native/destroy belong to OS main. With
* use_application_thread=0, application calls also belong to OS main and poll * use_application_thread=0, application calls also belong to OS main and poll
* pumps native work. With 1, attach/detach bind a separate application thread. * pumps native work. With 1, attach/detach bind a separate application thread.
* Other calls belong to the application thread except thread-safe app_wake. * Other calls belong to the application thread except thread-safe app_wake.
@ -229,11 +229,11 @@ typedef struct uhdk_app_desc {
} uhdk_app_desc; } uhdk_app_desc;
UHDK_API uint32_t UHDK_CALL uhdk_get_abi_version(void); UHDK_API uint32_t UHDK_CALL uhdk_get_abi_version(void);
/* OS main. Allocate and initialize native services and the I/O worker.
* With use_application_thread=0 also establishes main as the application thread.
* Failure unwinds initialization and sets *out=NULL; no destroy is needed. */
UHDK_API uhdk_status UHDK_CALL uhdk_app_create(const uhdk_app_desc *desc, uhdk_app **out); UHDK_API uhdk_status UHDK_CALL uhdk_app_create(const uhdk_app_desc *desc, uhdk_app **out);
/* OS main. Start once; does not enter a loop or create an application thread. /* Requires use_application_thread=1 and successful create. Attach exactly one
* Failure rolls back startup and leaves the app destroyable, not restartable. */
UHDK_API uhdk_status UHDK_CALL uhdk_app_start(uhdk_app *app);
/* Requires use_application_thread=1 and successful start. Attach exactly one
* non-main thread, once. Does not wait for run_native. */ * non-main thread, once. Does not wait for run_native. */
UHDK_API uhdk_status UHDK_CALL uhdk_app_attach(uhdk_app *app); UHDK_API uhdk_status UHDK_CALL uhdk_app_attach(uhdk_app *app);
/* OS main, use_application_thread=1. Pump until detach and command drain. /* OS main, use_application_thread=1. Pump until detach and command drain.
@ -242,15 +242,11 @@ UHDK_API uhdk_status UHDK_CALL uhdk_app_run_native(uhdk_app *app);
/* Attached application thread, outside GL brackets. Initiates shutdown and /* Attached application thread, outside GL brackets. Initiates shutdown and
* relinquishes ownership; undelivered events may be abandoned. */ * relinquishes ownership; undelivered events may be abandoned. */
UHDK_API uhdk_status UHDK_CALL uhdk_app_detach(uhdk_app *app); UHDK_API uhdk_status UHDK_CALL uhdk_app_detach(uhdk_app *app);
/* OS main, after application cleanup. With a separate application thread, /* Convenience runner on a newly created app; invokes entry once.
* detach and join it first and let run_native return. Also valid if no thread * With use_application_thread=1 creates/attaches/joins the app thread and runs
* attached and run_native was never entered. Drains teardown and joins I/O. * native services on main. Entry must not attach/detach/destroy the app.
* STOPPED still permits GL/resource cleanup until detach/stop. */ * Returning ends application execution; caller must still destroy the app.
UHDK_API uhdk_status UHDK_CALL uhdk_app_stop(uhdk_app *app); * Failure to create the application thread also leaves the app destroyable. */
/* Convenience runner on a newly created app: start, invoke entry, stop.
* With use_application_thread=1 also creates/attaches/joins the app thread and
* runs native services on main. Entry must not call attach/detach/stop.
* Returns ready for destroy, including startup/worker-creation failures. */
UHDK_API uhdk_status UHDK_CALL uhdk_app_run( UHDK_API uhdk_status UHDK_CALL uhdk_app_run(
uhdk_app *app, uhdk_app *app,
uhdk_app_main_fn main_fn, uhdk_app_main_fn main_fn,
@ -276,8 +272,13 @@ UHDK_API uint64_t UHDK_CALL uhdk_app_get_time_ns(uhdk_app *app);
* multiple timers by scheduling its earliest deadline. No real-time guarantee. * multiple timers by scheduling its earliest deadline. No real-time guarantee.
*/ */
UHDK_API uhdk_status UHDK_CALL uhdk_app_set_wake_deadline(uhdk_app *app, uint64_t deadline_ns); UHDK_API uhdk_status UHDK_CALL uhdk_app_set_wake_deadline(uhdk_app *app, uint64_t deadline_ns);
UHDK_API void UHDK_CALL uhdk_app_destroy(uhdk_app *app /* OS main, outside GL brackets and after application cleanup. Initiates shutdown,
); /* After stop/run or failed start; releases child objects. */ * drains teardown, joins internal I/O, and releases all app/child storage.
* With use_application_thread=1 first detach and join the caller-owned thread
* and return from run_native (if entered). Never joins a caller-owned thread.
* Also valid immediately after create or failed app-thread creation. NULL is safe.
* May wait for an active filesystem call. Undelivered events are abandoned. */
UHDK_API void UHDK_CALL uhdk_app_destroy(uhdk_app *app);
/* Linux: AUTO/false before successful startup; thereafter the selected backend /* Linux: AUTO/false before successful startup; thereafter the selected backend
* and implemented, available mechanisms. Capabilities do not guarantee success * and implemented, available mechanisms. Capabilities do not guarantee success
* for a particular operation. Other platforms retain their fixed backend. */ * for a particular operation. Other platforms retain their fixed backend. */

View File

@ -18,6 +18,7 @@ pub export fn uhdk_get_abi_version() u32 {
} }
pub export fn uhdk_app_create(desc: [*c]const c.uhdk_app_desc, out: [*c]?*c.uhdk_app) abi.Status { pub export fn uhdk_app_create(desc: [*c]const c.uhdk_app_desc, out: [*c]?*c.uhdk_app) abi.Status {
if (out != null) out.* = null;
if (c.z_native_is_main_thread() == 0) return c.UHDK_STATUS_WRONG_THREAD; if (c.z_native_is_main_thread() == 0) return c.UHDK_STATUS_WRONG_THREAD;
if (!abi.validDescriptor(c.uhdk_app_desc, desc) or if (!abi.validDescriptor(c.uhdk_app_desc, desc) or
out == null or out == null or
@ -47,16 +48,17 @@ pub export fn uhdk_app_create(desc: [*c]const c.uhdk_app_desc, out: [*c]?*c.uhdk
application.io_backend = backend; application.io_backend = backend;
application.use_application_thread = desc.*.use_application_thread != 0; application.use_application_thread = desc.*.use_application_thread != 0;
singleton = true; singleton = true;
const status = initialize(application);
if (status != abi.OK) {
releaseStorage(application);
return status;
}
out.* = runtime.toAppHandle(application); out.* = runtime.toAppHandle(application);
return abi.OK; return abi.OK;
} }
pub export fn uhdk_app_start(handle: ?*c.uhdk_app) abi.Status { fn initialize(application: *Application) abi.Status {
if (handle == null) return abi.INVALID; const handle = runtime.toAppHandle(application);
if (c.z_native_is_main_thread() == 0) return c.UHDK_STATUS_WRONG_THREAD;
const application = runtime.fromAppHandle(handle);
if (application.has_run) return abi.INVALID;
application.has_run = true;
profile.thread("UHDK platform"); profile.thread("UHDK platform");
const ready = runtime.prepareReady(application) catch return abi.OOM; const ready = runtime.prepareReady(application) catch return abi.OOM;
const status = c.z_native_start(handle, abi.textSpan(application.name)); const status = c.z_native_start(handle, abi.textSpan(application.name));
@ -130,37 +132,34 @@ pub export fn uhdk_app_run_native(handle: ?*c.uhdk_app) abi.Status {
pub export fn uhdk_app_run(handle: ?*c.uhdk_app, entry: c.uhdk_app_main_fn, user: ?*anyopaque) abi.Status { pub export fn uhdk_app_run(handle: ?*c.uhdk_app, entry: c.uhdk_app_main_fn, user: ?*anyopaque) abi.Status {
if (handle == null or entry == null) return abi.INVALID; if (handle == null or entry == null) return abi.INVALID;
const status = uhdk_app_start(handle); if (c.z_native_is_main_thread() == 0) return c.UHDK_STATUS_WRONG_THREAD;
if (status != abi.OK) return status;
const application = runtime.fromAppHandle(handle); const application = runtime.fromAppHandle(handle);
if (application.has_run or application.attached_once or application.native_entered or application.stopping) return abi.INVALID;
application.has_run = true;
application.entry = entry; application.entry = entry;
application.user = user; application.user = user;
if (application.use_application_thread) { if (application.use_application_thread) {
const worker = std.Thread.spawn(.{}, runtime.applicationMain, .{application}) catch { const worker = std.Thread.spawn(.{}, runtime.applicationMain, .{application}) catch {
_ = uhdk_app_stop(handle);
return abi.OOM; return abi.OOM;
}; };
const run_status = uhdk_app_run_native(handle); const run_status = uhdk_app_run_native(handle);
worker.join(); worker.join();
const stop_status = uhdk_app_stop(handle); return run_status;
return if (run_status != abi.OK) run_status else stop_status;
} }
entry.?(handle, user); entry.?(handle, user);
return uhdk_app_stop(handle); application.lock();
application.stopping = true;
application.worker_done = true;
application.worker_id = null;
application.unlock();
return application.native_error;
} }
pub export fn uhdk_app_stop(handle: ?*c.uhdk_app) abi.Status { fn shutdown(application: *Application) void {
if (handle == null) return abi.INVALID; const handle = runtime.toAppHandle(application);
if (c.z_native_is_main_thread() == 0) return c.UHDK_STATUS_WRONG_THREAD; std.debug.assert(!application.native_running);
const application = runtime.fromAppHandle(handle); std.debug.assert(!application.use_application_thread or application.worker_id == null);
if (!application.running) return abi.INVALID; std.debug.assert(!runtime.hasFrame(application));
application.lock();
if (application.native_running or (application.use_application_thread and application.worker_id != null)) {
application.unlock();
return c.UHDK_STATUS_BUSY;
}
application.unlock();
if (runtime.hasFrame(application)) return c.UHDK_STATUS_BUSY;
_ = c.z_native_gl(null, 4, null); _ = c.z_native_gl(null, 4, null);
application.lock(); application.lock();
application.stopping = true; application.stopping = true;
@ -206,7 +205,6 @@ pub export fn uhdk_app_stop(handle: ?*c.uhdk_app) abi.Status {
c.z_native_finish(); c.z_native_finish();
application.running = false; application.running = false;
application.worker_id = null; application.worker_id = null;
return application.native_error;
} }
pub export fn uhdk_app_poll_events( pub export fn uhdk_app_poll_events(
@ -387,7 +385,12 @@ pub export fn uhdk_request_cancel(handle: ?*c.uhdk_app, id: u64) abi.Status {
pub export fn uhdk_app_destroy(handle: ?*c.uhdk_app) void { pub export fn uhdk_app_destroy(handle: ?*c.uhdk_app) void {
if (handle == null) return; if (handle == null) return;
const application = runtime.fromAppHandle(handle); const application = runtime.fromAppHandle(handle);
std.debug.assert(c.z_native_is_main_thread() != 0 and !application.running); std.debug.assert(c.z_native_is_main_thread() != 0);
shutdown(application);
releaseStorage(application);
}
fn releaseStorage(application: *Application) void {
runtime.destroyEvents(application.published); runtime.destroyEvents(application.published);
runtime.destroyEvents(application.pending_head); runtime.destroyEvents(application.pending_head);
application.scratch.deinit(); application.scratch.deinit();