2013-10-27 08:14:13 -07:00
|
|
|
#ifndef AL_THREADS_H
|
|
|
|
#define AL_THREADS_H
|
|
|
|
|
2014-04-16 05:19:34 -07:00
|
|
|
#include <time.h>
|
|
|
|
|
2018-01-12 03:55:33 -08:00
|
|
|
#if defined(__GNUC__) && defined(__i386__)
|
|
|
|
/* force_align_arg_pointer is required for proper function arguments aligning
|
|
|
|
* when SSE code is used. Some systems (Windows, QNX) do not guarantee our
|
|
|
|
* thread functions will be properly aligned on the stack, even though GCC may
|
|
|
|
* generate code with the assumption that it is. */
|
|
|
|
#define FORCE_ALIGN __attribute__((force_align_arg_pointer))
|
|
|
|
#else
|
|
|
|
#define FORCE_ALIGN
|
|
|
|
#endif
|
|
|
|
|
2014-05-29 04:22:30 -07:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
2014-04-16 01:39:11 -07:00
|
|
|
|
|
|
|
enum {
|
|
|
|
althrd_success = 0,
|
2014-04-18 06:39:46 -07:00
|
|
|
althrd_error,
|
2014-04-16 08:21:45 -07:00
|
|
|
althrd_nomem,
|
|
|
|
althrd_timedout,
|
2014-04-18 06:39:46 -07:00
|
|
|
althrd_busy
|
2014-04-16 01:39:11 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
almtx_plain = 0,
|
|
|
|
almtx_recursive = 1,
|
|
|
|
};
|
|
|
|
|
2014-04-16 05:19:34 -07:00
|
|
|
typedef int (*althrd_start_t)(void*);
|
2014-04-16 01:39:11 -07:00
|
|
|
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <windows.h>
|
|
|
|
|
2014-04-17 21:39:51 -07:00
|
|
|
typedef DWORD althrd_t;
|
|
|
|
typedef CRITICAL_SECTION almtx_t;
|
2018-02-01 17:37:31 -08:00
|
|
|
typedef HANDLE alsem_t;
|
2014-04-16 05:19:34 -07:00
|
|
|
|
|
|
|
|
2018-03-10 12:10:58 -08:00
|
|
|
void althrd_deinit(void);
|
2014-04-16 05:19:34 -07:00
|
|
|
|
|
|
|
inline althrd_t althrd_current(void)
|
|
|
|
{
|
2014-04-17 21:01:54 -07:00
|
|
|
return GetCurrentThreadId();
|
2014-04-16 05:19:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
inline int althrd_equal(althrd_t thr0, althrd_t thr1)
|
|
|
|
{
|
2014-04-17 21:01:54 -07:00
|
|
|
return thr0 == thr1;
|
2014-04-16 05:19:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void althrd_yield(void)
|
|
|
|
{
|
|
|
|
SwitchToThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-16 01:39:11 -07:00
|
|
|
inline int almtx_lock(almtx_t *mtx)
|
|
|
|
{
|
|
|
|
if(!mtx) return althrd_error;
|
|
|
|
EnterCriticalSection(mtx);
|
|
|
|
return althrd_success;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int almtx_unlock(almtx_t *mtx)
|
|
|
|
{
|
|
|
|
if(!mtx) return althrd_error;
|
|
|
|
LeaveCriticalSection(mtx);
|
|
|
|
return althrd_success;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2014-04-16 06:11:40 -07:00
|
|
|
#include <stdint.h>
|
2014-04-16 07:18:28 -07:00
|
|
|
#include <errno.h>
|
2014-04-16 01:39:11 -07:00
|
|
|
#include <pthread.h>
|
2018-10-15 16:40:57 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#include <dispatch/dispatch.h>
|
|
|
|
#else /* !__APPLE__ */
|
2018-02-01 17:37:31 -08:00
|
|
|
#include <semaphore.h>
|
2018-10-15 16:40:57 +00:00
|
|
|
#endif /* __APPLE__ */
|
2014-04-16 01:39:11 -07:00
|
|
|
|
2014-04-16 05:19:34 -07:00
|
|
|
typedef pthread_t althrd_t;
|
2014-04-16 01:39:11 -07:00
|
|
|
typedef pthread_mutex_t almtx_t;
|
2018-10-15 16:40:57 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
typedef dispatch_semaphore_t alsem_t;
|
|
|
|
#else /* !__APPLE__ */
|
2018-02-01 17:37:31 -08:00
|
|
|
typedef sem_t alsem_t;
|
2018-10-15 16:40:57 +00:00
|
|
|
#endif /* __APPLE__ */
|
2014-04-17 21:39:51 -07:00
|
|
|
|
2014-04-16 01:39:11 -07:00
|
|
|
|
2018-11-17 02:41:21 -08:00
|
|
|
void althrd_deinit(void);
|
2014-04-16 05:19:34 -07:00
|
|
|
|
|
|
|
inline althrd_t althrd_current(void)
|
|
|
|
{
|
|
|
|
return pthread_self();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int althrd_equal(althrd_t thr0, althrd_t thr1)
|
|
|
|
{
|
|
|
|
return pthread_equal(thr0, thr1);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void althrd_yield(void)
|
|
|
|
{
|
|
|
|
sched_yield();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-16 01:39:11 -07:00
|
|
|
inline int almtx_lock(almtx_t *mtx)
|
|
|
|
{
|
2014-04-18 06:46:21 -07:00
|
|
|
if(pthread_mutex_lock(mtx) != 0)
|
|
|
|
return althrd_error;
|
|
|
|
return althrd_success;
|
2014-04-16 01:39:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
inline int almtx_unlock(almtx_t *mtx)
|
|
|
|
{
|
2014-04-18 06:39:46 -07:00
|
|
|
if(pthread_mutex_unlock(mtx) != 0)
|
|
|
|
return althrd_error;
|
2014-04-16 01:39:11 -07:00
|
|
|
return althrd_success;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2014-04-16 05:19:34 -07:00
|
|
|
|
|
|
|
int althrd_create(althrd_t *thr, althrd_start_t func, void *arg);
|
|
|
|
int althrd_detach(althrd_t thr);
|
|
|
|
int althrd_join(althrd_t thr, int *res);
|
2014-04-17 20:41:32 -07:00
|
|
|
void althrd_setname(althrd_t thr, const char *name);
|
2014-04-16 05:19:34 -07:00
|
|
|
|
2014-04-16 01:39:11 -07:00
|
|
|
int almtx_init(almtx_t *mtx, int type);
|
|
|
|
void almtx_destroy(almtx_t *mtx);
|
|
|
|
|
2018-02-01 17:37:31 -08:00
|
|
|
int alsem_init(alsem_t *sem, unsigned int initial);
|
|
|
|
void alsem_destroy(alsem_t *sem);
|
|
|
|
int alsem_post(alsem_t *sem);
|
|
|
|
int alsem_wait(alsem_t *sem);
|
2018-02-10 21:42:45 -08:00
|
|
|
int alsem_trywait(alsem_t *sem);
|
2018-02-01 17:37:31 -08:00
|
|
|
|
2014-05-29 04:22:30 -07:00
|
|
|
#ifdef __cplusplus
|
2018-10-31 10:35:12 -07:00
|
|
|
} // extern "C"
|
|
|
|
|
|
|
|
#include <mutex>
|
|
|
|
|
|
|
|
/* Add specializations for std::lock_guard and std::unique_lock which take an
|
|
|
|
* almtx_t and call the appropriate almtx_* functions.
|
|
|
|
*/
|
|
|
|
namespace std {
|
|
|
|
|
|
|
|
template<>
|
|
|
|
class lock_guard<almtx_t> {
|
|
|
|
almtx_t &mMtx;
|
|
|
|
|
|
|
|
public:
|
|
|
|
using mutex_type = almtx_t;
|
|
|
|
|
|
|
|
explicit lock_guard(almtx_t &mtx) : mMtx(mtx) { almtx_lock(&mMtx); }
|
|
|
|
lock_guard(almtx_t &mtx, std::adopt_lock_t) : mMtx(mtx) { }
|
|
|
|
~lock_guard() { almtx_unlock(&mMtx); }
|
|
|
|
|
|
|
|
lock_guard(const lock_guard&) = delete;
|
|
|
|
lock_guard& operator=(const lock_guard&) = delete;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
class unique_lock<almtx_t> {
|
|
|
|
almtx_t *mMtx{nullptr};
|
|
|
|
bool mLocked{false};
|
|
|
|
|
|
|
|
public:
|
|
|
|
using mutex_type = almtx_t;
|
|
|
|
|
|
|
|
explicit unique_lock(almtx_t &mtx) : mMtx(&mtx) { almtx_lock(mMtx); mLocked = true; }
|
|
|
|
unique_lock(unique_lock&& rhs) : mMtx(rhs.mMtx), mLocked(rhs.mLocked)
|
|
|
|
{ rhs.mMtx = nullptr; rhs.mLocked = false; }
|
|
|
|
~unique_lock() { if(mLocked) almtx_unlock(mMtx); }
|
|
|
|
|
|
|
|
unique_lock& operator=(const unique_lock&) = delete;
|
|
|
|
unique_lock& operator=(unique_lock&& rhs)
|
|
|
|
{
|
|
|
|
if(mLocked)
|
|
|
|
almtx_unlock(mMtx);
|
|
|
|
mMtx = rhs.mMtx; rhs.mMtx = nullptr;
|
|
|
|
mLocked = rhs.mLocked; rhs.mLocked = false;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void lock()
|
|
|
|
{
|
|
|
|
almtx_lock(mMtx);
|
|
|
|
mLocked = true;
|
|
|
|
}
|
|
|
|
void unlock()
|
|
|
|
{
|
|
|
|
mLocked = false;
|
|
|
|
almtx_unlock(mMtx);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace std
|
2014-05-29 04:22:30 -07:00
|
|
|
#endif
|
|
|
|
|
2013-10-27 08:14:13 -07:00
|
|
|
#endif /* AL_THREADS_H */
|