From 5ecdd10ca49a6365e80530428a17a0b02582bc4c Mon Sep 17 00:00:00 2001 From: Jolene Tan Date: Thu, 18 Jan 2024 16:27:52 -0500 Subject: [PATCH] New macro OQS_USE_PTHREADS conditioned on embedded --- .CMake/compiler_opts.cmake | 6 ++++++ src/common/CMakeLists.txt | 2 +- src/common/sha3/xkcp_sha3.c | 6 +++--- src/common/sha3/xkcp_sha3x4.c | 6 +++--- src/oqsconfig.h.cmake | 4 ++-- tests/CMakeLists.txt | 4 ++-- tests/test_kem.c | 6 +++--- tests/test_sig.c | 6 +++--- 8 files changed, 23 insertions(+), 17 deletions(-) diff --git a/.CMake/compiler_opts.cmake b/.CMake/compiler_opts.cmake index 33fd2c24db..a958906917 100644 --- a/.CMake/compiler_opts.cmake +++ b/.CMake/compiler_opts.cmake @@ -106,6 +106,9 @@ if(CMAKE_C_COMPILER_ID MATCHES "Clang") set(THREADS_PREFER_PTHREAD_FLAG ON) find_package(Threads) + if (CMAKE_USE_PTHREADS_INIT AND OQS_EMBEDDED_BUILD) + set(OQS_USE_PTHREADS ON) + endif() if(${OQS_DEBUG_BUILD}) add_compile_options(-g3) @@ -165,6 +168,9 @@ elseif(CMAKE_C_COMPILER_ID STREQUAL "GNU") set(THREADS_PREFER_PTHREAD_FLAG ON) find_package(Threads) + if (CMAKE_USE_PTHREADS_INIT AND OQS_EMBEDDED_BUILD) + set(OQS_USE_PTHREADS ON) + endif() if(${OQS_DEBUG_BUILD}) add_compile_options (-Wstrict-overflow) diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 2c49f115b1..5e53e77484 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -89,7 +89,7 @@ else() target_compile_definitions(common PRIVATE OQS_HAVE_GETENTROPY) endif() endif() -if(CMAKE_USE_PTHREADS_INIT) +if(OQS_USE_PTHREADS) target_link_libraries(common PRIVATE Threads::Threads) endif() diff --git a/src/common/sha3/xkcp_sha3.c b/src/common/sha3/xkcp_sha3.c index 0cc425458c..e9192862ef 100644 --- a/src/common/sha3/xkcp_sha3.c +++ b/src/common/sha3/xkcp_sha3.c @@ -13,7 +13,7 @@ #include -#if CMAKE_USE_PTHREADS_INIT +#if OQS_USE_PTHREADS #include #endif #include @@ -26,7 +26,7 @@ #define KECCAK_CTX_BYTES (KECCAK_CTX_ALIGNMENT * \ ((_KECCAK_CTX_BYTES + KECCAK_CTX_ALIGNMENT - 1)/KECCAK_CTX_ALIGNMENT)) -#if CMAKE_USE_PTHREADS_INIT +#if OQS_USE_PTHREADS static pthread_once_t dispatch_once_control = PTHREAD_ONCE_INIT; #endif @@ -85,7 +85,7 @@ static void Keccak_Dispatch(void) { * that have not been permuted, or not-yet-squeezed bytes. **************************************************/ static void keccak_inc_reset(uint64_t *s) { -#if CMAKE_USE_PTHREADS_INIT +#if OQS_USE_PTHREADS pthread_once(&dispatch_once_control, Keccak_Dispatch); #else if (Keccak_Initialize_ptr == NULL) { diff --git a/src/common/sha3/xkcp_sha3x4.c b/src/common/sha3/xkcp_sha3x4.c index 762a11adce..fbd9a0e8c5 100644 --- a/src/common/sha3/xkcp_sha3x4.c +++ b/src/common/sha3/xkcp_sha3x4.c @@ -8,7 +8,7 @@ #include #include -#if CMAKE_USE_PTHREADS_INIT +#if OQS_USE_PTHREADS #include #endif #include @@ -21,7 +21,7 @@ #define KECCAK_X4_CTX_BYTES (KECCAK_X4_CTX_ALIGNMENT * \ ((_KECCAK_X4_CTX_BYTES + KECCAK_X4_CTX_ALIGNMENT - 1)/KECCAK_X4_CTX_ALIGNMENT)) -#if CMAKE_USE_PTHREADS_INIT +#if OQS_USE_PTHREADS static pthread_once_t dispatch_once_control = PTHREAD_ONCE_INIT; #endif @@ -65,7 +65,7 @@ static void Keccak_X4_Dispatch(void) { } static void keccak_x4_inc_reset(uint64_t *s) { -#if CMAKE_USE_PTHREADS_INIT +#if OQS_USE_PTHREADS pthread_once(&dispatch_once_control, Keccak_X4_Dispatch); #else if (Keccak_X4_Initialize_ptr == NULL) { diff --git a/src/oqsconfig.h.cmake b/src/oqsconfig.h.cmake index e664c274b6..8fba8a160f 100644 --- a/src/oqsconfig.h.cmake +++ b/src/oqsconfig.h.cmake @@ -18,8 +18,6 @@ #cmakedefine USE_SANITIZER "@USE_SANITIZER@" #cmakedefine CMAKE_BUILD_TYPE "@CMAKE_BUILD_TYPE@" -#cmakedefine CMAKE_USE_PTHREADS_INIT 1 - #cmakedefine OQS_USE_OPENSSL 1 #cmakedefine OQS_USE_AES_OPENSSL 1 #cmakedefine OQS_USE_SHA2_OPENSSL 1 @@ -27,6 +25,8 @@ #cmakedefine OQS_EMBEDDED_BUILD 1 +#cmakedefine OQS_USE_PTHREADS 1 + #cmakedefine OQS_USE_ADX_INSTRUCTIONS 1 #cmakedefine OQS_USE_AES_INSTRUCTIONS 1 #cmakedefine OQS_USE_AVX_INSTRUCTIONS 1 diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 05fb20a074..3d71d206c3 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -30,7 +30,7 @@ if(NOT WIN32) else() set(INTERNAL_TEST_DEPS ${LIBM}) endif() - if(CMAKE_USE_PTHREADS_INIT) + if(OQS_USE_PTHREADS) set(INTERNAL_TEST_DEPS ${INTERNAL_TEST_DEPS} Threads::Threads) endif() if(DEFINED SANITIZER_LD_FLAGS) @@ -62,7 +62,7 @@ else() endif() set(API_TEST_DEPS oqs ${LIBM}) -if(CMAKE_USE_PTHREADS_INIT) +if(OQS_USE_PTHREADS) set(API_TEST_DEPS ${API_TEST_DEPS} Threads::Threads) endif() diff --git a/tests/test_kem.c b/tests/test_kem.c index 07ff68e79e..b7c7baad85 100644 --- a/tests/test_kem.c +++ b/tests/test_kem.c @@ -7,7 +7,7 @@ #include -#if CMAKE_USE_PTHREADS_INIT +#if OQS_USE_PTHREADS #include #endif @@ -197,7 +197,7 @@ static void TEST_KEM_randombytes(uint8_t *random_array, size_t bytes_to_read) { } #endif -#if CMAKE_USE_PTHREADS_INIT +#if OQS_USE_PTHREADS struct thread_data { char *alg_name; OQS_STATUS rc; @@ -245,7 +245,7 @@ int main(int argc, char **argv) { #endif OQS_STATUS rc; -#if CMAKE_USE_PTHREADS_INIT +#if OQS_USE_PTHREADS #define MAX_LEN_KEM_NAME_ 64 // don't run Classic McEliece in threads because of large stack usage char no_thread_kem_patterns[][MAX_LEN_KEM_NAME_] = {"Classic-McEliece", "HQC-256-"}; diff --git a/tests/test_sig.c b/tests/test_sig.c index 831e54e272..eb3ab0af12 100644 --- a/tests/test_sig.c +++ b/tests/test_sig.c @@ -10,7 +10,7 @@ #include -#if CMAKE_USE_PTHREADS_INIT +#if OQS_USE_PTHREADS #include #endif @@ -174,7 +174,7 @@ static void TEST_SIG_randombytes(uint8_t *random_array, size_t bytes_to_read) { } #endif -#if CMAKE_USE_PTHREADS_INIT +#if OQS_USE_PTHREADS struct thread_data { char *alg_name; OQS_STATUS rc; @@ -222,7 +222,7 @@ int main(int argc, char **argv) { #endif OQS_STATUS rc; -#if CMAKE_USE_PTHREADS_INIT +#if OQS_USE_PTHREADS #define MAX_LEN_SIG_NAME_ 64 pthread_t thread; struct thread_data td;