Skip to content

Commit

Permalink
New macro OQS_USE_PTHREADS conditioned on embedded
Browse files Browse the repository at this point in the history
  • Loading branch information
zxjtan committed Jan 18, 2024
1 parent 068a2cf commit 5ecdd10
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 17 deletions.
6 changes: 6 additions & 0 deletions .CMake/compiler_opts.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
6 changes: 3 additions & 3 deletions src/common/sha3/xkcp_sha3.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#include <oqs/common.h>

#if CMAKE_USE_PTHREADS_INIT
#if OQS_USE_PTHREADS
#include <pthread.h>
#endif
#include <stddef.h>
Expand All @@ -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

Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions src/common/sha3/xkcp_sha3x4.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <oqs/common.h>
#include <oqs/oqsconfig.h>

#if CMAKE_USE_PTHREADS_INIT
#if OQS_USE_PTHREADS
#include <pthread.h>
#endif
#include <stddef.h>
Expand All @@ -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

Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/oqsconfig.h.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
#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
#cmakedefine OQS_USE_SHA3_OPENSSL 1

#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
Expand Down
4 changes: 2 additions & 2 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()

Expand Down
6 changes: 3 additions & 3 deletions tests/test_kem.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include <oqs/oqs.h>

#if CMAKE_USE_PTHREADS_INIT
#if OQS_USE_PTHREADS
#include <pthread.h>
#endif

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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-"};
Expand Down
6 changes: 3 additions & 3 deletions tests/test_sig.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#include <oqs/oqs.h>

#if CMAKE_USE_PTHREADS_INIT
#if OQS_USE_PTHREADS
#include <pthread.h>
#endif

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 5ecdd10

Please sign in to comment.