281 lines
8.4 KiB
Plaintext
281 lines
8.4 KiB
Plaintext
#! @file threads.sakura
|
|||
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
||
|
|
#! @copyright Malunal Studios, LLC.
|
||
|
|
#! @license MIT
|
||
|
|
#!
|
||
|
|
#! Contains the threads of an executable: starting one with stacks of its own,
|
||
|
|
#! ending it, and the lock every thread takes around the runtime's memory.
|
||
|
|
#!
|
||
|
|
#! Each thread has a record, which `gs` points at in it, holding its stack's
|
||
|
|
#! bounds, its text being formatted, and the function it runs. The first
|
||
|
|
#! thread's is the executable's state, and the records are a list from it.
|
||
|
|
#! Until the program starts a thread, nothing here takes a lock.
|
||
|
|
|
||
|
|
import system::windows;
|
||
|
|
|
||
|
|
module runtime when backend == native;
|
||
|
|
|
||
|
|
|
||
|
|
# Whether the program started a thread, after which the runtime's memory is
|
||
|
|
# locked.
|
||
|
|
var threaded: bool;
|
||
|
|
|
||
|
|
# The lock around the runtime's memory: its word, 0 where no thread holds it, 1
|
||
|
|
# where one does, and 2 where others may wait, and the thread holding it, which may
|
||
|
|
# take it again, as allocating while formatting does.
|
||
|
|
var lock_word: atomic<u32>;
|
||
|
|
var lock_owner: u64;
|
||
|
|
var lock_depth: u64;
|
||
|
|
|
||
|
|
# The bytes of a record.
|
||
|
|
val record_bytes := 256u;
|
||
|
|
|
||
|
|
# The stack past a thread's limit, reserved for reporting a trap, as
|
||
|
|
# `native/native.sakura` reserves it.
|
||
|
|
val stack_reserve := 262144u;
|
||
|
|
module runtime when backend == native && os == linux;
|
||
|
|
val call_getpid: i64 = 39;
|
||
|
|
val call_clone_exit: i64 = 60;
|
||
|
|
val call_rt_sigprocmask: i64 = 14;
|
||
|
|
val call_tgkill: i64 = 234;
|
||
|
|
val call_futex: i64 = 202;
|
||
|
|
|
||
|
|
#! Takes the lock around the runtime's memory, once the program has threads.
|
||
|
|
def hold(): void {
|
||
|
|
return if !threaded;
|
||
|
|
val me := current() as u64;
|
||
|
|
if lock_owner == me {
|
||
|
|
lock_depth++;
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if lock_word.exchange(1) != 0 {
|
||
|
|
while lock_word.exchange(2) != 0 {
|
||
|
|
syscall(call_futex, lock_address(), 0, 2, 0, 0, 0);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
lock_owner = me;
|
||
|
|
lock_depth = 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Lets go of the lock around the runtime's memory.
|
||
|
|
def let_go(): void {
|
||
|
|
return if !threaded;
|
||
|
|
lock_depth--;
|
||
|
|
return if lock_depth != 0;
|
||
|
|
lock_owner = 0;
|
||
|
|
if lock_word.exchange(0) == 2 {
|
||
|
|
syscall(call_futex, lock_address(), 1, 1, 0, 0, 0);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
def lock_address(): u64 {
|
||
|
|
return ((&mut lock_word) as *mut atomic<u32>) as u64;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! `op::start`: starts a thread running a function, with a stack of its own as
|
||
|
|
#! large as the first thread's.
|
||
|
|
#! @param body The function, two words, then the context it is given, a word.
|
||
|
|
#! @returns The thread's number, or a negative error number.
|
||
|
|
def start_thread(body: *mut u8): i64 {
|
||
|
|
threaded = true;
|
||
|
|
hold();
|
||
|
|
reap();
|
||
|
|
val first := (*executable()).state;
|
||
|
|
val size := (*first).shadow_base as u64 - (*first).trap_frame as u64;
|
||
|
|
val record := mapped(record_bytes) as *mut state;
|
||
|
|
val stack := mapped(size);
|
||
|
|
if record == null || stack == null {
|
||
|
|
unmapped(record as *mut u8, record_bytes);
|
||
|
|
unmapped(stack, size);
|
||
|
|
let_go();
|
||
|
|
return -12;
|
||
|
|
}
|
||
|
|
|
||
|
|
(*record).itself = record;
|
||
|
|
(*record).trap_frame = stack;
|
||
|
|
(*record).shadow_limit = stack + stack_reserve;
|
||
|
|
(*record).shadow_base = stack + size;
|
||
|
|
(*record).map = stack;
|
||
|
|
(*record).map_size = size;
|
||
|
|
(*record).running = 1;
|
||
|
|
copy_bytes((&mut record[0].body) as *mut u8, body, 24);
|
||
|
|
(*record).next = (*first).next;
|
||
|
|
(*first).next = record;
|
||
|
|
|
||
|
|
# Every signal is blocked while it starts, so the thread begins with them blocked,
|
||
|
|
# and takes those it does once `gs` holds its record.
|
||
|
|
var all: u64 = 0xFFFFFFFFFFFFFFFF;
|
||
|
|
var kept: u64 = 0;
|
||
|
|
syscall(call_rt_sigprocmask, 2, ((&mut all) as *mut u64) as u64, ((&mut kept) as *mut u64) as u64, 8, 0, 0);
|
||
|
|
val number := clone_thread(record);
|
||
|
|
syscall(call_rt_sigprocmask, 2, ((&mut kept) as *mut u64) as u64, 0, 8, 0, 0);
|
||
|
|
if number < 0 {
|
||
|
|
(*first).next = (*record).next;
|
||
|
|
unmapped(record as *mut u8, record_bytes);
|
||
|
|
unmapped(stack, size);
|
||
|
|
}
|
||
|
|
let_go();
|
||
|
|
return number;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! What a thread runs, on its own stack: its function, then the end of it. The
|
||
|
|
#! system clears its number once it has ended, and its stack is unmapped after.
|
||
|
|
def thread_main(): void {
|
||
|
|
val me := current();
|
||
|
|
(*me).body((*me).context);
|
||
|
|
hold();
|
||
|
|
flush_cache(me);
|
||
|
|
if (*me).builders != null {
|
||
|
|
dispose((*me).builders as *mut u8, (*me).builder_capacity * size_of::<builder>());
|
||
|
|
}
|
||
|
|
(*me).running = 0;
|
||
|
|
let_go();
|
||
|
|
syscall(call_clone_exit, 0, 0, 0, 0, 0, 0);
|
||
|
|
}
|
||
|
|
|
||
|
|
# Unmaps the records and stacks of threads the system has ended.
|
||
|
|
def reap(): void {
|
||
|
|
val first := (*executable()).state;
|
||
|
|
var link := first;
|
||
|
|
while (*link).next != null {
|
||
|
|
val each := (*link).next;
|
||
|
|
if (*each).running == 0 && (*each).tid == 0 {
|
||
|
|
(*link).next = (*each).next;
|
||
|
|
unmapped((*each).map, (*each).map_size);
|
||
|
|
unmapped(each as *mut u8, record_bytes);
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
link = each;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
# Maps memory, giving null where there is none.
|
||
|
|
def mapped(size: u64): *mut u8 {
|
||
|
|
val address := syscall(call_mmap, 0, size, 3, 0x4022, 0xFFFFFFFFFFFFFFFF, 0);
|
||
|
|
return address < 0 ? null : address as *mut u8;
|
||
|
|
}
|
||
|
|
|
||
|
|
def unmapped(address: *mut u8, size: u64): void {
|
||
|
|
if address != null {
|
||
|
|
syscall(call_munmap, address as u64, size, 0, 0, 0, 0);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
module runtime when backend == native && os == windows;
|
||
|
|
|
||
|
|
# On Windows each thread runs on a stack of the system's, which the system
|
||
|
|
# reserves, commits as it grows, and frees as the thread ends, and the record's
|
||
|
|
# word for its stack holds the thread's handle instead. The lock's word is
|
||
|
|
# waited on as Linux waits on a futex.
|
||
|
|
|
||
|
|
#! Takes the lock around the runtime's memory, once the program has threads.
|
||
|
|
def hold(): void {
|
||
|
|
return if !threaded;
|
||
|
|
val me := current() as u64;
|
||
|
|
if lock_owner == me {
|
||
|
|
lock_depth++;
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if lock_word.exchange(1) != 0 {
|
||
|
|
var contended: u32 = 2;
|
||
|
|
while lock_word.exchange(2) != 0 {
|
||
|
|
WaitOnAddress(lock_address() as *mut u32, &contended, 4, 0xFFFFFFFF);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
lock_owner = me;
|
||
|
|
lock_depth = 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Lets go of the lock around the runtime's memory.
|
||
|
|
def let_go(): void {
|
||
|
|
return if !threaded;
|
||
|
|
lock_depth--;
|
||
|
|
return if lock_depth != 0;
|
||
|
|
lock_owner = 0;
|
||
|
|
if lock_word.exchange(0) == 2 {
|
||
|
|
WakeByAddressSingle(lock_address() as *mut u32);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
def lock_address(): u64 {
|
||
|
|
return ((&mut lock_word) as *mut atomic<u32>) as u64;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! `op::start`: starts a thread running a function, on a stack the system makes
|
||
|
|
#! it, as large as the first thread's.
|
||
|
|
#! @param body The function, two words, then the context it is given, a word.
|
||
|
|
#! @returns The thread's number, or a negative error number.
|
||
|
|
def start_thread(body: *mut u8): i64 {
|
||
|
|
threaded = true;
|
||
|
|
hold();
|
||
|
|
reap();
|
||
|
|
val first := (*executable()).state;
|
||
|
|
|
||
|
|
# The thread runs on a stack the system makes it, as large as the first
|
||
|
|
# thread's.
|
||
|
|
val size := (*first).shadow_base as u64 - (*first).trap_frame as u64;
|
||
|
|
val record := mapped(record_bytes) as *mut state;
|
||
|
|
if record == null {
|
||
|
|
let_go();
|
||
|
|
return -12;
|
||
|
|
}
|
||
|
|
|
||
|
|
(*record).itself = record;
|
||
|
|
(*record).map_size = size; # The system reserves its stack this large.
|
||
|
|
(*record).running = 1;
|
||
|
|
copy_bytes((&mut record[0].body) as *mut u8, body, 24);
|
||
|
|
(*record).next = (*first).next;
|
||
|
|
(*first).next = record;
|
||
|
|
|
||
|
|
val number := clone_thread(record);
|
||
|
|
if number < 0 {
|
||
|
|
(*first).next = (*record).next;
|
||
|
|
unmapped(record as *mut u8, record_bytes);
|
||
|
|
}
|
||
|
|
let_go();
|
||
|
|
return number;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! What a thread runs, on the stack the system made it: its function, then the
|
||
|
|
#! end of it, after which it returns to `beginning`, which ends the thread. The
|
||
|
|
#! system then signals its handle, and its record is unmapped after.
|
||
|
|
def thread_main(): void {
|
||
|
|
val me := current();
|
||
|
|
(*me).body((*me).context);
|
||
|
|
hold();
|
||
|
|
flush_cache(me);
|
||
|
|
if (*me).builders != null {
|
||
|
|
dispose((*me).builders as *mut u8, (*me).builder_capacity * size_of::<builder>());
|
||
|
|
}
|
||
|
|
(*me).running = 0;
|
||
|
|
let_go();
|
||
|
|
}
|
||
|
|
|
||
|
|
# Unmaps the records of threads the system has ended, closing their handles; the
|
||
|
|
# system frees their stacks itself.
|
||
|
|
def reap(): void {
|
||
|
|
val first := (*executable()).state;
|
||
|
|
var link := first;
|
||
|
|
while (*link).next != null {
|
||
|
|
val each := (*link).next;
|
||
|
|
val handle := (*each).spare_map[0];
|
||
|
|
if (*each).running == 0 && WaitForSingleObject(handle, 0) == 0 {
|
||
|
|
(*link).next = (*each).next;
|
||
|
|
CloseHandle(handle);
|
||
|
|
unmapped(each as *mut u8, record_bytes);
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
link = each;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
# Maps zeroed memory, giving null where there is none.
|
||
|
|
def mapped(size: u64): *mut u8 {
|
||
|
|
return VirtualAlloc(0, size, 0x3000, 0x04) as *mut u8;
|
||
|
|
}
|
||
|
|
|
||
|
|
def unmapped(address: *mut u8, size: u64): void {
|
||
|
|
if address != null {
|
||
|
|
VirtualFree(address as u64, 0, 0x8000);
|
||
|
|
}
|
||
|
|
}
|