|
18 | 18 | #else
|
19 | 19 | #include <unistd.h>
|
20 | 20 | #endif
|
21 |
| -#include <stdatomic.h> |
| 21 | +#include <threads.h> |
22 | 22 | #include <volk/volk_prefs.h>
|
23 | 23 |
|
24 | 24 | void volk_get_config_path(char* path, bool read)
|
@@ -77,27 +77,44 @@ void volk_get_config_path(char* path, bool read)
|
77 | 77 | static struct volk_preferences {
|
78 | 78 | volk_arch_pref_t* volk_arch_prefs;
|
79 | 79 | size_t n_arch_prefs;
|
80 |
| - atomic_int initialized; |
| 80 | + int initialized; |
| 81 | + mtx_t mutex; |
81 | 82 |
|
82 | 83 | } volk_preferences;
|
83 | 84 |
|
| 85 | +void init_struct_mutex(void) |
| 86 | +{ |
| 87 | + if (mtx_init(&volk_preferences.mutex, mtx_plain) != thrd_success) { |
| 88 | + printf("\n mutex init failed\n"); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +static once_flag mutex_init_once_flag = ONCE_FLAG_INIT; |
| 93 | +void initialize_mutex() { call_once(&mutex_init_once_flag, init_struct_mutex); } |
84 | 94 |
|
85 | 95 | void volk_initialize_preferences()
|
86 | 96 | {
|
87 |
| - if (!atomic_fetch_and(&volk_preferences.initialized, 1)) { |
| 97 | + initialize_mutex(); |
| 98 | + mtx_lock(&volk_preferences.mutex); |
| 99 | + if (!volk_preferences.initialized) { |
88 | 100 | volk_preferences.n_arch_prefs =
|
89 | 101 | volk_load_preferences(&volk_preferences.volk_arch_prefs);
|
| 102 | + volk_preferences.initialized = 1; |
90 | 103 | }
|
| 104 | + mtx_unlock(&volk_preferences.mutex); |
91 | 105 | }
|
92 | 106 |
|
93 | 107 |
|
94 | 108 | void volk_free_preferences()
|
95 | 109 | {
|
| 110 | + initialize_mutex(); |
| 111 | + mtx_lock(&volk_preferences.mutex); |
96 | 112 | if (volk_preferences.initialized) {
|
97 | 113 | free(volk_preferences.volk_arch_prefs);
|
98 | 114 | volk_preferences.n_arch_prefs = 0;
|
99 | 115 | volk_preferences.initialized = 0;
|
100 | 116 | }
|
| 117 | + mtx_unlock(&volk_preferences.mutex); |
101 | 118 | }
|
102 | 119 |
|
103 | 120 |
|
|
0 commit comments