Merge app startup and shutdown into create and destroy
This commit is contained in:
parent
894e3dee59
commit
6d9a0acf9a
32
README.md
32
README.md
|
|
@ -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` |
|
||||
| `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
|
||||
calls `stop` and `destroy`. With `1`, main calls `create` and `start`, creates a
|
||||
thread, and calls `run_native`. The application thread calls `attach`, runs its
|
||||
loop and cleans up, then calls `detach`. Main joins it after `run_native` returns,
|
||||
calls `stop`, then `destroy`. If creating the thread fails, stop and destroy the
|
||||
started app without entering `run_native`. Start is attempted once; failure leaves
|
||||
an app destroyable. Attachment is once per app, with no thread migration.
|
||||
With `0`, main calls `create`, runs its poll/update/render loop, then calls
|
||||
`destroy`. With `1`, main calls `create`, creates a thread, and calls `run_native`.
|
||||
The application thread calls `attach`, runs its loop and cleans up, then calls
|
||||
`detach`. Main joins it after `run_native` returns and destroys the app. If thread
|
||||
creation fails, destroy the app without entering `run_native`.
|
||||
|
||||
The convenience `uhdk_app_run(app, entry, user)` starts/stops services and invokes
|
||||
entry with application ownership already established. It honors the flag, creating
|
||||
and joining the application thread when enabled. Its callback owns its loop and
|
||||
must not attach/detach/stop. Create and destroy still belong to the caller.
|
||||
Create initializes native services and I/O. Failure unwinds initialization and
|
||||
sets the output handle to null; no destroy is needed. Destroy initiates shutdown,
|
||||
drains teardown and joins internal I/O workers before releasing storage. It must
|
||||
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
|
||||
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.
|
||||
|
||||
Accept shutdown with `uhdk_app_quit`; drain terminal request results until
|
||||
`STOPPED`, clean up GL resources inside frame brackets, then detach/stop. Native
|
||||
services remain alive for this cleanup. Early detach/stop abandons undelivered
|
||||
`STOPPED`, clean up GL resources inside frame brackets, then detach/destroy. Native
|
||||
services remain alive for this cleanup. Early detach/destroy abandons undelivered
|
||||
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
|
||||
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@
|
|||
* 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.
|
||||
* 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
|
||||
* pumps native work. With 1, attach/detach bind a separate application thread.
|
||||
* 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_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);
|
||||
/* OS main. Start once; does not enter a loop or create an application thread.
|
||||
* 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
|
||||
/* Requires use_application_thread=1 and successful create. Attach exactly one
|
||||
* non-main thread, once. Does not wait for run_native. */
|
||||
UHDK_API uhdk_status UHDK_CALL uhdk_app_attach(uhdk_app *app);
|
||||
/* 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
|
||||
* relinquishes ownership; undelivered events may be abandoned. */
|
||||
UHDK_API uhdk_status UHDK_CALL uhdk_app_detach(uhdk_app *app);
|
||||
/* OS main, after application cleanup. With a separate application thread,
|
||||
* detach and join it first and let run_native return. Also valid if no thread
|
||||
* attached and run_native was never entered. Drains teardown and joins I/O.
|
||||
* STOPPED still permits GL/resource cleanup until detach/stop. */
|
||||
UHDK_API uhdk_status UHDK_CALL uhdk_app_stop(uhdk_app *app);
|
||||
/* 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. */
|
||||
/* Convenience runner on a newly created app; invokes entry once.
|
||||
* With use_application_thread=1 creates/attaches/joins the app thread and runs
|
||||
* native services on main. Entry must not attach/detach/destroy the app.
|
||||
* Returning ends application execution; caller must still destroy the app.
|
||||
* Failure to create the application thread also leaves the app destroyable. */
|
||||
UHDK_API uhdk_status UHDK_CALL uhdk_app_run(
|
||||
uhdk_app *app,
|
||||
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.
|
||||
*/
|
||||
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
|
||||
); /* After stop/run or failed start; releases child objects. */
|
||||
/* OS main, outside GL brackets and after application cleanup. Initiates shutdown,
|
||||
* 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
|
||||
* and implemented, available mechanisms. Capabilities do not guarantee success
|
||||
* for a particular operation. Other platforms retain their fixed backend. */
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
if (out != null) out.* = null;
|
||||
if (c.z_native_is_main_thread() == 0) return c.UHDK_STATUS_WRONG_THREAD;
|
||||
if (!abi.validDescriptor(c.uhdk_app_desc, desc) 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.use_application_thread = desc.*.use_application_thread != 0;
|
||||
singleton = true;
|
||||
const status = initialize(application);
|
||||
if (status != abi.OK) {
|
||||
releaseStorage(application);
|
||||
return status;
|
||||
}
|
||||
out.* = runtime.toAppHandle(application);
|
||||
return abi.OK;
|
||||
}
|
||||
|
||||
pub export fn uhdk_app_start(handle: ?*c.uhdk_app) abi.Status {
|
||||
if (handle == null) return abi.INVALID;
|
||||
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;
|
||||
fn initialize(application: *Application) abi.Status {
|
||||
const handle = runtime.toAppHandle(application);
|
||||
profile.thread("UHDK platform");
|
||||
const ready = runtime.prepareReady(application) catch return abi.OOM;
|
||||
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 {
|
||||
if (handle == null or entry == null) return abi.INVALID;
|
||||
const status = uhdk_app_start(handle);
|
||||
if (status != abi.OK) return status;
|
||||
if (c.z_native_is_main_thread() == 0) return c.UHDK_STATUS_WRONG_THREAD;
|
||||
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.user = user;
|
||||
if (application.use_application_thread) {
|
||||
const worker = std.Thread.spawn(.{}, runtime.applicationMain, .{application}) catch {
|
||||
_ = uhdk_app_stop(handle);
|
||||
return abi.OOM;
|
||||
};
|
||||
const run_status = uhdk_app_run_native(handle);
|
||||
worker.join();
|
||||
const stop_status = uhdk_app_stop(handle);
|
||||
return if (run_status != abi.OK) run_status else stop_status;
|
||||
return run_status;
|
||||
}
|
||||
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 {
|
||||
if (handle == null) return abi.INVALID;
|
||||
if (c.z_native_is_main_thread() == 0) return c.UHDK_STATUS_WRONG_THREAD;
|
||||
const application = runtime.fromAppHandle(handle);
|
||||
if (!application.running) return abi.INVALID;
|
||||
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;
|
||||
fn shutdown(application: *Application) void {
|
||||
const handle = runtime.toAppHandle(application);
|
||||
std.debug.assert(!application.native_running);
|
||||
std.debug.assert(!application.use_application_thread or application.worker_id == null);
|
||||
std.debug.assert(!runtime.hasFrame(application));
|
||||
_ = c.z_native_gl(null, 4, null);
|
||||
application.lock();
|
||||
application.stopping = true;
|
||||
|
|
@ -206,7 +205,6 @@ pub export fn uhdk_app_stop(handle: ?*c.uhdk_app) abi.Status {
|
|||
c.z_native_finish();
|
||||
application.running = false;
|
||||
application.worker_id = null;
|
||||
return application.native_error;
|
||||
}
|
||||
|
||||
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 {
|
||||
if (handle == null) return;
|
||||
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.pending_head);
|
||||
application.scratch.deinit();
|
||||
|
|
|
|||
Loading…
Reference in New Issue