redox/kernel/context/mod.rs

71 lines
1.8 KiB
Rust
Raw Normal View History

2016-08-15 02:16:56 +02:00
//! Context management
2016-08-18 16:30:45 +02:00
use alloc::arc::Arc;
use collections::{BTreeMap, Vec};
use spin::{Once, RwLock, RwLockReadGuard, RwLockWriteGuard};
2016-08-15 02:16:56 +02:00
/// File operations
pub mod file;
2016-08-18 16:30:45 +02:00
/// Maximum context files
pub const CONTEXT_MAX_FILES: usize = 65536;
/// Context ID
pub type ContextId = u16;
/// Context list type
pub type ContextList = BTreeMap<ContextId, Arc<RwLock<Context>>>;
/// Contexts list
static CONTEXTS: Once<RwLock<ContextList>> = Once::new();
/// Initialize contexts, called if needed
fn init_contexts() -> RwLock<ContextList> {
let mut map: ContextList = BTreeMap::new();
map.insert(0, Arc::new(RwLock::new(Context::new())));
RwLock::new(map)
}
/// Get the global schemes list, const
pub fn contexts() -> RwLockReadGuard<'static, ContextList> {
CONTEXTS.call_once(init_contexts).read()
}
/// Get the global schemes list, mutable
pub fn contexts_mut() -> RwLockWriteGuard<'static, ContextList> {
CONTEXTS.call_once(init_contexts).write()
}
2016-08-15 02:16:56 +02:00
/// A context, which identifies either a process or a thread
2016-08-18 16:30:45 +02:00
#[derive(Clone, Debug)]
2016-08-15 02:16:56 +02:00
pub struct Context {
/// The open files in the scheme
2016-08-18 16:30:45 +02:00
pub files: Vec<Option<file::File>>
2016-08-15 02:16:56 +02:00
}
impl Context {
2016-08-18 16:30:45 +02:00
pub fn new() -> Context {
2016-08-15 02:16:56 +02:00
Context {
2016-08-18 16:30:45 +02:00
files: Vec::new()
2016-08-15 02:16:56 +02:00
}
}
/// Add a file to the lowest available slot.
/// Return the file descriptor number or None if no slot was found
pub fn add_file(&mut self, file: file::File) -> Option<usize> {
for (i, mut file_option) in self.files.iter_mut().enumerate() {
if file_option.is_none() {
*file_option = Some(file);
return Some(i);
}
}
2016-08-18 16:30:45 +02:00
let len = self.files.len();
if len < CONTEXT_MAX_FILES {
self.files.push(Some(file));
Some(len)
} else {
None
}
2016-08-15 02:16:56 +02:00
}
}