2013-12-25 21:40:33 -08:00
|
|
|
/*
|
2014-02-26 22:43:31 -08:00
|
|
|
* Copyright (c) 2013-2014 Hugh Bailey <obs.jim@gmail.com>
|
2013-12-25 21:40:33 -08:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2013-09-30 19:37:13 -07:00
|
|
|
|
2013-10-14 04:21:15 -07:00
|
|
|
#pragma once
|
2013-09-30 19:37:13 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Allows posix thread usage on windows as well as other operating systems.
|
|
|
|
* Use this header if you want to make your code more platform independent.
|
|
|
|
*
|
|
|
|
* Also provides a custom platform-independent "event" handler via
|
|
|
|
* pthread conditional waits.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "c99defs.h"
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
2013-10-28 06:29:12 -07:00
|
|
|
#include "../../deps/w32-pthreads/pthread.h"
|
2013-09-30 19:37:13 -07:00
|
|
|
#else
|
|
|
|
#include <errno.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2015-12-30 04:48:37 -08:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include "threading-windows.h"
|
|
|
|
#else
|
|
|
|
#include "threading-posix.h"
|
|
|
|
#endif
|
|
|
|
|
2013-10-18 20:25:13 -07:00
|
|
|
/* this may seem strange, but you can't use it unless it's an initializer */
|
|
|
|
static inline void pthread_mutex_init_value(pthread_mutex_t *mutex)
|
|
|
|
{
|
2014-04-14 14:21:32 -07:00
|
|
|
pthread_mutex_t init_val = PTHREAD_MUTEX_INITIALIZER;
|
2014-04-14 13:55:14 -07:00
|
|
|
if (!mutex)
|
|
|
|
return;
|
|
|
|
|
2013-10-18 20:25:13 -07:00
|
|
|
*mutex = init_val;
|
|
|
|
}
|
|
|
|
|
2021-08-23 21:33:51 -07:00
|
|
|
static inline int pthread_mutex_init_recursive(pthread_mutex_t *mutex)
|
|
|
|
{
|
|
|
|
pthread_mutexattr_t attr;
|
|
|
|
int ret = pthread_mutexattr_init(&attr);
|
|
|
|
if (ret == 0) {
|
|
|
|
ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
|
|
|
|
if (ret == 0) {
|
|
|
|
ret = pthread_mutex_init(mutex, &attr);
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutexattr_destroy(&attr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-03-10 19:04:00 -07:00
|
|
|
enum os_event_type {
|
|
|
|
OS_EVENT_TYPE_AUTO,
|
2019-06-22 22:13:45 -07:00
|
|
|
OS_EVENT_TYPE_MANUAL,
|
2014-01-09 21:04:25 -08:00
|
|
|
};
|
|
|
|
|
2014-03-10 19:04:00 -07:00
|
|
|
struct os_event_data;
|
|
|
|
struct os_sem_data;
|
2014-09-25 17:44:05 -07:00
|
|
|
typedef struct os_event_data os_event_t;
|
2019-06-22 22:13:45 -07:00
|
|
|
typedef struct os_sem_data os_sem_t;
|
2013-09-30 19:37:13 -07:00
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
EXPORT int os_event_init(os_event_t **event, enum os_event_type type);
|
2014-09-25 17:44:05 -07:00
|
|
|
EXPORT void os_event_destroy(os_event_t *event);
|
2019-06-22 22:13:45 -07:00
|
|
|
EXPORT int os_event_wait(os_event_t *event);
|
|
|
|
EXPORT int os_event_timedwait(os_event_t *event, unsigned long milliseconds);
|
|
|
|
EXPORT int os_event_try(os_event_t *event);
|
|
|
|
EXPORT int os_event_signal(os_event_t *event);
|
2014-09-25 17:44:05 -07:00
|
|
|
EXPORT void os_event_reset(os_event_t *event);
|
2014-03-10 19:04:00 -07:00
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
EXPORT int os_sem_init(os_sem_t **sem, int value);
|
2014-09-25 17:44:05 -07:00
|
|
|
EXPORT void os_sem_destroy(os_sem_t *sem);
|
2019-06-22 22:13:45 -07:00
|
|
|
EXPORT int os_sem_post(os_sem_t *sem);
|
|
|
|
EXPORT int os_sem_wait(os_sem_t *sem);
|
2013-09-30 19:37:13 -07:00
|
|
|
|
2015-01-02 05:35:04 -08:00
|
|
|
EXPORT void os_set_thread_name(const char *name);
|
|
|
|
|
2017-12-25 12:01:10 -08:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#define THREAD_LOCAL __declspec(thread)
|
|
|
|
#else
|
|
|
|
#define THREAD_LOCAL __thread
|
|
|
|
#endif
|
|
|
|
|
2013-09-30 19:37:13 -07:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|