Threads v0.1.0
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.
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.
Constructors
PThreadAttributes
pub func PThreadAttributes() -> PThreadAttributes
Zeroed storage for pthread_attr_init to initialize.
Returns: the storage
SC_NPROCESSORS_ONLN
pub const SC_NPROCESSORS_ONLN: int32 = 84;
_SC_NPROCESSORS_ONLN: the sysconf name for the number of processors currently online.
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://man7.org/linux/man-pages/man3/pthread_create.3.html
Parameters
| Name | Type | Description |
|---|---|---|
thread | *var PThread | receives the new thread |
attributes | *PThreadAttributes | initialized attributes, or null for the defaults |
start | *opaque | the thread's entry point, a function taking and returning one pointer |
argument | *var opaque | the 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://man7.org/linux/man-pages/man3/pthread_join.3.html
Parameters
| Name | Type | Description |
|---|---|---|
thread | PThread | a thread nothing has joined or detached |
result | *var *var opaque | receives 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://man7.org/linux/man-pages/man3/pthread_detach.3.html
Parameters
| Name | Type | Description |
|---|---|---|
thread | PThread | a 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://man7.org/linux/man-pages/man3/pthread_attr_init.3.html
Parameters
| Name | Type | Description |
|---|---|---|
attributes | *var PThreadAttributes | the 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://man7.org/linux/man-pages/man3/pthread_attr_setstacksize.3.html
Parameters
| Name | Type | Description |
|---|---|---|
attributes | *var PThreadAttributes | initialized attributes |
size | uint | the 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://man7.org/linux/man-pages/man3/pthread_attr_destroy.3.html
Parameters
| Name | Type | Description |
|---|---|---|
attributes | *var PThreadAttributes | initialized attributes |
Returns: zero, or an error number
gettid
pub extern func gettid() -> int32
Returns the kernel's identifier for the calling thread, which is the process identifier on its first thread.
See also: https://man7.org/linux/man-pages/man2/gettid.2.html
Returns: the calling thread's identifier
sched_yield
pub extern func sched_yield() -> int32
Gives up the processor so another ready thread can run.
See also: https://man7.org/linux/man-pages/man2/sched_yield.2.html
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://man7.org/linux/man-pages/man3/sysconf.3.html
Parameters
| Name | Type | Description |
|---|---|---|
name | int32 | the 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