mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
235 lines
6.9 KiB
C
235 lines
6.9 KiB
C
module std::thread::os @if(thread::THREAD_MODEL == ThreadModel.POSIX);
|
|
import libc;
|
|
|
|
const PTHREAD_MUTEX_NORMAL = 0;
|
|
const PTHREAD_MUTEX_ERRORCHECK = 1;
|
|
const PTHREAD_MUTEX_RECURSIVE = 2;
|
|
|
|
def NativeMutex = PthreadMutex;
|
|
def NativeConditionVariable = PthreadCond;
|
|
def NativeThread = Pthread;
|
|
def NativeOnceFlag = PthreadOnce;
|
|
|
|
def Pthread = distinct void*;
|
|
def PosixThreadFn = fn void*(void*);
|
|
|
|
extern fn int pthread_attr_destroy(PthreadAttribute*);
|
|
extern fn int pthread_attr_getdetachstate(PthreadAttribute*, int*);
|
|
extern fn int pthread_attr_init(PthreadAttribute*);
|
|
extern fn int pthread_mutex_init(PthreadMutex*, PthreadMutexAttribute*);
|
|
extern fn int pthread_mutex_destroy(PthreadMutex*);
|
|
extern fn int pthread_mutex_lock(PthreadMutex*);
|
|
extern fn int pthread_mutexattr_init(PthreadMutexAttribute*);
|
|
extern fn int pthread_mutexattr_destroy(PthreadMutexAttribute*);
|
|
extern fn int pthread_mutexattr_settype(PthreadMutexAttribute*, int);
|
|
extern fn int pthread_cond_init(PthreadCond*, PthreadCondAttribute*);
|
|
extern fn int pthread_cond_destroy(PthreadCond*);
|
|
extern fn int pthread_detach(Pthread);
|
|
extern fn int pthread_join(Pthread, void**);
|
|
extern fn int pthread_create(Pthread*, PthreadAttribute*, PosixThreadFn, void*);
|
|
extern fn Pthread pthread_self();
|
|
extern fn Errno pthread_mutex_trylock(PthreadMutex*);
|
|
extern fn Errno pthread_mutex_unlock(PthreadMutex*);
|
|
extern fn Errno pthread_cond_signal(PthreadCond*);
|
|
extern fn Errno pthread_cond_broadcast(PthreadCond*);
|
|
extern fn Errno pthread_cond_wait(PthreadCond*, PthreadMutex*);
|
|
extern fn Errno pthread_cond_timedwait(PthreadCond*, PthreadMutex*, TimeSpec*);
|
|
extern fn void pthread_exit(void*);
|
|
extern fn void pthread_once(PthreadOnce*, OnceFn);
|
|
extern fn int pthread_equal(Pthread this, Pthread other);
|
|
extern fn int sched_yield();
|
|
|
|
fn void! NativeMutex.init(NativeMutex* mutex, MutexType type)
|
|
{
|
|
PthreadMutexAttribute attr;
|
|
if (pthread_mutexattr_init(&attr)) return ThreadFault.INIT_FAILED?;
|
|
defer pthread_mutexattr_destroy(&attr);
|
|
if (type & thread::MUTEX_RECURSIVE)
|
|
{
|
|
if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE)) return ThreadFault.INIT_FAILED?;
|
|
}
|
|
if (pthread_mutex_init(mutex, &attr)) return ThreadFault.INIT_FAILED?;
|
|
}
|
|
|
|
fn void! NativeMutex.destroy(NativeMutex* mtx)
|
|
{
|
|
if (pthread_mutex_destroy(mtx)) return ThreadFault.DESTROY_FAILED?;
|
|
}
|
|
|
|
fn void! NativeMutex.lock(NativeMutex* mtx)
|
|
{
|
|
if (pthread_mutex_lock(mtx)) return ThreadFault.LOCK_FAILED?;
|
|
}
|
|
|
|
fn void! NativeMutex.lock_timeoutout(NativeMutex* mtx, ulong ms)
|
|
{
|
|
/* Try to acquire the lock and, if we fail, sleep for 5ms. */
|
|
Errno result;
|
|
while ((result = pthread_mutex_trylock(mtx)) == errno::EBUSY)
|
|
{
|
|
if (!ms) break;
|
|
ulong sleep = min(5, ms);
|
|
if (!libc::nanosleep(&& TimeSpec { .s = 0, .ns = (CLong)sleep * 1000_000 }, null)) return ThreadFault.LOCK_FAILED?;
|
|
ms -= sleep;
|
|
}
|
|
switch (result)
|
|
{
|
|
case errno::OK:
|
|
return;
|
|
case errno::EBUSY:
|
|
case errno::ETIMEDOUT:
|
|
return ThreadFault.LOCK_TIMEOUT?;
|
|
default:
|
|
return ThreadFault.LOCK_FAILED?;
|
|
}
|
|
}
|
|
|
|
fn bool NativeMutex.try_lock(NativeMutex* mtx)
|
|
{
|
|
return !pthread_mutex_trylock(mtx);
|
|
}
|
|
|
|
fn void! NativeMutex.unlock(NativeMutex* mtx)
|
|
{
|
|
if (pthread_mutex_unlock(mtx)) return ThreadFault.UNLOCK_FAILED?;
|
|
}
|
|
|
|
fn void! NativeConditionVariable.init(NativeConditionVariable* cond)
|
|
{
|
|
if (pthread_cond_init(cond, null)) return ThreadFault.INIT_FAILED?;
|
|
}
|
|
|
|
fn void! NativeConditionVariable.destroy(NativeConditionVariable* cond)
|
|
{
|
|
if (pthread_cond_destroy(cond)) return ThreadFault.DESTROY_FAILED?;
|
|
}
|
|
|
|
fn void! NativeConditionVariable.signal(NativeConditionVariable* cond)
|
|
{
|
|
if (pthread_cond_signal(cond)) return ThreadFault.SIGNAL_FAILED?;
|
|
}
|
|
|
|
fn void! NativeConditionVariable.broadcast(NativeConditionVariable* cond)
|
|
{
|
|
if (pthread_cond_broadcast(cond)) return ThreadFault.SIGNAL_FAILED?;
|
|
}
|
|
|
|
fn void! NativeConditionVariable.wait(NativeConditionVariable* cond, NativeMutex* mtx)
|
|
{
|
|
if (pthread_cond_wait(cond, mtx)) return ThreadFault.WAIT_FAILED?;
|
|
}
|
|
|
|
fn void! NativeConditionVariable.wait_timeout(NativeConditionVariable* cond, NativeMutex* mtx, ulong ms)
|
|
{
|
|
TimeSpec now;
|
|
if (libc::timespec_get(&now, libc::TIME_UTC) != libc::TIME_UTC) return ThreadFault.WAIT_FAILED?;
|
|
now.s += (Time_t)(ms / 1000);
|
|
now.ns += (CLong)((ms % 1000) * 1000_000);
|
|
switch (pthread_cond_timedwait(cond, mtx, &now))
|
|
{
|
|
case errno::ETIMEDOUT:
|
|
return ThreadFault.WAIT_TIMEOUT?;
|
|
case errno::OK:
|
|
return;
|
|
default:
|
|
return ThreadFault.WAIT_FAILED?;
|
|
}
|
|
}
|
|
|
|
|
|
fn void* callback(void* arg) @private
|
|
{
|
|
PosixThreadData *data = arg;
|
|
return (void*)(iptr)data.thread_fn(data.arg);
|
|
}
|
|
|
|
fn void! NativeThread.create(NativeThread* thread, ThreadFn thread_fn, void* arg)
|
|
{
|
|
PosixThreadData *thread_data = malloc(PosixThreadData);
|
|
*thread_data = { .thread_fn = thread_fn, .arg = arg };
|
|
if (pthread_create(thread, null, &callback, thread_data) != 0)
|
|
{
|
|
*thread = null;
|
|
free(thread_data);
|
|
return ThreadFault.INIT_FAILED?;
|
|
}
|
|
}
|
|
|
|
fn void! NativeThread.detach(NativeThread thread)
|
|
{
|
|
if (!pthread_detach(thread)) return ThreadFault.DETACH_FAILED?;
|
|
}
|
|
|
|
fn void native_thread_exit(int result)
|
|
{
|
|
pthread_exit((void*)(iptr)result);
|
|
}
|
|
|
|
fn NativeThread native_thread_current()
|
|
{
|
|
return pthread_self();
|
|
}
|
|
|
|
fn bool NativeThread.equals(NativeThread this, NativeThread other)
|
|
{
|
|
return (bool)pthread_equal(this, other);
|
|
}
|
|
|
|
fn int! NativeThread.join(NativeThread thread)
|
|
{
|
|
void *pres;
|
|
if (pthread_join(thread, &pres)) return ThreadFault.JOIN_FAILED?;
|
|
return (int)(iptr)pres;
|
|
}
|
|
|
|
fn void NativeOnceFlag.call_once(NativeOnceFlag* flag, OnceFn func)
|
|
{
|
|
pthread_once(flag, func);
|
|
}
|
|
|
|
fn void native_thread_yield()
|
|
{
|
|
sched_yield();
|
|
}
|
|
|
|
struct PosixThreadData @private
|
|
{
|
|
ThreadFn thread_fn;
|
|
void* arg;
|
|
}
|
|
|
|
fn void! native_sleep_nano(ulong nano)
|
|
{
|
|
TimeSpec to = { .s = 0, .ns = (CLong)nano };
|
|
if (libc::nanosleep(&to, null)) return ThreadFault.INTERRUPTED?;
|
|
}
|
|
|
|
fn void! native_sleep_ms(ulong ms)
|
|
{
|
|
TimeSpec to = { .s = (Time_t)(ms / 1000), .ns = (CLong)((ms % 1000) * 1000_000) };
|
|
if (libc::nanosleep(&to, null)) return ThreadFault.INTERRUPTED?;
|
|
}
|
|
|
|
fn void! native_sleep(double s)
|
|
{
|
|
Time_t si = (Time_t)s;
|
|
TimeSpec to = { .s = si, .ns = (CLong)((s - si) * 1000_000_000) };
|
|
if (libc::nanosleep(&to, null)) return ThreadFault.INTERRUPTED?;
|
|
}
|
|
|
|
module std::thread::os @if(thread::THREAD_MODEL == ThreadModel.POSIX && !env::LINUX);
|
|
def PthreadMutex = distinct ulong[8];
|
|
def PthreadMutexAttribute = distinct ulong[2];
|
|
def PthreadAttribute = distinct ulong[8];
|
|
def PthreadCond = distinct ulong[6];
|
|
def PthreadCondAttribute = distinct ulong[8];
|
|
def PthreadOnce = distinct ulong[2];
|
|
|
|
module std::thread::os @if(thread::THREAD_MODEL == ThreadModel.POSIX && env::LINUX);
|
|
def PthreadMutex = distinct ulong[5];
|
|
def PthreadAttribute = distinct ulong[7];
|
|
def PthreadMutexAttribute = distinct uint;
|
|
def PthreadCondAttribute = distinct uint;
|
|
def PthreadCond = distinct ulong[6];
|
|
def PthreadOnce = distinct uint;
|