13#define _POSIX_C_SOURCE 200809L
45#if defined(__unix__) || defined(__APPLE__) || defined(__linux__)
46#define SAPI_APPMANAGER_HAVE_POSIX_SIGNALS 1
50#define SAPI_APPMANAGER_HAVE_POSIX_SIGNALS 0
88 const char *stage_name,
89 uint32_t error_threshold)
99 (void)snprintf(msg,
sizeof(msg),
"%s failed: %d", stage_name, (
int)status);
103 (void)snprintf(msg,
sizeof(msg),
"Error threshold exceeded (%u/%u), shutting down",
154 for (i = 0U; i < 8U; i++) {
155 out[i] = (uint8_t)((value >> (8U * i)) & 0xFFU);
161 uint32_t low = (uint32_t)(signature & 0xFFFFFFFFULL);
162 uint32_t high = (uint32_t)((signature >> 32) & 0xFFFFFFFFULL);
174 uint8_t fold_buf[16];
184 (void)snprintf(text,
sizeof(text),
"%s", label);
185 }
else if (file != NULL) {
186 (void)snprintf(text,
sizeof(text),
"%s:%d", file, (
int)line);
208 if (config == NULL || config->
ops == NULL) {
213 if (config->
ops->
init == NULL ||
243 (void)snprintf(msg,
sizeof(msg),
244 "sapi_appmanager_run() called while an application is already running "
245 "(state=%d) - this is the single entry point, it cannot be re-entered concurrently",
296 (void)snprintf(msg,
sizeof(msg),
"Initialization failed: %d", (
int)status);
378 bool committed =
true;
429 (void)snprintf(msg,
sizeof(msg),
"Shutdown returned non-OK status: %d", (
int)status);
488#if SAPI_APPMANAGER_HAVE_POSIX_SIGNALS
493static void sapi_appmanager_signal_handler(
int signum)
511 memset(&sa, 0,
sizeof(sa));
512 sa.sa_handler = sapi_appmanager_signal_handler;
513 if (sigemptyset(&sa.sa_mask) != 0)
519 if (sigaction(SIGINT, &sa, NULL) != 0)
523 if (sigaction(SIGTERM, &sa, NULL) != 0)
void sapi_appmanager_request_shutdown(void)
Request application shutdown.
sapi_app_state_t sapi_appmanager_get_state(void)
Get current application state.
sapi_status_t sapi_appmanager_get_stats(sapi_appmanager_state_t *state)
Get application statistics.
uint32_t sapi_appmanager_checkpoint_fold_signature(uint64_t signature)
Pure helper: folds a 64-bit checkpoint signature down to the uint32_t sapi_checkpoint_config_t::check...
sapi_app_state_t
Application state enumeration.
int sapi_appmanager_run(const sapi_appmanager_config_t *config)
Run application with lifecycle management.
void sapi_appmanager_reset_state(void)
Forcibly resets the application manager's own bookkeeping (lifecycle state, iteration/error counters,...
#define SAPI_APPMANAGER_CHECKPOINT_SIGNATURE_SEED
Reset value of the per-cycle checkpoint signature before any marks are folded into it (ADR-034) - see...
sapi_status_t sapi_appmanager_install_default_signal_handlers(void)
POSIX-only convenience: installs SIGINT and SIGTERM handlers that call sapi_appmanager_request_shutdo...
void sapi_appmanager_checkpoint_mark(const char *file, int32_t line, const char *label)
Folds a hash of this call site into the current cycle's checkpoint signature (ADR-034).
@ SAPI_APP_STATE_SHUTDOWN
@ SAPI_APP_STATE_SHUTTING_DOWN
@ SAPI_APP_STATE_INITIALIZING
@ SAPI_APP_STATE_UNINITIALIZED
sapi_status_t sapi_channel_checkpoint(sapi_voter_t *voter, const sapi_checkpoint_config_t *config)
Performs one bounded checkpoint rendezvous across every channel registered with a voter.
uint64_t sapi_crc64_t
CRC-64 checksum value (64-bit).
sapi_crc64_t sapi_checksum_crc64(const uint8_t *data, size_t size)
Compute CRC-64 for data buffer.
void sapi_lifecycle_lock(void)
Locks the application's setup phase.
void sapi_lifecycle_unlock(void)
Unlocks the application's setup phase (setup is allowed again).
void sapi_log_write_event(sapi_log_level_t level, const char *site, uint32_t cycle, const char *source, const char *destination, const char *type, const char *info, const char *extra_fields)
Emits one structured inter-channel event/message-trail log line - for a message actually sent/receive...
void sapi_log_write(sapi_log_level_t level, const char *tag, const char *message)
Emits one log message. Non-blocking; never fails the caller's control flow even if the message is dro...
sapi_status_t
Common result/status codes.
@ SAPI_STATUS_INTERNAL_ERROR
@ SAPI_STATUS_NOT_SUPPORTED
@ SAPI_STATUS_INVALID_PARAM
static bool g_checkpoint_signature_active
True once g_checkpoint_signature has been reset for the CURRENT sapi_appmanager_run() cycle - see sap...
static void sapi_appmanager_checkpoint_signature_reset(void)
Resets g_checkpoint_signature to a fixed seed and marks it active for the current cycle - called once...
static uint64_t g_checkpoint_signature
Running per-cycle checkpoint-mark signature (ADR-034) - see sapi_appmanager_checkpoint_mark()'s own d...
static bool sapi_appmanager_handle_stage_result(sapi_status_t status, const char *stage_name, uint32_t error_threshold)
Records the outcome of one per-cycle stage (checkpoint, pre_execute, execute, or post_execute) agains...
static void sapi_appmanager_encode_u64_le(uint8_t out[8], uint64_t value)
Packs a uint64_t into an 8-byte buffer, explicit little-endian - same convention sapi_checkpoint....
static sapi_appmanager_state_t g_app_state
Global application state (single instance; no dynamic allocation).
static volatile int g_shutdown_requested
Set by the installed signal handler (or sapi_appmanager_request_shutdown()) to request that sapi_appm...
Application Manager abstraction for safeAPIFramework applications.
Bounded checkpoint rendezvous for distributed vital channels (ADR-017).
Checksum and CRC utilities for data integrity in redundant systems.
Process-wide application setup-phase lock (ADR-026).
OS Abstraction Layer - Logging/diagnostics service.
OS Abstraction Layer - Timer service.
uint32_t expected_node_count
sapi_duration_ms_t max_delay_ms
Application manager configuration.
const sapi_appmanager_checkpoint_config_t * checkpoint
const sapi_appmanager_operations_t * ops
sapi_status_t(*) init(void *context)
Initialize application.
sapi_status_t(*) on_checkpoint_result(void *context, bool committed)
Optional per-cycle checkpoint outcome hook (ADR-034).
sapi_status_t(*) execute(void *context)
Execute main application logic.
sapi_status_t(*) shutdown(void *context)
Shutdown application.
const char *(*) get_name(void)
Get human-readable application name.
sapi_status_t(*) pre_execute(void *context)
Optional per-cycle input/prepare stage (ADR-019).
sapi_status_t(*) post_execute(void *context)
Optional per-cycle output/cleanup stage (ADR-019).
const char *(*) get_version(void)
Get application version string.
Application manager runtime state.
Checkpoint configuration.
uint32_t expected_node_count
sapi_duration_ms_t max_delay_ms