Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

threadsafe: Load preferences threadsafe #453

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions include/volk/volk_prefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ typedef struct volk_arch_pref {
char impl_u[128]; // best unaligned impl
} volk_arch_pref_t;


VOLK_API void volk_initialize_preferences();
VOLK_API void volk_free_preferences();
VOLK_API const size_t volk_get_num_arch_prefs();
VOLK_API const volk_arch_pref_t* volk_get_arch_prefs();

////////////////////////////////////////////////////////////////////////
// get path to volk_config profiling info; second arguments specifies
// if config file should be tested on existence for reading.
Expand Down
1 change: 1 addition & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ if(ORC_FOUND)
endif()
if(NOT MSVC)
target_link_libraries(volk PUBLIC m)
target_link_libraries(volk PRIVATE pthread)
endif()
set_target_properties(volk PROPERTIES VERSION ${VERSION})
set_target_properties(volk PROPERTIES SOVERSION ${SOVERSION})
Expand Down
73 changes: 73 additions & 0 deletions lib/volk_prefs.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
#else
#include <unistd.h>
#endif
#ifndef __STDC_NO_THREADS__
#include <threads.h>
#endif
#include <volk/volk_prefs.h>

void volk_get_config_path(char* path, bool read)
Expand Down Expand Up @@ -72,6 +75,76 @@ void volk_get_config_path(char* path, bool read)
return;
}


static struct volk_preferences {
volk_arch_pref_t* volk_arch_prefs;
size_t n_arch_prefs;
int initialized;
#ifndef __STDC_NO_THREADS__
mtx_t mutex;
#endif
} volk_preferences;

#ifndef __STDC_NO_THREADS__
void init_struct_mutex(void)
{
if (mtx_init(&volk_preferences.mutex, mtx_plain) != thrd_success) {
printf("\n mutex init failed\n");
}
}

static once_flag mutex_init_once_flag = ONCE_FLAG_INIT;
void initialize_mutex() { call_once(&mutex_init_once_flag, init_struct_mutex); }
#endif

void volk_initialize_preferences()
{
#ifndef __STDC_NO_THREADS__
initialize_mutex();
mtx_lock(&volk_preferences.mutex);
#endif
if (!volk_preferences.initialized) {
volk_preferences.n_arch_prefs =
volk_load_preferences(&volk_preferences.volk_arch_prefs);
volk_preferences.initialized = 1;
}
#ifndef __STDC_NO_THREADS__
mtx_unlock(&volk_preferences.mutex);
#endif
}


void volk_free_preferences()
{
#ifndef __STDC_NO_THREADS__
initialize_mutex();
mtx_lock(&volk_preferences.mutex);
#endif
if (volk_preferences.initialized) {
free(volk_preferences.volk_arch_prefs);
volk_preferences.n_arch_prefs = 0;
volk_preferences.initialized = 0;
}
#ifndef __STDC_NO_THREADS__
mtx_unlock(&volk_preferences.mutex);
#endif
}


const size_t volk_get_num_arch_prefs()
{
volk_initialize_preferences();
return volk_preferences.n_arch_prefs;
}


const volk_arch_pref_t* volk_get_arch_prefs()
{
volk_initialize_preferences();
return volk_preferences.volk_arch_prefs;
}


size_t volk_load_preferences(volk_arch_pref_t** prefs_res)
{
FILE* config_file;
Expand Down
10 changes: 3 additions & 7 deletions lib/volk_rank_archs.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,9 @@ int volk_rank_archs(const char* kern_name, // name of the kernel to rank
)
{
size_t i;
static volk_arch_pref_t* volk_arch_prefs;
static size_t n_arch_prefs = 0;
static int prefs_loaded = 0;
if (!prefs_loaded) {
n_arch_prefs = volk_load_preferences(&volk_arch_prefs);
prefs_loaded = 1;
}

const volk_arch_pref_t* volk_arch_prefs = volk_get_arch_prefs();
const size_t n_arch_prefs = volk_get_num_arch_prefs();

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