Skip to content

Commit 2ddfe15

Browse files
committed
Fix libdispatch usage condition
1 parent 4d837e7 commit 2ddfe15

2 files changed

Lines changed: 72 additions & 60 deletions

File tree

wolfcrypt/src/wc_port.c

Lines changed: 60 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
#include <config.h>
2525
#endif
2626

27+
#ifdef __APPLE__
28+
#include <AvailabilityMacros.h>
29+
#endif
30+
2731
#include <wolfssl/wolfcrypt/settings.h>
2832
#include <wolfssl/wolfcrypt/types.h>
2933
#include <wolfssl/wolfcrypt/error-crypt.h>
@@ -3814,20 +3818,25 @@ char* mystrnstr(const char* s1, const char* s2, unsigned int n)
38143818
}
38153819

38163820
#ifdef WOLFSSL_COND
3817-
#ifndef __MACH__
3818-
/* Generic POSIX conditional */
3821+
#if defined(__APPLE__) && MAC_OS_X_VERSION_MIN_REQUIRED >= 1060 \
3822+
&& !defined(__ppc__)
3823+
/* Apple style dispatch semaphore */
38193824
int wolfSSL_CondInit(COND_TYPE* cond)
38203825
{
38213826
if (cond == NULL)
38223827
return BAD_FUNC_ARG;
38233828

3824-
if (pthread_mutex_init(&cond->mutex, NULL) != 0)
3829+
/* dispatch_release() fails hard, with Trace/BPT trap signal, if the
3830+
* sem's internal count is less than the value passed in with
3831+
* dispatch_semaphore_create(). work around this by initing
3832+
* with 0, then incrementing it afterwards.
3833+
*/
3834+
cond->cond = dispatch_semaphore_create(0);
3835+
if (cond->cond == NULL)
38253836
return MEMORY_E;
38263837

3827-
if (pthread_cond_init(&cond->cond, NULL) != 0) {
3828-
/* Keep compilers happy that we are using the return code */
3829-
if (pthread_mutex_destroy(&cond->mutex) != 0)
3830-
return MEMORY_E;
3838+
if (wc_InitMutex(&cond->mutex) != 0) {
3839+
dispatch_release(cond->cond);
38313840
return MEMORY_E;
38323841
}
38333842

@@ -3836,26 +3845,25 @@ char* mystrnstr(const char* s1, const char* s2, unsigned int n)
38363845

38373846
int wolfSSL_CondFree(COND_TYPE* cond)
38383847
{
3839-
int ret = 0;
3840-
38413848
if (cond == NULL)
38423849
return BAD_FUNC_ARG;
38433850

3844-
if (pthread_mutex_destroy(&cond->mutex) != 0)
3845-
ret = MEMORY_E;
3851+
dispatch_release(cond->cond);
3852+
cond->cond = NULL;
38463853

3847-
if (pthread_cond_destroy(&cond->cond) != 0)
3848-
ret = MEMORY_E;
3854+
if (wc_FreeMutex(&cond->mutex) != 0) {
3855+
return MEMORY_E;
3856+
}
38493857

3850-
return ret;
3858+
return 0;
38513859
}
38523860

38533861
int wolfSSL_CondStart(COND_TYPE* cond)
38543862
{
38553863
if (cond == NULL)
38563864
return BAD_FUNC_ARG;
38573865

3858-
if (pthread_mutex_lock(&cond->mutex) != 0)
3866+
if (wc_LockMutex(&cond->mutex) != 0)
38593867
return BAD_MUTEX_E;
38603868

38613869
return 0;
@@ -3866,8 +3874,13 @@ char* mystrnstr(const char* s1, const char* s2, unsigned int n)
38663874
if (cond == NULL)
38673875
return BAD_FUNC_ARG;
38683876

3869-
if (pthread_cond_signal(&cond->cond) != 0)
3870-
return MEMORY_E;
3877+
if (wc_UnLockMutex(&cond->mutex) != 0)
3878+
return BAD_MUTEX_E;
3879+
3880+
dispatch_semaphore_signal(cond->cond);
3881+
3882+
if (wc_LockMutex(&cond->mutex) != 0)
3883+
return BAD_MUTEX_E;
38713884

38723885
return 0;
38733886
}
@@ -3877,8 +3890,13 @@ char* mystrnstr(const char* s1, const char* s2, unsigned int n)
38773890
if (cond == NULL)
38783891
return BAD_FUNC_ARG;
38793892

3880-
if (pthread_cond_wait(&cond->cond, &cond->mutex) != 0)
3881-
return MEMORY_E;
3893+
if (wc_UnLockMutex(&cond->mutex) != 0)
3894+
return BAD_MUTEX_E;
3895+
3896+
dispatch_semaphore_wait(cond->cond, DISPATCH_TIME_FOREVER);
3897+
3898+
if (wc_LockMutex(&cond->mutex) != 0)
3899+
return BAD_MUTEX_E;
38823900

38833901
return 0;
38843902
}
@@ -3888,29 +3906,26 @@ char* mystrnstr(const char* s1, const char* s2, unsigned int n)
38883906
if (cond == NULL)
38893907
return BAD_FUNC_ARG;
38903908

3891-
if (pthread_mutex_unlock(&cond->mutex) != 0)
3909+
if (wc_UnLockMutex(&cond->mutex) != 0)
38923910
return BAD_MUTEX_E;
38933911

38943912
return 0;
38953913
}
3896-
#else /* __MACH__ */
3897-
/* Apple style dispatch semaphore */
3914+
3915+
#else /* Generic POSIX conditional */
3916+
38983917
int wolfSSL_CondInit(COND_TYPE* cond)
38993918
{
39003919
if (cond == NULL)
39013920
return BAD_FUNC_ARG;
39023921

3903-
/* dispatch_release() fails hard, with Trace/BPT trap signal, if the
3904-
* sem's internal count is less than the value passed in with
3905-
* dispatch_semaphore_create(). work around this by initing
3906-
* with 0, then incrementing it afterwards.
3907-
*/
3908-
cond->cond = dispatch_semaphore_create(0);
3909-
if (cond->cond == NULL)
3922+
if (pthread_mutex_init(&cond->mutex, NULL) != 0)
39103923
return MEMORY_E;
39113924

3912-
if (wc_InitMutex(&cond->mutex) != 0) {
3913-
dispatch_release(cond->cond);
3925+
if (pthread_cond_init(&cond->cond, NULL) != 0) {
3926+
/* Keep compilers happy that we are using the return code */
3927+
if (pthread_mutex_destroy(&cond->mutex) != 0)
3928+
return MEMORY_E;
39143929
return MEMORY_E;
39153930
}
39163931

@@ -3919,25 +3934,26 @@ char* mystrnstr(const char* s1, const char* s2, unsigned int n)
39193934

39203935
int wolfSSL_CondFree(COND_TYPE* cond)
39213936
{
3937+
int ret = 0;
3938+
39223939
if (cond == NULL)
39233940
return BAD_FUNC_ARG;
39243941

3925-
dispatch_release(cond->cond);
3926-
cond->cond = NULL;
3942+
if (pthread_mutex_destroy(&cond->mutex) != 0)
3943+
ret = MEMORY_E;
39273944

3928-
if (wc_FreeMutex(&cond->mutex) != 0) {
3929-
return MEMORY_E;
3930-
}
3945+
if (pthread_cond_destroy(&cond->cond) != 0)
3946+
ret = MEMORY_E;
39313947

3932-
return 0;
3948+
return ret;
39333949
}
39343950

39353951
int wolfSSL_CondStart(COND_TYPE* cond)
39363952
{
39373953
if (cond == NULL)
39383954
return BAD_FUNC_ARG;
39393955

3940-
if (wc_LockMutex(&cond->mutex) != 0)
3956+
if (pthread_mutex_lock(&cond->mutex) != 0)
39413957
return BAD_MUTEX_E;
39423958

39433959
return 0;
@@ -3948,13 +3964,8 @@ char* mystrnstr(const char* s1, const char* s2, unsigned int n)
39483964
if (cond == NULL)
39493965
return BAD_FUNC_ARG;
39503966

3951-
if (wc_UnLockMutex(&cond->mutex) != 0)
3952-
return BAD_MUTEX_E;
3953-
3954-
dispatch_semaphore_signal(cond->cond);
3955-
3956-
if (wc_LockMutex(&cond->mutex) != 0)
3957-
return BAD_MUTEX_E;
3967+
if (pthread_cond_signal(&cond->cond) != 0)
3968+
return MEMORY_E;
39583969

39593970
return 0;
39603971
}
@@ -3964,13 +3975,8 @@ char* mystrnstr(const char* s1, const char* s2, unsigned int n)
39643975
if (cond == NULL)
39653976
return BAD_FUNC_ARG;
39663977

3967-
if (wc_UnLockMutex(&cond->mutex) != 0)
3968-
return BAD_MUTEX_E;
3969-
3970-
dispatch_semaphore_wait(cond->cond, DISPATCH_TIME_FOREVER);
3971-
3972-
if (wc_LockMutex(&cond->mutex) != 0)
3973-
return BAD_MUTEX_E;
3978+
if (pthread_cond_wait(&cond->cond, &cond->mutex) != 0)
3979+
return MEMORY_E;
39743980

39753981
return 0;
39763982
}
@@ -3980,11 +3986,12 @@ char* mystrnstr(const char* s1, const char* s2, unsigned int n)
39803986
if (cond == NULL)
39813987
return BAD_FUNC_ARG;
39823988

3983-
if (wc_UnLockMutex(&cond->mutex) != 0)
3989+
if (pthread_mutex_unlock(&cond->mutex) != 0)
39843990
return BAD_MUTEX_E;
39853991

39863992
return 0;
39873993
}
3994+
39883995
#endif /* __MACH__ */
39893996
#endif /* WOLFSSL_COND */
39903997

wolfssl/wolfcrypt/types.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ decouple library dependencies with standard string, memory and so on.
3434
#include <wolfssl/wolfcrypt/settings.h>
3535
#include <wolfssl/wolfcrypt/wc_port.h>
3636

37+
#ifdef __APPLE__
38+
#include <AvailabilityMacros.h>
39+
#endif
40+
3741
#ifdef __cplusplus
3842
extern "C" {
3943
#endif
@@ -1490,18 +1494,19 @@ typedef struct w64wrapper {
14901494
typedef size_t THREAD_TYPE;
14911495
#define WOLFSSL_THREAD
14921496
#elif defined(WOLFSSL_PTHREADS)
1493-
#ifndef __MACH__
1494-
#include <pthread.h>
1495-
typedef struct COND_TYPE {
1496-
pthread_mutex_t mutex;
1497-
pthread_cond_t cond;
1498-
} COND_TYPE;
1499-
#else
1497+
#if defined(__APPLE__) && MAC_OS_X_VERSION_MIN_REQUIRED >= 1060 \
1498+
&& !defined(__ppc__)
15001499
#include <dispatch/dispatch.h>
15011500
typedef struct COND_TYPE {
15021501
wolfSSL_Mutex mutex;
15031502
dispatch_semaphore_t cond;
15041503
} COND_TYPE;
1504+
#else
1505+
#include <pthread.h>
1506+
typedef struct COND_TYPE {
1507+
pthread_mutex_t mutex;
1508+
pthread_cond_t cond;
1509+
} COND_TYPE;
15051510
#endif
15061511
typedef void* THREAD_RETURN;
15071512
typedef pthread_t THREAD_TYPE;

0 commit comments

Comments
 (0)