Skip to main content

abi_application/threads/
mod.rs

1//! Cooperative threads and synchronization in native processes.
2
3use core::ffi::{c_char, c_void};
4
5/// A native worker entry. Null is rejected.
6pub type ThreadEntry = Option<unsafe extern "C" fn(*mut c_void) -> *mut c_void>;
7
8pub const XILA_THREAD_STACK_MIN: usize = 2048;
9pub const XILA_THREAD_STACK_MAX: usize = 64 * 1024;
10pub const XILA_THREAD_KEYS_MAX: usize = 8;
11pub const XILA_MUTEX_RECURSIVE: u32 = 1;
12pub const XILA_MUTEX_TRY: u32 = 1;
13
14/// SDK-reserved coroutine-local libc state. Other applications should use TLS keys.
15#[repr(C)]
16pub struct ThreadLibc {
17    error: i32,
18    strtok_next: *mut c_char,
19    random_state: u64,
20}
21
22/// Process-local mutex. Zero initialize for a plain mutex, or use mutex_init.
23/// Keep its address fixed from first use through destruction. The fields are private
24/// to the host; copying a used mutex or sharing it across processes is invalid.
25#[repr(C)]
26pub struct Mutex {
27    owner: usize,
28    depth: u32,
29    recursive: bool,
30    waiters: *mut c_void,
31}
32
33/// Process-local condition variable. Zero initialize or use condition_init.
34/// Keep its address fixed from first use through destruction. The fields are private
35/// to the host; copying a used condition or sharing it across processes is invalid.
36#[repr(C)]
37pub struct Condition {
38    waiters: *mut c_void,
39}
40
41// Compare the guest ABI on every target, including the private thread record fields.
42include!(concat!(env!("OUT_DIR"), "/rust_layout.rs"));
43
44#[cfg(any(
45    all(
46        any(target_arch = "x86_64", target_arch = "aarch64"),
47        target_os = "linux"
48    ),
49    all(
50        any(
51            target_arch = "x86_64",
52            target_arch = "aarch64",
53            target_arch = "arm",
54            target_arch = "riscv32",
55            target_arch = "xtensa"
56        ),
57        target_os = "none"
58    )
59))]
60mod runtime;
61#[cfg(any(
62    all(
63        any(target_arch = "x86_64", target_arch = "aarch64"),
64        target_os = "linux"
65    ),
66    all(
67        any(
68            target_arch = "x86_64",
69            target_arch = "aarch64",
70            target_arch = "arm",
71            target_arch = "riscv32",
72            target_arch = "xtensa"
73        ),
74        target_os = "none"
75    )
76))]
77pub use runtime::*;
78
79#[cfg(not(any(
80    all(
81        any(target_arch = "x86_64", target_arch = "aarch64"),
82        target_os = "linux"
83    ),
84    all(
85        any(
86            target_arch = "x86_64",
87            target_arch = "aarch64",
88            target_arch = "arm",
89            target_arch = "riscv32",
90            target_arch = "xtensa"
91        ),
92        target_os = "none"
93    )
94)))]
95mod unsupported {
96    use super::*;
97    use crate::{Process, Status};
98    use core::sync::atomic::{AtomicPtr, Ordering};
99    include!(concat!(env!("OUT_DIR"), "/disabled_threads.rs"));
100
101    pub(crate) fn bind_process(_: *const Process) {}
102    pub(crate) fn bind_registry(_: *const crate::registry::Registry) {}
103
104    /// Without stack switching a program runs to completion on its task, so one slot is
105    /// enough: [`bind_startup`] saves what it replaces and the caller restores it.
106    static STARTUP: AtomicPtr<crate::XilaStartup> = AtomicPtr::new(core::ptr::null_mut());
107
108    /// Install `startup` and return the previous value, to restore when the entry ends.
109    pub(crate) fn bind_startup(startup: *const crate::XilaStartup) -> *const crate::XilaStartup {
110        STARTUP.swap(startup.cast_mut(), Ordering::AcqRel)
111    }
112    pub(crate) fn current_startup() -> *const crate::XilaStartup {
113        STARTUP.load(Ordering::Acquire)
114    }
115    pub(crate) fn stop_workers() {}
116    pub(crate) fn prepare_exit(_: *const Process) -> Result<(), Status> {
117        Ok(())
118    }
119}
120#[cfg(not(any(
121    all(
122        any(target_arch = "x86_64", target_arch = "aarch64"),
123        target_os = "linux"
124    ),
125    all(
126        any(
127            target_arch = "x86_64",
128            target_arch = "aarch64",
129            target_arch = "arm",
130            target_arch = "riscv32",
131            target_arch = "xtensa"
132        ),
133        target_os = "none"
134    )
135)))]
136pub use unsupported::*;