Browse the docs

silk.platform.wasm.thread.

The wasi-threads records of a multi-threaded wasm32 module.

When to use#

NativeThread starts and joins wasm32 threads through WasmThread. Applications use silk.concurrency.thread_native.

Details#

Each thread owns a record and a 1 MiB stack from the module's heap. The host starts the thread as a new instance on the shared memory and calls the module's wasi_thread_start, which the compiler defines: it moves the shadow stack to the record's stack top, calls the entry module's thread entry, then marks the record done and wakes the joiner. The joiner releases the stack and the record only after it sees the record done, which the thread sets after its last stack use.

Public declarations: 1.

WasmThread#

silk
pub struct WasmThread

The wasi-threads records that NativeThread starts and joins.

Implementation WasmThread#

silk
impl WasmThread

Associated function spawn#

silk
pub unsafe fn spawn(argument: ?*mut u8) -> Result<?[*]mut u8, i32>

Starts a thread that runs the entry module's thread entry with argument. It returns the thread's handle, or an error number when the host refuses the thread or the heap is full (12, ENOMEM).

Safety#

Join each started handle once with WasmThread.join.

Associated function argument#

silk
pub unsafe fn argument(handle: ?[*]mut u8) -> ?*mut u8

Returns the argument the thread behind handle was started with.

Safety#

handle is a live handle from WasmThread.spawn.

Associated function done#

silk
pub unsafe fn done(handle: ?[*]mut u8) -> *mut u32

Returns the word that wasi_thread_start sets to 1 once the thread's work is over.

Safety#

handle is a live handle from WasmThread.spawn.

Associated function join#

silk
pub unsafe fn join(handle: ?[*]mut u8) -> ()

Waits for the thread behind handle to finish, then releases its stack and record.

Safety#

handle is a live handle from WasmThread.spawn, joined only once.

Category: Native platform.

Profiles: wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2, wasm64-unknown-unknown.

View the source of this page