This commit is contained in:
Jeremy Soller 2016-08-30 16:23:56 -06:00
commit de761ce921
3 changed files with 24 additions and 11 deletions

View file

@ -26,6 +26,7 @@ pub struct ContextList {
} }
impl ContextList { impl ContextList {
/// Create a new context list.
pub fn new() -> Self { pub fn new() -> Self {
ContextList { ContextList {
map: BTreeMap::new(), map: BTreeMap::new(),
@ -33,14 +34,17 @@ impl ContextList {
} }
} }
/// Get the nth context.
pub fn get(&self, id: usize) -> Option<&RwLock<Context>> { pub fn get(&self, id: usize) -> Option<&RwLock<Context>> {
self.map.get(&id) self.map.get(&id)
} }
/// Get the current context.
pub fn current(&self) -> Option<&RwLock<Context>> { pub fn current(&self) -> Option<&RwLock<Context>> {
self.map.get(&CONTEXT_ID.load(Ordering::SeqCst)) self.map.get(&CONTEXT_ID.load(Ordering::SeqCst))
} }
/// Create a new context.
pub fn new_context(&mut self) -> Result<&RwLock<Context>> { pub fn new_context(&mut self) -> Result<&RwLock<Context>> {
if self.next_id >= CONTEXT_MAX_CONTEXTS { if self.next_id >= CONTEXT_MAX_CONTEXTS {
self.next_id = 1; self.next_id = 1;
@ -56,10 +60,13 @@ impl ContextList {
let id = self.next_id; let id = self.next_id;
self.next_id += 1; self.next_id += 1;
assert!(self.map.insert(id, RwLock::new(Context::new(id))).is_none()); 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>> { pub fn spawn(&mut self, func: extern fn()) -> Result<&RwLock<Context>> {
let context_lock = self.new_context()?; let context_lock = self.new_context()?;
{ {
@ -109,6 +116,9 @@ pub fn contexts_mut() -> RwLockWriteGuard<'static, ContextList> {
} }
/// Switch to the next context /// Switch to the next context
///
/// # Safety
///
/// Do not call this while holding locks! /// Do not call this while holding locks!
pub unsafe fn switch() { pub unsafe fn switch() {
use core::ops::DerefMut; use core::ops::DerefMut;

View file

@ -1,7 +1,7 @@
use core::str; use core::str;
use syscall::Result; use syscall::Result;
use super::Scheme; use super::{Scheme, Fd};
pub struct DebugScheme; pub struct DebugScheme;
@ -14,21 +14,21 @@ impl Scheme for DebugScheme {
/// Read the file `number` into the `buffer` /// Read the file `number` into the `buffer`
/// ///
/// Returns the number of bytes read /// Returns the number of bytes read
fn read(&mut self, _file: usize, _buffer: &mut [u8]) -> Result<usize> { fn read(&mut self, _file: Fd, _buffer: &mut [u8]) -> Result<usize> {
Ok(0) Ok(0)
} }
/// Write the `buffer` to the `file` /// Write the `buffer` to the `file`
/// ///
/// Returns the number of bytes written /// Returns the number of bytes written
fn write(&mut self, _file: usize, buffer: &[u8]) -> Result<usize> { fn write(&mut self, _file: Fd, buffer: &[u8]) -> Result<usize> {
//TODO: Write bytes, do not convert to str //TODO: Write bytes, do not convert to str
print!("{}", unsafe { str::from_utf8_unchecked(buffer) }); print!("{}", unsafe { str::from_utf8_unchecked(buffer) });
Ok(buffer.len()) Ok(buffer.len())
} }
/// Close the file `number` /// Close the file `number`
fn close(&mut self, _file: usize) -> Result<()> { fn close(&mut self, _file: Fd) -> Result<()> {
Ok(()) Ok(())
} }
} }

View file

@ -17,8 +17,11 @@ use syscall::Result;
use self::debug::DebugScheme; use self::debug::DebugScheme;
pub use self::fd::Fd;
/// Debug scheme /// Debug scheme
pub mod debug; pub mod debug;
mod fd;
/// Scheme list type /// Scheme list type
pub type SchemeList = BTreeMap<Box<[u8]>, Arc<Mutex<Box<Scheme + Send>>>>; pub type SchemeList = BTreeMap<Box<[u8]>, Arc<Mutex<Box<Scheme + Send>>>>;
@ -50,16 +53,16 @@ pub trait Scheme {
/// Returns a file descriptor or an error /// Returns a file descriptor or an error
fn open(&mut self, path: &[u8], flags: usize) -> Result<usize>; fn open(&mut self, path: &[u8], flags: usize) -> Result<usize>;
/// Read the file `number` into the `buffer` /// Read from some file descriptor into the `buffer`
/// ///
/// Returns the number of bytes read /// Returns the number of bytes read
fn read(&mut self, file: usize, buffer: &mut [u8]) -> Result<usize>; fn read(&mut self, fd: Fd, buffer: &mut [u8]) -> Result<usize>;
/// Write the `buffer` to the `file` /// Write the `buffer` to the file descriptor
/// ///
/// Returns the number of bytes written /// Returns the number of bytes written
fn write(&mut self, file: usize, buffer: &[u8]) -> Result<usize>; fn write(&mut self, fd: Fd, buffer: &[u8]) -> Result<usize>;
/// Close the file `number` /// Close the file descriptor
fn close(&mut self, file: usize) -> Result<()>; fn close(&mut self, fd: Fd) -> Result<()>;
} }