From 87c31d55640ac86fb098e7643e974173559ac6b8 Mon Sep 17 00:00:00 2001 From: gojimmypi Date: Thu, 22 Jun 2023 14:46:16 -0700 Subject: [PATCH 1/9] HAVE_PTHREAD gate in test.h --- wolfssl/test.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/wolfssl/test.h b/wolfssl/test.h index bc057ab3b7..38174c90eb 100644 --- a/wolfssl/test.h +++ b/wolfssl/test.h @@ -182,7 +182,9 @@ #include #include #include - #include + #ifdef HAVE_PTHREAD + #include + #endif #include #ifdef TEST_IPV6 #include From 6a4fbf7ba41db4721c5510db0007469464d3eb2c Mon Sep 17 00:00:00 2001 From: gojimmypi Date: Thu, 22 Jun 2023 16:08:47 -0700 Subject: [PATCH 2/9] add config.h and settings.h to test.h --- wolfssl/test.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/wolfssl/test.h b/wolfssl/test.h index 38174c90eb..b6ad1cc70c 100644 --- a/wolfssl/test.h +++ b/wolfssl/test.h @@ -28,6 +28,9 @@ #ifndef wolfSSL_TEST_H #define wolfSSL_TEST_H +#ifdef HAVE_CONFIG_H + #include +#endif #ifdef FUSION_RTOS #include #include @@ -40,6 +43,7 @@ #ifdef HAVE_ERRNO_H #include #endif +#include #include #include #include From ba787bffac443e4c1b375e3f3bcf3fe45759ce76 Mon Sep 17 00:00:00 2001 From: gojimmypi Date: Thu, 22 Jun 2023 16:27:33 -0700 Subject: [PATCH 3/9] added config.h and settings.h to other test.h in wolfcrypt/test --- wolfcrypt/test/test.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/wolfcrypt/test/test.h b/wolfcrypt/test/test.h index d49f5f013a..7e454653bc 100644 --- a/wolfcrypt/test/test.h +++ b/wolfcrypt/test/test.h @@ -28,6 +28,10 @@ extern "C" { #endif +#ifdef HAVE_CONFIG_H + #include +#endif + #ifdef HAVE_STACK_SIZE THREAD_RETURN WOLFSSL_THREAD wolfcrypt_test(void* args); #else @@ -69,6 +73,7 @@ int wolf_test_task(void); #if defined(HAVE_ERRNO_H) && !defined(NO_FILESYSTEM) && \ !defined(NO_STDIO_FILESYSTEM) && !defined(WOLFSSL_USER_IO) #include +#include #define WC_TEST_RET_ENC_ERRNO WC_TEST_RET_ENC(WC_TEST_RET_LN, errno, WC_TEST_RET_TAG_ERRNO) #else #define WC_TEST_RET_ENC_ERRNO WC_TEST_RET_ENC_NC From 1f2f5bc90d82faecdb6c69bc947ddf0ae109ca65 Mon Sep 17 00:00:00 2001 From: gojimmypi Date: Fri, 23 Jun 2023 07:44:55 -0700 Subject: [PATCH 4/9] settings #ifdef _POSIX_THREADS HAVE_PTHREAD --- wolfssl/wolfcrypt/settings.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/wolfssl/wolfcrypt/settings.h b/wolfssl/wolfcrypt/settings.h index 0234a377fc..aaf4ab489e 100644 --- a/wolfssl/wolfcrypt/settings.h +++ b/wolfssl/wolfcrypt/settings.h @@ -52,6 +52,12 @@ extern "C" { #endif +/* pick up compiler def; may need to turn on HAVE_PTHREAD for some configs */ +#ifdef _POSIX_THREADS + #undef HAVE_PTHREAD + #define HAVE_PTHREAD +#endif + /* This flag allows wolfSSL to include options.h instead of having client * projects do it themselves. This should *NEVER* be defined when building * wolfSSL as it can cause hard to debug problems. */ From 59c3c424465add892876dfd43a3f35af6545ba6b Mon Sep 17 00:00:00 2001 From: gojimmypi Date: Fri, 23 Jun 2023 08:11:50 -0700 Subject: [PATCH 5/9] cyassl settings _POSIX_THREADS HAVE_PTHREAD --- cyassl/ctaocrypt/settings.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cyassl/ctaocrypt/settings.h b/cyassl/ctaocrypt/settings.h index 46012d4d13..876a5e0c19 100644 --- a/cyassl/ctaocrypt/settings.h +++ b/cyassl/ctaocrypt/settings.h @@ -31,6 +31,12 @@ extern "C" { #endif +/* pick up compiler def; may need to turn on HAVE_PTHREAD for some configs */ +#ifdef _POSIX_THREADS + #undef HAVE_PTHREAD + #define HAVE_PTHREAD +#endif + /* Uncomment next line if using IPHONE */ /* #define IPHONE */ From 0336b07fde3d2a7abaa98de7b9b0c95b21794720 Mon Sep 17 00:00:00 2001 From: gojimmypi Date: Fri, 23 Jun 2023 08:44:24 -0700 Subject: [PATCH 6/9] undo cyassl _POSIX_THREADS HAVE_PTHREAD --- cyassl/ctaocrypt/settings.h | 6 ------ 1 file changed, 6 deletions(-) diff --git a/cyassl/ctaocrypt/settings.h b/cyassl/ctaocrypt/settings.h index 876a5e0c19..46012d4d13 100644 --- a/cyassl/ctaocrypt/settings.h +++ b/cyassl/ctaocrypt/settings.h @@ -31,12 +31,6 @@ extern "C" { #endif -/* pick up compiler def; may need to turn on HAVE_PTHREAD for some configs */ -#ifdef _POSIX_THREADS - #undef HAVE_PTHREAD - #define HAVE_PTHREAD -#endif - /* Uncomment next line if using IPHONE */ /* #define IPHONE */ From b6960671bfc510a08b1c1989487434da2c8e3031 Mon Sep 17 00:00:00 2001 From: gojimmypi Date: Fri, 23 Jun 2023 08:46:54 -0700 Subject: [PATCH 7/9] move settings.h #include in both test.h --- wolfcrypt/test/test.h | 2 +- wolfssl/test.h | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/wolfcrypt/test/test.h b/wolfcrypt/test/test.h index 7e454653bc..f3ca1f374d 100644 --- a/wolfcrypt/test/test.h +++ b/wolfcrypt/test/test.h @@ -31,6 +31,7 @@ #ifdef HAVE_CONFIG_H #include #endif +#include #ifdef HAVE_STACK_SIZE THREAD_RETURN WOLFSSL_THREAD wolfcrypt_test(void* args); @@ -73,7 +74,6 @@ int wolf_test_task(void); #if defined(HAVE_ERRNO_H) && !defined(NO_FILESYSTEM) && \ !defined(NO_STDIO_FILESYSTEM) && !defined(WOLFSSL_USER_IO) #include -#include #define WC_TEST_RET_ENC_ERRNO WC_TEST_RET_ENC(WC_TEST_RET_LN, errno, WC_TEST_RET_TAG_ERRNO) #else #define WC_TEST_RET_ENC_ERRNO WC_TEST_RET_ENC_NC diff --git a/wolfssl/test.h b/wolfssl/test.h index b6ad1cc70c..6a66ffa497 100644 --- a/wolfssl/test.h +++ b/wolfssl/test.h @@ -31,6 +31,8 @@ #ifdef HAVE_CONFIG_H #include #endif +#include + #ifdef FUSION_RTOS #include #include @@ -43,7 +45,6 @@ #ifdef HAVE_ERRNO_H #include #endif -#include #include #include #include From 96c02871ef3bfcf0f316749d2117eca2d3a0fa32 Mon Sep 17 00:00:00 2001 From: gojimmypi Date: Fri, 23 Jun 2023 09:39:27 -0700 Subject: [PATCH 8/9] add !defined(SINGLE_THREADED) logic --- examples/echoserver/echoserver.c | 2 +- tests/unit.c | 6 +++--- testsuite/testsuite.c | 6 +++--- wolfssl/test.h | 10 +++++----- wolfssl/wolfcrypt/types.h | 2 +- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/examples/echoserver/echoserver.c b/examples/echoserver/echoserver.c index e839f0b0f5..2b241d0f3b 100644 --- a/examples/echoserver/echoserver.c +++ b/examples/echoserver/echoserver.c @@ -67,7 +67,7 @@ static void SignalReady(void* args, word16 port) { -#if defined(_POSIX_THREADS) && defined(NO_MAIN_DRIVER) && !defined(__MINGW32__) +#if defined(_POSIX_THREADS) && defined(NO_MAIN_DRIVER) && !defined(__MINGW32__) && !defined(SINGLE_THREADED) /* signal ready to tcp_accept */ func_args* server_args = (func_args*)args; tcp_ready* ready = server_args->signal; diff --git a/tests/unit.c b/tests/unit.c index 0f54f7a72e..f0b1e17454 100644 --- a/tests/unit.c +++ b/tests/unit.c @@ -277,7 +277,7 @@ void wait_tcp_ready(func_args* args) { #ifdef SINGLE_THREADED (void)args; -#elif defined(_POSIX_THREADS) && !defined(__MINGW32__) +#elif defined(_POSIX_THREADS) && !defined(__MINGW32__) && !defined(SINGLE_THREADED) PTHREAD_CHECK_RET(pthread_mutex_lock(&args->signal->mutex)); if (!args->signal->ready) @@ -298,7 +298,7 @@ void start_thread(THREAD_FUNC fun, func_args* args, THREAD_TYPE* thread) (void)fun; (void)args; (void)thread; -#elif defined(_POSIX_THREADS) && !defined(__MINGW32__) +#elif defined(_POSIX_THREADS) && !defined(__MINGW32__) && !defined(SINGLE_THREADED) PTHREAD_CHECK_RET(pthread_create(thread, 0, fun, args)); return; #elif defined (WOLFSSL_TIRTOS) @@ -322,7 +322,7 @@ void join_thread(THREAD_TYPE thread) { #ifdef SINGLE_THREADED (void)thread; -#elif defined(_POSIX_THREADS) && !defined(__MINGW32__) +#elif defined(_POSIX_THREADS) && !defined(__MINGW32__) && !defined(SINGLE_THREADED) PTHREAD_CHECK_RET(pthread_join(thread, 0)); #elif defined (WOLFSSL_TIRTOS) while(1) { diff --git a/testsuite/testsuite.c b/testsuite/testsuite.c index 1b6df1b168..965d53c036 100644 --- a/testsuite/testsuite.c +++ b/testsuite/testsuite.c @@ -431,7 +431,7 @@ static void simple_test(func_args* args) */ void wait_tcp_ready(func_args* args) { -#if defined(_POSIX_THREADS) && !defined(__MINGW32__) +#if defined(_POSIX_THREADS) && !defined(__MINGW32__) && !defined(SINGLE_THREADED) PTHREAD_CHECK_RET(pthread_mutex_lock(&args->signal->mutex)); if (!args->signal->ready) @@ -471,7 +471,7 @@ void wait_tcp_ready(func_args* args) */ void start_thread(THREAD_FUNC fun, func_args* args, THREAD_TYPE* thread) { -#if defined(_POSIX_THREADS) && !defined(__MINGW32__) +#if defined(_POSIX_THREADS) && !defined(__MINGW32__) && !defined(SINGLE_THREADED) PTHREAD_CHECK_RET(pthread_create(thread, 0, fun, args)); return; #elif defined(WOLFSSL_TIRTOS) @@ -540,7 +540,7 @@ void start_thread(THREAD_FUNC fun, func_args* args, THREAD_TYPE* thread) */ void join_thread(THREAD_TYPE thread) { -#if defined(_POSIX_THREADS) && !defined(__MINGW32__) +#if defined(_POSIX_THREADS) && !defined(__MINGW32__) && !defined(SINGLE_THREADED) PTHREAD_CHECK_RET(pthread_join(thread, 0)); #elif defined(WOLFSSL_TIRTOS) while(1) { diff --git a/wolfssl/test.h b/wolfssl/test.h index 6a66ffa497..28533e9801 100644 --- a/wolfssl/test.h +++ b/wolfssl/test.h @@ -536,7 +536,7 @@ typedef struct tcp_ready { word16 ready; /* predicate */ word16 port; char* srfName; /* server ready file name */ -#if defined(_POSIX_THREADS) && !defined(__MINGW32__) +#if defined(_POSIX_THREADS) && !defined(__MINGW32__) && !defined(SINGLE_THREADED) pthread_mutex_t mutex; pthread_cond_t cond; #endif @@ -551,7 +551,7 @@ static WC_INLINE void InitTcpReady(tcp_ready* ready) ready->port = 0; ready->srfName = NULL; #ifdef SINGLE_THREADED -#elif defined(_POSIX_THREADS) && !defined(__MINGW32__) +#elif defined(_POSIX_THREADS) && !defined(__MINGW32__) && !defined(SINGLE_THREADED) PTHREAD_CHECK_RET(pthread_mutex_init(&ready->mutex, 0)); PTHREAD_CHECK_RET(pthread_cond_init(&ready->cond, 0)); #elif defined(NETOS) @@ -567,7 +567,7 @@ static WC_INLINE void FreeTcpReady(tcp_ready* ready) { #ifdef SINGLE_THREADED (void)ready; -#elif defined(_POSIX_THREADS) && !defined(__MINGW32__) +#elif defined(_POSIX_THREADS) && !defined(__MINGW32__) && !defined(SINGLE_THREADED) PTHREAD_CHECK_RET(pthread_mutex_destroy(&ready->mutex)); PTHREAD_CHECK_RET(pthread_cond_destroy(&ready->cond)); #elif defined(NETOS) @@ -2208,7 +2208,7 @@ static WC_INLINE void udp_accept(SOCKET_T* sockfd, SOCKET_T* clientfd, #endif if (args != NULL && args->signal != NULL) { -#if defined(_POSIX_THREADS) && !defined(__MINGW32__) +#if defined(_POSIX_THREADS) && !defined(__MINGW32__) && !defined(SINGLE_THREADED) /* signal ready to accept data */ tcp_ready* ready = args->signal; PTHREAD_CHECK_RET(pthread_mutex_lock(&ready->mutex)); @@ -2255,7 +2255,7 @@ static WC_INLINE void tcp_accept(SOCKET_T* sockfd, SOCKET_T* clientfd, if(do_listen) { tcp_listen(sockfd, &port, useAnyAddr, udp, sctp); - #if defined(_POSIX_THREADS) && defined(NO_MAIN_DRIVER) && !defined(__MINGW32__) + #if defined(_POSIX_THREADS) && defined(NO_MAIN_DRIVER) && !defined(__MINGW32__) && !defined(SINGLE_THREADED) /* signal ready to tcp_accept */ if (args) ready = args->signal; diff --git a/wolfssl/wolfcrypt/types.h b/wolfssl/wolfcrypt/types.h index 05943d0afb..4e107dfd01 100644 --- a/wolfssl/wolfcrypt/types.h +++ b/wolfssl/wolfcrypt/types.h @@ -1352,7 +1352,7 @@ typedef struct w64wrapper { typedef size_t THREAD_TYPE; #define WOLFSSL_THREAD #elif (defined(_POSIX_THREADS) || defined(HAVE_PTHREAD)) && \ - !defined(__MINGW32__) + !defined(__MINGW32__) && !defined(SINGLE_THREADED) typedef void* THREAD_RETURN; typedef pthread_t THREAD_TYPE; #define WOLFSSL_THREAD From e742e14f531fd673433638338102f084379a0b16 Mon Sep 17 00:00:00 2001 From: gojimmypi Date: Sat, 24 Jun 2023 13:59:50 -0700 Subject: [PATCH 9/9] refactor HAVE_PTHREAD, _POSIX_THREADS --- examples/benchmark/tls_bench.c | 13 +++++++++++-- examples/client/client.c | 8 ++++---- examples/echoserver/echoserver.c | 4 ++-- examples/server/server.c | 8 ++++---- tests/suites.c | 6 +++--- tests/unit.c | 14 +++++++------- testsuite/testsuite.c | 13 +++++++------ wolfcrypt/src/random.c | 5 ++--- wolfssl/test.h | 23 +++++++++++------------ wolfssl/wolfcrypt/settings.h | 23 +++++++++++++++++------ wolfssl/wolfcrypt/types.h | 6 ++++-- 11 files changed, 72 insertions(+), 51 deletions(-) diff --git a/examples/benchmark/tls_bench.c b/examples/benchmark/tls_bench.c index de384cb214..ba41a59fb1 100644 --- a/examples/benchmark/tls_bench.c +++ b/examples/benchmark/tls_bench.c @@ -66,8 +66,17 @@ Or #endif /* PTHREAD requires server and client enabled */ -#if defined(HAVE_PTHREAD) && (defined(NO_WOLFSSL_CLIENT) || defined(NO_WOLFSSL_SERVER)) - #undef HAVE_PTHREAD +#if defined(NO_WOLFSSL_CLIENT) || defined(NO_WOLFSSL_SERVER) + #if defined(HAVE_PTHREAD) + #ifdef __GNUC__ /* GCC compiler */ + #pragma message "PTHREAD requires server and client enabled." + #elif defined(_MSC_VER) /* Microsoft Visual C++ compiler */ + #pragma message("PTHREAD requires server and client enabled.") + #else + #warning "PTHREAD requires server and client enabled." + #endif + #undef HAVE_PTHREAD + #endif #endif #ifdef HAVE_PTHREAD diff --git a/examples/client/client.c b/examples/client/client.c index a3c52449ce..3ba4f87480 100644 --- a/examples/client/client.c +++ b/examples/client/client.c @@ -1740,7 +1740,7 @@ static int client_srtp_test(WOLFSSL *ssl, func_args *args) size_t srtp_secret_length; byte *srtp_secret, *p; int ret; -#if !defined(SINGLE_THREADED) && defined(_POSIX_THREADS) +#ifdef HAVE_PTHREAD srtp_test_helper *srtp_helper = args->srtp_helper; byte *other_secret = NULL; size_t other_size = 0; @@ -1774,7 +1774,7 @@ static int client_srtp_test(WOLFSSL *ssl, func_args *args) printf("%02X", *p); printf("\n"); -#if !defined(SINGLE_THREADED) && defined(_POSIX_THREADS) +#ifdef HAVE_PTHREAD if (srtp_helper != NULL) { srtp_helper_get_ekm(srtp_helper, &other_secret, &other_size); @@ -1790,7 +1790,7 @@ static int client_srtp_test(WOLFSSL *ssl, func_args *args) /* we are delegated from server to free this buffer */ XFREE(other_secret, NULL, DYNAMIC_TYPE_TMP_BUFFER); } -#endif +#endif /* HAVE_PTHREAD */ XFREE(srtp_secret, NULL, DYNAMIC_TYPE_TMP_BUFFER); @@ -4546,7 +4546,7 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args) StartTCP(); -#if defined(WOLFSSL_SRTP) && !defined(SINGLE_THREADED) && defined(_POSIX_THREADS) +#if defined(WOLFSSL_SRTP) && defined(HAVE_PTHREAD) args.srtp_helper = NULL; #endif args.argc = argc; diff --git a/examples/echoserver/echoserver.c b/examples/echoserver/echoserver.c index 2b241d0f3b..c0e875f6d3 100644 --- a/examples/echoserver/echoserver.c +++ b/examples/echoserver/echoserver.c @@ -67,7 +67,7 @@ static void SignalReady(void* args, word16 port) { -#if defined(_POSIX_THREADS) && defined(NO_MAIN_DRIVER) && !defined(__MINGW32__) && !defined(SINGLE_THREADED) +#if defined(NO_MAIN_DRIVER) && defined(HAVE_PTHREAD) /* signal ready to tcp_accept */ func_args* server_args = (func_args*)args; tcp_ready* ready = server_args->signal; @@ -76,7 +76,7 @@ static void SignalReady(void* args, word16 port) ready->port = port; PTHREAD_CHECK_RET(pthread_cond_signal(&ready->cond)); PTHREAD_CHECK_RET(pthread_mutex_unlock(&ready->mutex)); -#endif +#endif /* NO_MAIN_DRIVER && HAVE_PTHREAD */ (void)args; (void)port; } diff --git a/examples/server/server.c b/examples/server/server.c index 9b695a44f9..326f3b28dd 100644 --- a/examples/server/server.c +++ b/examples/server/server.c @@ -1319,7 +1319,7 @@ static int server_srtp_test(WOLFSSL *ssl, func_args *args) size_t srtp_secret_length; byte *srtp_secret, *p; int ret; -#if !defined(SINGLE_THREADED) && defined(_POSIX_THREADS) +#ifdef HAVE_PTHREAD srtp_test_helper *srtp_helper = args->srtp_helper; #else (void)args; @@ -1351,7 +1351,7 @@ static int server_srtp_test(WOLFSSL *ssl, func_args *args) printf("%02X", *p); printf("\n"); -#if !defined(SINGLE_THREADED) && defined(_POSIX_THREADS) +#ifdef HAVE_PTHREAD if (srtp_helper != NULL) { srtp_helper_set_ekm(srtp_helper, srtp_secret, srtp_secret_length); @@ -1359,7 +1359,7 @@ static int server_srtp_test(WOLFSSL *ssl, func_args *args) correctness */ return 0; } -#endif /* _POSIX_THREADS */ +#endif /* HAVE_PTHREAD */ XFREE(srtp_secret, NULL, DYNAMIC_TYPE_TMP_BUFFER); return 0; @@ -3807,7 +3807,7 @@ THREAD_RETURN WOLFSSL_THREAD server_test(void* args) args.argv = argv; args.signal = &ready; args.return_code = 0; -#if defined(WOLFSSL_SRTP) && !defined(SINGLE_THREADED) && defined(_POSIX_THREADS) +#if defined(WOLFSSL_SRTP) && defined(HAVE_PTHREAD) args.srtp_helper = NULL; #endif InitTcpReady(&ready); diff --git a/tests/suites.c b/tests/suites.c index f97d7edde3..b8049a54cd 100644 --- a/tests/suites.c +++ b/tests/suites.c @@ -329,7 +329,7 @@ static int execute_test_case(int svr_argc, char** svr_argv, int reqClientCert; #endif -#if defined(WOLFSSL_SRTP) && !defined(SINGLE_THREADED) && defined(_POSIX_THREADS) +#if defined(WOLFSSL_SRTP) && defined(HAVE_PTHREAD) srtp_test_helper srtp_helper; #endif /* Is Valid Cipher and Version Checks */ @@ -460,7 +460,7 @@ static int execute_test_case(int svr_argc, char** svr_argv, InitTcpReady(&ready); -#if defined(WOLFSSL_SRTP) && !defined(SINGLE_THREADED) && defined(_POSIX_THREADS) +#if defined(WOLFSSL_SRTP) && defined(HAVE_PTHREAD) srtp_helper_init(&srtp_helper); cliArgs.srtp_helper = &srtp_helper; svrArgs.srtp_helper = &srtp_helper; @@ -580,7 +580,7 @@ static int execute_test_case(int svr_argc, char** svr_argv, #endif FreeTcpReady(&ready); -#if defined (WOLFSSL_SRTP) &&!defined(SINGLE_THREADED) && defined(_POSIX_THREADS) +#if defined (WOLFSSL_SRTP) && defined(HAVE_PTHREAD) srtp_helper_free(&srtp_helper); #endif diff --git a/tests/unit.c b/tests/unit.c index f0b1e17454..1f1bfd3276 100644 --- a/tests/unit.c +++ b/tests/unit.c @@ -275,9 +275,7 @@ int unit_test(int argc, char** argv) void wait_tcp_ready(func_args* args) { -#ifdef SINGLE_THREADED - (void)args; -#elif defined(_POSIX_THREADS) && !defined(__MINGW32__) && !defined(SINGLE_THREADED) +#ifdef HAVE_PTHREAD PTHREAD_CHECK_RET(pthread_mutex_lock(&args->signal->mutex)); if (!args->signal->ready) @@ -287,6 +285,7 @@ void wait_tcp_ready(func_args* args) PTHREAD_CHECK_RET(pthread_mutex_unlock(&args->signal->mutex)); #else + /* no threading wait or single threaded */ (void)args; #endif } @@ -294,11 +293,11 @@ void wait_tcp_ready(func_args* args) void start_thread(THREAD_FUNC fun, func_args* args, THREAD_TYPE* thread) { -#ifdef SINGLE_THREADED +#if defined(SINGLE_THREADED) (void)fun; (void)args; (void)thread; -#elif defined(_POSIX_THREADS) && !defined(__MINGW32__) && !defined(SINGLE_THREADED) +#elif defined(HAVE_PTHREAD) PTHREAD_CHECK_RET(pthread_create(thread, 0, fun, args)); return; #elif defined (WOLFSSL_TIRTOS) @@ -313,6 +312,7 @@ void start_thread(THREAD_FUNC fun, func_args* args, THREAD_TYPE* thread) } Task_yield(); #else + /* custom / external thread type */ *thread = (THREAD_TYPE)_beginthreadex(0, 0, fun, args, 0, 0); #endif } @@ -320,9 +320,9 @@ void start_thread(THREAD_FUNC fun, func_args* args, THREAD_TYPE* thread) void join_thread(THREAD_TYPE thread) { -#ifdef SINGLE_THREADED +#if defined(SINGLE_THREADED) (void)thread; -#elif defined(_POSIX_THREADS) && !defined(__MINGW32__) && !defined(SINGLE_THREADED) +#elif defined(HAVE_PTHREAD) PTHREAD_CHECK_RET(pthread_join(thread, 0)); #elif defined (WOLFSSL_TIRTOS) while(1) { diff --git a/testsuite/testsuite.c b/testsuite/testsuite.c index 965d53c036..a4e25a17b1 100644 --- a/testsuite/testsuite.c +++ b/testsuite/testsuite.c @@ -431,7 +431,7 @@ static void simple_test(func_args* args) */ void wait_tcp_ready(func_args* args) { -#if defined(_POSIX_THREADS) && !defined(__MINGW32__) && !defined(SINGLE_THREADED) +#if defined(HAVE_PTHREAD) PTHREAD_CHECK_RET(pthread_mutex_lock(&args->signal->mutex)); if (!args->signal->ready) @@ -459,7 +459,7 @@ void wait_tcp_ready(func_args* args) (void)args; #else (void)args; -#endif +#endif /* thread checks */ } @@ -471,7 +471,7 @@ void wait_tcp_ready(func_args* args) */ void start_thread(THREAD_FUNC fun, func_args* args, THREAD_TYPE* thread) { -#if defined(_POSIX_THREADS) && !defined(__MINGW32__) && !defined(SINGLE_THREADED) +#if defined(HAVE_PTHREAD) PTHREAD_CHECK_RET(pthread_create(thread, 0, fun, args)); return; #elif defined(WOLFSSL_TIRTOS) @@ -527,10 +527,11 @@ void start_thread(THREAD_FUNC fun, func_args* args, THREAD_TYPE* thread) { printf("Ethernet Bypass Application: failed to create idle thread!\n"); } - + /* end if NETOS */ #else + /* custom / external thread type */ *thread = (THREAD_TYPE)_beginthreadex(0, 0, fun, args, 0, 0); -#endif +#endif /* thread types */ } @@ -540,7 +541,7 @@ void start_thread(THREAD_FUNC fun, func_args* args, THREAD_TYPE* thread) */ void join_thread(THREAD_TYPE thread) { -#if defined(_POSIX_THREADS) && !defined(__MINGW32__) && !defined(SINGLE_THREADED) +#if defined(HAVE_PTHREAD) PTHREAD_CHECK_RET(pthread_join(thread, 0)); #elif defined(WOLFSSL_TIRTOS) while(1) { diff --git a/wolfcrypt/src/random.c b/wolfcrypt/src/random.c index d15b2927c9..18e0f6e725 100644 --- a/wolfcrypt/src/random.c +++ b/wolfcrypt/src/random.c @@ -874,8 +874,7 @@ static WC_INLINE word64 Entropy_TimeHiRes(void) return now.tv_nsec; } -#elif !defined(SINGLE_THREADED) && defined(_POSIX_THREADS) && \ - !defined(__MINGW32__) +#elif defined(HAVE_PTHREAD) /* Start and stop thread that counts as a proxy for time counter. */ #define ENTROPY_MEMUSE_THREADED @@ -983,7 +982,7 @@ static void Entropy_StopThread(void) entropy_thread_started = 0; } } - + /* end if defined(HAVE_PTHREAD) */ #else #error "No high precision time available for MemUse Entropy." diff --git a/wolfssl/test.h b/wolfssl/test.h index 28533e9801..a46e0c60b3 100644 --- a/wolfssl/test.h +++ b/wolfssl/test.h @@ -536,7 +536,7 @@ typedef struct tcp_ready { word16 ready; /* predicate */ word16 port; char* srfName; /* server ready file name */ -#if defined(_POSIX_THREADS) && !defined(__MINGW32__) && !defined(SINGLE_THREADED) +#ifdef HAVE_PTHREAD pthread_mutex_t mutex; pthread_cond_t cond; #endif @@ -550,12 +550,13 @@ static WC_INLINE void InitTcpReady(tcp_ready* ready) ready->ready = 0; ready->port = 0; ready->srfName = NULL; -#ifdef SINGLE_THREADED -#elif defined(_POSIX_THREADS) && !defined(__MINGW32__) && !defined(SINGLE_THREADED) +#if defined(HAVE_PTHREAD) PTHREAD_CHECK_RET(pthread_mutex_init(&ready->mutex, 0)); PTHREAD_CHECK_RET(pthread_cond_init(&ready->cond, 0)); #elif defined(NETOS) tx_mutex_create(&ready->mutex, "wolfSSL Lock", TX_INHERIT); +#else + /* no threading init or single threaded */ #endif } @@ -565,9 +566,7 @@ static WC_INLINE void InitTcpReady(tcp_ready* ready) static WC_INLINE void FreeTcpReady(tcp_ready* ready) { -#ifdef SINGLE_THREADED - (void)ready; -#elif defined(_POSIX_THREADS) && !defined(__MINGW32__) && !defined(SINGLE_THREADED) +#if defined(HAVE_PTHREAD) PTHREAD_CHECK_RET(pthread_mutex_destroy(&ready->mutex)); PTHREAD_CHECK_RET(pthread_cond_destroy(&ready->cond)); #elif defined(NETOS) @@ -606,14 +605,14 @@ typedef struct callback_functions { unsigned char doUdp:1; } callback_functions; -#if defined(WOLFSSL_SRTP) && !defined(SINGLE_THREADED) && defined(_POSIX_THREADS) +#if defined(WOLFSSL_SRTP) && defined(HAVE_PTHREAD) typedef struct srtp_test_helper { pthread_mutex_t mutex; pthread_cond_t cond; uint8_t* server_srtp_ekm; size_t server_srtp_ekm_size; } srtp_test_helper; -#endif +#endif /* WOLFSSL_SRTP HAVE_PTHREAD */ typedef struct func_args { int argc; @@ -621,7 +620,7 @@ typedef struct func_args { int return_code; tcp_ready* signal; callback_functions *callbacks; -#if defined(WOLFSSL_SRTP) && !defined(SINGLE_THREADED) && defined(_POSIX_THREADS) +#if defined(WOLFSSL_SRTP) && defined(HAVE_PTHREAD) srtp_test_helper* srtp_helper; #endif } func_args; @@ -662,7 +661,7 @@ static const word16 wolfSSLPort = 11111; extern int myoptind; extern char* myoptarg; -#if defined(WOLFSSL_SRTP) && !defined(SINGLE_THREADED) && defined(_POSIX_THREADS) +#if defined(WOLFSSL_SRTP) && defined(HAVE_PTHREAD) static WC_INLINE void srtp_helper_init(srtp_test_helper *srtp) { @@ -2208,7 +2207,7 @@ static WC_INLINE void udp_accept(SOCKET_T* sockfd, SOCKET_T* clientfd, #endif if (args != NULL && args->signal != NULL) { -#if defined(_POSIX_THREADS) && !defined(__MINGW32__) && !defined(SINGLE_THREADED) +#if defined(HAVE_PTHREAD) /* signal ready to accept data */ tcp_ready* ready = args->signal; PTHREAD_CHECK_RET(pthread_mutex_lock(&ready->mutex)); @@ -2255,7 +2254,7 @@ static WC_INLINE void tcp_accept(SOCKET_T* sockfd, SOCKET_T* clientfd, if(do_listen) { tcp_listen(sockfd, &port, useAnyAddr, udp, sctp); - #if defined(_POSIX_THREADS) && defined(NO_MAIN_DRIVER) && !defined(__MINGW32__) && !defined(SINGLE_THREADED) + #if defined(NO_MAIN_DRIVER) && defined(HAVE_PTHREAD) /* signal ready to tcp_accept */ if (args) ready = args->signal; diff --git a/wolfssl/wolfcrypt/settings.h b/wolfssl/wolfcrypt/settings.h index 12a57e0e89..9017c9357c 100644 --- a/wolfssl/wolfcrypt/settings.h +++ b/wolfssl/wolfcrypt/settings.h @@ -52,12 +52,6 @@ extern "C" { #endif -/* pick up compiler def; may need to turn on HAVE_PTHREAD for some configs */ -#ifdef _POSIX_THREADS - #undef HAVE_PTHREAD - #define HAVE_PTHREAD -#endif - /* This flag allows wolfSSL to include options.h instead of having client * projects do it themselves. This should *NEVER* be defined when building * wolfSSL as it can cause hard to debug problems. */ @@ -281,6 +275,23 @@ #include +/* AFTER user_settings.h is loaded, +** determine if POSIX multi-threaded: HAVE_PTHREAD */ +#if defined(SINGLE_THREADED) || defined(__MINGW32__) + /* Never HAVE_PTHREAD in single thread, or non-POSIX mode. + ** Reminder: MING32 is win32 threads, not POSIX threads */ + #undef HAVE_PTHREAD +#else + #ifdef _POSIX_THREADS + /* HAVE_PTHREAD == POSIX threads capable and enabled. */ + #undef HAVE_PTHREAD + #define HAVE_PTHREAD 1 + #else + /* Not manually disabled, but POSIX threads not found. */ + #undef HAVE_PTHREAD + #endif +#endif + #define WOLFSSL_MAKE_FIPS_VERSION(major, minor) (((major) * 256) + (minor)) #if !defined(HAVE_FIPS) #define WOLFSSL_FIPS_VERSION_CODE WOLFSSL_MAKE_FIPS_VERSION(0,0) diff --git a/wolfssl/wolfcrypt/types.h b/wolfssl/wolfcrypt/types.h index 4e107dfd01..e42b410465 100644 --- a/wolfssl/wolfcrypt/types.h +++ b/wolfssl/wolfcrypt/types.h @@ -31,6 +31,9 @@ decouple library dependencies with standard string, memory and so on. #ifndef WOLF_CRYPT_TYPES_H #define WOLF_CRYPT_TYPES_H + #ifdef HAVE_CONFIG_H + #include + #endif #include #include @@ -1351,8 +1354,7 @@ typedef struct w64wrapper { typedef unsigned int THREAD_RETURN; typedef size_t THREAD_TYPE; #define WOLFSSL_THREAD - #elif (defined(_POSIX_THREADS) || defined(HAVE_PTHREAD)) && \ - !defined(__MINGW32__) && !defined(SINGLE_THREADED) + #elif defined(HAVE_PTHREAD) typedef void* THREAD_RETURN; typedef pthread_t THREAD_TYPE; #define WOLFSSL_THREAD