From 688bdb431f3d20d5c349e69cba5c130e08c800ba Mon Sep 17 00:00:00 2001 From: Jolene Tan <32562011+zxjtan@users.noreply.github.com> Date: Wed, 31 Jan 2024 03:17:56 -0500 Subject: [PATCH] find_package(Threads) regardless of BUILD_ONLY_LIB (#1653) * find_package(Threads) regardless of BUILD_ONLY_LIB * New macro OQS_USE_PTHREADS conditioned on embedded --- .CMake/compiler_opts.cmake | 14 ++++++++------ 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 | 2 +- tests/test_kem.c | 6 +++--- tests/test_sig.c | 6 +++--- 8 files changed, 24 insertions(+), 22 deletions(-) diff --git a/.CMake/compiler_opts.cmake b/.CMake/compiler_opts.cmake index 2b0aa31f85..252805dd73 100644 --- a/.CMake/compiler_opts.cmake +++ b/.CMake/compiler_opts.cmake @@ -104,9 +104,10 @@ if(CMAKE_C_COMPILER_ID MATCHES "Clang") add_link_options("-Wl,-z,noexecstack") endif() - if(NOT ${OQS_BUILD_ONLY_LIB}) - set(THREADS_PREFER_PTHREAD_FLAG ON) - find_package(Threads) + set(THREADS_PREFER_PTHREAD_FLAG ON) + find_package(Threads) + if (CMAKE_USE_PTHREADS_INIT AND NOT OQS_EMBEDDED_BUILD) + set(OQS_USE_PTHREADS ON) endif() if(${OQS_DEBUG_BUILD}) @@ -165,9 +166,10 @@ elseif(CMAKE_C_COMPILER_ID STREQUAL "GNU") endif() endif() - if(NOT ${OQS_BUILD_ONLY_LIB}) - set(THREADS_PREFER_PTHREAD_FLAG ON) - find_package(Threads) + set(THREADS_PREFER_PTHREAD_FLAG ON) + find_package(Threads) + if (CMAKE_USE_PTHREADS_INIT AND NOT OQS_EMBEDDED_BUILD) + set(OQS_USE_PTHREADS ON) endif() if(${OQS_DEBUG_BUILD}) diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 0a6d5182f0..79172404c1 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -99,7 +99,7 @@ else() target_compile_definitions(internal PRIVATE OQS_HAVE_GETENTROPY) endif() endif() -if(CMAKE_USE_PTHREADS_INIT) +if(OQS_USE_PTHREADS) target_link_libraries(common PRIVATE Threads::Threads) target_link_libraries(internal 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 e564c8d2b9..ff68438829 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -31,7 +31,7 @@ endif() # List oqs-internal after oqs so that oqs has linking precedence. set(TEST_DEPS oqs oqs-internal ${LIBM}) -if(CMAKE_USE_PTHREADS_INIT) +if(OQS_USE_PTHREADS) set(TEST_DEPS ${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;