thread/core

sourcethread/core.sx

ThreadError

pub enum ThreadError = { OutOfMemory, CreateFailed, JoinFailed, DetachFailed, AlreadyJoined }

Variants

  • OutOfMemory
  • CreateFailed
  • JoinFailed
  • DetachFailed
  • AlreadyJoined

alloc_in

private
fn alloc_in[A: allocators.GeneralAllocator](ac: A, size: usize) option[ref void]

free_in

private
fn free_in[A: allocators.GeneralAllocator](ac: A, ptr: ref void) void

Thread

pub type Thread = { handle: ref void, joined: bool }

Opaque thread handle — wraps pthread_t on POSIX, HANDLE on Windows.

Fields

NameType
handleref void
joinedbool

CreateThread

when windowsprivate
extern (C, header) fn CreateThread(lpThreadAttributes: ref void, dwStackSize: usize, lpStartAddress: usize, lpParameter: ref void, dwCreationFlags: u32, lpThreadId: ref u32) ref void = trust { ... } = "CreateThread"

WaitForSingleObject

when windowsprivate
extern (C, header) fn WaitForSingleObject(hHandle: ref void, dwMilliseconds: u32) u32 = trust { ... } = "WaitForSingleObject"

CloseHandle

when windowsprivate
extern (C, header) fn CloseHandle(hObject: ref void) i32 = trust { ... } = "CloseHandle"

GetCurrentThreadId

when windowsprivate
extern (C, header) fn GetCurrentThreadId() u32 = trust { ... } = "GetCurrentThreadId"

SwitchToThread

when windowsprivate
extern (C, header) fn SwitchToThread() i32 = trust { ... } = "SwitchToThread"

new

pub fn (Thread) new[A: allocators.GeneralAllocator](ac: A, entry: fn(ref void) ref void, arg: ref void) result[ref void, ThreadError]

Create a new thread running entry with the given arg.

join

pub fn (Thread) join[A: allocators.GeneralAllocator](ac: A, thread: Thread) result[ref void, ThreadError]

Join a thread, blocking until it completes. Returns the thread's return value, or an error if the thread cannot be joined.

A thread can only be joined once. Subsequent calls will return AlreadyJoined.

detach

pub fn (Thread) detach[A: allocators.GeneralAllocator](ac: A, thread: Thread) result[void, ThreadError]

Detach a thread, allowing it to run independently. Detached threads automatically release resources when they complete. Returns an error if detachment fails.

free

pub fn (Thread) free[A: allocators.GeneralAllocator](ac: A, thread: mut Thread) void

Free a thread handle after it has been joined or detached.

current_thread_id

pub fn current_thread_id() usize

Get the current thread's ID. Returns a platform-specific thread identifier.

yield_thread

pub fn yield_thread() void = trust { ... }

Yield the current thread's remaining time slice to allow other threads to run.

Mutex

pub type Mutex = { ptr: ref void }

A simple mutex using platform-specific primitives.

On POSIX, this uses pthread_mutex_t (40 bytes on most systems). On Windows, it uses CRITICAL_SECTION (48 bytes on most systems).

Fields

NameType
ptrref void

InitializeCriticalSection

when windowsprivate
extern (C, header) fn InitializeCriticalSection(lpCriticalSection: ref void) void = trust { ... } = "InitializeCriticalSection"

DeleteCriticalSection

when windowsprivate
extern (C, header) fn DeleteCriticalSection(lpCriticalSection: ref void) void = trust { ... } = "DeleteCriticalSection"

EnterCriticalSection

when windowsprivate
extern (C, header) fn EnterCriticalSection(lpCriticalSection: ref void) void = trust { ... } = "EnterCriticalSection"

LeaveCriticalSection

when windowsprivate
extern (C, header) fn LeaveCriticalSection(lpCriticalSection: ref void) void = trust { ... } = "LeaveCriticalSection"

new

pub fn (Mutex) new[A: allocators.GeneralAllocator](ac: A) option[ref void]

Create and initialize a new mutex. Returns the mutex handle or none on failure.

lock

pub fn (Mutex) lock(m: Mutex) void

Lock a mutex, blocking until it becomes available.

unlock

pub fn (Mutex) unlock(m: Mutex) void = trust { ... }

Unlock a previously locked mutex.

free

pub fn (Mutex) free[A: allocators.GeneralAllocator](ac: A, m: mut Mutex) void

Destroy and free a mutex.

thread_test_entry

private
fn thread_test_entry(arg: ref void) ref void