From 2ddfe15c4fb08e56f4eaee8973ee00b9fa0ac4e7 Mon Sep 17 00:00:00 2001 From: Sergey Fedorov Date: Sat, 31 Aug 2024 21:48:13 +0800 Subject: [PATCH] Fix libdispatch usage condition --- wolfcrypt/src/wc_port.c | 167 ++++++++++++++++++++------------------ wolfssl/wolfcrypt/types.h | 19 +++-- 2 files changed, 99 insertions(+), 87 deletions(-) diff --git a/wolfcrypt/src/wc_port.c b/wolfcrypt/src/wc_port.c index 294bc415c..fd901f988 100644 --- a/wolfcrypt/src/wc_port.c +++ b/wolfcrypt/src/wc_port.c @@ -24,6 +24,10 @@ #include #endif +#ifdef __APPLE__ + #include +#endif + #include #include #include @@ -3814,86 +3818,8 @@ 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_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_CondStart(COND_TYPE* cond) - { - if (cond == NULL) - return BAD_FUNC_ARG; - - if (pthread_mutex_lock(&cond->mutex) != 0) - return BAD_MUTEX_E; - - return 0; - } - - int wolfSSL_CondSignal(COND_TYPE* cond) - { - if (cond == NULL) - return BAD_FUNC_ARG; - - if (pthread_cond_signal(&cond->cond) != 0) - return MEMORY_E; - - return 0; - } - - int wolfSSL_CondWait(COND_TYPE* cond) - { - if (cond == NULL) - return BAD_FUNC_ARG; - - if (pthread_cond_wait(&cond->cond, &cond->mutex) != 0) - return MEMORY_E; - - return 0; - } - - int wolfSSL_CondEnd(COND_TYPE* cond) - { - if (cond == NULL) - return BAD_FUNC_ARG; - - if (pthread_mutex_unlock(&cond->mutex) != 0) - return BAD_MUTEX_E; - - return 0; - } - #else /* __MACH__ */ + #if defined(__APPLE__) && MAC_OS_X_VERSION_MIN_REQUIRED >= 1060 \ + && !defined(__ppc__) /* Apple style dispatch semaphore */ int wolfSSL_CondInit(COND_TYPE* cond) { @@ -3985,6 +3911,87 @@ char* mystrnstr(const char* s1, const char* s2, unsigned int n) return 0; } + + #else /* Generic POSIX conditional */ + + int wolfSSL_CondInit(COND_TYPE* cond) + { + if (cond == NULL) + return BAD_FUNC_ARG; + + 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_CondStart(COND_TYPE* cond) + { + if (cond == NULL) + return BAD_FUNC_ARG; + + if (pthread_mutex_lock(&cond->mutex) != 0) + return BAD_MUTEX_E; + + return 0; + } + + int wolfSSL_CondSignal(COND_TYPE* cond) + { + if (cond == NULL) + return BAD_FUNC_ARG; + + if (pthread_cond_signal(&cond->cond) != 0) + return MEMORY_E; + + return 0; + } + + int wolfSSL_CondWait(COND_TYPE* cond) + { + if (cond == NULL) + return BAD_FUNC_ARG; + + if (pthread_cond_wait(&cond->cond, &cond->mutex) != 0) + return MEMORY_E; + + return 0; + } + + int wolfSSL_CondEnd(COND_TYPE* cond) + { + if (cond == NULL) + return BAD_FUNC_ARG; + + if (pthread_mutex_unlock(&cond->mutex) != 0) + return BAD_MUTEX_E; + + return 0; + } + #endif /* __MACH__ */ #endif /* WOLFSSL_COND */ diff --git a/wolfssl/wolfcrypt/types.h b/wolfssl/wolfcrypt/types.h index 4696b065b..5b31d4b48 100644 --- a/wolfssl/wolfcrypt/types.h +++ b/wolfssl/wolfcrypt/types.h @@ -34,6 +34,10 @@ decouple library dependencies with standard string, memory and so on. #include #include + #ifdef __APPLE__ + #include + #endif + #ifdef __cplusplus extern "C" { #endif @@ -1490,18 +1494,19 @@ typedef struct w64wrapper { typedef size_t THREAD_TYPE; #define WOLFSSL_THREAD #elif defined(WOLFSSL_PTHREADS) - #ifndef __MACH__ - #include - typedef struct COND_TYPE { - pthread_mutex_t mutex; - pthread_cond_t cond; - } COND_TYPE; - #else + #if defined(__APPLE__) && MAC_OS_X_VERSION_MIN_REQUIRED >= 1060 \ + && !defined(__ppc__) #include typedef struct COND_TYPE { wolfSSL_Mutex mutex; dispatch_semaphore_t cond; } COND_TYPE; + #else + #include + typedef struct COND_TYPE { + pthread_mutex_t mutex; + pthread_cond_t cond; + } COND_TYPE; #endif typedef void* THREAD_RETURN; typedef pthread_t THREAD_TYPE;