engine: add function to stop engine thread

Signed-off-by: Ronald Caesar <github43132@proton.me>
This commit is contained in:
Ronald Caesar
2026-06-02 00:30:28 -04:00
parent 1c3880021f
commit 214e27381f
2 changed files with 24 additions and 0 deletions
+5
View File
@@ -113,6 +113,11 @@ extern "C"
/// Returns [`BAL_ENGINE_ALREADY_RUNNING`] if the thread is still running.
BAL_HOT bal_error_t bal_engine_run_thread(bal_engine_t *engine);
/// Stops the engine execution asynchronously.
///
/// This function is thread-safe and can be called from any thread.
BAL_HOT void bal_engine_stop_thread(bal_engine_t *engine);
/// Resets `engine` for the next compilation unit. This is a low-cost memory
/// operation designed to be called between translation units.
///
+19
View File
@@ -448,6 +448,25 @@ bal_engine_run_thread(bal_engine_t *engine)
return engine->status;
}
void
bal_engine_stop_thread(bal_engine_t *BAL_RESTRICT engine)
{
if (BAL_UNLIKELY(NULL == engine))
{
return;
}
if (BAL_UNLIKELY(NULL == engine->engine_state))
{
BAL_LOG_ERROR(&engine->logger, "Aborting function: engine state is NULL");
engine->status = BAL_ERROR_INVALID_ARGUMENT;
return;
}
internal_engine_state_t *BAL_RESTRICT engine_state = engine->engine_state;
atomic_store_explicit(&engine_state->thread_state.stop_requested, true, memory_order_release);
}
bal_error_t
bal_engine_reset(bal_engine_t *BAL_RESTRICT engine)
{