Line data Source code
1 : #include "cfd/core/cfd_init.h"
2 : #include "cfd/core/cfd_status.h"
3 : #include "cfd_threading_internal.h"
4 : #include <stdbool.h>
5 :
6 :
7 : /* Global initialization state */
8 : static cfd_atomic_int s_is_initialized = 0;
9 :
10 212 : cfd_status_t cfd_init(void) {
11 : // Check if already initialized (fast path)
12 212 : if (cfd_atomic_load(&s_is_initialized)) {
13 : return CFD_SUCCESS;
14 : }
15 :
16 : // Attempt to transition from 0 (uninitialized) to 1 (initialized)
17 211 : if (cfd_atomic_cas(&s_is_initialized, 0, 1)) {
18 : /*
19 : * This thread won the race to initialize.
20 : * Perform global initialization tasks here.
21 : */
22 : return CFD_SUCCESS;
23 : }
24 : // Another thread beat us to it or initialization is already done
25 : return CFD_SUCCESS;
26 : }
27 :
28 212 : void cfd_finalize(void) {
29 : // Attempt to transition from 1 to 0
30 212 : cfd_atomic_cas(&s_is_initialized, 1, 0);
31 212 : }
32 :
33 84 : int cfd_is_initialized(void) {
34 84 : return cfd_atomic_load(&s_is_initialized) ? 1 : 0;
35 : }
|