Newtype file descriptors.

To avoid various bugs regarding the typing of file descriptors, we
newtype them into a simple wrapper type.

- Document some stuff.
This commit is contained in:
ticki 2016-08-29 11:58:31 +02:00
parent 3a232cc60f
commit 94a1a0fa0c
3 changed files with 24 additions and 11 deletions

View file

@ -25,6 +25,7 @@ pub struct ContextList {
}
impl ContextList {
/// Create a new context list.
pub fn new() -> Self {
ContextList {
map: BTreeMap::new(),
@ -32,14 +33,17 @@ impl ContextList {
}
}
/// Get the nth context.
pub fn get(&self, id: usize) -> Option<&RwLock<Context>> {
self.map.get(&id)
}
/// Get the current context.
pub fn current(&self) -> Option<&RwLock<Context>> {
self.map.get(&CONTEXT_ID.load(Ordering::SeqCst))
}
/// Create a new context.
pub fn new_context(&mut self) -> Result<&RwLock<Context>> {
if self.next_id >= CONTEXT_MAX_CONTEXTS {
self.next_id = 1;
@ -55,10 +59,13 @@ impl ContextList {
let id = self.next_id;
self.next_id += 1;
assert!(self.map.insert(id, RwLock::new(Context::new(id))).is_none());
Ok(self.map.get(&id).expect("failed to insert new context"))
Ok(self.map.get(&id).expect("Failed to insert new context. ID is out of bounds."))
}
/// Spawn a context from a function.
pub fn spawn(&mut self, func: extern fn()) -> Result<&RwLock<Context>> {
let context_lock = self.new_context()?;
{
@ -107,6 +114,9 @@ pub fn contexts_mut() -> RwLockWriteGuard<'static, ContextList> {
}
/// Switch to the next context
///
/// # Safety
///
/// Do not call this while holding locks!
pub unsafe fn context_switch() {
// current.arch.switch_to(&mut next.arch);