burritos/src/kernel/ucontext.rs

73 lines
2.0 KiB
Rust

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: libc::ucontext_t,
pub stack_bottom: Vec<i8>
}
#[cfg(not(target_os = "windows"))]
#[allow(unused)] // Temporary as we currently doesn't use this structure (this structure may disapear in a near future)
impl UContextT {
pub fn new() -> Self {
let mut context = MaybeUninit::<libc::ucontext_t>::uninit();
unsafe { libc::getcontext(context.as_mut_ptr()) };
Self {
buf: unsafe { context.assume_init() },
stack_bottom: 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 {
libc::getcontext(&mut self.buf)
}
}
/// 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 {
libc::setcontext(&self.buf)
}
}
pub fn make_context(&mut self, func: extern "C" fn(), args: i32) {
unsafe {
libc::makecontext(&mut self.buf, func, args)
}
}
}
#[cfg(target_os = "windows")]
impl UContextT {
pub fn new() -> Self {
Self {
stackBottom: Vec::default()
}
}
pub fn get_context(&mut self) {
// no op
}
pub fn set_context(&mut self) {
// no op
}
}