mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
lib/std/io/stream: add LimitReader (#858)
* lib/std/io/stream: add LimitReader Signed-off-by: Pierre Curto <pierre.curto@gmail.com> * lib/std: more method conversions to use new receiver notation Signed-off-by: Pierre Curto <pierre.curto@gmail.com> --------- Signed-off-by: Pierre Curto <pierre.curto@gmail.com>
This commit is contained in:
@@ -6,7 +6,7 @@ def NativeConditionVariable = Pthread_cond_t;
|
||||
def NativeThread = Pthread_t;
|
||||
def NativeOnceFlag = Pthread_once_t;
|
||||
|
||||
fn void! NativeMutex.init(NativeMutex* mutex, MutexType type)
|
||||
fn void! NativeMutex.init(&mtx, MutexType type)
|
||||
{
|
||||
Pthread_mutexattr_t attr;
|
||||
if (pthread_mutexattr_init(&attr)) return ThreadFault.INIT_FAILED?;
|
||||
@@ -15,20 +15,20 @@ fn void! NativeMutex.init(NativeMutex* mutex, MutexType type)
|
||||
{
|
||||
if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE)) return ThreadFault.INIT_FAILED?;
|
||||
}
|
||||
if (pthread_mutex_init(mutex, &attr)) return ThreadFault.INIT_FAILED?;
|
||||
if (pthread_mutex_init(mtx, &attr)) return ThreadFault.INIT_FAILED?;
|
||||
}
|
||||
|
||||
fn void! NativeMutex.destroy(NativeMutex* mtx)
|
||||
fn void! NativeMutex.destroy(&mtx)
|
||||
{
|
||||
if (pthread_mutex_destroy(mtx)) return ThreadFault.DESTROY_FAILED?;
|
||||
}
|
||||
|
||||
fn void! NativeMutex.lock(NativeMutex* mtx)
|
||||
fn void! NativeMutex.lock(&mtx)
|
||||
{
|
||||
if (pthread_mutex_lock(mtx)) return ThreadFault.LOCK_FAILED?;
|
||||
}
|
||||
|
||||
fn void! NativeMutex.lock_timeoutout(NativeMutex* mtx, ulong ms)
|
||||
fn void! NativeMutex.lock_timeoutout(&mtx, ulong ms)
|
||||
{
|
||||
/* Try to acquire the lock and, if we fail, sleep for 5ms. */
|
||||
Errno result;
|
||||
@@ -51,42 +51,42 @@ fn void! NativeMutex.lock_timeoutout(NativeMutex* mtx, ulong ms)
|
||||
}
|
||||
}
|
||||
|
||||
fn bool NativeMutex.try_lock(NativeMutex* mtx)
|
||||
fn bool NativeMutex.try_lock(&mtx)
|
||||
{
|
||||
return !pthread_mutex_trylock(mtx);
|
||||
}
|
||||
|
||||
fn void! NativeMutex.unlock(NativeMutex* mtx)
|
||||
fn void! NativeMutex.unlock(&mtx)
|
||||
{
|
||||
if (pthread_mutex_unlock(mtx)) return ThreadFault.UNLOCK_FAILED?;
|
||||
}
|
||||
|
||||
fn void! NativeConditionVariable.init(NativeConditionVariable* cond)
|
||||
fn void! NativeConditionVariable.init(&cond)
|
||||
{
|
||||
if (pthread_cond_init(cond, null)) return ThreadFault.INIT_FAILED?;
|
||||
}
|
||||
|
||||
fn void! NativeConditionVariable.destroy(NativeConditionVariable* cond)
|
||||
fn void! NativeConditionVariable.destroy(&cond)
|
||||
{
|
||||
if (pthread_cond_destroy(cond)) return ThreadFault.DESTROY_FAILED?;
|
||||
}
|
||||
|
||||
fn void! NativeConditionVariable.signal(NativeConditionVariable* cond)
|
||||
fn void! NativeConditionVariable.signal(&cond)
|
||||
{
|
||||
if (pthread_cond_signal(cond)) return ThreadFault.SIGNAL_FAILED?;
|
||||
}
|
||||
|
||||
fn void! NativeConditionVariable.broadcast(NativeConditionVariable* cond)
|
||||
fn void! NativeConditionVariable.broadcast(&cond)
|
||||
{
|
||||
if (pthread_cond_broadcast(cond)) return ThreadFault.SIGNAL_FAILED?;
|
||||
}
|
||||
|
||||
fn void! NativeConditionVariable.wait(NativeConditionVariable* cond, NativeMutex* mtx)
|
||||
fn void! NativeConditionVariable.wait(&cond, NativeMutex* mtx)
|
||||
{
|
||||
if (pthread_cond_wait(cond, mtx)) return ThreadFault.WAIT_FAILED?;
|
||||
}
|
||||
|
||||
fn void! NativeConditionVariable.wait_timeout(NativeConditionVariable* cond, NativeMutex* mtx, ulong ms)
|
||||
fn void! NativeConditionVariable.wait_timeout(&cond, NativeMutex* mtx, ulong ms)
|
||||
{
|
||||
TimeSpec now;
|
||||
if (libc::timespec_get(&now, libc::TIME_UTC) != libc::TIME_UTC) return ThreadFault.WAIT_FAILED?;
|
||||
@@ -110,7 +110,7 @@ fn void* callback(void* arg) @private
|
||||
return (void*)(iptr)data.thread_fn(data.arg);
|
||||
}
|
||||
|
||||
fn void! NativeThread.create(NativeThread* thread, ThreadFn thread_fn, void* arg)
|
||||
fn void! NativeThread.create(&thread, ThreadFn thread_fn, void* arg)
|
||||
{
|
||||
PosixThreadData *thread_data = malloc(PosixThreadData);
|
||||
*thread_data = { .thread_fn = thread_fn, .arg = arg };
|
||||
@@ -122,7 +122,7 @@ fn void! NativeThread.create(NativeThread* thread, ThreadFn thread_fn, void* arg
|
||||
}
|
||||
}
|
||||
|
||||
fn void! NativeThread.detach(NativeThread thread)
|
||||
fn void! NativeThread.detach(thread)
|
||||
{
|
||||
if (!pthread_detach(thread)) return ThreadFault.DETACH_FAILED?;
|
||||
}
|
||||
@@ -137,19 +137,19 @@ fn NativeThread native_thread_current()
|
||||
return pthread_self();
|
||||
}
|
||||
|
||||
fn bool NativeThread.equals(NativeThread this, NativeThread other)
|
||||
fn bool NativeThread.equals(thread, NativeThread other)
|
||||
{
|
||||
return (bool)pthread_equal(this, other);
|
||||
return (bool)pthread_equal(thread, other);
|
||||
}
|
||||
|
||||
fn int! NativeThread.join(NativeThread thread)
|
||||
fn int! NativeThread.join(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)
|
||||
fn void NativeOnceFlag.call_once(&flag, OnceFn func)
|
||||
{
|
||||
pthread_once(flag, func);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
module std::thread::os @if(env::WIN32);
|
||||
import std::os::win32;
|
||||
|
||||
fn void! NativeMutex.init(NativeMutex* mtx, MutexType type)
|
||||
fn void! NativeMutex.init(&mtx, MutexType type)
|
||||
{
|
||||
mtx.already_locked = false;
|
||||
mtx.recursive = (bool)(type & thread::MUTEX_RECURSIVE);
|
||||
@@ -14,7 +14,7 @@ fn void! NativeMutex.init(NativeMutex* mtx, MutexType type)
|
||||
if (!(mtx.handle = win32::createMutex(null, false, null))) return ThreadFault.INIT_FAILED?;
|
||||
}
|
||||
|
||||
fn void! NativeMutex.destroy(NativeMutex* mtx)
|
||||
fn void! NativeMutex.destroy(&mtx)
|
||||
{
|
||||
if (!mtx.timed)
|
||||
{
|
||||
@@ -24,7 +24,7 @@ fn void! NativeMutex.destroy(NativeMutex* mtx)
|
||||
if (!win32::closeHandle(mtx.handle)) return ThreadFault.DESTROY_FAILED?;
|
||||
}
|
||||
|
||||
fn void! NativeMutex.lock(NativeMutex* mtx)
|
||||
fn void! NativeMutex.lock(&mtx)
|
||||
{
|
||||
if (!mtx.timed)
|
||||
{
|
||||
@@ -53,7 +53,7 @@ fn void! NativeMutex.lock(NativeMutex* mtx)
|
||||
/**
|
||||
* @require mtx.timed "Only available for timed locks"
|
||||
**/
|
||||
fn void! NativeMutex.lock_timeout(NativeMutex* mtx, uint ms)
|
||||
fn void! NativeMutex.lock_timeout(&mtx, uint ms)
|
||||
{
|
||||
switch (win32::waitForSingleObject(mtx.handle, ms))
|
||||
{
|
||||
@@ -72,7 +72,7 @@ fn void! NativeMutex.lock_timeout(NativeMutex* mtx, uint ms)
|
||||
}
|
||||
}
|
||||
|
||||
fn bool NativeMutex.try_lock(NativeMutex* mtx)
|
||||
fn bool NativeMutex.try_lock(&mtx)
|
||||
{
|
||||
bool success = mtx.timed
|
||||
? win32::waitForSingleObject(mtx.handle, 0) == win32::WAIT_OBJECT_0
|
||||
@@ -92,7 +92,7 @@ fn bool NativeMutex.try_lock(NativeMutex* mtx)
|
||||
return true;
|
||||
}
|
||||
|
||||
fn void! NativeMutex.unlock(NativeMutex* mtx)
|
||||
fn void! NativeMutex.unlock(&mtx)
|
||||
{
|
||||
mtx.already_locked = false;
|
||||
if (!mtx.timed)
|
||||
@@ -106,7 +106,7 @@ fn void! NativeMutex.unlock(NativeMutex* mtx)
|
||||
const int CONDITION_EVENT_ONE = 0;
|
||||
const int CONDITION_EVENT_ALL = 1;
|
||||
|
||||
fn void! NativeConditionVariable.init(NativeConditionVariable* cond)
|
||||
fn void! NativeConditionVariable.init(&cond)
|
||||
{
|
||||
cond.waiters_count = 0;
|
||||
win32::initializeCriticalSection(&cond.waiters_count_lock);
|
||||
@@ -125,14 +125,14 @@ fn void! NativeConditionVariable.init(NativeConditionVariable* cond)
|
||||
}
|
||||
}
|
||||
|
||||
fn void! NativeConditionVariable.destroy(NativeConditionVariable* cond) @maydiscard
|
||||
fn void! NativeConditionVariable.destroy(&cond) @maydiscard
|
||||
{
|
||||
if (cond.event_one) win32::closeHandle(cond.event_one);
|
||||
if (cond.event_all) win32::closeHandle(cond.event_all);
|
||||
win32::deleteCriticalSection(&cond.waiters_count_lock);
|
||||
}
|
||||
|
||||
fn void! NativeConditionVariable.signal(NativeConditionVariable* cond)
|
||||
fn void! NativeConditionVariable.signal(&cond)
|
||||
{
|
||||
win32::enterCriticalSection(&cond.waiters_count_lock);
|
||||
bool have_waiters = cond.waiters_count > 0;
|
||||
@@ -140,7 +140,7 @@ fn void! NativeConditionVariable.signal(NativeConditionVariable* cond)
|
||||
if (have_waiters && !win32::setEvent(cond.event_one)) return ThreadFault.SIGNAL_FAILED?;
|
||||
}
|
||||
|
||||
fn void! NativeConditionVariable.broadcast(NativeConditionVariable* cond)
|
||||
fn void! NativeConditionVariable.broadcast(&cond)
|
||||
{
|
||||
win32::enterCriticalSection(&cond.waiters_count_lock);
|
||||
bool have_waiters = cond.waiters_count > 0;
|
||||
@@ -187,22 +187,22 @@ fn void! timedwait(NativeConditionVariable* cond, NativeMutex* mtx, uint timeout
|
||||
mtx.lock()!;
|
||||
}
|
||||
|
||||
fn void! NativeConditionVariable.wait(NativeConditionVariable* cond, NativeMutex* mtx) @inline
|
||||
fn void! NativeConditionVariable.wait(&cond, NativeMutex* mtx) @inline
|
||||
{
|
||||
return timedwait(cond, mtx, win32::INFINITE) @inline;
|
||||
}
|
||||
|
||||
fn void! NativeConditionVariable.wait_timeout(NativeConditionVariable* cond, NativeMutex* mtx, uint time) @inline
|
||||
fn void! NativeConditionVariable.wait_timeout(&cond, NativeMutex* mtx, uint time) @inline
|
||||
{
|
||||
return timedwait(cond, mtx, time) @inline;
|
||||
}
|
||||
|
||||
fn void! NativeThread.create(NativeThread* thread, ThreadFn func, void* args)
|
||||
fn void! NativeThread.create(&thread, ThreadFn func, void* args)
|
||||
{
|
||||
if (!(*thread = win32::createThread(null, 0, func, args, 0, null))) return ThreadFault.INIT_FAILED?;
|
||||
}
|
||||
|
||||
fn void! NativeThread.detach(NativeThread thread) @inline
|
||||
fn void! NativeThread.detach(thread) @inline
|
||||
{
|
||||
if (!win32::closeHandle(thread)) return ThreadFault.DETACH_FAILED?;
|
||||
}
|
||||
@@ -218,7 +218,7 @@ fn void native_thread_yield()
|
||||
win32::sleep(0);
|
||||
}
|
||||
|
||||
fn void NativeOnceFlag.call_once(NativeOnceFlag* flag, OnceFn func)
|
||||
fn void NativeOnceFlag.call_once(&flag, OnceFn func)
|
||||
{
|
||||
while (@volatile_load(flag.status) < 3)
|
||||
{
|
||||
@@ -246,7 +246,7 @@ fn void NativeOnceFlag.call_once(NativeOnceFlag* flag, OnceFn func)
|
||||
}
|
||||
}
|
||||
|
||||
fn void! NativeThread.join(NativeThread thread, int *res)
|
||||
fn void! NativeThread.join(thread, int *res)
|
||||
{
|
||||
if (win32::waitForSingleObject(thread, win32::INFINITE) == win32::WAIT_FAILED) return ThreadFault.JOIN_FAILED?;
|
||||
if (!win32::getExitCodeThread(thread, (uint*)res)) return ThreadFault.JOIN_FAILED?;
|
||||
@@ -258,9 +258,9 @@ fn NativeThread native_thread_current()
|
||||
return win32::getCurrentThread();
|
||||
}
|
||||
|
||||
fn bool NativeThread.equals(NativeThread this, NativeThread other)
|
||||
fn bool NativeThread.equals(thread, NativeThread other)
|
||||
{
|
||||
return win32::getThreadId(this) == win32::getThreadId(other);
|
||||
return win32::getThreadId(thread) == win32::getThreadId(other);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -40,33 +40,33 @@ fault ThreadFault
|
||||
INTERRUPTED,
|
||||
}
|
||||
|
||||
macro void! Mutex.init(Mutex* mutex, MutexType type) => NativeMutex.init((NativeMutex*)mutex, type);
|
||||
macro void! Mutex.destroy(Mutex* mutex) => NativeMutex.destroy((NativeMutex*)mutex);
|
||||
macro void! Mutex.lock(Mutex* mutex) => NativeMutex.lock((NativeMutex*)mutex);
|
||||
macro void! Mutex.lock_timeout(Mutex* mutex, ulong ms) => NativeMutex.lock((NativeMutex*)mutex, ms);
|
||||
macro bool Mutex.try_lock(Mutex* mutex) => NativeMutex.try_lock((NativeMutex*)mutex);
|
||||
macro bool Mutex.unlock(Mutex* mutex) => NativeMutex.unlock((NativeMutex*)mutex);
|
||||
macro void! Mutex.init(&mutex, MutexType type) => NativeMutex.init((NativeMutex*)mutex, type);
|
||||
macro void! Mutex.destroy(&mutex) => NativeMutex.destroy((NativeMutex*)mutex);
|
||||
macro void! Mutex.lock(&mutex) => NativeMutex.lock((NativeMutex*)mutex);
|
||||
macro void! Mutex.lock_timeout(&mutex, ulong ms) => NativeMutex.lock((NativeMutex*)mutex, ms);
|
||||
macro bool Mutex.try_lock(&mutex) => NativeMutex.try_lock((NativeMutex*)mutex);
|
||||
macro bool Mutex.unlock(&mutex) => NativeMutex.unlock((NativeMutex*)mutex);
|
||||
|
||||
macro void! ConditionVariable.init(ConditionVariable* cond) => NativeConditionVariable.init((NativeConditionVariable*)cond);
|
||||
macro void! ConditionVariable.destroy(ConditionVariable* cond) => NativeConditionVariable.destroy((NativeConditionVariable*)cond);
|
||||
macro void! ConditionVariable.signal(ConditionVariable* cond) => NativeConditionVariable.signal((NativeConditionVariable*)cond);
|
||||
macro void! ConditionVariable.broadcast(ConditionVariable* cond) => NativeConditionVariable.broadcast((NativeConditionVariable*)cond);
|
||||
macro void! ConditionVariable.wait(ConditionVariable* cond, Mutex* mutex)
|
||||
macro void! ConditionVariable.init(&cond) => NativeConditionVariable.init((NativeConditionVariable*)cond);
|
||||
macro void! ConditionVariable.destroy(&cond) => NativeConditionVariable.destroy((NativeConditionVariable*)cond);
|
||||
macro void! ConditionVariable.signal(&cond) => NativeConditionVariable.signal((NativeConditionVariable*)cond);
|
||||
macro void! ConditionVariable.broadcast(&cond) => NativeConditionVariable.broadcast((NativeConditionVariable*)cond);
|
||||
macro void! ConditionVariable.wait(&cond, Mutex* mutex)
|
||||
{
|
||||
return NativeConditionVariable.wait((NativeConditionVariable*)cond, (NativeMutex*)mutex);
|
||||
}
|
||||
macro void! ConditionVariable.wait_timeout(ConditionVariable* cond, Mutex* mutex, ulong timeout)
|
||||
macro void! ConditionVariable.wait_timeout(&cond, Mutex* mutex, ulong timeout)
|
||||
{
|
||||
return NativeConditionVariable.wait_timeout((NativeConditionVariable*)cond, (NativeMutex*)mutex, timeout);
|
||||
}
|
||||
|
||||
|
||||
macro void! Thread.create(Thread* thread, ThreadFn thread_fn, void* arg) => NativeThread.create((NativeThread*)thread, thread_fn, arg);
|
||||
macro void! Thread.detach(Thread thread) => NativeThread.detach((NativeThread)thread);
|
||||
macro int! Thread.join(Thread thread) => NativeThread.join((NativeThread)thread);
|
||||
macro bool Thread.equals(Thread thread, Thread other) => NativeThread.equals((NativeThread)this, (NativeThread)other);
|
||||
macro void! Thread.create(&thread, ThreadFn thread_fn, void* arg) => NativeThread.create((NativeThread*)thread, thread_fn, arg);
|
||||
macro void! Thread.detach(thread) => NativeThread.detach((NativeThread)thread);
|
||||
macro int! Thread.join(thread) => NativeThread.join((NativeThread)thread);
|
||||
macro bool Thread.equals(thread, Thread other) => NativeThread.equals((NativeThread)this, (NativeThread)other);
|
||||
|
||||
macro void OnceFlag.call_once(OnceFlag* flag, OnceFn func) => NativeOnceFlag.call_once((NativeOnceFlag*)flag, func);
|
||||
macro void OnceFlag.call_once(&flag, OnceFn func) => NativeOnceFlag.call_once((NativeOnceFlag*)flag, func);
|
||||
|
||||
macro void yield() => os::native_thread_yield();
|
||||
macro Thread current() => os::native_thread_current();
|
||||
|
||||
Reference in New Issue
Block a user