Skip to content

Commit

Permalink
semaphores: use Mach semaphores functions on MacOS
Browse files Browse the repository at this point in the history
As macOS doesn't support unnamed semaphores via sem_init, work
around those via Mach semaphores using wrapper functions.
As semaphore_wait() doesn't imply pthread_cancel(), imply that.
  • Loading branch information
bartoldeman committed Jul 23, 2023
1 parent 3163923 commit f63aa00
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/base/misc/utilities.c
Original file line number Diff line number Diff line change
Expand Up @@ -1024,8 +1024,6 @@ static int pts_open(int pty_fd)
return pts_fd;
}

typedef sem_t *pshared_sem_t;

static int pshared_sem_init(pshared_sem_t *sem, unsigned int value)
{
char sem_name[] = "/dosemu2_psem_%PXXXXXX";
Expand Down Expand Up @@ -1086,7 +1084,7 @@ pid_t run_external_command(const char *path, int argc, const char **argv,
pts_fd = pts_open(pty_fd);
/* Reading master side before slave opened, results in EOF.
* Notify user that reads are now safe. */
sem_post(pty_sem);
pshared_sem_post(pty_sem);
pshared_sem_destroy(&pty_sem);
if (pts_fd == -1) {
error("run_unix_command(): open pts failed %s\n", strerror(errno));
Expand Down Expand Up @@ -1140,7 +1138,7 @@ pid_t run_external_command(const char *path, int argc, const char **argv,
}
sigprocmask(SIG_SETMASK, &oset, NULL);
/* wait until its safe to read from pty_fd */
sem_wait(pty_sem);
pshared_sem_wait(pty_sem);
pshared_sem_destroy(&pty_sem);
return pid;
}
Expand Down
24 changes: 24 additions & 0 deletions src/include/utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,30 @@ FILE *fstream_tee(FILE *orig, FILE *copy);
pthread_cleanup_pop(0); \
}

typedef sem_t *pshared_sem_t;
static inline int pshared_sem_post(sem_t *sem)
{
return sem_post(sem);
}
static inline int pshared_sem_wait(sem_t *sem)
{
return sem_wait(sem);
}

/* macOS doesn't support sem_init(), so use Mach semaphores instead */
#ifdef __APPLE__
#include <mach/mach_init.h>
#include <mach/task.h>
#include <mach/semaphore.h>
#undef PAGE_SIZE
#define PAGE_SIZE 4096
#define sem_t semaphore_t
#define sem_init(x,y,z) semaphore_create(mach_task_self(), x, SYNC_POLICY_FIFO, z)
#define sem_post(x) semaphore_signal(*(x))
#define sem_wait(x) do {semaphore_wait(*(x)); pthread_testcancel();} while(0)
#define sem_destroy(x) semaphore_destroy(mach_task_self(), *(x))
#endif

pid_t run_external_command(const char *path, int argc,
const char **argv,
int use_stdin, int close_from, int pty_fd);
Expand Down
1 change: 1 addition & 0 deletions src/plugin/fluidsynth/mid_o_flus.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "emu.h"
#include "init.h"
#include "timers.h"
#include "utilities.h"
#include "sound/midi.h"
#include "sound/sound.h"

Expand Down
1 change: 1 addition & 0 deletions src/plugin/libao/snd_o_ao.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

#include "emu.h"
#include "init.h"
#include "utilities.h"
#include "sound/sound.h"
#include "ao_init.h"
#include <stdio.h>
Expand Down
1 change: 1 addition & 0 deletions src/plugin/munt/munt.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "emu.h"
#include "init.h"
#include "timers.h"
#include "utilities.h"
#include "sound/midi.h"
#include "sound/sound.h"

Expand Down

0 comments on commit f63aa00

Please sign in to comment.