Skip to content

Commit 6b7fe09

Browse files
committed
Implement proper MacOS dispatch for conditional signal/wait. Note: this logic was pulled from wolfMQTT and is well established.
1 parent f119086 commit 6b7fe09

2 files changed

Lines changed: 40 additions & 6 deletions

File tree

wolfcrypt/src/wc_port.c

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3962,7 +3962,21 @@ char* mystrnstr(const char* s1, const char* s2, unsigned int n)
39623962
{
39633963
if (cond == NULL)
39643964
return BAD_FUNC_ARG;
3965-
#if defined(__OS2__)
3965+
#if defined(__MACH__)
3966+
cond->cond = dispatch_semaphore_create(0);
3967+
if (cond->cond == NULL)
3968+
return MEMORY_E;
3969+
3970+
/* dispatch_release() fails hard, with Trace/BPT trap signal, if the
3971+
* sem's internal count is less than the value passed in with
3972+
* dispatch_semaphore_create(). work around this by initializing
3973+
* with 0, then incrementing it afterwards.
3974+
*/
3975+
if (dispatch_semaphore_signal(s->sem) < 0) {
3976+
dispatch_release(s->sem);
3977+
return return MEMORY_E;
3978+
}
3979+
#elif defined(__OS2__)
39663980
DosCreateMutexSem( NULL, &cond->mutex, 0, FALSE );
39673981
DosCreateEventSem( NULL, &cond->cond, DCE_POSTONE, FALSE );
39683982
#elif defined(__NT__)
@@ -3993,7 +4007,9 @@ char* mystrnstr(const char* s1, const char* s2, unsigned int n)
39934007
{
39944008
if (cond == NULL)
39954009
return BAD_FUNC_ARG;
3996-
#if defined(__OS2__)
4010+
#if defined(__MACH__)
4011+
dispatch_release(cond->cond);
4012+
#elif defined(__OS2__)
39974013
DosCloseMutexSem(cond->mutex);
39984014
DosCloseEventSem(cond->cond);
39994015
#elif defined(__NT__)
@@ -4013,7 +4029,8 @@ char* mystrnstr(const char* s1, const char* s2, unsigned int n)
40134029
{
40144030
if (cond == NULL)
40154031
return BAD_FUNC_ARG;
4016-
#if defined(__OS2__)
4032+
#if defined(__MACH__)
4033+
#elif defined(__OS2__)
40174034
#elif defined(__NT__)
40184035
if (wc_LockMutex(&cond->mutex) != 0)
40194036
return BAD_MUTEX_E;
@@ -4028,7 +4045,9 @@ char* mystrnstr(const char* s1, const char* s2, unsigned int n)
40284045
{
40294046
if (cond == NULL)
40304047
return BAD_FUNC_ARG;
4031-
#if defined(__OS2__)
4048+
#if defined(__MACH__)
4049+
dispatch_semaphore_signal(cond->cond);
4050+
#elif defined(__OS2__)
40324051
#elif defined(__NT__)
40334052
if (wc_UnLockMutex(&cond->mutex) != 0)
40344053
return BAD_MUTEX_E;
@@ -4049,7 +4068,9 @@ char* mystrnstr(const char* s1, const char* s2, unsigned int n)
40494068
{
40504069
if (cond == NULL)
40514070
return BAD_FUNC_ARG;
4052-
#if defined(__OS2__)
4071+
#if defined(__MACH__)
4072+
dispatch_semaphore_wait(cond->cond, DISPATCH_TIME_FOREVER);
4073+
#elif defined(__OS2__)
40534074
#elif defined(__NT__)
40544075
if (wc_UnLockMutex(&cond->mutex) != 0)
40554076
return BAD_MUTEX_E;

wolfssl/wolfcrypt/types.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1572,7 +1572,20 @@ WOLFSSL_API word32 CheckRunTimeSettings(void);
15721572
#if __WATCOMC__ < 1300
15731573
#define _WCCALLBACK
15741574
#endif
1575-
#if defined(__NT__)
1575+
#if defined(__MACH__)
1576+
#include <dispatch/dispatch.h>
1577+
#include <pthread.h>
1578+
typedef struct COND_TYPE {
1579+
dispatch_semaphore_t cond;
1580+
} COND_TYPE;
1581+
typedef void* THREAD_RETURN;
1582+
typedef pthread_t THREAD_TYPE;
1583+
#define WOLFSSL_COND
1584+
#define WOLFSSL_THREAD
1585+
#ifndef HAVE_SELFTEST
1586+
#define WOLFSSL_THREAD_NO_JOIN
1587+
#endif
1588+
#elif defined(__NT__)
15761589
typedef unsigned THREAD_RETURN;
15771590
typedef uintptr_t THREAD_TYPE;
15781591
typedef struct COND_TYPE {

0 commit comments

Comments
 (0)