Merge pull request #236 from alexey-lysiuk/macos_semaphore

Use GCD semaphore on macOS
This commit is contained in:
kcat 2018-10-15 14:16:49 -07:00 committed by GitHub
commit 56b8b97642
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 0 deletions

View File

@ -631,6 +631,39 @@ void alcnd_destroy(alcnd_t *cond)
}
#ifdef __APPLE__
int alsem_init(alsem_t *sem, unsigned int initial)
{
*sem = dispatch_semaphore_create(initial);
return *sem ? althrd_success : althrd_error;
}
void alsem_destroy(alsem_t *sem)
{
dispatch_release(*sem);
}
int alsem_post(alsem_t *sem)
{
dispatch_semaphore_signal(*sem);
return althrd_success;
}
int alsem_wait(alsem_t *sem)
{
dispatch_semaphore_wait(*sem, DISPATCH_TIME_FOREVER);
return althrd_success;
}
int alsem_trywait(alsem_t *sem)
{
long value = dispatch_semaphore_wait(*sem, DISPATCH_TIME_NOW);
return value == 0 ? althrd_success : althrd_busy;
}
#else /* !__APPLE__ */
int alsem_init(alsem_t *sem, unsigned int initial)
{
if(sem_init(sem, 0, initial) == 0)
@ -665,6 +698,8 @@ int alsem_trywait(alsem_t *sem)
return althrd_error;
}
#endif /* __APPLE__ */
int altss_create(altss_t *tss_id, altss_dtor_t callback)
{

View File

@ -130,13 +130,21 @@ inline int altss_set(altss_t tss_id, void *val)
#include <stdint.h>
#include <errno.h>
#include <pthread.h>
#ifdef __APPLE__
#include <dispatch/dispatch.h>
#else /* !__APPLE__ */
#include <semaphore.h>
#endif /* __APPLE__ */
typedef pthread_t althrd_t;
typedef pthread_mutex_t almtx_t;
typedef pthread_cond_t alcnd_t;
#ifdef __APPLE__
typedef dispatch_semaphore_t alsem_t;
#else /* !__APPLE__ */
typedef sem_t alsem_t;
#endif /* __APPLE__ */
typedef pthread_key_t altss_t;
typedef pthread_once_t alonce_flag;