twz_rt/
runtime.rs

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