plat/linux_thread.rs (Linux)#
Source: lib/plat/linux_thread.rs
linux_thread -- Linux threads (clone + futex; linked against glibc: pthread_create, at the end of this file), semaphores, the wake-up of the UI thread,
system information, the date [src/plat/linux/lx_thread.s, plat_linux.s]
A thread's stack is an mmap with a PROT_NONE guard page below it; the thread record sits
at its top. The kernel clears the record's tid word and wakes it (a shared futex wake)
when the thread ends, which is what join waits for. Semaphores are process-private
futex counters. The wake-up is an eventfd: the display backends poll it next to their
socket, wait_events polls it alone (headless).
struct Thread(*mut ThreadRec)a thread started by thread_spawn; join it to wait for its end (and free its stack)fn thread_spawn(stack: usize, f: Start) -> Option<Thread>run f on a new thread with stack bytes of stack (0 = 1 MB) [plat_thread_create]
impl Thread
fn join(self)wait until the thread has ended, then free its stack [plat_thread_join]fn thread_yield()give the rest of the time slice away [plat_thread_yield]struct Sem(Box<SemInner>)a counting semaphore
impl Sem
fn new(initial: u32) -> Option<Sem>fn post(&self, n: u32)add n units, wake sleepers (0: nothing)fn wait(&self, ms: i32) -> booltake one unit: true taken, false timeout (ms -1 = forever, 0 = only try)fn wake()any thread: make the UI thread's wait return with E_WAKE (coalesced) [plat_wake]fn wait_events(deadline_us: u64, on_event: &mut dyn FnMut(&Event)) -> u32headless wait (no display): until a wake-up (E_WAKE to on_event, returns 1) or the deadline in time_us() (0 = only poll, u64::MAX = forever; returns 0) [lx_wait_nodisp]fn cpu_count() -> u32CPUs this process may run on (sched_getaffinity), >= 1 [plat_cpu_count]fn mem_total() -> u64physical memory in bytes (sysinfo), 0 unknown [plat_mem_total]fn date() -> (Date, bool)the date and time in UTC (no time zone database); the flag is true (UTC) [plat_date]
impl Thread
fn join(self)wait until the thread has ended, then free its stack [plat_thread_join] (a thread joining itself returns at once and keeps its stack: it still runs on it)