libstd/thread

Real threads sharing memory with their creator, with the same API on Linux (clone) and Windows (CreateThread). thread_spawn starts a function, and every thread ends one of two ways. thread_join waits for it without busy waiting and returns its result; thread_detach lets it run alone, and the thread frees its own memory when it returns. There is no way to kill a thread from outside: thread_request_stop asks it to stop, and the thread checks with thread_stop_requested.

#include <libstd/thread>

Includes libstd/process, libstd/sync. Same functions and constant names on Linux and Windows: the system-specific part is in wrapper/linux/systhread and wrapper/windows/systhread (see Linux and Windows).

Example

#include <libstd/stdio>
#include <libstd/time>
#include <libstd/thread>

def worker(x: long, stop: long) -> int:
    // its own arena: it may allocate, but not touch global str/object/array/ref values
    rounds = 0
    while thread_stop_requested(stop) == 0:
        rounds = rounds + 1
    return 42

t = thread_spawn(worker, 0 as long, CHILD_STACK_SIZE)
if t.tid < 0:
    print("thread_spawn failed\n")
sleep_ms(10 as long)
thread_request_stop(t)
print(thread_join(t), "\n")            // 42
every thread has its own arena

A thread function allocates like the rest of the program (new, string concatenation, lists, dicts), and what it allocates is freed when it returns. Arena values do not cross threads: entry_fn takes and returns integers, and the compiler rejects a thread that reads or writes a global str, object, array or ref, even through the functions it calls (libstd/re keeps its caches in globals, so it cannot be used in a thread). Share data through raw_alloc memory and the atomics and futexes of libstd/sync.

A thread that is neither joined nor detached leaks its memory. On Linux each thread has a single mmap: the sync block, a guard page, then the stack. A stack overflow stops at the guard page with a SIGSEGV instead of overwriting the sync block. The kernel clears the thread id when the thread exits (CLONE_CHILD_CLEARTID), and thread_join waits for that before freeing the stack.

Classes

class
Thread

Returned by thread_spawn: tid (negative if the thread was not created), the sync block, the Windows HANDLE (0 on Linux), and whether it was already joined or detached.

    tid int
    sync_block str
    os_handle long
    finished int

Functions

def thread_spawn(entry_fn: &(long, long) -> int, arg: long, stack_size: int) -> Thread

Starts entry_fn(arg, stop) (pass the bare function name: the compiler checks what runs in the thread) with stack_size bytes of stack and its own arena, and returns a Thread. Check tid < 0: -12 if memory is missing, -errno if the system refused. The second argument of entry_fn is the address of its stop request, for thread_stop_requested.

def thread_join(handle: Thread) -> int

Sleeps until the thread has exited, frees its stack and sync block and returns the value entry_fn returned. Returns -1 with a message if the thread was already joined or detached.

def thread_detach(handle: Thread) -> int

Lets the thread run on its own: when entry_fn returns the thread frees its own memory, and the return value is lost. If the thread had already finished, the memory is freed here.

def thread_request_stop(handle: Thread) -> None

Asks the thread to stop: its entry_fn sees it through thread_stop_requested and decides when to return. Call it before thread_join or thread_detach.

def thread_stop_requested(stop: long) -> int

From inside the thread: 1 if thread_request_stop was called, 0 otherwise. stop is the second argument of entry_fn.

Internal helpers

Used by the functions above; not meant to be called directly.