Refactor context list
This commit is contained in:
parent
2de2d4cac4
commit
490dd16776
6 changed files with 89 additions and 34 deletions
|
@ -1,22 +1,52 @@
|
|||
//! Context management
|
||||
|
||||
use alloc::arc::Arc;
|
||||
use collections::{BTreeMap, Vec};
|
||||
use spin::{Once, RwLock, RwLockReadGuard, RwLockWriteGuard};
|
||||
|
||||
/// File operations
|
||||
pub mod file;
|
||||
|
||||
/// Context list
|
||||
pub static mut CONTEXT: Context = Context::new();
|
||||
/// 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()
|
||||
}
|
||||
|
||||
/// A context, which identifies either a process or a thread
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Context {
|
||||
/// The open files in the scheme
|
||||
pub files: [Option<file::File>; 32]
|
||||
pub files: Vec<Option<file::File>>
|
||||
}
|
||||
|
||||
impl Context {
|
||||
pub const fn new() -> Context {
|
||||
pub fn new() -> Context {
|
||||
Context {
|
||||
files: [None; 32]
|
||||
files: Vec::new()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -29,6 +59,12 @@ impl Context {
|
|||
return Some(i);
|
||||
}
|
||||
}
|
||||
None
|
||||
let len = self.files.len();
|
||||
if len < CONTEXT_MAX_FILES {
|
||||
self.files.push(Some(file));
|
||||
Some(len)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue