Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor HAVE_PTHREAD and _POSIX_THREADS #6536

Merged
merged 10 commits into from
Jun 26, 2023
13 changes: 11 additions & 2 deletions examples/benchmark/tls_bench.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions examples/client/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions examples/echoserver/echoserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@

static void SignalReady(void* args, word16 port)
{
#if defined(_POSIX_THREADS) && defined(NO_MAIN_DRIVER) && !defined(__MINGW32__)
#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;
Expand All @@ -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;
}
Expand Down
8 changes: 4 additions & 4 deletions examples/server/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1351,15 +1351,15 @@ 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);

/* client code will free srtp_secret buffer after checking for
correctness */
return 0;
}
#endif /* _POSIX_THREADS */
#endif /* HAVE_PTHREAD */

XFREE(srtp_secret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return 0;
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions tests/suites.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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

Expand Down
14 changes: 7 additions & 7 deletions tests/unit.c
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
#ifdef HAVE_PTHREAD
PTHREAD_CHECK_RET(pthread_mutex_lock(&args->signal->mutex));

if (!args->signal->ready)
Expand All @@ -287,18 +285,19 @@ 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
}


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__)
#elif defined(HAVE_PTHREAD)
PTHREAD_CHECK_RET(pthread_create(thread, 0, fun, args));
return;
#elif defined (WOLFSSL_TIRTOS)
Expand All @@ -313,16 +312,17 @@ 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
}


void join_thread(THREAD_TYPE thread)
{
#ifdef SINGLE_THREADED
#if defined(SINGLE_THREADED)
(void)thread;
#elif defined(_POSIX_THREADS) && !defined(__MINGW32__)
#elif defined(HAVE_PTHREAD)
PTHREAD_CHECK_RET(pthread_join(thread, 0));
#elif defined (WOLFSSL_TIRTOS)
while(1) {
Expand Down
13 changes: 7 additions & 6 deletions testsuite/testsuite.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(HAVE_PTHREAD)
PTHREAD_CHECK_RET(pthread_mutex_lock(&args->signal->mutex));

if (!args->signal->ready)
Expand Down Expand Up @@ -459,7 +459,7 @@ void wait_tcp_ready(func_args* args)
(void)args;
#else
(void)args;
#endif
#endif /* thread checks */
}


Expand All @@ -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(HAVE_PTHREAD)
PTHREAD_CHECK_RET(pthread_create(thread, 0, fun, args));
return;
#elif defined(WOLFSSL_TIRTOS)
Expand Down Expand Up @@ -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 */
}


Expand All @@ -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__)
#if defined(HAVE_PTHREAD)
PTHREAD_CHECK_RET(pthread_join(thread, 0));
#elif defined(WOLFSSL_TIRTOS)
while(1) {
Expand Down
5 changes: 2 additions & 3 deletions wolfcrypt/src/random.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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."
Expand Down
5 changes: 5 additions & 0 deletions wolfcrypt/test/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
typedef sword32 wc_test_ret_t;
#endif

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <wolfssl/wolfcrypt/settings.h>

#ifdef HAVE_STACK_SIZE
THREAD_RETURN WOLFSSL_THREAD wolfcrypt_test(void* args);
#else
Expand Down
32 changes: 19 additions & 13 deletions wolfssl/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
#ifndef wolfSSL_TEST_H
#define wolfSSL_TEST_H

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <wolfssl/wolfcrypt/settings.h>

#ifdef FUSION_RTOS
#include <fclstdio.h>
#include <fclstdlib.h>
Expand Down Expand Up @@ -182,7 +187,9 @@
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <pthread.h>
#ifdef HAVE_PTHREAD
dgarske marked this conversation as resolved.
Show resolved Hide resolved
#include <pthread.h>
#endif
#include <fcntl.h>
#ifdef TEST_IPV6
#include <netdb.h>
Expand Down Expand Up @@ -529,7 +536,7 @@ typedef struct tcp_ready {
word16 ready; /* predicate */
word16 port;
char* srfName; /* server ready file name */
#if defined(_POSIX_THREADS) && !defined(__MINGW32__)
#ifdef HAVE_PTHREAD
pthread_mutex_t mutex;
pthread_cond_t cond;
#endif
Expand All @@ -543,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__)
#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
}

Expand All @@ -558,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__)
#if defined(HAVE_PTHREAD)
PTHREAD_CHECK_RET(pthread_mutex_destroy(&ready->mutex));
PTHREAD_CHECK_RET(pthread_cond_destroy(&ready->cond));
#elif defined(NETOS)
Expand Down Expand Up @@ -599,22 +605,22 @@ 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;
char** argv;
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;
Expand Down Expand Up @@ -655,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)
{
Expand Down Expand Up @@ -2201,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__)
#if defined(HAVE_PTHREAD)
/* signal ready to accept data */
tcp_ready* ready = args->signal;
PTHREAD_CHECK_RET(pthread_mutex_lock(&ready->mutex));
Expand Down Expand Up @@ -2248,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__)
#if defined(NO_MAIN_DRIVER) && defined(HAVE_PTHREAD)
/* signal ready to tcp_accept */
if (args)
ready = args->signal;
Expand Down
Loading