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>
|
|
|
|
|
2014-04-16 01:39:11 -07:00
|
|
|
|
|
|
|
enum {
|
|
|
|
althrd_success = 0,
|
2014-04-16 08:21:45 -07:00
|
|
|
althrd_nomem,
|
|
|
|
althrd_timedout,
|
2014-04-16 01:39:11 -07:00
|
|
|
althrd_busy,
|
2014-04-16 08:21:45 -07:00
|
|
|
althrd_error
|
2014-04-16 01:39:11 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
almtx_plain = 0,
|
|
|
|
almtx_recursive = 1,
|
2014-04-16 08:00:54 -07:00
|
|
|
almtx_timed = 2
|
2014-04-16 01:39:11 -07:00
|
|
|
};
|
|
|
|
|
2014-04-16 05:19:34 -07:00
|
|
|
typedef int (*althrd_start_t)(void*);
|
2014-04-17 00:11:12 -07:00
|
|
|
typedef void (*altss_dtor_t)(void*);
|
2014-04-16 01:39:11 -07:00
|
|
|
|
|
|
|
|
2014-04-17 09:03:57 -07:00
|
|
|
#define AL_TIME_UTC 1
|
|
|
|
|
|
|
|
|
2014-04-16 01:39:11 -07:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <windows.h>
|
|
|
|
|
2014-04-16 05:19:34 -07:00
|
|
|
typedef HANDLE althrd_t;
|
2014-04-16 01:39:11 -07:00
|
|
|
typedef CRITICAL_SECTION almtx_t;
|
2014-04-17 00:11:12 -07:00
|
|
|
typedef DWORD altss_t;
|
2014-04-16 01:39:11 -07:00
|
|
|
|
2014-04-16 05:19:34 -07:00
|
|
|
#ifndef __MINGW32__
|
|
|
|
struct timespec {
|
|
|
|
time_t tv_sec;
|
|
|
|
long tv_nsec;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
int althrd_sleep(const struct timespec *ts, struct timespec *rem);
|
|
|
|
|
|
|
|
|
|
|
|
inline althrd_t althrd_current(void)
|
|
|
|
{
|
|
|
|
/* This is wrong. GetCurrentThread() returns a psuedo-handle of -1 which
|
|
|
|
* various functions will interpret as the calling thread. There is no
|
|
|
|
* apparent way to retrieve the same handle that was returned by
|
|
|
|
* CreateThread. */
|
|
|
|
return GetCurrentThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int althrd_equal(althrd_t thr0, althrd_t thr1)
|
|
|
|
{
|
|
|
|
return GetThreadId(thr0) == GetThreadId(thr1);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void althrd_exit(int res)
|
|
|
|
{
|
|
|
|
ExitThread(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int almtx_trylock(almtx_t *mtx)
|
|
|
|
{
|
|
|
|
if(!mtx) return althrd_error;
|
|
|
|
if(!TryEnterCriticalSection(mtx))
|
|
|
|
return althrd_busy;
|
|
|
|
return althrd_success;
|
|
|
|
}
|
|
|
|
|
2014-04-17 00:11:12 -07:00
|
|
|
|
|
|
|
inline void *altss_get(altss_t tss_id)
|
|
|
|
{
|
|
|
|
return TlsGetValue(tss_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int altss_set(altss_t tss_id, void *val)
|
|
|
|
{
|
|
|
|
TlsSetValue(tss_id, val);
|
|
|
|
return althrd_success;
|
|
|
|
}
|
|
|
|
|
2014-04-16 01:39:11 -07:00
|
|
|
#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>
|
|
|
|
|
|
|
|
|
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;
|
2014-04-17 00:11:12 -07:00
|
|
|
typedef pthread_key_t altss_t;
|
2014-04-16 01:39:11 -07:00
|
|
|
|
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_exit(int res)
|
|
|
|
{
|
|
|
|
pthread_exit((void*)(intptr_t)res);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void althrd_yield(void)
|
|
|
|
{
|
|
|
|
sched_yield();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int althrd_sleep(const struct timespec *ts, struct timespec *rem)
|
|
|
|
{
|
2014-04-16 07:18:28 -07:00
|
|
|
int ret = nanosleep(ts, rem);
|
|
|
|
if(ret != 0)
|
|
|
|
{
|
|
|
|
ret = ((errno==EINTR) ? -1 : -2);
|
|
|
|
errno = 0;
|
|
|
|
}
|
|
|
|
return ret;
|
2014-04-16 05:19:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-16 01:39:11 -07:00
|
|
|
inline int almtx_lock(almtx_t *mtx)
|
|
|
|
{
|
2014-04-16 08:21:45 -07:00
|
|
|
int ret = EINVAL;
|
|
|
|
if(mtx != NULL)
|
|
|
|
ret = pthread_mutex_lock(mtx);
|
|
|
|
switch(ret)
|
|
|
|
{
|
|
|
|
case 0: return althrd_success;
|
|
|
|
case EAGAIN: return althrd_busy;
|
|
|
|
}
|
|
|
|
return althrd_error;
|
2014-04-16 01:39:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
inline int almtx_unlock(almtx_t *mtx)
|
|
|
|
{
|
|
|
|
if(!mtx) return althrd_error;
|
|
|
|
pthread_mutex_unlock(mtx);
|
|
|
|
return althrd_success;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int almtx_trylock(almtx_t *mtx)
|
|
|
|
{
|
2014-04-16 08:21:45 -07:00
|
|
|
int ret = EINVAL;
|
|
|
|
if(mtx != NULL)
|
|
|
|
ret = pthread_mutex_trylock(mtx);
|
|
|
|
switch(ret)
|
|
|
|
{
|
|
|
|
case 0: return althrd_success;
|
|
|
|
case EAGAIN:
|
|
|
|
case EBUSY: return althrd_busy;
|
|
|
|
}
|
|
|
|
return althrd_error;
|
2014-04-16 01:39:11 -07:00
|
|
|
}
|
|
|
|
|
2014-04-17 00:11:12 -07:00
|
|
|
|
|
|
|
inline void *altss_get(altss_t tss_id)
|
|
|
|
{
|
|
|
|
return pthread_getspecific(tss_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int altss_set(altss_t tss_id, void *val)
|
|
|
|
{
|
|
|
|
if(pthread_setspecific(tss_id, val) != 0)
|
|
|
|
return althrd_error;
|
|
|
|
return althrd_success;
|
|
|
|
}
|
|
|
|
|
2014-04-16 01:39:11 -07:00
|
|
|
#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);
|
2014-04-16 05:19:34 -07:00
|
|
|
int almtx_timedlock(almtx_t *mtx, const struct timespec *ts);
|
2014-04-16 01:39:11 -07:00
|
|
|
|
2014-04-17 00:11:12 -07:00
|
|
|
int altss_create(altss_t *tss_id, altss_dtor_t callback);
|
|
|
|
void altss_delete(altss_t tss_id);
|
|
|
|
|
2014-04-17 09:03:57 -07:00
|
|
|
int altimespec_get(struct timespec *ts, int base);
|
|
|
|
|
2014-04-17 09:14:03 -07:00
|
|
|
void al_nssleep(time_t sec, long nsec);
|
2014-04-16 06:11:40 -07:00
|
|
|
|
2013-10-27 08:14:13 -07:00
|
|
|
#endif /* AL_THREADS_H */
|