Browse Source

Port testing to wolfSSL threading interface

Juliusz Sosinowicz 9 months ago
parent
commit
67d6d438c5

+ 81 - 95
examples/benchmark/tls_bench.c

@@ -40,6 +40,8 @@ Or
     #include <wolfssl/options.h>
 #endif
 #include <wolfssl/wolfcrypt/settings.h>
+#include <wolfssl/wolfcrypt/types.h>
+#include <wolfssl/wolfcrypt/wc_port.h>
 #include <wolfssl/ssl.h>
 #include <wolfssl/wolfcrypt/hash.h> /* WC_MAX_DIGEST_SIZE */
 #include <wolfssl/test.h>
@@ -60,14 +62,15 @@ Or
 #include <sys/time.h>
 #include <errno.h>
 
-/* For testing no pthread support */
+/* For testing no threading support */
 #if 0
     #undef HAVE_PTHREAD
+    #define SINGLE_THREADED
 #endif
 
 /* PTHREAD requires server and client enabled */
 #if defined(NO_WOLFSSL_CLIENT) || defined(NO_WOLFSSL_SERVER)
-    #if defined(HAVE_PTHREAD)
+    #if !defined(SINGLE_THREADED)
         #ifdef __GNUC__  /* GCC compiler */
             #pragma message "PTHREAD requires server and client enabled."
         #elif defined(_MSC_VER) /* Microsoft Visual C++ compiler */
@@ -75,21 +78,17 @@ Or
         #else
             #warning "PTHREAD requires server and client enabled."
         #endif
-        #undef HAVE_PTHREAD
+        #define SINGLE_THREADED
     #endif
 #endif
 /* Conversely, if both server and client are enabled, we must require pthreads */
 #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) \
-    && !defined(HAVE_PTHREAD)
-    #error "pthreads must be enabled if building benchmark suite \
+    && defined(SINGLE_THREADED)
+    #error "threads must be enabled if building benchmark suite \
 to run both client and server. Please define HAVE_PTHREAD if your \
 platform supports it"
 #endif
 
-#ifdef HAVE_PTHREAD
-    #include <pthread.h>
-#endif
-
 #if 0
 #define BENCH_USE_NONBLOCK
 #endif
@@ -300,7 +299,7 @@ static struct group_info groups[] = {
 };
 #endif /* WOLFSSL_TLS13 && HAVE_SUPPORTED_CURVES */
 
-#ifdef HAVE_PTHREAD
+#ifndef SINGLE_THREADED
 typedef struct {
     unsigned char buf[MEM_BUFFER_SZ];
     int write_bytes;
@@ -308,9 +307,8 @@ typedef struct {
     int read_bytes;
     int read_idx;
 
-    pthread_t tid;
-    pthread_mutex_t mutex;
-    pthread_cond_t cond;
+    wolfSSL_Mutex mutex;
+    COND_TYPE cond;
 
     int done;
 } memBuf_t;
@@ -348,17 +346,17 @@ typedef struct {
     int doDTLS;
     struct sockaddr_in serverAddr;
     struct sockaddr_in clientAddr;
-#ifdef HAVE_PTHREAD
+#ifndef SINGLE_THREADED
     int serverReady;
     int clientOrserverOnly;
-    pthread_mutex_t dtls_mutex;
-    pthread_cond_t dtls_cond;
+    wolfSSL_Mutex dtls_mutex;
+    COND_TYPE dtls_cond;
 #endif
 #endif
     side_t client;
     side_t server;
 
-#ifdef HAVE_PTHREAD
+#ifndef SINGLE_THREADED
     int useLocalMem;
 
     /* client messages to server in memory */
@@ -396,16 +394,16 @@ static double gettime_secs(int reset)
 }
 
 
-#ifdef HAVE_PTHREAD
+#ifndef SINGLE_THREADED
 /* server send callback */
 static int ServerMemSend(info_t* info, char* buf, int sz)
 {
-    PTHREAD_CHECK_RET(pthread_mutex_lock(&info->to_client.mutex));
+    THREAD_CHECK_RET(wc_LockMutex(&info->to_client.mutex));
 
 #ifndef BENCH_USE_NONBLOCK
     /* check for overflow */
     if (info->to_client.write_idx + sz > MEM_BUFFER_SZ) {
-        PTHREAD_CHECK_RET(pthread_mutex_unlock(&info->to_client.mutex));
+        THREAD_CHECK_RET(wc_UnLockMutex(&info->to_client.mutex));
         fprintf(stderr, "ServerMemSend overflow\n");
         return -1;
     }
@@ -419,8 +417,8 @@ static int ServerMemSend(info_t* info, char* buf, int sz)
     info->to_client.write_idx += sz;
     info->to_client.write_bytes += sz;
 
-    PTHREAD_CHECK_RET(pthread_cond_signal(&info->to_client.cond));
-    PTHREAD_CHECK_RET(pthread_mutex_unlock(&info->to_client.mutex));
+    THREAD_CHECK_RET(wolfSSL_CondSignal(&info->to_client.cond));
+    THREAD_CHECK_RET(wc_UnLockMutex(&info->to_client.mutex));
 
 #ifdef BENCH_USE_NONBLOCK
     if (sz == 0) {
@@ -433,12 +431,12 @@ static int ServerMemSend(info_t* info, char* buf, int sz)
 /* server recv callback */
 static int ServerMemRecv(info_t* info, char* buf, int sz)
 {
-    PTHREAD_CHECK_RET(pthread_mutex_lock(&info->to_server.mutex));
+    THREAD_CHECK_RET(wc_LockMutex(&info->to_server.mutex));
 
 #ifndef BENCH_USE_NONBLOCK
     while (info->to_server.write_idx - info->to_server.read_idx < sz &&
             !info->to_client.done) {
-        PTHREAD_CHECK_RET(pthread_cond_wait(&info->to_server.cond,
+        THREAD_CHECK_RET(wolfSSL_CondWait(&info->to_server.cond,
                                             &info->to_server.mutex));
     }
 #else
@@ -457,7 +455,7 @@ static int ServerMemRecv(info_t* info, char* buf, int sz)
         info->to_server.write_bytes = info->to_server.write_idx = 0;
     }
 
-    PTHREAD_CHECK_RET(pthread_mutex_unlock(&info->to_server.mutex));
+    THREAD_CHECK_RET(wc_UnLockMutex(&info->to_server.mutex));
 
     if (info->to_client.done != 0) {
         return -1;
@@ -474,14 +472,14 @@ static int ServerMemRecv(info_t* info, char* buf, int sz)
 /* client send callback */
 static int ClientMemSend(info_t* info, char* buf, int sz)
 {
-    PTHREAD_CHECK_RET(pthread_mutex_lock(&info->to_server.mutex));
+    THREAD_CHECK_RET(wc_LockMutex(&info->to_server.mutex));
 
 #ifndef BENCH_USE_NONBLOCK
     /* check for overflow */
     if (info->to_server.write_idx + sz > MEM_BUFFER_SZ) {
         fprintf(stderr, "ClientMemSend overflow %d %d %d\n",
             info->to_server.write_idx, sz, MEM_BUFFER_SZ);
-        PTHREAD_CHECK_RET(pthread_mutex_unlock(&info->to_server.mutex));
+        THREAD_CHECK_RET(wc_UnLockMutex(&info->to_server.mutex));
         return -1;
     }
 #else
@@ -494,8 +492,8 @@ static int ClientMemSend(info_t* info, char* buf, int sz)
     info->to_server.write_idx += sz;
     info->to_server.write_bytes += sz;
 
-    PTHREAD_CHECK_RET(pthread_cond_signal(&info->to_server.cond));
-    PTHREAD_CHECK_RET(pthread_mutex_unlock(&info->to_server.mutex));
+    THREAD_CHECK_RET(wolfSSL_CondSignal(&info->to_server.cond));
+    THREAD_CHECK_RET(wc_UnLockMutex(&info->to_server.mutex));
 
 #ifdef BENCH_USE_NONBLOCK
     if (sz == 0) {
@@ -508,12 +506,12 @@ static int ClientMemSend(info_t* info, char* buf, int sz)
 /* client recv callback */
 static int ClientMemRecv(info_t* info, char* buf, int sz)
 {
-    PTHREAD_CHECK_RET(pthread_mutex_lock(&info->to_client.mutex));
+    THREAD_CHECK_RET(wc_LockMutex(&info->to_client.mutex));
 
 #ifndef BENCH_USE_NONBLOCK
     while (info->to_client.write_idx - info->to_client.read_idx < sz &&
             !info->to_server.done) {
-        PTHREAD_CHECK_RET(pthread_cond_wait(&info->to_client.cond,
+        THREAD_CHECK_RET(wolfSSL_CondWait(&info->to_client.cond,
                                             &info->to_client.mutex));
     }
 #else
@@ -532,7 +530,7 @@ static int ClientMemRecv(info_t* info, char* buf, int sz)
         info->to_client.write_bytes = info->to_client.write_idx = 0;
     }
 
-    PTHREAD_CHECK_RET(pthread_mutex_unlock(&info->to_client.mutex));
+    THREAD_CHECK_RET(wc_UnLockMutex(&info->to_client.mutex));
 
     if (info->to_server.done != 0) {
         return -1;
@@ -545,7 +543,7 @@ static int ClientMemRecv(info_t* info, char* buf, int sz)
 #endif
     return sz;
 }
-#endif /* HAVE_PTHREAD */
+#endif /* !SINGLE_THREADED */
 
 static int SocketRecv(int sockFd, char* buf, int sz)
 {
@@ -701,7 +699,7 @@ static int ServerSend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
 {
     info_t* info = (info_t*)ctx;
     (void)ssl;
-#ifdef HAVE_PTHREAD
+#ifndef SINGLE_THREADED
     if (info->useLocalMem) {
         return ServerMemSend(info, buf, sz);
     }
@@ -721,7 +719,7 @@ static int ServerRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
 {
     info_t* info = (info_t*)ctx;
     (void)ssl;
-#ifdef HAVE_PTHREAD
+#ifndef SINGLE_THREADED
     if (info->useLocalMem) {
         return ServerMemRecv(info, buf, sz);
     }
@@ -743,7 +741,7 @@ static int ClientSend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
 {
     info_t* info = (info_t*)ctx;
     (void)ssl;
-#ifdef HAVE_PTHREAD
+#ifndef SINGLE_THREADED
     if (info->useLocalMem) {
         return ClientMemSend(info, buf, sz);
     }
@@ -763,7 +761,7 @@ static int ClientRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
 {
     info_t* info = (info_t*)ctx;
     (void)ssl;
-#ifdef HAVE_PTHREAD
+#ifndef SINGLE_THREADED
     if (info->useLocalMem) {
         return ClientMemRecv(info, buf, sz);
     }
@@ -852,7 +850,7 @@ static int SetupSocketAndConnect(info_t* info, const char* host,
         }
 
         /* Connect to the server */
-    #ifdef HAVE_PTHREAD
+    #ifndef SINGLE_THREADED
         while ((info->serverListening == 0) && (info->server.shutdown == 0)) {
             if (info->showVerbose) {
                 fprintf(stderr, "Waiting for server to listen...\n");
@@ -1008,7 +1006,7 @@ static int bench_tls_client(info_t* info)
         int err;
     #endif
 
-    #ifdef HAVE_PTHREAD
+    #ifndef SINGLE_THREADED
         if (!info->useLocalMem)
     #endif
         {
@@ -1051,17 +1049,17 @@ static int bench_tls_client(info_t* info)
         wolfSSL_SetIOReadCtx(cli_ssl, info);
         wolfSSL_SetIOWriteCtx(cli_ssl, info);
 
-#if defined(HAVE_PTHREAD) && defined(WOLFSSL_DTLS)
+#if !defined(SINGLE_THREADED) && defined(WOLFSSL_DTLS)
         /* synchronize with server */
         if (info->doDTLS && !info->clientOrserverOnly) {
-            PTHREAD_CHECK_RET(pthread_mutex_lock(&info->dtls_mutex));
+            THREAD_CHECK_RET(wc_LockMutex(&info->dtls_mutex));
             if (info->serverReady != 1) {
-                PTHREAD_CHECK_RET(pthread_cond_wait(&info->dtls_cond,
+                THREAD_CHECK_RET(wolfSSL_CondWait(&info->dtls_cond,
                                                     &info->dtls_mutex));
             }
             /* for next loop */
             info->serverReady = 0;
-            PTHREAD_CHECK_RET(pthread_mutex_unlock(&info->dtls_mutex));
+            THREAD_CHECK_RET(wc_UnLockMutex(&info->dtls_mutex));
         }
 #endif
         /* perform connect */
@@ -1195,21 +1193,21 @@ exit:
     return ret;
 }
 
-#ifdef HAVE_PTHREAD
-static void* client_thread(void* args)
+#if !defined(SINGLE_THREADED) && defined(WOLFSSL_THREAD_NO_JOIN)
+static THREAD_RETURN WOLFSSL_THREAD_NO_JOIN client_thread(void* args)
 {
     int ret;
     info_t* info = (info_t*)args;
 
     ret = bench_tls_client(info);
 
-    PTHREAD_CHECK_RET(pthread_cond_signal(&info->to_server.cond));
+    THREAD_CHECK_RET(wolfSSL_CondSignal(&info->to_server.cond));
     info->to_client.done = 1;
     info->client.ret = ret;
 
-    return NULL;
+    WOLFSSL_RETURN_FROM_THREAD(NULL);
 }
-#endif /* HAVE_PTHREAD */
+#endif /* !SINGLE_THREADED */
 #endif /* !NO_WOLFSSL_CLIENT */
 
 
@@ -1289,12 +1287,12 @@ static int SocketWaitClient(info_t* info)
 #ifdef WOLFSSL_DTLS
     if (info->doDTLS) {
         char msg[64];
-#ifdef HAVE_PTHREAD
+#ifndef SINGLE_THREADED
         if (!info->clientOrserverOnly) {
-            PTHREAD_CHECK_RET(pthread_mutex_lock(&info->dtls_mutex));
+            THREAD_CHECK_RET(wc_LockMutex(&info->dtls_mutex));
             info->serverReady = 1;
-            PTHREAD_CHECK_RET(pthread_cond_signal(&info->dtls_cond));
-            PTHREAD_CHECK_RET(pthread_mutex_unlock(&info->dtls_mutex));
+            THREAD_CHECK_RET(wolfSSL_CondSignal(&info->dtls_cond));
+            THREAD_CHECK_RET(wc_UnLockMutex(&info->dtls_mutex));
         }
 #endif
         connd = (int)recvfrom(info->listenFd, (char *)msg, sizeof(msg),
@@ -1309,7 +1307,7 @@ static int SocketWaitClient(info_t* info)
     else
 #endif
     {
-    #ifdef HAVE_PTHREAD
+    #ifndef SINGLE_THREADED
         info->serverListening = 1;
     #endif
         if ((connd = accept(info->listenFd, (struct sockaddr*)&clientAddr,
@@ -1456,7 +1454,7 @@ static int bench_tls_server(info_t* info)
         int err;
     #endif
 
-    #ifdef HAVE_PTHREAD
+    #ifndef SINGLE_THREADED
         if (!info->useLocalMem)
     #endif
         {
@@ -1519,7 +1517,7 @@ static int bench_tls_server(info_t* info)
     #endif
         start = gettime_secs(0) - start;
         if (ret != WOLFSSL_SUCCESS) {
-        #ifdef HAVE_PTHREAD
+        #ifndef SINGLE_THREADED
             if (info->to_client.done) {
                 ret = 0; /* done - success */
             }
@@ -1567,7 +1565,7 @@ static int bench_tls_server(info_t* info)
 
             info->server_stats.rxTime += rxTime;
             if (ret < 0) {
-            #ifdef HAVE_PTHREAD
+            #ifndef SINGLE_THREADED
                 if (info->to_client.done) {
                     ret = 0; /* done - success */
                 }
@@ -1638,8 +1636,8 @@ exit:
     return ret;
 }
 
-#ifdef HAVE_PTHREAD
-static void* server_thread(void* args)
+#if !defined(SINGLE_THREADED) && defined(WOLFSSL_THREAD_NO_JOIN)
+static THREAD_RETURN WOLFSSL_THREAD_NO_JOIN server_thread(void* args)
 {
     int ret = 0;
     info_t* info = (info_t*)args;
@@ -1661,13 +1659,13 @@ static void* server_thread(void* args)
         }
     }
 
-    PTHREAD_CHECK_RET(pthread_cond_signal(&info->to_client.cond));
+    THREAD_CHECK_RET(wolfSSL_CondSignal(&info->to_client.cond));
     info->to_server.done = 1;
     info->server.ret = ret;
 
-    return NULL;
+    WOLFSSL_RETURN_FROM_THREAD(NULL);
 }
-#endif /* HAVE_PTHREAD */
+#endif /* !SINGLE_THREADED */
 #endif /* !NO_WOLFSSL_SERVER */
 
 
@@ -1739,7 +1737,7 @@ static void Usage(void)
 #ifdef DEBUG_WOLFSSL
     fprintf(stderr, "-d          Enable debug messages\n");
 #endif
-#ifdef HAVE_PTHREAD
+#ifndef SINGLE_THREADED
     fprintf(stderr, "-T <num>    Number of threaded server/client pairs (default %d)\n", NUM_THREAD_PAIRS);
     fprintf(stderr, "-m          Use local memory, not socket\n");
 #endif
@@ -1838,10 +1836,10 @@ int bench_tls(void* args)
     const char* argHost = BENCH_DEFAULT_HOST;
     int argPort = BENCH_DEFAULT_PORT;
     int argShowPeerInfo = 0;
-#ifdef HAVE_PTHREAD
+#ifndef SINGLE_THREADED
     int doShutdown;
 #endif
-#if !defined(NO_WOLFSSL_SERVER) || defined(HAVE_PTHREAD)
+#if !defined(NO_WOLFSSL_SERVER) || !defined(SINGLE_THREADED)
     int argLocalMem = 0;
     int listenFd = -1;
 #endif
@@ -1940,13 +1938,13 @@ int bench_tls(void* args)
                 break;
 
             case 'T' :
-            #ifdef HAVE_PTHREAD
+            #ifndef SINGLE_THREADED
                 argThreadPairs = atoi(myoptarg);
             #endif
                 break;
 
             case 'm':
-            #ifdef HAVE_PTHREAD
+            #ifndef SINGLE_THREADED
                 argLocalMem = 1;
             #endif
                 break;
@@ -1996,7 +1994,7 @@ int bench_tls(void* args)
     if (argServerOnly || argClientOnly) {
         argThreadPairs = 1;
     }
-#ifndef HAVE_PTHREAD
+#ifdef SINGLE_THREADED
     else {
         fprintf(stderr, "Threading is not enabled, so please use -s or -c to indicate side\n");
         Usage();
@@ -2102,7 +2100,7 @@ int bench_tls(void* args)
 
         #ifdef WOLFSSL_DTLS
                 info->doDTLS = doDTLS;
-        #ifdef HAVE_PTHREAD
+        #ifndef SINGLE_THREADED
                 info->serverReady = 0;
                 if (argServerOnly || argClientOnly) {
                     info->clientOrserverOnly = 1;
@@ -2110,7 +2108,7 @@ int bench_tls(void* args)
         #endif
         #endif
                 if (argClientOnly) {
-            #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT) && defined(HAVE_PTHREAD)
+            #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT) && !defined(SINGLE_THREADED)
                     /* to avoid to wait server forever */
                     info->serverListening = 1;
             #endif
@@ -2124,38 +2122,26 @@ int bench_tls(void* args)
             #endif
                 }
                 else {
-            #ifdef HAVE_PTHREAD
+            #if !defined(SINGLE_THREADED) && defined(WOLFSSL_THREAD_NO_JOIN)
                     info->useLocalMem = argLocalMem;
-                    PTHREAD_CHECK_RET(pthread_mutex_init(&info->to_server.mutex,
-                                                         NULL));
-                    PTHREAD_CHECK_RET(pthread_mutex_init(&info->to_client.mutex,
-                                                         NULL));
+                    THREAD_CHECK_RET(wc_InitMutex(&info->to_server.mutex));
+                    THREAD_CHECK_RET(wc_InitMutex(&info->to_client.mutex));
             #ifdef WOLFSSL_DTLS
-                    PTHREAD_CHECK_RET(pthread_mutex_init(&info->dtls_mutex,
-                                                         NULL));
-                    PTHREAD_CHECK_RET(pthread_cond_init(&info->dtls_cond,
-                                                        NULL));
+                    THREAD_CHECK_RET(wc_InitMutex(&info->dtls_mutex));
+                    THREAD_CHECK_RET(wolfSSL_CondInit(&info->dtls_cond));
             #endif
-                    PTHREAD_CHECK_RET(pthread_cond_init(&info->to_server.cond,
-                                                        NULL));
-                    PTHREAD_CHECK_RET(pthread_cond_init(&info->to_client.cond,
-                                                        NULL));
-
-                    PTHREAD_CHECK_RET(
-                        pthread_create(&info->to_server.tid, NULL,
-                                       server_thread, info));
-                    PTHREAD_CHECK_RET(
-                        pthread_create(&info->to_client.tid, NULL,
-                                       client_thread, info));
-
-                    /* State that we won't be joining this thread */
-                    PTHREAD_CHECK_RET(pthread_detach(info->to_server.tid));
-                    PTHREAD_CHECK_RET(pthread_detach(info->to_client.tid));
+                    THREAD_CHECK_RET(wolfSSL_CondInit(&info->to_server.cond));
+                    THREAD_CHECK_RET(wolfSSL_CondInit(&info->to_client.cond));
+
+                    THREAD_CHECK_RET(
+                        wolfSSL_NewThreadNoJoin(server_thread, info));
+                    THREAD_CHECK_RET(
+                        wolfSSL_NewThreadNoJoin(client_thread, info));
             #endif
                 }
             }
 
-    #ifdef HAVE_PTHREAD
+    #ifndef SINGLE_THREADED
             /* For threading, wait for completion */
             if (!argClientOnly && !argServerOnly) {
                 /* Wait until threads are marked done */
@@ -2175,7 +2161,7 @@ int bench_tls(void* args)
                     fprintf(stderr, "Shutdown complete\n");
                 }
             }
-    #endif /* HAVE_PTHREAD */
+    #endif /* !SINGLE_THREADED */
 
             if (argShowVerbose) {
                 /* print results */

+ 6 - 8
examples/client/client.c

@@ -1765,7 +1765,7 @@ static int client_srtp_test(WOLFSSL *ssl, func_args *args)
     size_t srtp_secret_length;
     byte *srtp_secret, *p;
     int ret;
-#ifdef HAVE_PTHREAD
+#ifdef WOLFSSL_COND
     srtp_test_helper *srtp_helper = args->srtp_helper;
     byte *other_secret = NULL;
     size_t other_size = 0;
@@ -1799,7 +1799,7 @@ static int client_srtp_test(WOLFSSL *ssl, func_args *args)
         printf("%02X", *p);
     printf("\n");
 
-#ifdef HAVE_PTHREAD
+#ifdef WOLFSSL_COND
     if (srtp_helper != NULL) {
         srtp_helper_get_ekm(srtp_helper, &other_secret, &other_size);
 
@@ -1815,7 +1815,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 /* HAVE_PTHREAD */
+#endif /* WOLFSSL_COND */
 
     XFREE(srtp_secret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
 
@@ -3983,7 +3983,7 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
         wolfSSL_CTX_free(ctx); ctx = NULL;
 
         ((func_args*)args)->return_code = 0;
-        return 0;
+        WOLFSSL_RETURN_FROM_THREAD(0);
     }
 
 #ifdef HAVE_ALPN
@@ -4559,9 +4559,7 @@ exit:
     (void) useVerifyCb;
     (void) customVerifyCert;
 
-#if !defined(WOLFSSL_TIRTOS)
-    return 0;
-#endif
+    WOLFSSL_RETURN_FROM_THREAD(0);
 }
 
 #endif /* !NO_WOLFSSL_CLIENT */
@@ -4577,7 +4575,7 @@ exit:
 
         StartTCP();
 
-#if defined(WOLFSSL_SRTP) && defined(HAVE_PTHREAD)
+#if defined(WOLFSSL_SRTP) && defined(WOLFSSL_COND)
         args.srtp_helper = NULL;
 #endif
         args.argc = argc;

+ 11 - 8
examples/echoserver/echoserver.c

@@ -67,16 +67,21 @@
 
 static void SignalReady(void* args, word16 port)
 {
-#if defined(NO_MAIN_DRIVER) && defined(HAVE_PTHREAD)
+#if defined(NO_MAIN_DRIVER) && defined(WOLFSSL_COND)
     /* signal ready to tcp_accept */
     func_args* server_args = (func_args*)args;
     tcp_ready* ready = server_args->signal;
-    PTHREAD_CHECK_RET(pthread_mutex_lock(&ready->mutex));
+    THREAD_CHECK_RET(wc_LockMutex(&ready->mutex));
     ready->ready = 1;
     ready->port = port;
-    PTHREAD_CHECK_RET(pthread_cond_signal(&ready->cond));
-    PTHREAD_CHECK_RET(pthread_mutex_unlock(&ready->mutex));
-#endif /* NO_MAIN_DRIVER && HAVE_PTHREAD */
+#ifdef COND_NO_REQUIRE_LOCKED_MUTEX
+    THREAD_CHECK_RET(wc_UnLockMutex(&ready->mutex));
+#endif
+    THREAD_CHECK_RET(wolfSSL_CondSignal(&ready->cond));
+#ifndef COND_NO_REQUIRE_LOCKED_MUTEX
+    THREAD_CHECK_RET(wc_UnLockMutex(&ready->mutex));
+#endif
+#endif /* NO_MAIN_DRIVER && WOLFSSL_COND */
     (void)args;
     (void)port;
 }
@@ -525,9 +530,7 @@ THREAD_RETURN WOLFSSL_THREAD echoserver_test(void* args)
     wolfAsync_DevClose(&devId);
 #endif
 
-#ifndef WOLFSSL_TIRTOS
-    return 0;
-#endif
+    WOLFSSL_RETURN_FROM_THREAD(0);
 }
 
 #endif /* !NO_WOLFSSL_SERVER */

+ 5 - 7
examples/server/server.c

@@ -1327,7 +1327,7 @@ static int server_srtp_test(WOLFSSL *ssl, func_args *args)
     size_t srtp_secret_length;
     byte *srtp_secret, *p;
     int ret;
-#ifdef HAVE_PTHREAD
+#ifdef WOLFSSL_COND
     srtp_test_helper *srtp_helper = args->srtp_helper;
 #else
     (void)args;
@@ -1359,7 +1359,7 @@ static int server_srtp_test(WOLFSSL *ssl, func_args *args)
         printf("%02X", *p);
     printf("\n");
 
-#ifdef HAVE_PTHREAD
+#ifdef WOLFSSL_COND
     if (srtp_helper != NULL) {
         srtp_helper_set_ekm(srtp_helper, srtp_secret, srtp_secret_length);
 
@@ -1367,7 +1367,7 @@ static int server_srtp_test(WOLFSSL *ssl, func_args *args)
            correctness */
         return 0;
     }
-#endif /* HAVE_PTHREAD */
+#endif /* WOLFSSL_COND */
 
     XFREE(srtp_secret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
     return 0;
@@ -3822,9 +3822,7 @@ exit:
 #if defined(WOLFSSL_CALLBACKS) && defined(WOLFSSL_EARLY_DATA)
     (void) earlyData;
 #endif
-#ifndef WOLFSSL_TIRTOS
-    return 0;
-#endif
+    WOLFSSL_RETURN_FROM_THREAD(0);
 }
 
 #endif /* !NO_WOLFSSL_SERVER */
@@ -3844,7 +3842,7 @@ exit:
         args.argv = argv;
         args.signal = &ready;
         args.return_code = 0;
-#if defined(WOLFSSL_SRTP) && defined(HAVE_PTHREAD)
+#if defined(WOLFSSL_SRTP) && defined(WOLFSSL_COND)
         args.srtp_helper = NULL;
 #endif
         InitTcpReady(&ready);

+ 3 - 3
src/crl.c

@@ -76,7 +76,7 @@ int InitCRL(WOLFSSL_CRL* crl, WOLFSSL_CERT_MANAGER* cm)
     crl->mfd = WOLFSSL_CRL_MFD_INIT_VAL;
     crl->setup = 0; /* thread setup done predicate */
     if (wolfSSL_CondInit(&crl->cond) != 0) {
-        WOLFSSL_MSG("Pthread condition init failed");
+        WOLFSSL_MSG("thread condition init failed");
         return BAD_COND_E;
     }
 #endif
@@ -229,14 +229,14 @@ void FreeCRL(WOLFSSL_CRL* crl, int dynamic)
         WOLFSSL_MSG("stopping monitor thread");
         if (StopMonitor(crl->mfd) == 0) {
             if (wolfSSL_JoinThread(crl->tid) != 0)
-                WOLFSSL_MSG("stop monitor failed in pthread_join");
+                WOLFSSL_MSG("stop monitor failed in wolfSSL_JoinThread");
         }
         else {
             WOLFSSL_MSG("stop monitor failed");
         }
     }
     if (wolfSSL_CondFree(&crl->cond) != 0)
-        WOLFSSL_MSG("pthread_cond_destroy failed in FreeCRL");
+        WOLFSSL_MSG("wolfSSL_CondFree failed in FreeCRL");
 #endif
     wc_FreeMutex(&crl->crlLock);
     if (dynamic)   /* free self */

+ 19 - 57
tests/api.c

@@ -6179,10 +6179,7 @@ static THREAD_RETURN WOLFSSL_THREAD test_server_nofail(void* args)
     }
     if (ctx == NULL) {
         /* Release the wait for TCP ready. */
-        PTHREAD_CHECK_RET(pthread_mutex_lock(&opts->signal->mutex));
-        opts->signal->ready = 1;
-        PTHREAD_CHECK_RET(pthread_cond_signal(&opts->signal->cond));
-        PTHREAD_CHECK_RET(pthread_mutex_unlock(&opts->signal->mutex));
+        signal_ready(opts->signal);
         goto done;
     }
 
@@ -6446,9 +6443,7 @@ done:
 
     wolfSSL_SetLoggingPrefix(NULL);
 
-#ifndef WOLFSSL_TIRTOS
-    return 0;
-#endif
+    WOLFSSL_RETURN_FROM_THREAD(0);
 }
 
 #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
@@ -6519,10 +6514,7 @@ static THREAD_RETURN WOLFSSL_THREAD test_server_loop(void* args)
                                                            != WOLFSSL_SUCCESS) {
         /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
         /* Release the wait for TCP ready. */
-        PTHREAD_CHECK_RET(pthread_mutex_lock(&opts->signal->mutex));
-        opts->signal->ready = 1;
-        PTHREAD_CHECK_RET(pthread_cond_signal(&opts->signal->cond));
-        PTHREAD_CHECK_RET(pthread_mutex_unlock(&opts->signal->mutex));
+        signal_ready(opts->signal);
         goto done;
     }
     if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
@@ -6530,10 +6522,7 @@ static THREAD_RETURN WOLFSSL_THREAD test_server_loop(void* args)
         /*err_sys("can't load server cert chain file, "
                 "Please run from wolfSSL home dir");*/
         /* Release the wait for TCP ready. */
-        PTHREAD_CHECK_RET(pthread_mutex_lock(&opts->signal->mutex));
-        opts->signal->ready = 1;
-        PTHREAD_CHECK_RET(pthread_cond_signal(&opts->signal->cond));
-        PTHREAD_CHECK_RET(pthread_mutex_unlock(&opts->signal->mutex));
+        signal_ready(opts->signal);
         goto done;
     }
     if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
@@ -6541,10 +6530,7 @@ static THREAD_RETURN WOLFSSL_THREAD test_server_loop(void* args)
         /*err_sys("can't load server key file, "
                 "Please run from wolfSSL home dir");*/
         /* Release the wait for TCP ready. */
-        PTHREAD_CHECK_RET(pthread_mutex_lock(&opts->signal->mutex));
-        opts->signal->ready = 1;
-        PTHREAD_CHECK_RET(pthread_cond_signal(&opts->signal->cond));
-        PTHREAD_CHECK_RET(pthread_mutex_unlock(&opts->signal->mutex));
+        signal_ready(opts->signal);
         goto done;
     }
     /* call ctx setup callback */
@@ -6555,11 +6541,7 @@ static THREAD_RETURN WOLFSSL_THREAD test_server_loop(void* args)
     while (count != loop_count) {
         ssl = wolfSSL_new(ctx);
         if (ssl == NULL) {
-            /* Release the wait for TCP ready. */
-            PTHREAD_CHECK_RET(pthread_mutex_lock(&opts->signal->mutex));
-            opts->signal->ready = 1;
-            PTHREAD_CHECK_RET(pthread_cond_signal(&opts->signal->cond));
-            PTHREAD_CHECK_RET(pthread_mutex_unlock(&opts->signal->mutex));
+            signal_ready(opts->signal);
             goto done;
         }
         if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
@@ -6567,10 +6549,7 @@ static THREAD_RETURN WOLFSSL_THREAD test_server_loop(void* args)
             /*err_sys("can't load server cert chain file, "
                     "Please run from wolfSSL home dir");*/
             /* Release the wait for TCP ready. */
-            PTHREAD_CHECK_RET(pthread_mutex_lock(&opts->signal->mutex));
-            opts->signal->ready = 1;
-            PTHREAD_CHECK_RET(pthread_cond_signal(&opts->signal->cond));
-            PTHREAD_CHECK_RET(pthread_mutex_unlock(&opts->signal->mutex));
+            signal_ready(opts->signal);
             goto done;
         }
         if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
@@ -6578,10 +6557,7 @@ static THREAD_RETURN WOLFSSL_THREAD test_server_loop(void* args)
             /*err_sys("can't load server key file, "
                     "Please run from wolfSSL home dir");*/
             /* Release the wait for TCP ready. */
-            PTHREAD_CHECK_RET(pthread_mutex_lock(&opts->signal->mutex));
-            opts->signal->ready = 1;
-            PTHREAD_CHECK_RET(pthread_cond_signal(&opts->signal->cond));
-            PTHREAD_CHECK_RET(pthread_mutex_unlock(&opts->signal->mutex));
+            signal_ready(opts->signal);
             goto done;
         }
 
@@ -6666,9 +6642,7 @@ done:
     wc_ecc_fp_free();  /* free per thread cache */
 #endif
 
-#ifndef WOLFSSL_TIRTOS
-    return 0;
-#endif
+    WOLFSSL_RETURN_FROM_THREAD(0);
 }
 #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
 
@@ -7498,9 +7472,7 @@ cleanup:
     wc_ecc_fp_free();  /* free per thread cache */
 #endif
 
-#ifndef WOLFSSL_TIRTOS
-    return 0;
-#endif
+    WOLFSSL_RETURN_FROM_THREAD(0);
 }
 
 /* TLS Client for API unit testing - generic */
@@ -9455,11 +9427,7 @@ static THREAD_RETURN WOLFSSL_THREAD tls_export_server(void* args)
 
     if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
         /*err_sys("SSL_write failed");*/
-#ifdef WOLFSSL_TIRTOS
-        return;
-#else
-        return 0;
-#endif
+        WOLFSSL_RETURN_FROM_THREAD(0);
     }
 
 #ifdef WOLFSSL_TIRTOS
@@ -9493,9 +9461,7 @@ done:
 #endif
 #endif
 
-#ifndef WOLFSSL_TIRTOS
-    return 0;
-#endif
+    WOLFSSL_RETURN_FROM_THREAD(0);
 }
 
 
@@ -34548,7 +34514,7 @@ static THREAD_RETURN WOLFSSL_THREAD server_task_ech(void* args)
     wc_ecc_fp_free();
 #endif
 
-    return 0;
+    WOLFSSL_RETURN_FROM_THREAD(0);
 }
 #endif /* HAVE_ECH && WOLFSSL_TLS13 */
 
@@ -38812,7 +38778,7 @@ static THREAD_RETURN WOLFSSL_THREAD test_logging(void* args)
     /* test that the 3 errors over the max were dropped */
     AssertIntEQ(errorCount, ERROR_QUEUE_MAX);
 
-    return 0;
+    WOLFSSL_RETURN_FROM_THREAD(0);
 }
 #endif
 
@@ -40770,7 +40736,7 @@ static THREAD_RETURN WOLFSSL_THREAD test_wolfSSL_BIO_accept_client(void* args)
     wc_ecc_fp_free();  /* free per thread cache */
 #endif
 
-    return 0;
+    WOLFSSL_RETURN_FROM_THREAD(0);
 }
 #endif
 
@@ -58999,7 +58965,7 @@ static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_server_thread(void* args)
     int  err = 0;
 
     if (!args)
-        return 0;
+        WOLFSSL_RETURN_FROM_THREAD(0);
 
     ((func_args*)args)->return_code = TEST_FAIL;
 
@@ -59115,9 +59081,7 @@ static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_server_thread(void* args)
 #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
     wc_ecc_fp_free();  /* free per thread cache */
 #endif
-#ifndef WOLFSSL_TIRTOS
-    return 0;
-#endif
+    WOLFSSL_RETURN_FROM_THREAD(0);
 }
 static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_client_thread(void* args)
 {
@@ -59133,7 +59097,7 @@ static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_client_thread(void* args)
     int  ret, err;
 
     if (!args)
-        return 0;
+        WOLFSSL_RETURN_FROM_THREAD(0);
 
     ((func_args*)args)->return_code = TEST_FAIL;
     callbacks   = ((func_args*)args)->callbacks;
@@ -59192,9 +59156,7 @@ static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_client_thread(void* args)
 #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
     wc_ecc_fp_free();  /* free per thread cache */
 #endif
-#ifndef WOLFSSL_TIRTOS
-    return 0;
-#endif
+    WOLFSSL_RETURN_FROM_THREAD(0);
 }
 #endif /* OPENSSL_EXTRA && WOLFSSL_ERROR_CODE_OPENSSL &&
           HAVE_IO_TESTS_DEPENDENCIES && !WOLFSSL_NO_TLS12 */

+ 3 - 3
tests/suites.c

@@ -330,7 +330,7 @@ static int execute_test_case(int svr_argc, char** svr_argv,
     int         reqClientCert;
 #endif
 
-#if defined(WOLFSSL_SRTP) && defined(HAVE_PTHREAD)
+#if defined(WOLFSSL_SRTP) && defined(WOLFSSL_COND)
     srtp_test_helper srtp_helper;
 #endif
     /* Is Valid Cipher and Version Checks */
@@ -461,7 +461,7 @@ static int execute_test_case(int svr_argc, char** svr_argv,
 
     InitTcpReady(&ready);
 
-#if defined(WOLFSSL_SRTP) && defined(HAVE_PTHREAD)
+#if defined(WOLFSSL_SRTP) && defined(WOLFSSL_COND)
     srtp_helper_init(&srtp_helper);
     cliArgs.srtp_helper = &srtp_helper;
     svrArgs.srtp_helper = &srtp_helper;
@@ -581,7 +581,7 @@ static int execute_test_case(int svr_argc, char** svr_argv,
 #endif
     FreeTcpReady(&ready);
 
-#if defined (WOLFSSL_SRTP) && defined(HAVE_PTHREAD)
+#if defined (WOLFSSL_SRTP) && defined(WOLFSSL_COND)
     srtp_helper_free(&srtp_helper);
 #endif
 

+ 0 - 74
tests/unit.c

@@ -271,77 +271,3 @@ exit:
 
     return ret;
 }
-
-
-
-void wait_tcp_ready(func_args* args)
-{
-#ifdef HAVE_PTHREAD
-    PTHREAD_CHECK_RET(pthread_mutex_lock(&args->signal->mutex));
-
-    if (!args->signal->ready)
-        PTHREAD_CHECK_RET(pthread_cond_wait(&args->signal->cond,
-                                            &args->signal->mutex));
-    args->signal->ready = 0; /* reset */
-
-    PTHREAD_CHECK_RET(pthread_mutex_unlock(&args->signal->mutex));
-#else
-    /* no threading wait or single threaded */
-    (void)args;
-#endif
-}
-
-#ifndef SINGLE_THREADED
-
-void start_thread(THREAD_CB fun, func_args* args, THREAD_TYPE* thread)
-{
-#if defined(SINGLE_THREADED)
-    (void)fun;
-    (void)args;
-    (void)thread;
-#elif defined(HAVE_PTHREAD)
-    PTHREAD_CHECK_RET(pthread_create(thread, 0, fun, args));
-    return;
-#elif defined (WOLFSSL_TIRTOS)
-    /* Initialize the defaults and set the parameters. */
-    Task_Params taskParams;
-    Task_Params_init(&taskParams);
-    taskParams.arg0 = (UArg)args;
-    taskParams.stackSize = 65535;
-    *thread = Task_create((Task_FuncPtr)fun, &taskParams, NULL);
-    if (*thread == NULL) {
-        fprintf(stderr, "Failed to create new Task\n");
-    }
-    Task_yield();
-#else
-    /* custom / external thread type */
-    *thread = (THREAD_TYPE)_beginthreadex(0, 0, fun, args, 0, 0);
-#endif
-}
-
-
-void join_thread(THREAD_TYPE thread)
-{
-#if defined(SINGLE_THREADED)
-    (void)thread;
-#elif defined(HAVE_PTHREAD)
-    PTHREAD_CHECK_RET(pthread_join(thread, 0));
-#elif defined (WOLFSSL_TIRTOS)
-    while(1) {
-        if (Task_getMode(thread) == Task_Mode_TERMINATED) {
-            Task_sleep(5);
-            break;
-        }
-        Task_yield();
-    }
-#else
-    int res = WaitForSingleObject((HANDLE)thread, INFINITE);
-    assert(res == WAIT_OBJECT_0);
-    res = CloseHandle((HANDLE)thread);
-    assert(res);
-    (void)res; /* Suppress un-used variable warning */
-#endif
-}
-
-#endif /* SINGLE_THREADED */
-

+ 59 - 0
tests/utils.c

@@ -317,4 +317,63 @@ int test_memio_setup(struct test_memio_ctx *ctx,
 }
 #endif
 
+#if !defined(SINGLE_THREADED) && defined(WOLFSSL_COND)
+void signal_ready(tcp_ready* ready)
+{
+    THREAD_CHECK_RET(wc_LockMutex(&ready->mutex));
+    ready->ready = 1;
+#ifdef COND_NO_REQUIRE_LOCKED_MUTEX
+    THREAD_CHECK_RET(wc_UnLockMutex(&ready->mutex));
+#endif
+    THREAD_CHECK_RET(wolfSSL_CondSignal(&ready->cond));
+#ifndef COND_NO_REQUIRE_LOCKED_MUTEX
+    THREAD_CHECK_RET(wc_UnLockMutex(&ready->mutex));
+#endif
+}
+#endif
+
+void wait_tcp_ready(func_args* args)
+{
+#if !defined(SINGLE_THREADED) && defined(WOLFSSL_COND)
+    if (!args->signal->ready) {
+#ifndef COND_NO_REQUIRE_LOCKED_MUTEX
+        THREAD_CHECK_RET(wc_LockMutex(&args->signal->mutex));
+#endif
+        THREAD_CHECK_RET(wolfSSL_CondWait(&args->signal->cond,
+                                            &args->signal->mutex));
+#ifndef COND_NO_REQUIRE_LOCKED_MUTEX
+        THREAD_CHECK_RET(wc_UnLockMutex(&args->signal->mutex));
+#endif
+    }
+    args->signal->ready = 0; /* reset */
+
+#else
+    /* no threading wait or single threaded */
+    (void)args;
+#endif
+}
+
+#ifndef SINGLE_THREADED
+/* Start a thread.
+ *
+ * @param [in]  fun     Function to executre in thread.
+ * @param [in]  args    Object to send to function in thread.
+ * @param [out] thread  Handle to thread.
+ */
+void start_thread(THREAD_CB fun, func_args* args, THREAD_TYPE* thread)
+{
+    THREAD_CHECK_RET(wolfSSL_NewThread(thread, fun, args));
+}
+
+
+/* Join thread to wait for completion.
+ *
+ * @param [in] thread  Handle to thread.
+ */
+void join_thread(THREAD_TYPE thread)
+{
+    THREAD_CHECK_RET(wolfSSL_JoinThread(thread));
+}
+#endif /* SINGLE_THREADED */
+
 #endif /* WOLFSSL_TEST_UTILS_INCLUDED */

+ 0 - 137
testsuite/testsuite.c

@@ -583,143 +583,6 @@ static void simple_test(func_args* args)
 }
 #endif /* !NO_WOLFSSL_SERVER && !NO_WOLFSSL_CLIENT */
 
-
-/* Wait for the server to be ready for a connection.
- *
- * @param [in] args  Object to send to thread.
- */
-void wait_tcp_ready(func_args* args)
-{
-#if defined(HAVE_PTHREAD)
-    PTHREAD_CHECK_RET(pthread_mutex_lock(&args->signal->mutex));
-
-    if (!args->signal->ready)
-        PTHREAD_CHECK_RET(pthread_cond_wait(&args->signal->cond,
-                                            &args->signal->mutex));
-    args->signal->ready = 0; /* reset */
-
-    PTHREAD_CHECK_RET(pthread_mutex_unlock(&args->signal->mutex));
-#elif defined(NETOS)
-    (void)tx_mutex_get(&args->signal->mutex, TX_WAIT_FOREVER);
-
-    /* TODO:
-     * if (!args->signal->ready)
-     *    pthread_cond_wait(&args->signal->cond, &args->signal->mutex);
-     * args->signal->ready = 0; */
-
-    (void)tx_mutex_put(&args->signal->mutex);
-#elif defined(USE_WINDOWS_API)
-    /* Give peer a moment to get running */
-    #if defined(__MINGW32__) || defined(__MINGW64__)
-        Sleep(500);
-    #else
-        _sleep(500);
-    #endif
-    (void)args;
-#else
-    (void)args;
-#endif /* thread checks */
-}
-
-/* Start a thread.
- *
- * @param [in]  fun     Function to execute in thread.
- * @param [in]  args    Object to send to function in thread.
- * @param [out] thread  Handle to thread.
- */
-void start_thread(THREAD_CB fun, func_args* args, THREAD_TYPE* thread)
-{
-#if defined(HAVE_PTHREAD)
-    PTHREAD_CHECK_RET(pthread_create(thread, 0, fun, args));
-    return;
-#elif defined(WOLFSSL_TIRTOS)
-    /* Initialize the defaults and set the parameters. */
-    Task_Params taskParams;
-    Task_Params_init(&taskParams);
-    taskParams.arg0 = (UArg)args;
-    taskParams.stackSize = 65535;
-    *thread = Task_create((Task_FuncPtr)fun, &taskParams, NULL);
-    if (*thread == NULL) {
-        printf("Failed to create new Task\n");
-    }
-    Task_yield();
-#elif defined(NETOS)
-    /* This can be adjusted by defining in user_settings.h, will default to 65k
-     * in the event it is undefined */
-    #ifndef TESTSUITE_THREAD_STACK_SZ
-        #define TESTSUITE_THREAD_STACK_SZ 65535
-    #endif
-    int result;
-    static void * TestSuiteThreadStack = NULL;
-
-    /* Assume only one additional thread is created concurrently. */
-    if (TestSuiteThreadStack == NULL)
-    {
-        TestSuiteThreadStack = (void *)malloc(TESTSUITE_THREAD_STACK_SZ);
-        if (TestSuiteThreadStack == NULL)
-        {
-            printf ("Stack allocation failure.\n");
-            return;
-        }
-    }
-
-    memset (thread, 0, sizeof *thread);
-
-    /* first create the idle thread:
-     * ARGS:
-     * Param1: pointer to thread
-     * Param2: name
-     * Param3 and 4: entry function and input
-     * Param5: pointer to thread stack
-     * Param6: stack size
-     * Param7 and 8: priority level and preempt threshold
-     * Param9 and 10: time slice and auto-start indicator */
-    result = tx_thread_create(thread,
-                       "WolfSSL TestSuiteThread",
-                       (entry_functionType)fun, (ULONG)args,
-                       TestSuiteThreadStack,
-                       TESTSUITE_THREAD_STACK_SZ,
-                       2, 2,
-                       1, TX_AUTO_START);
-    if (result != TX_SUCCESS)
-    {
-        printf("Ethernet Bypass Application: failed to create idle thread!\n");
-    }
-    /* end if NETOS */
-#else
-    /* windows thread type */
-    *thread = (THREAD_TYPE)_beginthreadex(NULL, 0, fun, args, 0, 0);
-#endif /* thread types */
-}
-
-
-/* Join thread to wait for completion.
- *
- * @param [in] thread  Handle to thread.
- */
-void join_thread(THREAD_TYPE thread)
-{
-#if defined(HAVE_PTHREAD)
-    PTHREAD_CHECK_RET(pthread_join(thread, 0));
-#elif defined(WOLFSSL_TIRTOS)
-    while(1) {
-        if (Task_getMode(thread) == Task_Mode_TERMINATED) {
-            Task_sleep(5);
-            break;
-        }
-        Task_yield();
-    }
-#elif defined(NETOS)
-    /* TODO: */
-#else
-    DWORD res = WaitForSingleObject((HANDLE)thread, INFINITE);
-    assert(res == WAIT_OBJECT_0);
-    res = CloseHandle((HANDLE)thread);
-    assert(res);
-    (void)res; /* Suppress un-used variable warning */
-#endif
-}
-
 #ifndef NO_SHA256
 /* Create SHA-256 hash of the file based on filename.
  *

+ 12 - 12
wolfcrypt/benchmark/benchmark.c

@@ -444,14 +444,14 @@
     } while(0)
 #endif
 
-#undef PTHREAD_CHECK_RET
-#define PTHREAD_CHECK_RET(...) do {                                  \
-        int _pthread_ret = (__VA_ARGS__);                            \
-        if (_pthread_ret != 0) {                                     \
-            errno = _pthread_ret;                                    \
+#undef THREAD_CHECK_RET
+#define THREAD_CHECK_RET(...) do {                                   \
+        int _thread_ret = (__VA_ARGS__);                             \
+        if (_thread_ret != 0) {                                      \
+            errno = _thread_ret;                                     \
             printf("%s%s L%d error %d for \"%s\"\n",                 \
                    err_prefix, __FILE__, __LINE__,                   \
-                   _pthread_ret, #__VA_ARGS__);                      \
+                   _thread_ret, #__VA_ARGS__);                       \
             XFFLUSH(stdout);                                         \
             _exit(1);                                                \
         }                                                            \
@@ -1673,7 +1673,7 @@ typedef enum bench_stat_type {
 
     #ifdef WC_ENABLE_BENCH_THREADING
         /* protect bench_stats_head and bench_stats_tail access */
-        PTHREAD_CHECK_RET(pthread_mutex_lock(&bench_lock));
+        THREAD_CHECK_RET(pthread_mutex_lock(&bench_lock));
     #endif
 
         if (algo != NULL) {
@@ -1722,7 +1722,7 @@ typedef enum bench_stat_type {
                 bstat->lastRet = ret; /* track last error */
         }
     #ifdef WC_ENABLE_BENCH_THREADING
-        PTHREAD_CHECK_RET(pthread_mutex_unlock(&bench_lock));
+        THREAD_CHECK_RET(pthread_mutex_unlock(&bench_lock));
     #endif
         return bstat;
     }
@@ -1733,7 +1733,7 @@ typedef enum bench_stat_type {
 
     #ifdef WC_ENABLE_BENCH_THREADING
         /* protect bench_stats_head and bench_stats_tail access */
-        PTHREAD_CHECK_RET(pthread_mutex_lock(&bench_lock));
+        THREAD_CHECK_RET(pthread_mutex_lock(&bench_lock));
     #endif
 
         for (bstat = bench_stats_head; bstat != NULL; ) {
@@ -1754,7 +1754,7 @@ typedef enum bench_stat_type {
         }
 
     #ifdef WC_ENABLE_BENCH_THREADING
-        PTHREAD_CHECK_RET(pthread_mutex_unlock(&bench_lock));
+        THREAD_CHECK_RET(pthread_mutex_unlock(&bench_lock));
     #endif
     }
 #endif /* WC_BENCH_TRACK_STATS */
@@ -3186,12 +3186,12 @@ static int benchmark_test_threaded(void* args)
     }
 
     for (i = 0; i < g_threadCount; i++) {
-        PTHREAD_CHECK_RET(pthread_create(&g_threadData[i].thread_id,
+        THREAD_CHECK_RET(pthread_create(&g_threadData[i].thread_id,
                                          NULL, run_bench, args));
     }
 
     for (i = 0; i < g_threadCount; i++) {
-        PTHREAD_CHECK_RET(pthread_join(g_threadData[i].thread_id, 0));
+        THREAD_CHECK_RET(pthread_join(g_threadData[i].thread_id, 0));
     }
 
     printf("\n");

+ 16 - 23
wolfcrypt/src/random.c

@@ -822,7 +822,18 @@ static WC_INLINE word64 Entropy_TimeHiRes(void)
 
     return now.tv_nsec;
 }
-#elif defined(HAVE_PTHREAD)
+#elif defined(_WIN32) /* USE_WINDOWS_API */
+/* Get the high resolution time counter.
+ *
+ * @return  64-bit timer
+ */
+static WC_INLINE word64 Entropy_TimeHiRes(void)
+{
+    LARGE_INTEGER count;
+    QueryPerformanceCounter(&count);
+    return (word64)(count.QuadPart);
+}
+#elif defined(WOLFSSL_THREAD_NO_JOIN)
 
 /* Start and stop thread that counts as a proxy for time counter. */
 #define ENTROPY_MEMUSE_THREADED
@@ -837,8 +848,6 @@ typedef struct ENTROPY_THREAD_DATA {
 
 /* Track whether entropy thread has been started already. */
 static int entropy_thread_started = 0;
-/* Cache thread id for joining on exit. */
-static THREAD_TYPE entropy_thread_id = 0;
 /* Data for thread to update/observer. */
 static volatile ENTROPY_THREAD_DATA entropy_thread_data = { 0, 0 };
 
@@ -857,13 +866,10 @@ static WC_INLINE word64 Entropy_TimeHiRes(void)
  * @param [in,out] args  Entropy data including: counter and stop flag.
  * @return  NULL always.
  */
-static THREAD_RETURN WOLFSSL_THREAD Entropy_IncCounter(void* args)
+static THREAD_RETURN WOLFSSL_THREAD_NO_JOIN Entropy_IncCounter(void* args)
 {
     (void)args;
 
-    /* Thread resources to be disposed of. */
-    pthread_detach(pthread_self());
-
     /* Keep going until caller tells us to stop and exit. */
     while (!entropy_thread_data.stop) {
         /* Increment counter acting as high resolution timer. */
@@ -874,7 +880,7 @@ static THREAD_RETURN WOLFSSL_THREAD Entropy_IncCounter(void* args)
     fprintf(stderr, "EXITING ENTROPY COUNTER THREAD\n");
 #endif
     /* Exit from thread. */
-    pthread_exit(NULL);
+    WOLFSSL_RETURN_FROM_THREAD(0);
 }
 
 /* Start a thread that increments counter if not one already.
@@ -901,8 +907,8 @@ static int Entropy_StartThread(void)
         fprintf(stderr, "STARTING ENTROPY COUNTER THREAD\n");
     #endif
         /* Create a thread that increments the counter in the data. */
-        ret = pthread_create(&entropy_thread_id, NULL, Entropy_IncCounter,
-            NULL);
+        /* Thread resources to be disposed of. */
+        ret = wolfSSL_NewThreadNoJoin(Entropy_IncCounter, NULL);
         if (ret == 0) {
             /* Wait for the counter to increase indicating thread started. */
             while (entropy_thread_data.counter == start_counter) {
@@ -932,19 +938,6 @@ static void Entropy_StopThread(void)
 }
     /* end if defined(HAVE_PTHREAD) */
 
-#elif defined(_WIN32) /* USE_WINDOWS_API */
-
-/* Get the high resolution time counter.
- *
- * @return  64-bit timer
- */
-static WC_INLINE word64 Entropy_TimeHiRes(void)
-{
-    LARGE_INTEGER count;
-    QueryPerformanceCounter(&count);
-    return (word64)(count.QuadPart);
-}
-
 #else
 
 #error "No high precision time available for MemUse Entropy."

+ 211 - 2
wolfcrypt/src/wc_port.c

@@ -1742,7 +1742,7 @@ int wolfSSL_CryptHwMutexUnLock(void)
     #ifdef WOLFSSL_USE_RWLOCK
         int wc_InitRwLock(wolfSSL_RwLock* m)
         {
-            if (pthread_rwlock_init(m, 0) == 0)
+            if (pthread_rwlock_init(m, NULL) == 0)
                 return 0;
             else
                 return BAD_MUTEX_E;
@@ -1783,7 +1783,7 @@ int wolfSSL_CryptHwMutexUnLock(void)
 
     int wc_InitMutex(wolfSSL_Mutex* m)
     {
-        if (pthread_mutex_init(m, 0) == 0)
+        if (pthread_mutex_init(m, NULL) == 0)
             return 0;
         else
             return BAD_MUTEX_E;
@@ -2811,6 +2811,35 @@ int wolfSSL_CryptHwMutexUnLock(void)
         return 0;
     }
 
+#elif defined(NETOS)
+
+    int wc_InitMutex(wolfSSL_Mutex* m)
+    {
+        if (tx_mutex_create(&ready->mutex, "wolfSSL Lock", TX_INHERIT)
+                == TX_SUCCESS)
+            return 0;
+        else
+            return BAD_MUTEX_E;
+    }
+
+    int wc_FreeMutex(wolfSSL_Mutex* m)
+    {
+        if (tx_mutex_delete(&ready->mutex) == TX_SUCCESS)
+            return 0;
+        else
+            return BAD_MUTEX_E;
+    }
+
+    int wc_LockMutex(wolfSSL_Mutex* m)
+    {
+
+    }
+
+    int wc_UnLockMutex(wolfSSL_Mutex* m)
+    {
+
+    }
+
 #elif defined(WOLFSSL_USER_MUTEX)
 
     /* Use user own mutex */
@@ -3395,6 +3424,21 @@ char* mystrnstr(const char* s1, const char* s2, unsigned int n)
         return 0;
     }
 
+    int wolfSSL_NewThreadNoJoin(THREAD_CB_NOJOIN cb, void* arg)
+    {
+        THREAD_TYPE thread;
+
+        if (cb == NULL)
+            return BAD_FUNC_ARG;
+
+        thread = _beginthread(cb, 0, arg);
+        if (thread == -1L) {
+            return MEMORY_E;
+        }
+
+        return 0;
+    }
+
     int wolfSSL_JoinThread(THREAD_TYPE thread)
     {
         int ret = 0;
@@ -3462,6 +3506,158 @@ char* mystrnstr(const char* s1, const char* s2, unsigned int n)
     }
 #endif /* WOLFSSL_COND */
 
+#elif defined(WOLFSSL_TIRTOS)
+
+    int wolfSSL_NewThread(THREAD_TYPE* thread,
+        THREAD_CB cb, void* arg)
+    {
+        /* Initialize the defaults and set the parameters. */
+        Task_Params taskParams;
+        Task_Params_init(&taskParams);
+        taskParams.arg0 = (UArg)arg;
+        taskParams.stackSize = 65535;
+        *thread = Task_create((Task_FuncPtr)cb, &taskParams, NULL);
+        if (*thread == NULL) {
+            return MEMORY_E;
+        }
+        Task_yield();
+        return 0;
+    }
+
+    int wolfSSL_JoinThread(THREAD_TYPE thread)
+    {
+        while(1) {
+            if (Task_getMode(thread) == Task_Mode_TERMINATED) {
+                Task_sleep(5);
+                break;
+            }
+            Task_yield();
+        }
+        return 0;
+    }
+
+#elif defined(NETOS)
+
+    int wolfSSL_NewThread(THREAD_TYPE* thread,
+        THREAD_CB cb, void* arg)
+    {
+        /* For backwards compatibility allow using this declaration as well. */
+        #ifdef TESTSUITE_THREAD_STACK_SZ
+            #define WOLFSSL_NETOS_STACK_SZ TESTSUITE_THREAD_STACK_SZ
+        #endif
+        /* This can be adjusted by defining in user_settings.h, will default to
+         * 65k in the event it is undefined */
+        #ifndef WOLFSSL_NETOS_STACK_SZ
+            #define WOLFSSL_NETOS_STACK_SZ 65535
+        #endif
+        int result;
+
+        if (thread == NULL || cb == NULL)
+            return BAD_FUNC_ARG;
+
+        XMEMSET(thread, 0, sizeof(*thread));
+
+        thread->threadStack = (void *)XMALLOC(WOLFSSL_NETOS_STACK_SZ, NULL,
+                DYNAMIC_TYPE_TMP_BUFFER);
+        if (thread->threadStack == NULL)
+            return MEMORY_E;
+
+
+        /* first create the idle thread:
+         * ARGS:
+         * Param1: pointer to thread
+         * Param2: name
+         * Param3 and 4: entry function and input
+         * Param5: pointer to thread stack
+         * Param6: stack size
+         * Param7 and 8: priority level and preempt threshold
+         * Param9 and 10: time slice and auto-start indicator */
+        result = tx_thread_create(&thread->tid,
+                           "wolfSSL thread",
+                           (entry_functionType)cb, (ULONG)arg,
+                           thread->threadStack,
+                           TESTSUITE_THREAD_STACK_SZ,
+                           2, 2,
+                           1, TX_AUTO_START);
+        if (result != TX_SUCCESS) {
+            free(thread->threadStack);
+            thread->threadStack = NULL;
+            return MEMORY_E;
+        }
+
+        return 0;
+    }
+
+    int wolfSSL_JoinThread(THREAD_TYPE thread)
+    {
+        /* TODO: maybe have to use tx_thread_delete? */
+        free(thread.threadStack);
+        thread.threadStack = NULL;
+    }
+
+#elif defined(WOLFSSL_ZEPHYR)
+
+    int wolfSSL_NewThread(THREAD_TYPE* thread,
+        THREAD_CB cb, void* arg)
+    {
+        #ifndef WOLFSSL_ZEPHYR_STACK_SZ
+            #define WOLFSSL_ZEPHYR_STACK_SZ (24*1024)
+        #endif
+
+        if (thread == NULL || cb == NULL)
+            return BAD_FUNC_ARG;
+
+        XMEMSET(thread, 0, sizeof(*thread));
+
+        /* TODO: Use the following once k_thread_stack_alloc makes it into a
+         * release.
+         * thread->threadStack = k_thread_stack_alloc(WOLFSSL_ZEPHYR_STACK_SZ,
+         *                                            0);
+         */
+        thread->threadStack = (void*)XMALLOC(
+                Z_KERNEL_STACK_SIZE_ADJUST(WOLFSSL_ZEPHYR_STACK_SZ), 0,
+                                             DYNAMIC_TYPE_TMP_BUFFER);
+        if (thread->threadStack == NULL)
+            return MEMORY_E;
+
+        /* k_thread_create does not return any error codes */
+        /* Casting to k_thread_entry_t should be fine since we just ignore the
+         * extra arguments being passed in */
+        k_thread_create(&thread->tid, thread->threadStack,
+                WOLFSSL_ZEPHYR_STACK_SZ, (k_thread_entry_t)cb, arg, NULL, NULL,
+                5, 0, K_NO_WAIT);
+
+        return 0;
+    }
+
+    int wolfSSL_JoinThread(THREAD_TYPE thread)
+    {
+        int ret = 0;
+        int err;
+
+        err = k_thread_join(&thread.tid, K_FOREVER);
+        if (err != 0)
+            ret = MEMORY_E;
+
+        /* TODO: Use the following once k_thread_stack_free makes it into a
+         * release.
+         * err = k_thread_stack_free(thread.threadStack);
+         * if (err != 0)
+         *     ret = MEMORY_E;
+         */
+        XFREE(thread.threadStack, NULL, DYNAMIC_TYPE_TMP_BUFFER);
+        thread.threadStack = NULL;
+
+        /* No thread resources to free. Everything is stored in thread.tid */
+
+        return ret;
+    }
+
+#ifdef WOLFSSL_COND
+    /* Use the pthreads translation layer for signaling */
+
+#endif /* WOLFSSL_COND */
+
 #elif defined(WOLFSSL_PTHREADS)
 
     int wolfSSL_NewThread(THREAD_TYPE* thread,
@@ -3476,6 +3672,19 @@ char* mystrnstr(const char* s1, const char* s2, unsigned int n)
         return 0;
     }
 
+#ifdef WOLFSSL_THREAD_NO_JOIN
+    int wolfSSL_NewThreadNoJoin(THREAD_CB_NOJOIN cb, void* arg)
+    {
+        THREAD_TYPE thread;
+        int ret;
+        XMEMSET(&thread, 0, sizeof(thread));
+        ret = wolfSSL_NewThread(&thread, cb, arg);
+        if (ret == 0)
+            pthread_detach(thread);
+        return ret;
+    }
+#endif
+
     int wolfSSL_JoinThread(THREAD_TYPE thread)
     {
         if (thread == INVALID_THREAD_VAL)

+ 81 - 79
wolfssl/test.h

@@ -363,13 +363,13 @@ err_sys_with_errno(const char* msg)
         }                                                            \
     } while(0)
 
-#define PTHREAD_CHECK_RET(...) do {                                  \
-        int _pthread_ret = (__VA_ARGS__);                            \
-        if (_pthread_ret != 0) {                                     \
-            errno = _pthread_ret;                                    \
+#define THREAD_CHECK_RET(...) do {                                   \
+        int _thread_ret = (__VA_ARGS__);                             \
+        if (_thread_ret != 0) {                                      \
+            errno = _thread_ret;                                     \
             fprintf(stderr, "%s L%d error %d for \"%s\"\n",          \
-                    __FILE__, __LINE__, _pthread_ret, #__VA_ARGS__); \
-            err_sys("pthread call failed");                          \
+                    __FILE__, __LINE__, _thread_ret, #__VA_ARGS__);  \
+            err_sys("thread call failed");                           \
         }                                                            \
     } while(0)
 
@@ -534,12 +534,13 @@ typedef struct tcp_ready {
     word16 ready;              /* predicate */
     word16 port;
     char*  srfName;     /* server ready file name */
-#ifdef HAVE_PTHREAD
-    pthread_mutex_t mutex;
-    pthread_cond_t  cond;
+#ifndef SINGLE_THREADED
+#ifdef WOLFSSL_COND
+    wolfSSL_Mutex mutex;
+    COND_TYPE     cond;
+#else /* No signaling available, rely only on the mutex */
+    wolfSSL_Mutex mutex;
 #endif
-#ifdef NETOS
-    TX_MUTEX mutex;
 #endif
 } tcp_ready;
 
@@ -548,13 +549,14 @@ static WC_INLINE void InitTcpReady(tcp_ready* ready)
     ready->ready = 0;
     ready->port = 0;
     ready->srfName = NULL;
-#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 */
+
+#ifndef SINGLE_THREADED
+#ifdef WOLFSSL_COND
+    THREAD_CHECK_RET(wc_InitMutex(&ready->mutex));
+    THREAD_CHECK_RET(wolfSSL_CondInit(&ready->cond));
+#else /* No signaling available, rely only on the mutex */
+    THREAD_CHECK_RET(wc_InitMutex(&ready->mutex));
+#endif
 #endif
 }
 
@@ -564,11 +566,13 @@ static WC_INLINE void InitTcpReady(tcp_ready* ready)
 
 static WC_INLINE void FreeTcpReady(tcp_ready* ready)
 {
-#if defined(HAVE_PTHREAD)
-    PTHREAD_CHECK_RET(pthread_mutex_destroy(&ready->mutex));
-    PTHREAD_CHECK_RET(pthread_cond_destroy(&ready->cond));
-#elif defined(NETOS)
-    tx_mutex_delete(&ready->mutex);
+#ifndef SINGLE_THREADED
+#ifdef WOLFSSL_COND
+    THREAD_CHECK_RET(wc_FreeMutex(&ready->mutex));
+    THREAD_CHECK_RET(wolfSSL_CondFree(&ready->cond));
+#else /* No signaling available, rely only on the mutex */
+    THREAD_CHECK_RET(wc_FreeMutex(&ready->mutex));
+#endif
 #else
     (void)ready;
 #endif
@@ -603,14 +607,14 @@ typedef struct callback_functions {
     unsigned char doUdp:1;
 } callback_functions;
 
-#if defined(WOLFSSL_SRTP) && defined(HAVE_PTHREAD)
+#if defined(WOLFSSL_SRTP) && defined(WOLFSSL_COND)
 typedef struct srtp_test_helper {
-    pthread_mutex_t mutex;
-    pthread_cond_t  cond;
+    wolfSSL_Mutex mutex;
+    COND_TYPE     cond;
     uint8_t* server_srtp_ekm;
     size_t   server_srtp_ekm_size;
 } srtp_test_helper;
-#endif /* WOLFSSL_SRTP HAVE_PTHREAD */
+#endif /* WOLFSSL_SRTP WOLFSSL_COND */
 
 typedef struct func_args {
     int    argc;
@@ -618,7 +622,7 @@ typedef struct func_args {
     int    return_code;
     tcp_ready* signal;
     callback_functions *callbacks;
-#if defined(WOLFSSL_SRTP) && defined(HAVE_PTHREAD)
+#if defined(WOLFSSL_SRTP) && defined(WOLFSSL_COND)
     srtp_test_helper* srtp_helper;
 #endif
 } func_args;
@@ -654,6 +658,8 @@ int rem_dir(const char* dirName);
 int rem_file(const char* fileName);
 int copy_file(const char* in, const char* out);
 
+void signal_ready(tcp_ready* ready);
+
 /* wolfSSL */
 #ifndef TEST_IPV6
     static const char* const wolfSSLIP   = "127.0.0.1";
@@ -666,15 +672,15 @@ static const word16      wolfSSLPort = 11111;
 extern int   myoptind;
 extern char* myoptarg;
 
-#if defined(WOLFSSL_SRTP) && defined(HAVE_PTHREAD)
+#if defined(WOLFSSL_SRTP) && defined(WOLFSSL_COND)
 
 static WC_INLINE void srtp_helper_init(srtp_test_helper *srtp)
 {
     srtp->server_srtp_ekm_size = 0;
     srtp->server_srtp_ekm = NULL;
 
-    PTHREAD_CHECK_RET(pthread_mutex_init(&srtp->mutex, 0));
-    PTHREAD_CHECK_RET(pthread_cond_init(&srtp->cond, 0));
+    THREAD_CHECK_RET(wc_InitMutex(&srtp->mutex));
+    THREAD_CHECK_RET(wolfSSL_CondInit(&srtp->cond));
 }
 
 /**
@@ -689,10 +695,16 @@ static WC_INLINE void srtp_helper_init(srtp_test_helper *srtp)
 static WC_INLINE void srtp_helper_get_ekm(srtp_test_helper *srtp,
                                           uint8_t **ekm, size_t *size)
 {
-    PTHREAD_CHECK_RET(pthread_mutex_lock(&srtp->mutex));
+#ifndef COND_NO_REQUIRE_LOCKED_MUTEX
+    THREAD_CHECK_RET(wc_LockMutex(&srtp->mutex));
+#endif
 
     if (srtp->server_srtp_ekm == NULL)
-        PTHREAD_CHECK_RET(pthread_cond_wait(&srtp->cond, &srtp->mutex));
+        THREAD_CHECK_RET(wolfSSL_CondWait(&srtp->cond, &srtp->mutex));
+
+#ifdef COND_NO_REQUIRE_LOCKED_MUTEX
+    THREAD_CHECK_RET(wc_LockMutex(&srtp->mutex));
+#endif
 
     *ekm = srtp->server_srtp_ekm;
     *size = srtp->server_srtp_ekm_size;
@@ -701,7 +713,7 @@ static WC_INLINE void srtp_helper_get_ekm(srtp_test_helper *srtp,
     srtp->server_srtp_ekm = NULL;
     srtp->server_srtp_ekm_size = 0;
 
-    PTHREAD_CHECK_RET(pthread_mutex_unlock(&srtp->mutex));
+    THREAD_CHECK_RET(wc_UnLockMutex(&srtp->mutex));
 }
 
 /**
@@ -718,19 +730,24 @@ static WC_INLINE void srtp_helper_get_ekm(srtp_test_helper *srtp,
 static WC_INLINE void srtp_helper_set_ekm(srtp_test_helper *srtp,
                                           uint8_t *ekm, size_t size)
 {
-    PTHREAD_CHECK_RET(pthread_mutex_lock(&srtp->mutex));
+    THREAD_CHECK_RET(wc_LockMutex(&srtp->mutex));
 
     srtp->server_srtp_ekm_size = size;
     srtp->server_srtp_ekm = ekm;
-    PTHREAD_CHECK_RET(pthread_cond_signal(&srtp->cond));
+#ifdef COND_NO_REQUIRE_LOCKED_MUTEX
+    THREAD_CHECK_RET(wc_UnLockMutex(&srtp->mutex));
+#endif
+    THREAD_CHECK_RET(wolfSSL_CondSignal(&srtp->cond));
 
-    PTHREAD_CHECK_RET(pthread_mutex_unlock(&srtp->mutex));
+#ifndef COND_NO_REQUIRE_LOCKED_MUTEX
+    THREAD_CHECK_RET(wc_UnLockMutex(&srtp->mutex));
+#endif
 }
 
 static WC_INLINE void srtp_helper_free(srtp_test_helper *srtp)
 {
-    PTHREAD_CHECK_RET(pthread_mutex_destroy(&srtp->mutex));
-    PTHREAD_CHECK_RET(pthread_cond_destroy(&srtp->cond));
+    THREAD_CHECK_RET(wc_FreeMutex(&srtp->mutex));
+    THREAD_CHECK_RET(wolfSSL_CondFree(&srtp->cond));
 }
 
 #endif /* WOLFSSL_SRTP && !SINGLE_THREADED && POSIX_THREADS */
@@ -2212,27 +2229,23 @@ static WC_INLINE void udp_accept(SOCKET_T* sockfd, SOCKET_T* clientfd,
     #endif
 
     if (args != NULL && args->signal != NULL) {
-#if defined(HAVE_PTHREAD)
-        /* signal ready to accept data */
-        tcp_ready* ready = args->signal;
-        PTHREAD_CHECK_RET(pthread_mutex_lock(&ready->mutex));
-        ready->ready = 1;
-        ready->port = port;
-        PTHREAD_CHECK_RET(pthread_cond_signal(&ready->cond));
-        PTHREAD_CHECK_RET(pthread_mutex_unlock(&ready->mutex));
-#elif defined (WOLFSSL_TIRTOS)
-        /* Need mutex? */
-        tcp_ready* ready = args->signal;
-        ready->ready = 1;
-        ready->port = port;
-#elif defined(NETOS)
+#ifndef SINGLE_THREADED
         tcp_ready* ready = args->signal;
-        (void)tx_mutex_get(&ready->mutex, TX_WAIT_FOREVER);
+        THREAD_CHECK_RET(wc_LockMutex(&ready->mutex));
         ready->ready = 1;
         ready->port = port;
-        (void)tx_mutex_put(&ready->mutex);
+#ifdef WOLFSSL_COND
+        /* signal ready to accept data */
+#ifdef COND_NO_REQUIRE_LOCKED_MUTEX
+        THREAD_CHECK_RET(wc_UnLockMutex(&ready->mutex));
+#endif
+        THREAD_CHECK_RET(wolfSSL_CondSignal(&ready->cond));
+#ifndef COND_NO_REQUIRE_LOCKED_MUTEX
+        THREAD_CHECK_RET(wc_UnLockMutex(&ready->mutex));
+#endif
 #else
-        (void)port;
+        THREAD_CHECK_RET(wc_UnLockMutex(&ready->mutex));
+#endif
 #endif
     }
     else {
@@ -2259,36 +2272,25 @@ static WC_INLINE void tcp_accept(SOCKET_T* sockfd, SOCKET_T* clientfd,
     if(do_listen) {
         tcp_listen(sockfd, &port, useAnyAddr, udp, sctp);
 
-    #if defined(NO_MAIN_DRIVER) && defined(HAVE_PTHREAD)
-        /* signal ready to tcp_accept */
-        if (args)
-            ready = args->signal;
-        if (ready) {
-            PTHREAD_CHECK_RET(pthread_mutex_lock(&ready->mutex));
-            ready->ready = 1;
-            ready->port = port;
-            PTHREAD_CHECK_RET(pthread_cond_signal(&ready->cond));
-            PTHREAD_CHECK_RET(pthread_mutex_unlock(&ready->mutex));
-        }
-    #elif defined (WOLFSSL_TIRTOS)
-        /* Need mutex? */
-        if (args)
-            ready = args->signal;
-        if (ready) {
-            ready->ready = 1;
-            ready->port = port;
-        }
-    #elif defined(NETOS)
+#ifndef SINGLE_THREADED
         /* signal ready to tcp_accept */
         if (args)
             ready = args->signal;
         if (ready) {
-            (void)tx_mutex_get(&ready->mutex, TX_WAIT_FOREVER);
+            THREAD_CHECK_RET(wc_LockMutex(&ready->mutex));
             ready->ready = 1;
             ready->port = port;
-            (void)tx_mutex_put(&ready->mutex);
+#ifdef WOLFSSL_COND
+#ifdef COND_NO_REQUIRE_LOCKED_MUTEX
+            THREAD_CHECK_RET(wc_UnLockMutex(&ready->mutex));
+#endif
+            THREAD_CHECK_RET(wolfSSL_CondSignal(&ready->cond));
+#ifndef COND_NO_REQUIRE_LOCKED_MUTEX
+            THREAD_CHECK_RET(wc_UnLockMutex(&ready->mutex));
+#endif
+#endif
         }
-    #endif
+#endif
 
         if (ready_file) {
         #if !defined(NO_FILESYSTEM) || defined(FORCE_BUFFER_TEST) && \

+ 70 - 13
wolfssl/wolfcrypt/types.h

@@ -1354,15 +1354,23 @@ typedef struct w64wrapper {
         #define WOLFSSL_THREAD
     #elif defined(WOLFSSL_TIRTOS)
         typedef void          THREAD_RETURN;
+        #define WOLFSSL_THREAD_VOID_RETURN
         typedef Task_Handle   THREAD_TYPE;
         #define WOLFSSL_THREAD
     #elif defined(WOLFSSL_ZEPHYR)
         typedef void            THREAD_RETURN;
-        typedef struct k_thread THREAD_TYPE;
+        #define WOLFSSL_THREAD_VOID_RETURN
+        typedef struct {
+            struct k_thread tid;
+            k_thread_stack_t* threadStack;
+        } THREAD_TYPE;
         #define WOLFSSL_THREAD
     #elif defined(NETOS)
         typedef UINT        THREAD_RETURN;
-        typedef TX_THREAD   THREAD_TYPE;
+        typedef struct {
+            TX_THREAD tid;
+            void* threadStack;
+        } THREAD_TYPE;
         #define WOLFSSL_THREAD
         #define INFINITE TX_WAIT_FOREVER
         #define WAIT_OBJECT_0 TX_NO_WAIT
@@ -1377,6 +1385,9 @@ typedef struct w64wrapper {
         typedef pthread_cond_t COND_TYPE;
         #define WOLFSSL_COND
         #define WOLFSSL_THREAD
+        #ifndef HAVE_SELFTEST
+            #define WOLFSSL_THREAD_NO_JOIN
+        #endif
     #elif defined(FREERTOS)
         typedef unsigned int   THREAD_RETURN;
         typedef TaskHandle_t   THREAD_TYPE;
@@ -1389,6 +1400,7 @@ typedef struct w64wrapper {
         #define INVALID_THREAD_VAL ((THREAD_TYPE)(INVALID_HANDLE_VALUE))
         #define COND_NO_REQUIRE_LOCKED_MUTEX
         #define WOLFSSL_THREAD __stdcall
+        #define WOLFSSL_THREAD_NO_JOIN __cdecl
     #else
         typedef unsigned int  THREAD_RETURN;
         typedef size_t        THREAD_TYPE;
@@ -1399,30 +1411,75 @@ typedef struct w64wrapper {
     #ifndef SINGLE_THREADED
         /* Necessary headers should already be included. */
 
-        /* We don't support returns from threads */
-        typedef THREAD_RETURN (WOLFSSL_THREAD *THREAD_CB)(void* arg);
-
         #ifndef INVALID_THREAD_VAL
             #define INVALID_THREAD_VAL ((THREAD_TYPE)(-1))
         #endif
 
+        #ifndef WOLFSSL_THREAD_VOID_RETURN
+            #define WOLFSSL_RETURN_FROM_THREAD(x) return (THREAD_RETURN)(x)
+        #else
+            #define WOLFSSL_RETURN_FROM_THREAD(x) \
+                do { (void)(x); return; } while(0)
+        #endif
+
+        /* List of defines/types and what they mean:
+         * THREAD_RETURN - return type of a thread callback
+         * THREAD_TYPE - type that should be passed into thread handling API
+         * INVALID_THREAD_VAL - a value that THREAD_TYPE can be checked against
+         *                      to check if the value is an invalid thread
+         * WOLFSSL_THREAD - attribute that should be used to declare thread
+         *                  callbacks
+         * WOLFSSL_THREAD_NO_JOIN - attribute that should be used to declare
+         *                          thread callbacks that don't require cleanup
+         * WOLFSSL_COND - defined if this system suports signaling
+         * COND_TYPE - type that should be passed into the signaling API
+         * COND_NO_REQUIRE_LOCKED_MUTEX - defined if the signaling doesn't
+         *                                require locking a mutex
+         * WOLFSSL_THREAD_VOID_RETURN - defined if the thread callback has a
+         *                              void return
+         * WOLFSSL_RETURN_FROM_THREAD - define used to correctly return from a
+         *                              thread callback
+         * THREAD_CB - thread callback type for regular threading API
+         * THREAD_CB_NOJOIN - thread callback type for threading API that don't
+         *                    require cleanup
+         *
+         * Other defines/types are specific for the threading implementation
+         */
+
         /* Internal wolfSSL threading interface. It does NOT need to be ported
-         * during initial porting efforts.
+         * during initial porting efforts. This is a very basic interface. Some
+         * areas don't use this interface on purpose as they need more control
+         * over threads.
          *
          * It is currently used for:
-         * - CRL monitor */
+         * - CRL monitor
+         * - Testing
+         * - Entropy generation */
 
-        WOLFSSL_LOCAL int wolfSSL_NewThread(THREAD_TYPE* thread,
+        /* We don't support returns from threads */
+        typedef THREAD_RETURN (WOLFSSL_THREAD *THREAD_CB)(void* arg);
+        WOLFSSL_API int wolfSSL_NewThread(THREAD_TYPE* thread,
             THREAD_CB cb, void* arg);
-        WOLFSSL_LOCAL int wolfSSL_JoinThread(THREAD_TYPE thread);
+        #ifdef WOLFSSL_THREAD_NO_JOIN
+            /* Create a thread that will be automatically cleaned up. We can't
+             * return a handle/pointer to the new thread because there are no
+             * guarantees for how long it will be valid. */
+            typedef THREAD_RETURN (WOLFSSL_THREAD_NO_JOIN *THREAD_CB_NOJOIN)
+                    (void* arg);
+            WOLFSSL_API int wolfSSL_NewThreadNoJoin(THREAD_CB_NOJOIN cb,
+                    void* arg);
+        #endif
+        WOLFSSL_API int wolfSSL_JoinThread(THREAD_TYPE thread);
 
         #ifdef WOLFSSL_COND
-            WOLFSSL_LOCAL int wolfSSL_CondInit(COND_TYPE* cond);
-            WOLFSSL_LOCAL int wolfSSL_CondFree(COND_TYPE* cond);
-            WOLFSSL_LOCAL int wolfSSL_CondSignal(COND_TYPE* cond);
-            WOLFSSL_LOCAL int wolfSSL_CondWait(COND_TYPE* cond,
+            WOLFSSL_API int wolfSSL_CondInit(COND_TYPE* cond);
+            WOLFSSL_API int wolfSSL_CondFree(COND_TYPE* cond);
+            WOLFSSL_API int wolfSSL_CondSignal(COND_TYPE* cond);
+            WOLFSSL_API int wolfSSL_CondWait(COND_TYPE* cond,
                 wolfSSL_Mutex* mutex);
         #endif
+    #else
+        #define WOLFSSL_RETURN_FROM_THREAD(x) return (THREAD_RETURN)(x)
     #endif /* SINGLE_THREADED */
 
     #if defined(HAVE_STACK_SIZE)

+ 8 - 22
zephyr/samples/wolfssl_tls_sock/src/tls_sock.c

@@ -33,12 +33,8 @@
 
 #define BUFFER_SIZE           2048
 #define STATIC_MEM_SIZE       (192*1024)
-#define THREAD_STACK_SIZE     (24*1024)
 #define MAX_SEND_SIZE         256
 
-/* The stack to use in the server's thread. */
-K_THREAD_STACK_DEFINE(server_stack, THREAD_STACK_SIZE);
-
 #ifdef WOLFSSL_STATIC_MEMORY
     static WOLFSSL_HEAP_HINT* HEAP_HINT_SERVER;
     static WOLFSSL_HEAP_HINT* HEAP_HINT_CLIENT;
@@ -292,22 +288,6 @@ static void wolfssl_memstats(WOLFSSL* ssl)
 #endif
 }
 
-
-/* Start the server thread. */
-void start_thread(THREAD_CB func, func_args* args, THREAD_TYPE* thread)
-{
-    /* Casting to k_thread_entry_t should be fine since we just ignore the
-     * extra arguments being passed in */
-    k_thread_create(thread, server_stack, K_THREAD_STACK_SIZEOF(server_stack),
-                    (k_thread_entry_t)func, args, NULL, NULL, 5, 0, K_NO_WAIT);
-}
-
-void join_thread(THREAD_TYPE thread)
-{
-    /* Threads are handled in the kernel. */
-}
-
-
 int wolfssl_server_accept_tcp(WOLFSSL* ssl, SOCKET_T* fd, SOCKET_T* acceptfd)
 {
     int ret = 0;
@@ -511,12 +491,18 @@ int main()
 #endif
 
     /* Start server */
-    start_thread(server_thread, NULL, &serverThread);
+    if (wolfSSL_NewThread(&serverThread, server_thread, NULL) != 0) {
+        printf("Failed to start server thread\n");
+        return -1;
+    }
 
     k_sleep(Z_TIMEOUT_TICKS(100));
     client_thread();
 
-    join_thread(serverThread);
+    if (wolfSSL_JoinThread(serverThread) != 0) {
+        printf("Failed to join server thread\n");
+        return -1;
+    }
 
     wolfSSL_Cleanup();
 

+ 8 - 23
zephyr/samples/wolfssl_tls_thread/src/tls_threaded.c

@@ -44,10 +44,6 @@
 
 #define BUFFER_SIZE           2048
 #define STATIC_MEM_SIZE       (192*1024)
-#define THREAD_STACK_SIZE     (24*1024)
-
-/* The stack to use in the server's thread. */
-K_THREAD_STACK_DEFINE(server_stack, THREAD_STACK_SIZE);
 
 #ifdef WOLFSSL_STATIC_MEMORY
     static WOLFSSL_HEAP_HINT* HEAP_HINT_SERVER;
@@ -515,22 +511,6 @@ static void wolfssl_memstats(WOLFSSL* ssl)
 #endif
 }
 
-
-/* Start the server thread. */
-void start_thread(THREAD_CB func, func_args* args, THREAD_TYPE* thread)
-{
-    /* Casting to k_thread_entry_t should be fine since we just ignore the
-     * extra arguments being passed in */
-    k_thread_create(thread, server_stack, K_THREAD_STACK_SIZEOF(server_stack),
-                    (k_thread_entry_t)func, args, NULL, NULL, 5, 0, K_NO_WAIT);
-}
-
-void join_thread(THREAD_TYPE thread)
-{
-    /* Threads are handled in the kernel. */
-}
-
-
 /* Thread to do the server operations. */
 void server_thread(void* arg1)
 {
@@ -604,7 +584,10 @@ int main()
     wc_InitMutex(&server_mutex);
 
     /* Start server */
-    start_thread(server_thread, NULL, &serverThread);
+    if (wolfSSL_NewThread(&serverThread, server_thread, NULL) != 0) {
+        printf("Failed to start server thread\n");
+        return -1;
+    }
 
 #ifdef WOLFSSL_STATIC_MEMORY
     if (wc_LoadStaticMemory(&HEAP_HINT_CLIENT, gMemoryClient,
@@ -645,8 +628,10 @@ int main()
     printf("Client Return: %d\n", ret);
     printf("Client Error: %d\n", wolfSSL_get_error(client_ssl, ret));
 
-
-    join_thread(serverThread);
+    if (wolfSSL_JoinThread(serverThread) != 0) {
+        printf("Failed to join server thread\n");
+        return -1;
+    }
 
 #ifdef WOLFSSL_STATIC_MEMORY
     printf("Client Memory Stats\n");