ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
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_shutdown (void)
 Shutdown RCU options system.
 

Function Documentation

◆ 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 117 of file rcu.c.

117 {
118 if (g_options_initialized) {
119 log_warn("Options state already initialized");
120 return ASCIICHAT_OK;
121 }
122
123 // Initialize write mutex
124 if (mutex_init(&g_options_write_mutex) != 0) {
125 return SET_ERRNO(ERROR_THREAD, "Failed to initialize options write mutex");
126 }
127
128 // Initialize deferred free mutex
129 if (mutex_init(&g_deferred_free_mutex) != 0) {
130 mutex_destroy(&g_options_write_mutex);
131 return SET_ERRNO(ERROR_THREAD, "Failed to initialize deferred free mutex");
132 }
133
134 // Allocate initial options struct (will be populated later by options_state_populate_from_globals)
135 options_t *initial_opts = SAFE_MALLOC(sizeof(options_t), options_t *);
136 if (!initial_opts) {
137 mutex_destroy(&g_options_write_mutex);
138 mutex_destroy(&g_deferred_free_mutex);
139 return SET_ERRNO(ERROR_MEMORY, "Failed to allocate initial options struct");
140 }
141
142 // Zero-initialize (all fields start at 0/false/NULL)
143 memset(initial_opts, 0, sizeof(*initial_opts));
144
145 // Set non-zero defaults using defines from options.h
146 // These match the documented defaults in --help output
147
148 // Network defaults
149 SAFE_STRNCPY(initial_opts->port, OPT_PORT_DEFAULT, sizeof(initial_opts->port));
150 SAFE_STRNCPY(initial_opts->address, OPT_ADDRESS_DEFAULT, sizeof(initial_opts->address));
151 SAFE_STRNCPY(initial_opts->address6, OPT_ADDRESS6_DEFAULT, sizeof(initial_opts->address6));
152
153 // Server defaults
154 initial_opts->max_clients = OPT_MAX_CLIENTS_DEFAULT;
156
157 // Client defaults
163
164 // Color and rendering defaults (using zero values which are the correct defaults)
165 initial_opts->color_mode = COLOR_MODE_AUTO; // -1
166
167 // Publish initial struct (release semantics - make all fields visible to readers)
168 atomic_store_explicit(&g_options, initial_opts, memory_order_release);
169
170 g_options_initialized = true;
171 log_debug("Options state initialized with RCU pattern");
172
173 return ASCIICHAT_OK;
174}
#define SAFE_STRNCPY(dst, src, size)
Definition common.h:358
#define SAFE_MALLOC(size, cast)
Definition common.h:208
#define SET_ERRNO(code, context_msg,...)
Set error code with custom context message and log it.
@ ERROR_MEMORY
Definition error_codes.h:53
@ ASCIICHAT_OK
Definition error_codes.h:48
@ ERROR_THREAD
Definition error_codes.h:95
#define log_warn(...)
Log a WARN message.
#define log_debug(...)
Log a DEBUG message.
#define COLOR_MODE_AUTO
Backward compatibility aliases for color mode enum values.
Definition options.h:156
#define OPT_MICROPHONE_INDEX_DEFAULT
Default microphone device index (-1 means system default)
Definition options.h:237
#define OPT_RECONNECT_ATTEMPTS_DEFAULT
Default reconnect attempts (-1 means auto/infinite)
Definition options.h:243
#define OPT_COMPRESSION_LEVEL_DEFAULT
Default compression level (1-9)
Definition options.h:228
#define OPT_ADDRESS6_DEFAULT
Default IPv6 server address.
Definition options.h:222
#define OPT_PORT_DEFAULT
Default TCP port for client/server communication.
Definition options.h:216
#define OPT_WEBCAM_INDEX_DEFAULT
Default webcam device index.
Definition options.h:234
#define SNAPSHOT_DELAY_DEFAULT
Default snapshot delay in seconds.
Definition options.h:212
#define OPT_MAX_CLIENTS_DEFAULT
Default maximum concurrent clients (server only)
Definition options.h:225
#define OPT_ADDRESS_DEFAULT
Default server address for client connections.
Definition options.h:219
#define OPT_SPEAKERS_INDEX_DEFAULT
Default speakers device index (-1 means system default)
Definition options.h:240
int mutex_init(mutex_t *mutex)
Initialize a mutex.
int mutex_destroy(mutex_t *mutex)
Destroy a mutex.
Consolidated options structure.
Definition options.h:439
terminal_color_mode_t color_mode
Color mode (auto/none/16/256/truecolor)
Definition options.h:507
char port[256]
Server port number.
Definition options.h:464
int compression_level
zstd compression level (1-9)
Definition options.h:487
int microphone_index
Microphone device index (-1 = default)
Definition options.h:517
unsigned short int webcam_index
Webcam device index (0 = first)
Definition options.h:499
int max_clients
Maximum concurrent clients (server only)
Definition options.h:465
char address6[256]
IPv6 bind address (server only)
Definition options.h:463
int speakers_index
Speakers device index (-1 = default)
Definition options.h:518
double snapshot_delay
Snapshot delay in seconds.
Definition options.h:533
int reconnect_attempts
Number of reconnection attempts (-1=infinite, 0=none)
Definition options.h:494
char address[256]
Server address (client) or bind address (server)
Definition options.h:462

References options_state::address, options_state::address6, ASCIICHAT_OK, options_state::color_mode, COLOR_MODE_AUTO, options_state::compression_level, ERROR_MEMORY, ERROR_THREAD, log_debug, log_warn, options_state::max_clients, options_state::microphone_index, mutex_destroy(), mutex_init(), OPT_ADDRESS6_DEFAULT, OPT_ADDRESS_DEFAULT, OPT_COMPRESSION_LEVEL_DEFAULT, OPT_MAX_CLIENTS_DEFAULT, OPT_MICROPHONE_INDEX_DEFAULT, OPT_PORT_DEFAULT, OPT_RECONNECT_ATTEMPTS_DEFAULT, OPT_SPEAKERS_INDEX_DEFAULT, OPT_WEBCAM_INDEX_DEFAULT, options_state::port, options_state::reconnect_attempts, SAFE_MALLOC, SAFE_STRNCPY, SET_ERRNO, options_state::snapshot_delay, SNAPSHOT_DELAY_DEFAULT, options_state::speakers_index, and options_state::webcam_index.

Referenced by __attribute__(), and 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 176 of file rcu.c.

176 {
177 if (!opts) {
178 return SET_ERRNO(ERROR_INVALID_PARAM, "opts is NULL");
179 }
180
181 if (!g_options_initialized) {
182 return SET_ERRNO(ERROR_INVALID_STATE, "Options state not initialized (call options_state_init first)");
183 }
184
185 // Get current struct (should be the initial zero-initialized one during startup)
186 options_t *current = atomic_load_explicit(&g_options, memory_order_acquire);
187
188 // Copy provided options into current struct
189 // Note: No need for RCU update here since this is called during initialization
190 // before any reader threads exist
191 memcpy(current, opts, sizeof(options_t));
192
193 log_debug("Options state set from parsed struct");
194 return ASCIICHAT_OK;
195}
@ ERROR_INVALID_STATE
@ ERROR_INVALID_PARAM

References ASCIICHAT_OK, ERROR_INVALID_PARAM, ERROR_INVALID_STATE, log_debug, and SET_ERRNO.

Referenced by options_init().

◆ options_state_shutdown()

void options_state_shutdown ( 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 197 of file rcu.c.

197 {
198 if (!g_options_initialized) {
199 return;
200 }
201
202 // Get current options pointer
203 options_t *current = atomic_load_explicit(&g_options, memory_order_acquire);
204
205 // Clear the atomic pointer (prevent further reads)
206 atomic_store_explicit(&g_options, NULL, memory_order_release);
207
208 // Free current struct
209 SAFE_FREE(current);
210
211 // Free all deferred structs
212 deferred_free_all();
213
214 // Destroy mutexes
215 mutex_destroy(&g_options_write_mutex);
216 mutex_destroy(&g_deferred_free_mutex);
217
218 g_options_initialized = false;
219 log_debug("Options state shutdown complete");
220}
#define SAFE_FREE(ptr)
Definition common.h:320

References log_debug, mutex_destroy(), and SAFE_FREE.

Referenced by asciichat_shared_init(), and server_main().