Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3e3d08d

Browse files
committedFeb 27, 2021
threadsafe: Load preferences threadsafe
Instead of static variables in a function, we store preferences in a struct and use an `atomic_int` to prevent any more than one thread from loading preferences. Fixes gnuradio#440 Signed-off-by: Johannes Demel <[email protected]>
1 parent f41deb6 commit 3e3d08d

File tree

3 files changed

+52
-7
lines changed

3 files changed

+52
-7
lines changed
 

‎include/volk/volk_prefs.h

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ typedef struct volk_arch_pref {
1313
char impl_u[128]; // best unaligned impl
1414
} volk_arch_pref_t;
1515

16+
17+
VOLK_API void volk_initialize_preferences();
18+
VOLK_API void volk_free_preferences();
19+
VOLK_API const size_t volk_get_num_arch_prefs();
20+
VOLK_API const volk_arch_pref_t* volk_get_arch_prefs();
21+
1622
////////////////////////////////////////////////////////////////////////
1723
// get path to volk_config profiling info; second arguments specifies
1824
// if config file should be tested on existence for reading.

‎lib/volk_prefs.c

+43
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#else
1010
#include <unistd.h>
1111
#endif
12+
#include <stdatomic.h>
1213
#include <volk/volk_prefs.h>
1314

1415
void volk_get_config_path(char* path, bool read)
@@ -63,6 +64,48 @@ void volk_get_config_path(char* path, bool read)
6364
return;
6465
}
6566

67+
68+
static struct volk_preferences {
69+
volk_arch_pref_t* volk_arch_prefs;
70+
size_t n_arch_prefs;
71+
atomic_int initialized;
72+
73+
} volk_preferences;
74+
75+
76+
void volk_initialize_preferences()
77+
{
78+
if (!atomic_fetch_and(&volk_preferences.initialized, 1)) {
79+
volk_preferences.n_arch_prefs =
80+
volk_load_preferences(&volk_preferences.volk_arch_prefs);
81+
}
82+
}
83+
84+
85+
void volk_free_preferences()
86+
{
87+
if (volk_preferences.initialized) {
88+
free(volk_preferences.volk_arch_prefs);
89+
volk_preferences.n_arch_prefs = 0;
90+
volk_preferences.initialized = 0;
91+
}
92+
}
93+
94+
95+
const size_t volk_get_num_arch_prefs()
96+
{
97+
volk_initialize_preferences();
98+
return volk_preferences.n_arch_prefs;
99+
}
100+
101+
102+
const volk_arch_pref_t* volk_get_arch_prefs()
103+
{
104+
volk_initialize_preferences();
105+
return volk_preferences.volk_arch_prefs;
106+
}
107+
108+
66109
size_t volk_load_preferences(volk_arch_pref_t** prefs_res)
67110
{
68111
FILE* config_file;

‎lib/volk_rank_archs.c

+3-7
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,9 @@ int volk_rank_archs(const char* kern_name, // name of the kernel to rank
5353
)
5454
{
5555
size_t i;
56-
static volk_arch_pref_t* volk_arch_prefs;
57-
static size_t n_arch_prefs = 0;
58-
static int prefs_loaded = 0;
59-
if (!prefs_loaded) {
60-
n_arch_prefs = volk_load_preferences(&volk_arch_prefs);
61-
prefs_loaded = 1;
62-
}
56+
57+
const volk_arch_pref_t* volk_arch_prefs = volk_get_arch_prefs();
58+
const size_t n_arch_prefs = volk_get_num_arch_prefs();
6359

6460
// If we've defined VOLK_GENERIC to be anything, always return the
6561
// 'generic' kernel. Used in GR's QA code.

0 commit comments

Comments
 (0)
Please sign in to comment.