code wiki / (root) / nx_thread_kat.nx

nx_thread_kat.nx source

↩ module page · 25 lines · 951 B

1// THE THREADING TEST: __thread_clone with a REAL function address (now that &fn works). A worker thread 2// writes 42 to shared memory (CLONE_VM); main spins until it sees it. If the thread actually RUNS (no NULL-entry 3// SIGSEGV), prints THREAD_RAN 42. This is the direct test of the codec-unlock: a second thread executing. 4import "nx_syscalls.nx" 5 6func t_worker(ctx: *u8) -> i64 { 7 let flag: *i64 = ctx as *i64 8 flag[0] = 42 9 return 0 10} 11 12func main() -> i64 { 13 let stack: *u8 = sys_mmap(131072) 14 let top: *u8 = ((stack as i64) + 131072) as *u8 15 let ctx: *i64 = sys_mmap(16) as *i64 16 ctx[0] = 0 17 let tid: i64 = __thread_clone(top, &t_worker, ctx as *u8) 18 var spins: i64 = 0 19 while ctx[0] == 0 { 20 spins = spins + 1 21 if spins > 200000000 { ctx[0] = 0 - 1 } 22 } 23 if ctx[0] == 42 { sys_write(1, "THREAD_RAN 42\n" as *u8, 14) } else { sys_write(1, "THREAD_FAIL\n" as *u8, 12) } 24 sys_exit(0); return 0 25}