Skip to content

Commit

Permalink
Add context module.
Browse files Browse the repository at this point in the history
The module wraps context handling related functions and structs.
  • Loading branch information
fiveop committed Mar 14, 2016
1 parent cb6a921 commit fbe5696
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/*
Expand Down
1 change: 0 additions & 1 deletion src/sys/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#[cfg(any(target_os = "linux", target_os = "android"))]
pub mod epoll;

Expand Down
25 changes: 25 additions & 0 deletions src/ucontext.rs
Original file line number Diff line number Diff line change
@@ -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<UContext> {
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)
}
}

0 comments on commit fbe5696

Please sign in to comment.