Skip to content

Commit

Permalink
set_available_cpu_extensions with pthread_once
Browse files Browse the repository at this point in the history
  • Loading branch information
zxjtan committed Jan 19, 2024
1 parent 6a0aa38 commit 40eccd3
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/common/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
#include <stdlib.h>
#include <string.h>

#if defined(OQS_DIST_BUILD) && defined(OQS_USE_PTHREADS)
#include <pthread.h>
#endif

#if !defined(OQS_HAVE_POSIX_MEMALIGN) || defined(__MINGW32__) || defined(__MINGW64__) || defined(_MSC_VER)
#include <malloc.h>
#endif
Expand All @@ -30,6 +34,9 @@ CRYPTO_ONCE OQS_ONCE_STATIC_FREE;
/* Identifying the CPU is expensive so we cache the results in cpu_ext_data */
#if defined(OQS_DIST_BUILD)
static unsigned int cpu_ext_data[OQS_CPU_EXT_COUNT] = {0};
#if defined(OQS_USE_PTHREADS)
static pthread_once_t once_control = PTHREAD_ONCE_INIT;
#endif
#endif

#if defined(OQS_DIST_X86_64_BUILD)
Expand Down Expand Up @@ -206,9 +213,13 @@ static void set_available_cpu_extensions(void) {

OQS_API int OQS_CPU_has_extension(OQS_CPU_EXT ext) {
#if defined(OQS_DIST_BUILD)
#if defined(OQS_USE_PTHREADS)
pthread_once(&once_control, &set_available_cpu_extensions);
#else
if (0 == cpu_ext_data[OQS_CPU_EXT_INIT]) {
set_available_cpu_extensions();
}
#endif
if (0 < ext && ext < OQS_CPU_EXT_COUNT) {
return (int)cpu_ext_data[ext];
}
Expand Down

0 comments on commit 40eccd3

Please sign in to comment.