twz_rt/
runtime.rs

1//! Top level runtime module, managing the basic presentation of the runtime.
2
3use std::{
4    collections::BTreeMap,
5    path::PathBuf,
6    sync::atomic::{AtomicU32, Ordering},
7};
8
9mod alloc;
10mod core;
11mod debug;
12mod exec;
13mod file;
14mod object;
15mod process;
16mod slot;
17mod thread;
18mod time;
19mod trace;
20pub(crate) mod upcall;
21
22use twizzler_abi::simple_mutex::Mutex;
23use twizzler_rt_abi::fd::NameRoot;
24pub use upcall::set_upcall_handler;
25
26use self::object::ObjectHandleManager;
27
28/// The runtime trait implementer itself.
29pub struct ReferenceRuntime {
30    pub(crate) state: AtomicU32,
31    pub(crate) object_manager: Mutex<ObjectHandleManager>,
32    pub(crate) nameroots: Mutex<BTreeMap<NameRoot, PathBuf>>,
33}
34
35impl std::fmt::Debug for ReferenceRuntime {
36    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37        write!(
38            f,
39            "RefRun({})",
40            if self.state().contains(RuntimeState::READY) {
41                "ready"
42            } else {
43                "not-ready"
44            }
45        )
46    }
47}
48
49bitflags::bitflags! {
50    /// Various state flags for the runtime.
51    #[derive(Copy, Clone, Debug)]
52    pub struct RuntimeState : u32 {
53        const READY = 1;
54        const IS_MONITOR = 2;
55    }
56}
57
58impl ReferenceRuntime {
59    /// Returns the runtime state flags.
60    pub fn state(&self) -> RuntimeState {
61        RuntimeState::from_bits_truncate(self.state.load(Ordering::SeqCst))
62    }
63
64    /// Set the runtime ready state. If the runtime has not been initialized, the result is
65    /// undefined.
66    pub unsafe fn set_runtime_ready(&self) {
67        self.state
68            .fetch_or(RuntimeState::READY.bits(), Ordering::SeqCst);
69    }
70
71    fn set_is_monitor(&self) {
72        self.state
73            .fetch_or(RuntimeState::IS_MONITOR.bits(), Ordering::SeqCst);
74    }
75}
76
77pub static OUR_RUNTIME: ReferenceRuntime = ReferenceRuntime {
78    state: AtomicU32::new(0),
79    object_manager: Mutex::new(ObjectHandleManager::new()),
80    nameroots: Mutex::new(BTreeMap::new()),
81};
82
83// These are exported by libunwind, but not re-exported by the standard library that pulls that in.
84// Or, at least, that's what it seems like. In any case, they're no-ops in libunwind and musl, so
85// this is fine for now.
86#[no_mangle]
87#[linkage = "weak"]
88pub extern "C" fn __register_frame_info() {}
89#[no_mangle]
90#[linkage = "weak"]
91pub extern "C" fn __deregister_frame_info() {}
92#[no_mangle]
93#[linkage = "weak"]
94pub extern "C" fn __cxa_finalize() {}