osdep: add POSIX semaphore emulation for OSX
OSX is POSIX conformant, but it's a sad joke: it provides the
<semaphore.h> prototype as the standard demands, but they're empty
wrappers, and all functions just return ENOSYS.
Emulate them similar to how osdep/io.h emulate filesystem functions on
Windows. By including the header, working sem_* functions become
available.
To make it async-signal safe, use a pipe for wakeup (write() is AS-safe,
but mutexes can't be). Actually I'm not sure anymore if we really need
AS-safety, but for now the emulation can do it.
On Linux, the system provides a far more efficient and robust
implementation. We definitely want to avoid using the emulation if
possible, so this code is active on OSX only. For convenience we always
build the source file though, even if the implementation is disabled and
no actual code is generated.
(Linux provides working semaphores, but is formally not POSIX
conformant. On OSX it's the opposite. Is POSIX a complete joke?)
2014-09-10 01:09:41 +00:00
|
|
|
#ifndef MP_SEMAPHORE_H_
|
|
|
|
#define MP_SEMAPHORE_H_
|
|
|
|
|
2014-09-20 02:18:40 +00:00
|
|
|
#include <sys/types.h>
|
osdep: add POSIX semaphore emulation for OSX
OSX is POSIX conformant, but it's a sad joke: it provides the
<semaphore.h> prototype as the standard demands, but they're empty
wrappers, and all functions just return ENOSYS.
Emulate them similar to how osdep/io.h emulate filesystem functions on
Windows. By including the header, working sem_* functions become
available.
To make it async-signal safe, use a pipe for wakeup (write() is AS-safe,
but mutexes can't be). Actually I'm not sure anymore if we really need
AS-safety, but for now the emulation can do it.
On Linux, the system provides a far more efficient and robust
implementation. We definitely want to avoid using the emulation if
possible, so this code is active on OSX only. For convenience we always
build the source file though, even if the implementation is disabled and
no actual code is generated.
(Linux provides working semaphores, but is formally not POSIX
conformant. On OSX it's the opposite. Is POSIX a complete joke?)
2014-09-10 01:09:41 +00:00
|
|
|
#include <semaphore.h>
|
|
|
|
|
|
|
|
// OSX provides non-working empty stubs, so we emulate them.
|
|
|
|
// This should be AS-safe, but cancellation issues were ignored.
|
|
|
|
// sem_getvalue() is not provided.
|
|
|
|
// sem_post() won't always correctly return an error on overflow.
|
|
|
|
// Process-shared semantics are not provided.
|
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
|
|
|
|
#define MP_SEMAPHORE_EMULATION
|
|
|
|
|
2023-10-21 02:55:41 +00:00
|
|
|
#include "osdep/threads.h"
|
osdep: add POSIX semaphore emulation for OSX
OSX is POSIX conformant, but it's a sad joke: it provides the
<semaphore.h> prototype as the standard demands, but they're empty
wrappers, and all functions just return ENOSYS.
Emulate them similar to how osdep/io.h emulate filesystem functions on
Windows. By including the header, working sem_* functions become
available.
To make it async-signal safe, use a pipe for wakeup (write() is AS-safe,
but mutexes can't be). Actually I'm not sure anymore if we really need
AS-safety, but for now the emulation can do it.
On Linux, the system provides a far more efficient and robust
implementation. We definitely want to avoid using the emulation if
possible, so this code is active on OSX only. For convenience we always
build the source file though, even if the implementation is disabled and
no actual code is generated.
(Linux provides working semaphores, but is formally not POSIX
conformant. On OSX it's the opposite. Is POSIX a complete joke?)
2014-09-10 01:09:41 +00:00
|
|
|
|
|
|
|
#define MP_SEM_VALUE_MAX 4096
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int wakeup_pipe[2];
|
2023-10-21 02:55:41 +00:00
|
|
|
mp_mutex lock;
|
osdep: add POSIX semaphore emulation for OSX
OSX is POSIX conformant, but it's a sad joke: it provides the
<semaphore.h> prototype as the standard demands, but they're empty
wrappers, and all functions just return ENOSYS.
Emulate them similar to how osdep/io.h emulate filesystem functions on
Windows. By including the header, working sem_* functions become
available.
To make it async-signal safe, use a pipe for wakeup (write() is AS-safe,
but mutexes can't be). Actually I'm not sure anymore if we really need
AS-safety, but for now the emulation can do it.
On Linux, the system provides a far more efficient and robust
implementation. We definitely want to avoid using the emulation if
possible, so this code is active on OSX only. For convenience we always
build the source file though, even if the implementation is disabled and
no actual code is generated.
(Linux provides working semaphores, but is formally not POSIX
conformant. On OSX it's the opposite. Is POSIX a complete joke?)
2014-09-10 01:09:41 +00:00
|
|
|
// protected by lock
|
|
|
|
unsigned int count;
|
|
|
|
} mp_sem_t;
|
|
|
|
|
|
|
|
int mp_sem_init(mp_sem_t *sem, int pshared, unsigned int value);
|
|
|
|
int mp_sem_wait(mp_sem_t *sem);
|
|
|
|
int mp_sem_trywait(mp_sem_t *sem);
|
2023-10-22 05:33:19 +00:00
|
|
|
int mp_sem_timedwait(mp_sem_t *sem, int64_t until);
|
osdep: add POSIX semaphore emulation for OSX
OSX is POSIX conformant, but it's a sad joke: it provides the
<semaphore.h> prototype as the standard demands, but they're empty
wrappers, and all functions just return ENOSYS.
Emulate them similar to how osdep/io.h emulate filesystem functions on
Windows. By including the header, working sem_* functions become
available.
To make it async-signal safe, use a pipe for wakeup (write() is AS-safe,
but mutexes can't be). Actually I'm not sure anymore if we really need
AS-safety, but for now the emulation can do it.
On Linux, the system provides a far more efficient and robust
implementation. We definitely want to avoid using the emulation if
possible, so this code is active on OSX only. For convenience we always
build the source file though, even if the implementation is disabled and
no actual code is generated.
(Linux provides working semaphores, but is formally not POSIX
conformant. On OSX it's the opposite. Is POSIX a complete joke?)
2014-09-10 01:09:41 +00:00
|
|
|
int mp_sem_post(mp_sem_t *sem);
|
|
|
|
int mp_sem_destroy(mp_sem_t *sem);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|