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