Add ucontext_t
This commit is contained in:
committed by
François Autin
parent
e4db7ec96b
commit
3457f67a7c
72
src/kernel/ucontext.rs
Normal file
72
src/kernel/ucontext.rs
Normal file
@ -0,0 +1,72 @@
|
||||
|
||||
use std::mem::MaybeUninit;
|
||||
|
||||
use libc::{ucontext_t, getcontext, setcontext, makecontext};
|
||||
|
||||
/// 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: ucontext_t,
|
||||
pub stackBottom: Vec<i8>
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
impl UContextT {
|
||||
|
||||
pub fn new() -> Self {
|
||||
let mut context = MaybeUninit::<ucontext_t>::uninit();
|
||||
unsafe { getcontext(context.as_mut_ptr()) };
|
||||
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 {
|
||||
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 {
|
||||
setcontext(&self.buf)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn make_context(&mut self, func: extern "C" fn(), args: i32) {
|
||||
unsafe {
|
||||
makecontext(&mut self.buf, func, args)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
impl UContextT {
|
||||
|
||||
pub fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
|
||||
pub fn get_context(&mut self) {
|
||||
// no op
|
||||
}
|
||||
|
||||
pub fn set_context(&mut self) {
|
||||
// no op
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user