Skip to content

Commit 454ccde

Browse files
committed
macOS Semaphore Cleanup
macOS uses GCD for threading and semaphores, but they aren't quite like POSIX semaphores. macOS allows the use of named POSIX semaphores. 1. Convert the semaphores to named POSIX semaphores. 2. Simplify all calls for semaphores into single function calls of the wrapper API. 3. Update both examples/client/client.c and apps/wolfssh/wolfssh.c.
1 parent 0103806 commit 454ccde

File tree

2 files changed

+102
-62
lines changed

2 files changed

+102
-62
lines changed

apps/wolfssh/wolfssh.c

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -272,22 +272,54 @@ static int sendCurrentWindowSize(thread_args* args)
272272

273273
#ifndef _MSC_VER
274274

275-
#if (defined(__OSX__) || defined(__APPLE__))
276-
#include <dispatch/dispatch.h>
277-
dispatch_semaphore_t windowSem;
278-
#else
275+
#include <errno.h>
276+
#include <fcntl.h>
279277
#include <semaphore.h>
280-
static sem_t windowSem;
281-
#endif
278+
#include <stdio.h>
279+
#include <unistd.h>
280+
281+
typedef struct {
282+
sem_t* s;
283+
char name[32];
284+
} WOLFSSH_SEMAPHORE;
285+
286+
static inline
287+
int wolfSSH_SEMAPHORE_Init(WOLFSSH_SEMAPHORE* s, unsigned int n)
288+
{
289+
snprintf(s->name, sizeof(s->name), "/wolfssh_winch_%d", (int)getpid());
290+
s->s = sem_open(s->name, O_CREAT | O_EXCL, 0600, n);
291+
return (s->s != SEM_FAILED);
292+
}
293+
294+
static inline
295+
void wolfSSH_SEMAPHORE_Release(WOLFSSH_SEMAPHORE* s)
296+
{
297+
sem_close(s->s);
298+
sem_unlink(s->name);
299+
}
300+
301+
static inline
302+
int wolfSSH_SEMAPHORE_Wait(WOLFSSH_SEMAPHORE* s)
303+
{
304+
int ret;
305+
do {
306+
ret = sem_wait(s->s);
307+
} while (ret == -1 && errno == EINTR);
308+
return (ret == 0);
309+
}
310+
311+
static inline
312+
void wolfSSH_SEMAPHORE_Post(WOLFSSH_SEMAPHORE* s)
313+
{
314+
sem_post(s->s);
315+
}
316+
317+
WOLFSSH_SEMAPHORE windowSem;
282318

283319
/* capture window change signals */
284320
static void WindowChangeSignal(int sig)
285321
{
286-
#if (defined(__OSX__) || defined(__APPLE__))
287-
dispatch_semaphore_signal(windowSem);
288-
#else
289-
sem_post(&windowSem);
290-
#endif
322+
wolfSSH_SEMAPHORE_Post(&windowSem);
291323
(void)sig;
292324
}
293325

@@ -299,11 +331,9 @@ static THREAD_RET windowMonitor(void* in)
299331

300332
args = (thread_args*)in;
301333
do {
302-
#if (defined(__OSX__) || defined(__APPLE__))
303-
dispatch_semaphore_wait(windowSem, DISPATCH_TIME_FOREVER);
304-
#else
305-
sem_wait(&windowSem);
306-
#endif
334+
if (!wolfSSH_SEMAPHORE_Wait(&windowSem)) {
335+
break;
336+
}
307337
if (args->quit) {
308338
break;
309339
}
@@ -1060,11 +1090,9 @@ static THREAD_RETURN WOLFSSH_THREAD wolfSSH_Client(void* args)
10601090
arg.readError = 0;
10611091
#ifdef WOLFSSH_TERM
10621092
arg.quit = 0;
1063-
#if (defined(__OSX__) || defined(__APPLE__))
1064-
windowSem = dispatch_semaphore_create(0);
1065-
#else
1066-
sem_init(&windowSem, 0, 0);
1067-
#endif
1093+
if (!wolfSSH_SEMAPHORE_Init(&windowSem, 0)) {
1094+
err_sys("Couldn't initialize window semaphore.");
1095+
}
10681096

10691097
if (config.command) {
10701098
int err;
@@ -1087,21 +1115,13 @@ static THREAD_RETURN WOLFSSH_THREAD wolfSSH_Client(void* args)
10871115
#ifdef WOLFSSH_TERM
10881116
/* Wake the windowMonitor thread so it can exit. */
10891117
arg.quit = 1;
1090-
#if (defined(__OSX__) || defined(__APPLE__))
1091-
dispatch_semaphore_signal(windowSem);
1092-
#else
1093-
sem_post(&windowSem);
1094-
#endif
1118+
wolfSSH_SEMAPHORE_Post(&windowSem);
10951119
pthread_join(thread[0], NULL);
10961120
#endif /* WOLFSSH_TERM */
10971121
pthread_cancel(thread[1]);
10981122
pthread_join(thread[1], NULL);
10991123
#ifdef WOLFSSH_TERM
1100-
#if (defined(__OSX__) || defined(__APPLE__))
1101-
dispatch_release(windowSem);
1102-
#else
1103-
sem_destroy(&windowSem);
1104-
#endif
1124+
wolfSSH_SEMAPHORE_Release(&windowSem);
11051125
#endif /* WOLFSSH_TERM */
11061126
ioErr = arg.readError;
11071127
#elif defined(_MSC_VER)

examples/client/client.c

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -240,22 +240,54 @@ static int sendCurrentWindowSize(thread_args* args)
240240
#ifdef WOLFSSH_TERM
241241
#ifndef _MSC_VER
242242

243-
#if (defined(__OSX__) || defined(__APPLE__))
244-
#include <dispatch/dispatch.h>
245-
dispatch_semaphore_t windowSem;
246-
#else
243+
#include <errno.h>
244+
#include <fcntl.h>
247245
#include <semaphore.h>
248-
static sem_t windowSem;
249-
#endif
246+
#include <stdio.h>
247+
#include <unistd.h>
248+
249+
typedef struct {
250+
sem_t* s;
251+
char name[32];
252+
} WOLFSSH_SEMAPHORE;
253+
254+
static inline
255+
int wolfSSH_SEMAPHORE_Init(WOLFSSH_SEMAPHORE* s, unsigned int n)
256+
{
257+
snprintf(s->name, sizeof(s->name), "/wolfssh_winch_%d", (int)getpid());
258+
s->s = sem_open(s->name, O_CREAT | O_EXCL, 0600, n);
259+
return (s->s != SEM_FAILED);
260+
}
261+
262+
static inline
263+
void wolfSSH_SEMAPHORE_Release(WOLFSSH_SEMAPHORE* s)
264+
{
265+
sem_close(s->s);
266+
sem_unlink(s->name);
267+
}
268+
269+
static inline
270+
int wolfSSH_SEMAPHORE_Wait(WOLFSSH_SEMAPHORE* s)
271+
{
272+
int ret;
273+
do {
274+
ret = sem_wait(s->s);
275+
} while (ret == -1 && errno == EINTR);
276+
return (ret == 0);
277+
}
278+
279+
static inline
280+
void wolfSSH_SEMAPHORE_Post(WOLFSSH_SEMAPHORE* s)
281+
{
282+
sem_post(s->s);
283+
}
284+
285+
WOLFSSH_SEMAPHORE windowSem;
250286

251287
/* capture window change signals */
252288
static void WindowChangeSignal(int sig)
253289
{
254-
#if (defined(__OSX__) || defined(__APPLE__))
255-
dispatch_semaphore_signal(windowSem);
256-
#else
257-
sem_post(&windowSem);
258-
#endif
290+
wolfSSH_SEMAPHORE_Post(&windowSem);
259291
(void)sig;
260292
}
261293

@@ -267,11 +299,9 @@ static THREAD_RET windowMonitor(void* in)
267299

268300
args = (thread_args*)in;
269301
do {
270-
#if (defined(__OSX__) || defined(__APPLE__))
271-
dispatch_semaphore_wait(windowSem, DISPATCH_TIME_FOREVER);
272-
#else
273-
sem_wait(&windowSem);
274-
#endif
302+
if (!wolfSSH_SEMAPHORE_Wait(&windowSem)) {
303+
break;
304+
}
275305
if (args->quit) {
276306
break;
277307
}
@@ -1032,11 +1062,9 @@ THREAD_RETURN WOLFSSH_THREAD client_test(void* args)
10321062
arg.quit = 0;
10331063
wc_InitMutex(&arg.lock);
10341064
#ifdef WOLFSSH_TERM
1035-
#if (defined(__OSX__) || defined(__APPLE__))
1036-
windowSem = dispatch_semaphore_create(0);
1037-
#else
1038-
sem_init(&windowSem, 0, 0);
1039-
#endif
1065+
if (!wolfSSH_SEMAPHORE_Init(&windowSem, 0)) {
1066+
err_sys("Couldn't initialize window semaphore.");
1067+
}
10401068

10411069
if (cmd) {
10421070
int err;
@@ -1057,21 +1085,13 @@ THREAD_RETURN WOLFSSH_THREAD client_test(void* args)
10571085
#ifdef WOLFSSH_TERM
10581086
/* Wake the windowMonitor thread so it can exit. */
10591087
arg.quit = 1;
1060-
#if (defined(__OSX__) || defined(__APPLE__))
1061-
dispatch_semaphore_signal(windowSem);
1062-
#else
1063-
sem_post(&windowSem);
1064-
#endif
1088+
wolfSSH_SEMAPHORE_Post(&windowSem);
10651089
pthread_join(thread[0], NULL);
10661090
#endif /* WOLFSSH_TERM */
10671091
pthread_cancel(thread[1]);
10681092
pthread_join(thread[1], NULL);
10691093
#ifdef WOLFSSH_TERM
1070-
#if (defined(__OSX__) || defined(__APPLE__))
1071-
dispatch_release(windowSem);
1072-
#else
1073-
sem_destroy(&windowSem);
1074-
#endif
1094+
wolfSSH_SEMAPHORE_Release(&windowSem);
10751095
#endif /* WOLFSSH_TERM */
10761096
#elif defined(_MSC_VER)
10771097
thread_args arg;

0 commit comments

Comments
 (0)