diff --git a/src/lib.rs b/src/lib.rs index f3724ec3ed..46ddf7ee3f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -49,6 +49,13 @@ pub mod net; pub mod sched; pub mod sys; + +// This can be implemented for other platforms as soon as libc +// provides bindings for them. +#[cfg(all(target_os = "linux", + any(target_arch = "x86", target_arch = "x86_64")))] +pub mod ucontext; + pub mod unistd; /* diff --git a/src/sys/mod.rs b/src/sys/mod.rs index de449b769a..82934164b9 100644 --- a/src/sys/mod.rs +++ b/src/sys/mod.rs @@ -1,4 +1,3 @@ - #[cfg(any(target_os = "linux", target_os = "android"))] pub mod epoll; diff --git a/src/ucontext.rs b/src/ucontext.rs new file mode 100644 index 0000000000..f77b481529 --- /dev/null +++ b/src/ucontext.rs @@ -0,0 +1,25 @@ +use libc; +use {Errno, Result}; +use std::mem; + +#[derive(Clone, Copy)] +pub struct UContext { + context: libc::ucontext_t, +} + +impl UContext { + pub fn get() -> Result { + let mut context: libc::ucontext_t = unsafe { mem::uninitialized() }; + let res = unsafe { + libc::getcontext(&mut context as *mut libc::ucontext_t) + }; + Errno::result(res).map(|_| UContext { context: context }) + } + + pub fn set(&self) -> Result<()> { + let res = unsafe { + libc::setcontext(&self.context as *const libc::ucontext_t) + }; + Errno::result(res).map(drop) + } +}