2014-02-26 22:43:31 -08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2014 Hugh Bailey <obs.jim@gmail.com>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2015-02-03 21:11:18 -08:00
|
|
|
#if defined(__APPLE__) || defined(__MINGW32__)
|
2014-02-26 22:43:31 -08:00
|
|
|
#include <sys/time.h>
|
2015-02-03 21:11:18 -08:00
|
|
|
#endif
|
|
|
|
#ifdef __APPLE__
|
2014-03-10 19:04:00 -07:00
|
|
|
#include <mach/semaphore.h>
|
2014-03-10 19:24:09 -07:00
|
|
|
#include <mach/task.h>
|
|
|
|
#include <mach/mach_init.h>
|
2014-03-10 19:04:00 -07:00
|
|
|
#else
|
2015-01-02 05:35:04 -08:00
|
|
|
#define _GNU_SOURCE
|
2014-03-10 19:04:00 -07:00
|
|
|
#include <semaphore.h>
|
2014-02-26 22:43:31 -08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "bmem.h"
|
|
|
|
#include "threading.h"
|
|
|
|
|
2014-03-10 19:08:42 -07:00
|
|
|
struct os_event_data {
|
2014-02-26 22:43:31 -08:00
|
|
|
pthread_mutex_t mutex;
|
|
|
|
pthread_cond_t cond;
|
|
|
|
volatile bool signalled;
|
|
|
|
bool manual;
|
|
|
|
};
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
int os_event_init(os_event_t **event, enum os_event_type type)
|
2014-02-26 22:43:31 -08:00
|
|
|
{
|
|
|
|
int code = 0;
|
|
|
|
|
2014-03-10 19:08:42 -07:00
|
|
|
struct os_event_data *data = bzalloc(sizeof(struct os_event_data));
|
2014-02-26 22:43:31 -08:00
|
|
|
|
|
|
|
if ((code = pthread_mutex_init(&data->mutex, NULL)) < 0) {
|
|
|
|
bfree(data);
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((code = pthread_cond_init(&data->cond, NULL)) < 0) {
|
|
|
|
pthread_mutex_destroy(&data->mutex);
|
|
|
|
bfree(data);
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
2014-03-10 19:04:00 -07:00
|
|
|
data->manual = (type == OS_EVENT_TYPE_MANUAL);
|
2014-02-26 22:43:31 -08:00
|
|
|
data->signalled = false;
|
|
|
|
*event = data;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
void os_event_destroy(os_event_t *event)
|
2014-02-26 22:43:31 -08:00
|
|
|
{
|
|
|
|
if (event) {
|
|
|
|
pthread_mutex_destroy(&event->mutex);
|
|
|
|
pthread_cond_destroy(&event->cond);
|
|
|
|
bfree(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
int os_event_wait(os_event_t *event)
|
2014-02-26 22:43:31 -08:00
|
|
|
{
|
|
|
|
int code = 0;
|
|
|
|
pthread_mutex_lock(&event->mutex);
|
|
|
|
if (!event->signalled)
|
|
|
|
code = pthread_cond_wait(&event->cond, &event->mutex);
|
|
|
|
|
|
|
|
if (code == 0) {
|
|
|
|
if (!event->manual)
|
|
|
|
event->signalled = false;
|
|
|
|
pthread_mutex_unlock(&event->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void add_ms_to_ts(struct timespec *ts,
|
|
|
|
unsigned long milliseconds)
|
|
|
|
{
|
|
|
|
ts->tv_sec += milliseconds/1000;
|
|
|
|
ts->tv_nsec += (milliseconds%1000)*1000000;
|
|
|
|
if (ts->tv_nsec > 1000000000) {
|
|
|
|
ts->tv_sec += 1;
|
|
|
|
ts->tv_nsec -= 1000000000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
int os_event_timedwait(os_event_t *event, unsigned long milliseconds)
|
2014-02-26 22:43:31 -08:00
|
|
|
{
|
|
|
|
int code = 0;
|
|
|
|
pthread_mutex_lock(&event->mutex);
|
|
|
|
if (!event->signalled) {
|
|
|
|
struct timespec ts;
|
2015-02-03 21:11:18 -08:00
|
|
|
#if defined(__APPLE__) || defined(__MINGW32__)
|
2014-02-26 22:43:31 -08:00
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
ts.tv_sec = tv.tv_sec;
|
|
|
|
ts.tv_nsec = tv.tv_usec * 1000;
|
|
|
|
#else
|
|
|
|
clock_gettime(CLOCK_REALTIME, &ts);
|
|
|
|
#endif
|
|
|
|
add_ms_to_ts(&ts, milliseconds);
|
|
|
|
code = pthread_cond_timedwait(&event->cond, &event->mutex, &ts);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (code == 0) {
|
|
|
|
if (!event->manual)
|
|
|
|
event->signalled = false;
|
|
|
|
}
|
|
|
|
|
2014-02-27 18:01:47 -08:00
|
|
|
pthread_mutex_unlock(&event->mutex);
|
|
|
|
|
2014-02-26 22:43:31 -08:00
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
int os_event_try(os_event_t *event)
|
2014-02-26 22:43:31 -08:00
|
|
|
{
|
2014-02-27 18:01:47 -08:00
|
|
|
int ret = EAGAIN;
|
|
|
|
|
2014-02-26 22:43:31 -08:00
|
|
|
pthread_mutex_lock(&event->mutex);
|
|
|
|
if (event->signalled) {
|
|
|
|
if (!event->manual)
|
|
|
|
event->signalled = false;
|
2014-02-27 18:01:47 -08:00
|
|
|
ret = 0;
|
2014-02-26 22:43:31 -08:00
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&event->mutex);
|
|
|
|
|
2014-02-27 18:01:47 -08:00
|
|
|
return ret;
|
2014-02-26 22:43:31 -08:00
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
int os_event_signal(os_event_t *event)
|
2014-02-26 22:43:31 -08:00
|
|
|
{
|
|
|
|
int code = 0;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&event->mutex);
|
|
|
|
code = pthread_cond_signal(&event->cond);
|
|
|
|
event->signalled = true;
|
|
|
|
pthread_mutex_unlock(&event->mutex);
|
|
|
|
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
void os_event_reset(os_event_t *event)
|
2014-02-26 22:43:31 -08:00
|
|
|
{
|
|
|
|
pthread_mutex_lock(&event->mutex);
|
|
|
|
event->signalled = false;
|
|
|
|
pthread_mutex_unlock(&event->mutex);
|
|
|
|
}
|
2014-03-10 19:04:00 -07:00
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
|
|
|
|
struct os_sem_data {
|
|
|
|
semaphore_t sem;
|
|
|
|
task_t task;
|
|
|
|
};
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
int os_sem_init(os_sem_t **sem, int value)
|
2014-03-10 19:04:00 -07:00
|
|
|
{
|
|
|
|
semaphore_t new_sem;
|
|
|
|
task_t task = mach_task_self();
|
|
|
|
|
|
|
|
if (semaphore_create(task, &new_sem, 0, value) != KERN_SUCCESS)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
*sem = bzalloc(sizeof(struct os_sem_data));
|
2014-04-14 13:55:14 -07:00
|
|
|
if (!*sem)
|
|
|
|
return -2;
|
|
|
|
|
2014-03-10 19:04:00 -07:00
|
|
|
(*sem)->sem = new_sem;
|
|
|
|
(*sem)->task = task;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
void os_sem_destroy(os_sem_t *sem)
|
2014-03-10 19:04:00 -07:00
|
|
|
{
|
|
|
|
if (sem) {
|
|
|
|
semaphore_destroy(sem->task, sem->sem);
|
|
|
|
bfree(sem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
int os_sem_post(os_sem_t *sem)
|
2014-03-10 19:04:00 -07:00
|
|
|
{
|
|
|
|
if (!sem) return -1;
|
|
|
|
return (semaphore_signal(sem->sem) == KERN_SUCCESS) ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
int os_sem_wait(os_sem_t *sem)
|
2014-03-10 19:04:00 -07:00
|
|
|
{
|
|
|
|
if (!sem) return -1;
|
|
|
|
return (semaphore_wait(sem->sem) == KERN_SUCCESS) ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
struct os_sem_data {
|
|
|
|
sem_t sem;
|
|
|
|
};
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
int os_sem_init(os_sem_t **sem, int value)
|
2014-03-10 19:04:00 -07:00
|
|
|
{
|
|
|
|
sem_t new_sem;
|
|
|
|
int ret = sem_init(&new_sem, 0, value);
|
|
|
|
if (ret != 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
*sem = bzalloc(sizeof(struct os_sem_data));
|
|
|
|
(*sem)->sem = new_sem;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
void os_sem_destroy(os_sem_t *sem)
|
2014-03-10 19:04:00 -07:00
|
|
|
{
|
|
|
|
if (sem) {
|
2014-03-10 19:27:59 -07:00
|
|
|
sem_destroy(&sem->sem);
|
2014-03-10 19:04:00 -07:00
|
|
|
bfree(sem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
int os_sem_post(os_sem_t *sem)
|
2014-03-10 19:04:00 -07:00
|
|
|
{
|
|
|
|
if (!sem) return -1;
|
|
|
|
return sem_post(&sem->sem);
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
int os_sem_wait(os_sem_t *sem)
|
2014-03-10 19:04:00 -07:00
|
|
|
{
|
|
|
|
if (!sem) return -1;
|
|
|
|
return sem_wait(&sem->sem);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2014-03-16 18:26:46 -07:00
|
|
|
|
|
|
|
long os_atomic_inc_long(volatile long *val)
|
|
|
|
{
|
2014-03-17 14:16:05 -07:00
|
|
|
return __sync_add_and_fetch(val, 1);
|
2014-03-16 18:26:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
long os_atomic_dec_long(volatile long *val)
|
|
|
|
{
|
2014-03-17 14:16:05 -07:00
|
|
|
return __sync_sub_and_fetch(val, 1);
|
2014-03-16 18:26:46 -07:00
|
|
|
}
|
2015-01-02 05:35:04 -08:00
|
|
|
|
|
|
|
void os_set_thread_name(const char *name)
|
|
|
|
{
|
2015-02-03 21:11:18 -08:00
|
|
|
#if defined(__APPLE__)
|
2015-01-02 05:35:04 -08:00
|
|
|
pthread_setname_np(name);
|
2015-02-03 21:11:18 -08:00
|
|
|
#elif !defined(__MINGW32__)
|
2015-01-02 05:35:04 -08:00
|
|
|
pthread_setname_np(pthread_self(), name);
|
|
|
|
#endif
|
|
|
|
}
|