1-- The C library's threads, POSIX's `pthread` calls, alike on every system
2-- with a C library: glibc on Linux under --link=libc, where they are in
3-- libc itself since glibc 2.34, Darwin's libSystem, and FreeBSD's libthr,
4-- which a FreeBSD program that names one of them links (D518). Each
5-- returns its errno itself, zero for success, and sets no errno cell.
6
7--- A thread's attributes, `pthread_attr_t`, as room for any system's:
8--- glibc's is 56 bytes on x86-64 and RV64 and 64 on arm64, Darwin's 64,
9--- and FreeBSD's one pointer. Declare it `zeroed` and fill it with
10--- `pthread_attr_init`.
11public thread_attributes: type = struct
12 opaque: [8]u64
13end thread_attributes
14
15--- A thread the C library started, `pthread_t`: one word on every system.
16public thread_handle: type = usize
17
18--- The routine a thread the C library starts runs, `void *(*)(void *)`:
19--- called with the argument `pthread_create` was given, and its result
20--- is what `pthread_join` may read.
21public thread_routine: type = extern(c) (argument: usize) -> (result: usize)
22
23--- Fill `attributes` with the defaults: 0, or the errno.
24public extern(c) pthread_attr_init: (attributes: ptr mut thread_attributes)
25 -> (status: i32)
26
27--- Free what `pthread_attr_init` took: 0, or the errno.
28public extern(c) pthread_attr_destroy:
29 (attributes: ptr mut thread_attributes) -> (status: i32)
30
31--- Make the `size` bytes from `lowest` the stack of a thread started with
32--- `attributes`, the C library's own frames and, under glibc, its thread
33--- block among them: 0, or the errno, EINVAL when `size` is below the
34--- system's PTHREAD_STACK_MIN, and on Darwin when either is not a multiple
35--- of the page.
36public extern(c) pthread_attr_setstack:
37 (attributes: ptr mut thread_attributes, lowest: usize, size: usize)
38 -> (status: i32)
39
40--- Start a thread with `attributes` that calls `routine(argument)`, and
41--- put its handle in `thread`: 0, or the errno, EAGAIN when the system
42--- lacks what another thread needs. The new thread starts with the
43--- creator's signal mask.
44public extern(c) pthread_create:
45 (thread: ptr mut thread_handle, attributes: ptr thread_attributes,
46 routine: thread_routine, argument: usize) -> (status: i32)
47
48--- Wait until `thread` has ended, and release it, discarding its result:
49--- 0, or the errno. A thread is joined once.
50public extern(c) pthread_join: (thread: thread_handle, result: address_filled)
51 -> (status: i32)
52
53--- Change the calling thread's mask of blocked signals by `how`,
54--- `sig_block`, `sig_unblock` or `sig_setmask`, with `set` when it is
55--- given, and put the mask it had in `previous` when that is given: 0, or
56--- the errno. glibc leaves its own two signals unblocked whatever `set`
57--- says.
58public extern(c) pthread_sigmask: (how: i32, set: signals_given,
59 previous: signals_kept) -> (status: i32)
60
61--- Make `set` hold every signal: 0, or -1 with the reason in errno.
62public extern(c) sigfillset: (set: ptr mut signal_set) -> (status: i32)
63
64--- Take `signal` out of `set`: 0, or -1 with the reason in errno, EINVAL
65--- for a number that is no signal.
66public extern(c) sigdelset: (set: ptr mut signal_set, signal: i32)
67 -> (status: i32)