ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
rcu.h File Reference

Go to the source code of this file.

Functions

asciichat_error_t options_state_init (void)
 Initialize RCU options system.
asciichat_error_t options_state_set (const options_t *opts)
 Set options from a parsed options struct.
void options_state_destroy (void)
 Shutdown RCU options system.
const options_t * options_get (void)
 Get current options (lock-free read).
size_t options_retired_snapshot_count (void)
void options_cleanup_schema (void)
 Clean up schema resources.

Function Documentation

◆ options_cleanup_schema()

void options_cleanup_schema ( void )

Clean up schema resources.

Frees the dynamically allocated options schema array and all associated strings. Should be called at program shutdown after options_state_destroy(). Safe to call multiple times or if schema was never built.

◆ options_retired_snapshot_count()

size_t options_retired_snapshot_count ( void )

Number of retired borrowed snapshots retained until shutdown.

Definition at line 320 of file rcu.c.

320 {
321 if (!lifecycle_is_initialized(&g_options_lifecycle))
322 return 0;
323 mutex_lock(&g_deferred_free_mutex);
324 size_t count = g_retired_count;
325 mutex_unlock(&g_deferred_free_mutex);
326 return count;
327}

◆ options_state_destroy()

void options_state_destroy ( void )

Shutdown RCU options system.

Frees the current options struct and cleans up resources. Should be called at program shutdown after all threads have exited.

Definition at line 483 of file rcu.c.

483 {
484 // Check if initialized using lifecycle
485 if (!lifecycle_is_initialized(&g_options_lifecycle)) {
486 return;
487 }
488
489 flags_explicit_state_destroy();
490
491 /* Shutdown lifecycle to mark as no longer initialized */
492 lifecycle_shutdown(&g_options_lifecycle);
493
494 // Get current options pointer
495 options_t *current = atomic_ptr_load(&g_options);
496
497 // Clear the atomic pointer (prevent further reads)
498 atomic_ptr_store(&g_options, NULL);
499
500 // Free current struct
501 SAFE_FREE(current);
502
503 // Free all deferred structs
504 deferred_free_all();
505
506 // Destroy dynamically created mutexes
507 mutex_destroy(&g_options_write_mutex);
508 mutex_destroy(&g_deferred_free_mutex);
509
510 log_debug("Options state shutdown complete");
511}
void atomic_ptr_store(atomic_ptr_t *a, void *value)
Atomically store a pointer.
void * atomic_ptr_load(atomic_ptr_t *a)
Atomically load a pointer.
#define SAFE_FREE(ptr)
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:565
struct options_state options_t
Immutable, RCU-published snapshot of the application's runtime configuration.

References atomic_ptr_load(), atomic_ptr_store(), log_debug, and SAFE_FREE.

Referenced by asciichat_shared_destroy(), and mirror_cleanup().

◆ options_state_init()

asciichat_error_t options_state_init ( void )

Initialize RCU options system.

Must be called once at program startup before any threads access options. Allocates initial options struct and sets up atomic pointer.

Returns
ASCIICHAT_OK on success, error code on failure

Definition at line 382 of file rcu.c.

382 {
383 // RCU setters validate against the option registry, including when callers
384 // use the state API directly in tests or embedded integrations. Initialize
385 // the registry here so those callers observe the same metadata as the CLI.
387
388 // Detect fork: after fork(), child process must reinitialize
389 pid_t current_pid = platform_get_pid();
390 if ((lifecycle_is_initialized(&g_options_lifecycle) || g_init_pid != -1) &&
391 g_init_pid != current_pid) {
392 log_debug(
393 "Detected fork (parent PID %d, child PID %d) - resetting lifecycle",
394 g_init_pid, current_pid);
395 /* Reset lifecycle to allow reinitialization after fork */
396 lifecycle_shutdown(&g_options_lifecycle);
397 g_init_pid = current_pid;
398 }
399
400 // Check if already initialized in this process using lifecycle
401 if (lifecycle_is_initialized(&g_options_lifecycle) &&
402 g_init_pid == current_pid && atomic_ptr_load(&g_options) != NULL) {
403 log_warn("Options state already initialized");
404 return ASCIICHAT_OK;
405 }
406 if (lifecycle_is_initialized(&g_options_lifecycle) &&
407 atomic_ptr_load(&g_options) == NULL) {
408 lifecycle_shutdown(&g_options_lifecycle);
409 }
410
411 /* Use lifecycle to serialize initialization */
412 if (!lifecycle_init(&g_options_lifecycle, "options")) {
413 /* Another thread is initializing or already initialized */
414 if (lifecycle_is_initialized(&g_options_lifecycle)) {
415 return ASCIICHAT_OK;
416 }
418 "Failed to acquire options initialization lock");
419 }
420
421 // Initialize write mutex
422 if (mutex_init(&g_options_write_mutex, "options_write") != 0) {
423 lifecycle_shutdown(&g_options_lifecycle);
424 return SET_ERRNO(ERROR_THREAD, "Failed to initialize options write mutex");
425 }
426
427 // Initialize deferred free mutex
428 if (mutex_init(&g_deferred_free_mutex, "deferred_free") != 0) {
429 mutex_destroy(&g_options_write_mutex);
430 lifecycle_shutdown(&g_options_lifecycle);
431 return SET_ERRNO(ERROR_THREAD, "Failed to initialize deferred free mutex");
432 }
433
434 // Allocate initial options struct (will be populated later by
435 // options_state_populate_from_globals)
436 options_t *initial_opts = SAFE_MALLOC(sizeof(options_t), options_t *);
437 if (!initial_opts) {
438 mutex_destroy(&g_options_write_mutex);
439 mutex_destroy(&g_deferred_free_mutex);
440 lifecycle_shutdown(&g_options_lifecycle);
441 return SET_ERRNO(ERROR_MEMORY, "Failed to allocate initial options struct");
442 }
443
444 // Initialize all defaults using options_t_new()
445 *initial_opts = options_t_new();
446
447 // Publish initial struct (release semantics - make all fields visible to
448 // readers)
449 atomic_ptr_store(&g_options, initial_opts);
450
451 ++g_revision;
452 g_origin = OPTIONS_ORIGIN_CONFIG;
453 g_init_pid = current_pid; // Record PID to detect fork later
454 log_dev("Options state initialized with RCU pattern (PID %d)", current_pid);
455
456 return ASCIICHAT_OK;
457}
#define SET_ERRNO(code,...)
void registry_init_size(void)
Initialize registry size and metadata.
Definition core.c:117
#define SAFE_MALLOC(size, cast)
@ ERROR_INVALID_STATE
@ ERROR_MEMORY
Definition error_codes.h:55
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_THREAD
#define log_warn(...)
Log a WARN message.
Definition log/log.h:591
#define log_dev(...)
Log a DEV message (most verbose, development only).
Definition log/log.h:551
options_t options_t_new(void)
Initialize options by parsing command-line arguments.
Definition options.c:688
@ OPTIONS_ORIGIN_CONFIG
pid_t platform_get_pid(void)
Definition system.c:67

References ASCIICHAT_OK, atomic_ptr_load(), atomic_ptr_store(), ERROR_INVALID_STATE, ERROR_MEMORY, ERROR_THREAD, log_debug, log_dev, log_warn, OPTIONS_ORIGIN_CONFIG, options_t_new(), platform_get_pid(), registry_init_size(), SAFE_MALLOC, and SET_ERRNO.

Referenced by options_init().

◆ options_state_set()

asciichat_error_t options_state_set ( const options_t * opts)

Set options from a parsed options struct.

Called by options_init() after parsing to publish the options struct to RCU. This atomically replaces the current options with the provided struct.

Parameters
optsPointer to options struct to publish (will be copied)
Returns
ASCIICHAT_OK on success, error code on failure

Definition at line 459 of file rcu.c.

459 {
460 if (!opts) {
461 return SET_ERRNO(ERROR_INVALID_PARAM, "opts is NULL");
462 }
463
464 // Check initialization state using lifecycle
465 if (!lifecycle_is_initialized(&g_options_lifecycle)) {
466 return SET_ERRNO(
468 "Options state not initialized (call options_state_init first)");
469 }
470
471 mutex_lock(&g_options_write_mutex);
472 options_t *next = SAFE_MALLOC(sizeof(*next), options_t *);
473 *next = *opts;
474 options_t *previous = atomic_ptr_load(&g_options);
475 atomic_ptr_store(&g_options, next);
476 ++g_revision;
477 g_origin = OPTIONS_ORIGIN_CONFIG;
478 deferred_free_add(previous);
479 mutex_unlock(&g_options_write_mutex);
480 return ASCIICHAT_OK;
481}
@ ERROR_INVALID_PARAM

References ASCIICHAT_OK, atomic_ptr_load(), atomic_ptr_store(), ERROR_INVALID_PARAM, ERROR_INVALID_STATE, retired_options::next, OPTIONS_ORIGIN_CONFIG, SAFE_MALLOC, and SET_ERRNO.

Referenced by config_load_and_apply().