Threads v0.1.0

Source
POSIX threads: starting, joining and detaching them, their stack size, and the processor count.

PThread

pub type PThread = uint64

A thread as the C library names it, pthread_t. Every system this package family binds keeps it in one word.

Source

PThreadAttributes

pub struct PThreadAttributes {
    // private fields
}

Room for a pthread_attr_t, which each C library sizes its own way and none larger than this. Only ever passed by address to the pthread_attr_* calls, which initialize and read it.

Source

Constructors

PThreadAttributes

pub func PThreadAttributes() -> PThreadAttributes

Zeroed storage for pthread_attr_init to initialize.

Returns: the storage

Source

SC_NPROCESSORS_ONLN

pub const SC_NPROCESSORS_ONLN: int32 = 58;

_SC_NPROCESSORS_ONLN: the sysconf name for the number of processors currently online.

Source

pthread_create

pub extern func pthread_create(
    thread: *var PThread,
    attributes: *PThreadAttributes,
    start: *opaque,
    argument: *var opaque
) -> int32

Starts a thread running start(argument).

See also: https://keith.github.io/xcode-man-pages/pthread_create.3.html

Source

Parameters

NameTypeDescription
thread*var PThreadreceives the new thread
attributes*PThreadAttributesinitialized attributes, or null for the defaults
start*opaquethe thread's entry point, a function taking and returning one pointer
argument*var opaquethe value handed to the entry point

Returns: zero, or an error number

pthread_join

pub extern func pthread_join(
    thread: PThread,
    result: *var *var opaque
) -> int32

Waits for a thread to finish and releases it.

See also: https://keith.github.io/xcode-man-pages/pthread_join.3.html

Source

Parameters

NameTypeDescription
threadPThreada thread nothing has joined or detached
result*var *var opaquereceives the value its entry point returned, or null to ignore it

Returns: zero, or an error number

pthread_detach

pub extern func pthread_detach(thread: PThread) -> int32

Lets a thread release itself when it finishes, so nothing has to join it.

See also: https://keith.github.io/xcode-man-pages/pthread_detach.3.html

Source

Parameters

NameTypeDescription
threadPThreada thread nothing has joined or detached

Returns: zero, or an error number

pthread_attr_init

pub extern func pthread_attr_init(attributes: *var PThreadAttributes) -> int32

Initializes thread attributes to the defaults.

See also: https://keith.github.io/xcode-man-pages/pthread_attr_init.3.html

Source

Parameters

NameTypeDescription
attributes*var PThreadAttributesthe storage to initialize

Returns: zero, or an error number

pthread_attr_setstacksize

pub extern func pthread_attr_setstacksize(
    attributes: *var PThreadAttributes,
    size: uint
) -> int32

Sets the stack size a thread started with these attributes gets.

See also: https://keith.github.io/xcode-man-pages/pthread_attr_setstacksize.3.html

Source

Parameters

NameTypeDescription
attributes*var PThreadAttributesinitialized attributes
sizeuintthe stack size in bytes, at least the system's minimum

Returns: zero, or an error number

pthread_attr_destroy

pub extern func pthread_attr_destroy(attributes: *var PThreadAttributes) -> int32

Releases what pthread_attr_init set up.

See also: https://keith.github.io/xcode-man-pages/pthread_attr_destroy.3.html

Source

Parameters

NameTypeDescription
attributes*var PThreadAttributesinitialized attributes

Returns: zero, or an error number

pthread_threadid_np

pub extern func pthread_threadid_np(
    thread: PThread,
    id: *var uint64
) -> int32

Reads the system-wide identifier of a thread, or of the calling thread when thread is zero.

See also: https://keith.github.io/xcode-man-pages/pthread_threadid_np.3.html

Source

Parameters

NameTypeDescription
threadPThreadthe thread to ask about, or zero for the calling thread
id*var uint64receives the identifier

Returns: zero, or an error number

sched_yield

pub extern func sched_yield() -> int32

Gives up the processor so another ready thread can run.

See also: https://keith.github.io/xcode-man-pages/sched_yield.2.html

Source

Returns: zero, or -1 with the reason in errno

sysconf

pub extern func sysconf(name: int32) -> int64

Reads a system configuration value.

See also: https://keith.github.io/xcode-man-pages/sysconf.3.html

Source

Parameters

NameTypeDescription
nameint32the value to read, such as SC_NPROCESSORS_ONLN

Returns: the value, or -1 when the system has no limit or does not know the name