code wiki / (root) / nx_thread.nx

nx_thread.nx

buildroot/runtime/nx_thread.nx

6110 B137 linesdepth 4pulls 4 transitivereach 211 importersview sourcekind tooltopic thread
docsdependenciesstructsconstsfunctions

about

nx_thread.nx -- clone(2)-based thread creation (CLONE_VM, CLONE_FS, CLONE_FILES, CLONE_SIGHAND, CLONE_THREAD). A "thread" on Linux is just a process that shares its parent's address space + file table + signal handlers. clone() with the right flags creates one. This module exposes a focused API on top of the raw syscall. Key constraint: the new thread starts at the function specified by `entry` with stack pointer set to `stack_top`. Caller must allocate the stack themselves (typically 64 KiB - 8 MiB). We don't pass the function via a function pointer (NishiLang has no first-class func type) -- caller passes the symbol address as i64 they obtained out-of-band. When NishiLang grows func types, the API takes a typed callback. Today the sim is single-threaded. Calling nx_thread_spawn against the host kernel will succeed (real threads are created) but the in-process simulator can't observe them. When SMP lands in nx_rv64_sim, the simulated CPU array maps onto host threads via this primitive. Pairs with nx_atom (atomic state across threads) + nx_mutex (futex-based blocking) + nx_proc (parent process abstraction).

dependencies 2 imports · 16 importers

nx_syscalls.nx nx_proc.nx nx_thread.nx nx_chan.nx nx_chan_mpmc_test.nx nx_mutex_race_test.nx nx_natbw_mt.nx nx_natfu_probe.nx nx_net_chan_blob_test.nx nx_net_chan_test.nx nx_pipeline.nx nx_pteam.nx nx_q4k_gemm_mt.nx

diagram shows first 10 each side; +0 more imports, +6 more importers in the complete lists below.

imports: nx_syscalls.nxnx_proc.nx

imported by: nx_chan.nxnx_chan_mpmc_test.nxnx_mutex_race_test.nxnx_natbw_mt.nxnx_natfu_probe.nxnx_net_chan_blob_test.nxnx_net_chan_test.nxnx_pipeline.nxnx_pteam.nxnx_q4k_gemm_mt.nxnx_remote_worker.nxnx_rpc_test.nxnx_socket_pingpong_test.nxnx_thread_pool.nxnx_thread_pool_test.nxnx_thread_spawn_test.nx

call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown

main nx_thread_self nx_proc_getpid nx_thread_yield

structs

none

consts

35const NX_MAGIC_65536: i64 = 65536
36const NX_MAGIC_262144: i64 = 262144
40const NX_THREAD_CLONE_FLAGS: i64 = 0x100 // CLONE_VM
41const NX_THREAD_FS: i64 = 0x200 // CLONE_FS
42const NX_THREAD_FILES: i64 = 0x400 // CLONE_FILES
43const NX_THREAD_SIGHAND: i64 = 0x800 // CLONE_SIGHAND
44const NX_THREAD_THREAD: i64 = 0x10000 // CLONE_THREAD
45const NX_THREAD_SETTLS: i64 = 0x80000 // CLONE_SETTLS
46const NX_THREAD_PARENT_TID: i64 = 0x100000 // CLONE_PARENT_SETTID
47const NX_THREAD_CHILD_TID: i64 = 0x200000 // CLONE_CHILD_CLEARTID
51const NX_THREAD_DEFAULT_FLAGS: i64 = 0x100 // CLONE_VM

functions

58func nx_thread_spawn_raw(flags: i64, stack_top: i64) -> i64
73func nx_thread_spawn(entry: i64, ctx: i64, stack_size: i64) -> i64
84func nx_thread_spawn_fn(entry: func(*u8) -> i64, ctx: *u8, stack_size: i64) -> i64
95func sys_thread_create(argptr: i64) -> i64
101func nx_thread_self() -> i64
called by 1: main
107func nx_thread_yield() -> i64
113func nx_thread_exit(code: i64) -> i64
119func main() -> i64