diff --git a/.github/workflows/multi-compiler.yml b/.github/workflows/multi-compiler.yml index 60feb3b41..3099139c9 100644 --- a/.github/workflows/multi-compiler.yml +++ b/.github/workflows/multi-compiler.yml @@ -26,7 +26,7 @@ jobs: CXX: clang++-14 runs-on: ubuntu-latest # This should be a safe limit for the tests to run. - timeout-minutes: 2 + timeout-minutes: 4 steps: - uses: actions/checkout@v3 - name: Build diff --git a/examples/benchmark/tls_bench.c b/examples/benchmark/tls_bench.c index 2d114e8d8..686060aa4 100644 --- a/examples/benchmark/tls_bench.c +++ b/examples/benchmark/tls_bench.c @@ -416,9 +416,9 @@ static int ServerMemSend(info_t* info, char* buf, int sz) XMEMCPY(&info->to_client.buf[info->to_client.write_idx], buf, sz); info->to_client.write_idx += sz; info->to_client.write_bytes += sz; + THREAD_CHECK_RET(wc_UnLockMutex(&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) { @@ -436,8 +436,9 @@ static int ServerMemRecv(info_t* info, char* buf, int sz) #ifndef BENCH_USE_NONBLOCK while (info->to_server.write_idx - info->to_server.read_idx < sz && !info->to_client.done) { - THREAD_CHECK_RET(wolfSSL_CondWait(&info->to_server.cond, - &info->to_server.mutex)); + THREAD_CHECK_RET(wc_UnLockMutex(&info->to_server.mutex)); + THREAD_CHECK_RET(wolfSSL_CondWait(&info->to_server.cond)); + THREAD_CHECK_RET(wc_LockMutex(&info->to_server.mutex)); } #else if (info->to_server.write_idx - info->to_server.read_idx < sz) { @@ -492,8 +493,8 @@ static int ClientMemSend(info_t* info, char* buf, int sz) info->to_server.write_idx += sz; info->to_server.write_bytes += sz; - THREAD_CHECK_RET(wolfSSL_CondSignal(&info->to_server.cond)); THREAD_CHECK_RET(wc_UnLockMutex(&info->to_server.mutex)); + THREAD_CHECK_RET(wolfSSL_CondSignal(&info->to_server.cond)); #ifdef BENCH_USE_NONBLOCK if (sz == 0) { @@ -511,8 +512,9 @@ static int ClientMemRecv(info_t* info, char* buf, int sz) #ifndef BENCH_USE_NONBLOCK while (info->to_client.write_idx - info->to_client.read_idx < sz && !info->to_server.done) { - THREAD_CHECK_RET(wolfSSL_CondWait(&info->to_client.cond, - &info->to_client.mutex)); + THREAD_CHECK_RET(wc_UnLockMutex(&info->to_client.mutex)); + THREAD_CHECK_RET(wolfSSL_CondWait(&info->to_client.cond)); + THREAD_CHECK_RET(wc_LockMutex(&info->to_client.mutex)); } #else if (info->to_client.write_idx - info->to_client.read_idx < sz) { @@ -1054,8 +1056,9 @@ static int bench_tls_client(info_t* info) if (info->doDTLS && !info->clientOrserverOnly) { THREAD_CHECK_RET(wc_LockMutex(&info->dtls_mutex)); if (info->serverReady != 1) { - THREAD_CHECK_RET(wolfSSL_CondWait(&info->dtls_cond, - &info->dtls_mutex)); + THREAD_CHECK_RET(wc_UnLockMutex(&info->dtls_mutex)); + THREAD_CHECK_RET(wolfSSL_CondWait(&info->dtls_cond)); + THREAD_CHECK_RET(wc_LockMutex(&info->dtls_mutex)); } /* for next loop */ info->serverReady = 0; @@ -1201,9 +1204,9 @@ static THREAD_RETURN WOLFSSL_THREAD_NO_JOIN client_thread(void* args) ret = bench_tls_client(info); - THREAD_CHECK_RET(wolfSSL_CondSignal(&info->to_server.cond)); info->to_client.done = 1; info->client.ret = ret; + THREAD_CHECK_RET(wolfSSL_CondSignal(&info->to_server.cond)); WOLFSSL_RETURN_FROM_THREAD(NULL); } @@ -1291,8 +1294,8 @@ static int SocketWaitClient(info_t* info) if (!info->clientOrserverOnly) { THREAD_CHECK_RET(wc_LockMutex(&info->dtls_mutex)); info->serverReady = 1; - THREAD_CHECK_RET(wolfSSL_CondSignal(&info->dtls_cond)); THREAD_CHECK_RET(wc_UnLockMutex(&info->dtls_mutex)); + THREAD_CHECK_RET(wolfSSL_CondSignal(&info->dtls_cond)); } #endif connd = (int)recvfrom(info->listenFd, (char *)msg, sizeof(msg), @@ -1659,9 +1662,9 @@ static THREAD_RETURN WOLFSSL_THREAD_NO_JOIN server_thread(void* args) } } - THREAD_CHECK_RET(wolfSSL_CondSignal(&info->to_client.cond)); info->to_server.done = 1; info->server.ret = ret; + THREAD_CHECK_RET(wolfSSL_CondSignal(&info->to_client.cond)); WOLFSSL_RETURN_FROM_THREAD(NULL); } diff --git a/examples/echoserver/echoserver.c b/examples/echoserver/echoserver.c index a392fe18b..5f6293e0e 100644 --- a/examples/echoserver/echoserver.c +++ b/examples/echoserver/echoserver.c @@ -74,13 +74,8 @@ static void SignalReady(void* args, word16 port) THREAD_CHECK_RET(wc_LockMutex(&ready->mutex)); ready->ready = 1; ready->port = port; -#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; diff --git a/src/bio.c b/src/bio.c index 2e6de0a9c..85de16dd5 100644 --- a/src/bio.c +++ b/src/bio.c @@ -1963,10 +1963,10 @@ int wolfSSL_BIO_flush(WOLFSSL_BIO* bio) WOLFSSL_BIO_METHOD* wolfSSL_BIO_f_md(void) { - static WOLFSSL_BIO_METHOD meth; + static WOLFSSL_BIO_METHOD meth = + WOLFSSL_BIO_METHOD_INIT(WOLFSSL_BIO_MD); WOLFSSL_ENTER("wolfSSL_BIO_f_md"); - meth.type = WOLFSSL_BIO_MD; return &meth; } @@ -1986,10 +1986,10 @@ int wolfSSL_BIO_flush(WOLFSSL_BIO* bio) WOLFSSL_BIO_METHOD* wolfSSL_BIO_f_buffer(void) { - static WOLFSSL_BIO_METHOD meth; + static WOLFSSL_BIO_METHOD meth = + WOLFSSL_BIO_METHOD_INIT(WOLFSSL_BIO_BUFFER); WOLFSSL_ENTER("wolfSSL_BIO_f_buffer"); - meth.type = WOLFSSL_BIO_BUFFER; return &meth; } @@ -2017,10 +2017,10 @@ int wolfSSL_BIO_flush(WOLFSSL_BIO* bio) WOLFSSL_BIO_METHOD* wolfSSL_BIO_s_bio(void) { - static WOLFSSL_BIO_METHOD bio_meth; + static WOLFSSL_BIO_METHOD bio_meth = + WOLFSSL_BIO_METHOD_INIT(WOLFSSL_BIO_BIO); WOLFSSL_ENTER("wolfSSL_BIO_s_bio"); - bio_meth.type = WOLFSSL_BIO_BIO; return &bio_meth; } @@ -2029,10 +2029,10 @@ int wolfSSL_BIO_flush(WOLFSSL_BIO* bio) #ifndef NO_FILESYSTEM WOLFSSL_BIO_METHOD* wolfSSL_BIO_s_file(void) { - static WOLFSSL_BIO_METHOD file_meth; + static WOLFSSL_BIO_METHOD file_meth = + WOLFSSL_BIO_METHOD_INIT(WOLFSSL_BIO_FILE); WOLFSSL_ENTER("wolfSSL_BIO_s_file"); - file_meth.type = WOLFSSL_BIO_FILE; return &file_meth; } @@ -2041,10 +2041,10 @@ int wolfSSL_BIO_flush(WOLFSSL_BIO* bio) WOLFSSL_BIO_METHOD* wolfSSL_BIO_f_ssl(void) { - static WOLFSSL_BIO_METHOD meth; + static WOLFSSL_BIO_METHOD meth = + WOLFSSL_BIO_METHOD_INIT(WOLFSSL_BIO_SSL); WOLFSSL_ENTER("wolfSSL_BIO_f_ssl"); - meth.type = WOLFSSL_BIO_SSL; return &meth; } @@ -2052,10 +2052,10 @@ int wolfSSL_BIO_flush(WOLFSSL_BIO* bio) WOLFSSL_BIO_METHOD *wolfSSL_BIO_s_socket(void) { - static WOLFSSL_BIO_METHOD meth; + static WOLFSSL_BIO_METHOD meth = + WOLFSSL_BIO_METHOD_INIT(WOLFSSL_BIO_SOCKET); WOLFSSL_ENTER("wolfSSL_BIO_s_socket"); - meth.type = WOLFSSL_BIO_SOCKET; return &meth; } @@ -2803,10 +2803,10 @@ WOLFSSL_BIO* wolfSSL_BIO_pop(WOLFSSL_BIO* bio) WOLFSSL_BIO_METHOD* wolfSSL_BIO_s_mem(void) { - static WOLFSSL_BIO_METHOD meth; + static WOLFSSL_BIO_METHOD meth = + WOLFSSL_BIO_METHOD_INIT(WOLFSSL_BIO_MEMORY); WOLFSSL_ENTER("wolfSSL_BIO_s_mem"); - meth.type = WOLFSSL_BIO_MEMORY; return &meth; } @@ -2814,10 +2814,10 @@ WOLFSSL_BIO_METHOD* wolfSSL_BIO_s_mem(void) WOLFSSL_BIO_METHOD* wolfSSL_BIO_f_base64(void) { - static WOLFSSL_BIO_METHOD meth; + static WOLFSSL_BIO_METHOD meth = + WOLFSSL_BIO_METHOD_INIT(WOLFSSL_BIO_BASE64); WOLFSSL_ENTER("wolfSSL_BIO_f_base64"); - meth.type = WOLFSSL_BIO_BASE64; return &meth; } diff --git a/src/crl.c b/src/crl.c index b167914c0..49048703c 100644 --- a/src/crl.c +++ b/src/crl.c @@ -931,20 +931,17 @@ static int SignalSetup(WOLFSSL_CRL* crl, int status) int ret; /* signal to calling thread we're setup */ -#ifndef COND_NO_REQUIRE_LOCKED_MUTEX if (wc_LockMutex(&crl->crlLock) != 0) { WOLFSSL_MSG("wc_LockMutex crlLock failed"); return BAD_MUTEX_E; } -#endif - crl->setup = status; + if (wc_UnLockMutex(&crl->crlLock) != 0) { + WOLFSSL_MSG("wc_UnLockMutex crlLock failed"); + return BAD_MUTEX_E; + } + ret = wolfSSL_CondSignal(&crl->cond); - -#ifndef COND_NO_REQUIRE_LOCKED_MUTEX - wc_UnLockMutex(&crl->crlLock); -#endif - if (ret != 0) return BAD_COND_E; @@ -1491,31 +1488,38 @@ static int StartMonitorCRL(WOLFSSL_CRL* crl) return THREAD_CREATE_E; } -#ifndef COND_NO_REQUIRE_LOCKED_MUTEX /* wait for setup to complete */ if (wc_LockMutex(&crl->crlLock) != 0) { - WOLFSSL_MSG("wc_LockMutex crlLock error"); + WOLFSSL_MSG("wc_LockMutex crlLock failed"); return BAD_MUTEX_E; } -#endif - while (crl->setup == 0) { - if (wolfSSL_CondWait(&crl->cond, &crl->crlLock) != 0) { + int condRet; + if (wc_UnLockMutex(&crl->crlLock) != 0) { + WOLFSSL_MSG("wc_UnLockMutex crlLock failed"); + return BAD_MUTEX_E; + } + condRet = wolfSSL_CondWait(&crl->cond); + if (wc_LockMutex(&crl->crlLock) != 0) { + WOLFSSL_MSG("wc_LockMutex crlLock failed"); + return BAD_MUTEX_E; + } + if (condRet != 0) { ret = BAD_COND_E; break; } } - if (crl->setup < 0) + if (ret >= 0 && crl->setup < 0) ret = crl->setup; /* store setup error */ -#ifndef COND_NO_REQUIRE_LOCKED_MUTEX - wc_UnLockMutex(&crl->crlLock); -#endif - if (ret < 0) { WOLFSSL_MSG("DoMonitor setup failure"); crl->tid = INVALID_THREAD_VAL; /* thread already done */ } + if (wc_UnLockMutex(&crl->crlLock) != 0) { + WOLFSSL_MSG("wc_UnLockMutex crlLock failed"); + return BAD_MUTEX_E; + } return ret; } diff --git a/tests/utils.c b/tests/utils.c index 368c7f44a..14480855c 100644 --- a/tests/utils.c +++ b/tests/utils.c @@ -322,31 +322,23 @@ 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 + tcp_ready* ready = args->signal; + THREAD_CHECK_RET(wc_LockMutex(&ready->mutex)); + if (!ready->ready) { + THREAD_CHECK_RET(wc_UnLockMutex(&ready->mutex)); + THREAD_CHECK_RET(wolfSSL_CondWait(&ready->cond)); + THREAD_CHECK_RET(wc_LockMutex(&ready->mutex)); } - args->signal->ready = 0; /* reset */ - + ready->ready = 0; /* reset */ + THREAD_CHECK_RET(wc_UnLockMutex(&ready->mutex)); #else /* no threading wait or single threaded */ (void)args; @@ -356,7 +348,7 @@ void wait_tcp_ready(func_args* args) #ifndef SINGLE_THREADED /* Start a thread. * - * @param [in] fun Function to executre in 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. */ diff --git a/wolfcrypt/src/wc_port.c b/wolfcrypt/src/wc_port.c index 61104b99a..13ca5799b 100644 --- a/wolfcrypt/src/wc_port.c +++ b/wolfcrypt/src/wc_port.c @@ -3491,11 +3491,8 @@ char* mystrnstr(const char* s1, const char* s2, unsigned int n) return 0; } - int wolfSSL_CondWait(COND_TYPE* cond, - wolfSSL_Mutex* mutex) + int wolfSSL_CondWait(COND_TYPE* cond) { - (void)mutex; - if (cond == NULL) return BAD_FUNC_ARG; @@ -3698,14 +3695,94 @@ char* mystrnstr(const char* s1, const char* s2, unsigned int n) } #ifdef WOLFSSL_COND + #ifndef __MACH__ + /* Generic POSIX conditional */ int wolfSSL_CondInit(COND_TYPE* cond) { if (cond == NULL) return BAD_FUNC_ARG; - if (pthread_cond_init(cond, NULL) != 0) + if (pthread_mutex_init(&cond->mutex, NULL) != 0) return MEMORY_E; + if (pthread_cond_init(&cond->cond, NULL) != 0) { + /* Keep compilers happy that we are using the return code */ + if (pthread_mutex_destroy(&cond->mutex) != 0) + return MEMORY_E; + return MEMORY_E; + } + + return 0; + } + + int wolfSSL_CondFree(COND_TYPE* cond) + { + int ret = 0; + + if (cond == NULL) + return BAD_FUNC_ARG; + + if (pthread_mutex_destroy(&cond->mutex) != 0) + ret = MEMORY_E; + + if (pthread_cond_destroy(&cond->cond) != 0) + ret = MEMORY_E; + + return ret; + } + + int wolfSSL_CondSignal(COND_TYPE* cond) + { + int ret = 0; + + if (cond == NULL) + return BAD_FUNC_ARG; + + if (pthread_mutex_lock(&cond->mutex) != 0) + return BAD_MUTEX_E; + + if (pthread_cond_signal(&cond->cond) != 0) + ret = MEMORY_E; + + if (pthread_mutex_unlock(&cond->mutex) != 0) + ret = MEMORY_E; + + return ret; + } + + int wolfSSL_CondWait(COND_TYPE* cond) + { + int ret = 0; + + if (cond == NULL) + return BAD_FUNC_ARG; + + if (pthread_mutex_lock(&cond->mutex) != 0) + return BAD_MUTEX_E; + + if (pthread_cond_wait(&cond->cond, &cond->mutex) != 0) + ret = MEMORY_E; + + if (pthread_mutex_unlock(&cond->mutex) != 0) + ret = MEMORY_E; + + return ret; + } + #else /* __MACH__ */ + /* Apple style dispatch semaphore */ + int wolfSSL_CondInit(COND_TYPE* cond) + { + if (cond == NULL) + return BAD_FUNC_ARG; + + /* dispatch_release() fails hard, with Trace/BPT trap signal, if the + * sem's internal count is less than the value passed in with + * dispatch_semaphore_create(). work around this by initing + * with 0, then incrementing it afterwards. + */ + *cond = dispatch_semaphore_create(0); + if (*cond == NULL) + return MEMORY_E; return 0; } @@ -3714,9 +3791,8 @@ char* mystrnstr(const char* s1, const char* s2, unsigned int n) if (cond == NULL) return BAD_FUNC_ARG; - if (pthread_cond_destroy(cond) != 0) - return MEMORY_E; - + dispatch_release(*cond); + *cond = NULL; return 0; } @@ -3725,26 +3801,19 @@ char* mystrnstr(const char* s1, const char* s2, unsigned int n) if (cond == NULL) return BAD_FUNC_ARG; - if (pthread_cond_signal(cond) != 0) - return MEMORY_E; - + dispatch_semaphore_signal(*cond); return 0; } - int wolfSSL_CondWait(COND_TYPE* cond, - wolfSSL_Mutex* mutex) + int wolfSSL_CondWait(COND_TYPE* cond) { - if (cond == NULL || mutex == NULL) + if (cond == NULL) return BAD_FUNC_ARG; - /* mutex has to be locked on entry so we can't touch */ - - if (pthread_cond_wait(cond, mutex) != 0) - return MEMORY_E; - + dispatch_semaphore_wait(*cond, DISPATCH_TIME_FOREVER); return 0; } - + #endif /* __MACH__ */ #endif /* WOLFSSL_COND */ #endif diff --git a/wolfssl/quic.h b/wolfssl/quic.h index 871951169..c1462cbae 100644 --- a/wolfssl/quic.h +++ b/wolfssl/quic.h @@ -52,7 +52,7 @@ typedef struct wolfssl_quic_method_t WOLFSSL_QUIC_METHOD; struct wolfssl_quic_method_t { /** - * Provide secrets to the QUIC stack when they becaome available in the SSL + * Provide secrets to the QUIC stack when they become available in the SSL * instance during handshake processing. read/write secrets have the same * length. A call may only provide one, passing NULL as the other. */ diff --git a/wolfssl/ssl.h b/wolfssl/ssl.h index 26082f850..20f85982c 100644 --- a/wolfssl/ssl.h +++ b/wolfssl/ssl.h @@ -525,6 +525,9 @@ struct WOLFSSL_BIO_METHOD { wolfssl_BIO_meth_ctrl_info_cb ctrlInfoCb; }; +#define WOLFSSL_BIO_METHOD_INIT(bio_type) \ + { bio_type, { 0 }, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL } + /* wolfSSL BIO type */ typedef long (*wolf_bio_info_cb)(WOLFSSL_BIO *bio, int event, const char *parg, int iarg, long larg, long return_value); diff --git a/wolfssl/test.h b/wolfssl/test.h index fb82a7034..e468e7bae 100644 --- a/wolfssl/test.h +++ b/wolfssl/test.h @@ -551,12 +551,10 @@ static WC_INLINE void InitTcpReady(tcp_ready* ready) ready->srfName = NULL; #ifndef SINGLE_THREADED -#ifdef WOLFSSL_COND THREAD_CHECK_RET(wc_InitMutex(&ready->mutex)); + #ifdef WOLFSSL_COND 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 #endif } @@ -567,11 +565,9 @@ static WC_INLINE void InitTcpReady(tcp_ready* ready) static WC_INLINE void FreeTcpReady(tcp_ready* ready) { #ifndef SINGLE_THREADED + THREAD_CHECK_RET(wc_FreeMutex(&ready->mutex)); #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; @@ -695,24 +691,18 @@ 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) { -#ifndef COND_NO_REQUIRE_LOCKED_MUTEX THREAD_CHECK_RET(wc_LockMutex(&srtp->mutex)); -#endif - - if (srtp->server_srtp_ekm == NULL) - THREAD_CHECK_RET(wolfSSL_CondWait(&srtp->cond, &srtp->mutex)); - -#ifdef COND_NO_REQUIRE_LOCKED_MUTEX - THREAD_CHECK_RET(wc_LockMutex(&srtp->mutex)); -#endif - + if (srtp->server_srtp_ekm == NULL) { + THREAD_CHECK_RET(wc_UnLockMutex(&srtp->mutex)); + THREAD_CHECK_RET(wolfSSL_CondWait(&srtp->cond)); + THREAD_CHECK_RET(wc_LockMutex(&srtp->mutex)); + } *ekm = srtp->server_srtp_ekm; *size = srtp->server_srtp_ekm_size; /* reset */ srtp->server_srtp_ekm = NULL; srtp->server_srtp_ekm_size = 0; - THREAD_CHECK_RET(wc_UnLockMutex(&srtp->mutex)); } @@ -731,17 +721,10 @@ static WC_INLINE void srtp_helper_set_ekm(srtp_test_helper *srtp, uint8_t *ekm, size_t size) { THREAD_CHECK_RET(wc_LockMutex(&srtp->mutex)); - srtp->server_srtp_ekm_size = size; srtp->server_srtp_ekm = ekm; -#ifdef COND_NO_REQUIRE_LOCKED_MUTEX THREAD_CHECK_RET(wc_UnLockMutex(&srtp->mutex)); -#endif THREAD_CHECK_RET(wolfSSL_CondSignal(&srtp->cond)); - -#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) @@ -750,7 +733,8 @@ static WC_INLINE void srtp_helper_free(srtp_test_helper *srtp) THREAD_CHECK_RET(wolfSSL_CondFree(&srtp->cond)); } -#endif /* WOLFSSL_SRTP && !SINGLE_THREADED && POSIX_THREADS */ +#endif /* WOLFSSL_SRTP && WOLFSSL_COND */ + /** * @@ -2237,19 +2221,12 @@ static WC_INLINE void udp_accept(SOCKET_T* sockfd, SOCKET_T* clientfd, THREAD_CHECK_RET(wc_LockMutex(&ready->mutex)); ready->ready = 1; ready->port = port; -#ifdef WOLFSSL_COND + THREAD_CHECK_RET(wc_UnLockMutex(&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 - THREAD_CHECK_RET(wc_UnLockMutex(&ready->mutex)); -#endif -#endif + #endif +#endif /* !SINGLE_THREADED */ } else { fprintf(stderr, "args or args->signal was NULL. Not setting ready info."); @@ -2283,17 +2260,12 @@ static WC_INLINE void tcp_accept(SOCKET_T* sockfd, SOCKET_T* clientfd, THREAD_CHECK_RET(wc_LockMutex(&ready->mutex)); ready->ready = 1; ready->port = port; -#ifdef WOLFSSL_COND -#ifdef COND_NO_REQUIRE_LOCKED_MUTEX THREAD_CHECK_RET(wc_UnLockMutex(&ready->mutex)); -#endif + #ifdef WOLFSSL_COND THREAD_CHECK_RET(wolfSSL_CondSignal(&ready->cond)); -#ifndef COND_NO_REQUIRE_LOCKED_MUTEX - THREAD_CHECK_RET(wc_UnLockMutex(&ready->mutex)); -#endif -#endif + #endif } -#endif +#endif /* !SINGLE_THREADED */ if (ready_file) { #if !defined(NO_FILESYSTEM) || defined(FORCE_BUFFER_TEST) && \ diff --git a/wolfssl/wolfcrypt/types.h b/wolfssl/wolfcrypt/types.h index 93413f3da..3a6b8a7af 100644 --- a/wolfssl/wolfcrypt/types.h +++ b/wolfssl/wolfcrypt/types.h @@ -1380,9 +1380,18 @@ typedef struct w64wrapper { #define WOLFSSL_THREAD #elif (defined(_POSIX_THREADS) || defined(HAVE_PTHREAD)) && \ !defined(__MINGW32__) + #ifndef __MACH__ + #include + typedef struct COND_TYPE { + pthread_mutex_t mutex; + pthread_cond_t cond; + } COND_TYPE; + #else + #include + typedef dispatch_semaphore_t COND_TYPE; + #endif typedef void* THREAD_RETURN; typedef pthread_t THREAD_TYPE; - typedef pthread_cond_t COND_TYPE; #define WOLFSSL_COND #define WOLFSSL_THREAD #ifndef HAVE_SELFTEST @@ -1398,7 +1407,6 @@ typedef struct w64wrapper { typedef HANDLE COND_TYPE; #define WOLFSSL_COND #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 @@ -1433,8 +1441,6 @@ typedef struct w64wrapper { * 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 @@ -1475,8 +1481,7 @@ typedef struct w64wrapper { 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); + WOLFSSL_API int wolfSSL_CondWait(COND_TYPE* cond); #endif #else #define WOLFSSL_RETURN_FROM_THREAD(x) return (THREAD_RETURN)(x)