Implementation:AGenUI AGenUI Engine Lifecycle
| Knowledge Sources | |
|---|---|
| Domains | Software_Architecture, Concurrency |
| Last Updated | 2026-10-24 17:21 GMT |
Overview
Concrete API for initializing, accessing, and destroying the AGenUI engine singleton, provided by the AGenUI C++ core.
Description
The engine lifecycle API consists of three free functions declared in `agenui_engine_entry.h`: `initAGenUIEngine()`, `getAGenUIEngine()`, and `destroyAGenUIEngine()`. Together they implement a thread-safe singleton using `std::call_once` for initialization and `std::atomic` for the instance pointer.
`initAGenUIEngine()` creates the `AGenUIEngine` instance, calls `start()` to create all subsystems (FunctionCallManager, ComponentPropertySpecManager, TemplateRegistry, PathConfig, shared worker thread, MeasurementManagerImpl, engine context), and returns the `IAGenUIEngine*`. Subsequent calls return the same pointer without re-initialization.
`destroyAGenUIEngine()` atomically swaps the pointer to `nullptr`, calls `stop()` to tear down all subsystems in reverse order, then deletes the engine. The shared worker thread is destroyed first to guarantee no queued task runs after any object it references has been freed.
Usage
- Application launch: call `initAGenUIEngine()` once to get the engine instance.
- During runtime: call `getAGenUIEngine()` to access the engine from any module.
- Application shutdown: call `destroyAGenUIEngine()` after all surfaces are destroyed.
- Cross-platform: the same API is called from iOS, Android (via JNI), and HarmonyOS.
Code Reference
Source Location
- Repository: AGenUI
- Declaration: `core/include/agenui_engine_entry.h`
- Implementation: `core/src/module/agenui_engine_entry.cpp`
- Engine start/stop: `core/src/module/agenui_engine_impl.cpp`
Signature
// core/include/agenui_engine_entry.h
namespace agenui {
// Creates and initializes the engine singleton (thread-safe via std::call_once)
IAGenUIEngine* initAGenUIEngine();
// Returns the existing engine instance, or nullptr if not yet created
IAGenUIEngine* getAGenUIEngine();
// Destroys the engine singleton (thread-safe via atomic exchange)
void destroyAGenUIEngine();
// Engine internal lifecycle
class AGenUIEngine : public IAGenUIEngine {
void start(); // creates all subsystems in order
void stop(); // destroys all subsystems in reverse order
};
} // namespace agenui
Import
#include "agenui_engine_entry.h"
// Also: #include "agenui_engine.h" for the IAGenUIEngine interface
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| (none) | — | — | `initAGenUIEngine()` and `destroyAGenUIEngine()` take no parameters |
Outputs
| Name | Type | Description |
|---|---|---|
| initAGenUIEngine() returns | `IAGenUIEngine*` | Engine singleton pointer (never null after first call) |
| getAGenUIEngine() returns | `IAGenUIEngine*` | Current engine pointer, or nullptr if not initialized |
| destroyAGenUIEngine() returns | void | Engine and all subsystems destroyed |
| Subsystems created by start() | Various | FunctionCallManager, ComponentPropertySpecManager, TemplateRegistry, PathConfig, ThreadManager (shared worker thread), MeasurementManagerImpl, EngineContext |
Usage Examples
Basic Engine Initialization
#include "agenui_engine_entry.h"
#include "agenui_engine.h"
// 1. Initialize the engine (call once at app launch)
agenui::IAGenUIEngine* engine = agenui::initAGenUIEngine();
// 2. Use the engine: create surfaces, register functions, etc.
auto* surfaceManager = engine->createSurfaceManager();
// 3. At app shutdown, destroy the engine
agenui::destroyAGenUIEngine();
Accessing Engine from Anywhere
#include "agenui_engine_entry.h"
// Get the engine instance without initializing (returns nullptr if not yet created)
agenui::IAGenUIEngine* engine = agenui::getAGenUIEngine();
if (engine) {
// Safe to use
engine->setDayNightMode("dark");
}