burritos/src/kernel/ucontext.rs

72 lines
1.9 KiB
Rust
Raw Normal View History

2023-03-06 16:31:35 +01:00
use std::mem::MaybeUninit;
/// Safe wrapper for ucontext_t struct of linux-gnu libc
///
/// setcontext and getcontext are unsafe function, this wrap unsafe libc functions
///
/// This struct doesn't work on windows, because this struct is unavailable
///
/// todo ucontext_t is not thread-safe (doesn't implements Send and Sync trait), and cannot be use in Threads as rust require var in Mutex (see system.rs) to have everything inside to implements thread-safe traits
#[derive(PartialEq)]
pub struct UContextT {
#[cfg(not(target_os = "windows"))] // struct non disponible sur la libc sur windows
pub buf: lib::ucontext_t,
2023-03-06 16:31:35 +01:00
pub stackBottom: Vec<i8>
}
#[cfg(not(target_os = "windows"))]
impl UContextT {
pub fn new() -> Self {
let mut context = MaybeUninit::<ucontext_t>::uninit();
unsafe { lib::getcontext(context.as_mut_ptr()) };
2023-03-06 16:31:35 +01:00
Self {
buf: unsafe { context.assume_init() },
stackBottom: Vec::default(),
}
}
/// Get user context and store it in variable pointed to by UCP.
///
/// Use `man getcontext` for more informations
pub fn get_context(&mut self) -> i32 {
unsafe {
lib::getcontext(&mut self.buf)
2023-03-06 16:31:35 +01:00
}
}
/// Set user context from information of variable pointed to by UCP.
///
/// Use `man setcontext` for more informations
pub fn set_context(&mut self) -> i32 {
unsafe {
lib::setcontext(&self.buf)
2023-03-06 16:31:35 +01:00
}
}
pub fn make_context(&mut self, func: extern "C" fn(), args: i32) {
unsafe {
lib::makecontext(&mut self.buf, func, args)
2023-03-06 16:31:35 +01:00
}
}
}
#[cfg(target_os = "windows")]
impl UContextT {
pub fn new() -> Self {
Self {
stackBottom: Vec::default()
}
2023-03-06 16:31:35 +01:00
}
pub fn get_context(&mut self) {
// no op
}
pub fn set_context(&mut self) {
// no op
}
}