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
2 changes: 1 addition & 1 deletion 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(_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;
Expand Down
6 changes: 3 additions & 3 deletions tests/unit.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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) {
Expand Down
6 changes: 3 additions & 3 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(_POSIX_THREADS) && !defined(__MINGW32__) && !defined(SINGLE_THREADED)
PTHREAD_CHECK_RET(pthread_mutex_lock(&args->signal->mutex));

if (!args->signal->ready)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions wolfcrypt/test/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
extern "C" {
#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
19 changes: 13 additions & 6 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__)
#if defined(_POSIX_THREADS) && !defined(__MINGW32__) && !defined(SINGLE_THREADED)
pthread_mutex_t mutex;
pthread_cond_t cond;
#endif
Expand All @@ -544,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)
Expand All @@ -560,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)
Expand Down Expand Up @@ -2201,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));
Expand Down Expand Up @@ -2248,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;
Expand Down
6 changes: 6 additions & 0 deletions wolfssl/wolfcrypt/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me. Now that this is in settings.h I think it best to change the _POSIX_THREADS elsewhere to use HAVE_PTHREAD. I also think you should add && !defined(SINGLE_THREADED) and remove it from the refactor you did.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool. I've applied your suggested changes and changed the PR description & title to reflect the global nature of this change

#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. */
Expand Down
2 changes: 1 addition & 1 deletion wolfssl/wolfcrypt/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down