2016-08-15 02:16:56 +02:00
|
|
|
//! Context management
|
|
|
|
|
2016-08-20 21:43:35 +02:00
|
|
|
use core::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
|
2016-08-18 16:30:45 +02:00
|
|
|
use spin::{Once, RwLock, RwLockReadGuard, RwLockWriteGuard};
|
|
|
|
|
2016-09-16 19:10:53 +02:00
|
|
|
pub use self::context::{Context, Status};
|
2016-09-16 18:44:52 +02:00
|
|
|
pub use self::list::ContextList;
|
|
|
|
pub use self::switch::switch;
|
2016-08-20 21:43:35 +02:00
|
|
|
|
2016-09-16 18:44:52 +02:00
|
|
|
/// Context struct
|
|
|
|
mod context;
|
|
|
|
|
|
|
|
/// Context list
|
|
|
|
mod list;
|
|
|
|
|
|
|
|
/// Context switch function
|
|
|
|
mod switch;
|
|
|
|
|
|
|
|
/// File struct - defines a scheme and a file number
|
2016-08-15 02:16:56 +02:00
|
|
|
pub mod file;
|
|
|
|
|
2016-09-16 18:44:52 +02:00
|
|
|
/// Memory struct - contains a set of pages for a context
|
2016-09-12 05:04:34 +02:00
|
|
|
pub mod memory;
|
|
|
|
|
2016-08-20 21:43:35 +02:00
|
|
|
/// Limit on number of contexts
|
|
|
|
pub const CONTEXT_MAX_CONTEXTS: usize = 65536;
|
|
|
|
|
2016-08-18 16:30:45 +02:00
|
|
|
/// Maximum context files
|
|
|
|
pub const CONTEXT_MAX_FILES: usize = 65536;
|
|
|
|
|
|
|
|
/// Contexts list
|
|
|
|
static CONTEXTS: Once<RwLock<ContextList>> = Once::new();
|
|
|
|
|
2016-08-20 21:43:35 +02:00
|
|
|
#[thread_local]
|
|
|
|
static CONTEXT_ID: AtomicUsize = ATOMIC_USIZE_INIT;
|
|
|
|
|
2016-08-20 22:32:45 +02:00
|
|
|
pub fn init() {
|
|
|
|
let mut contexts = contexts_mut();
|
|
|
|
let context_lock = contexts.new_context().expect("could not initialize first context");
|
2016-08-31 00:23:51 +02:00
|
|
|
let mut context = context_lock.write();
|
2016-09-16 19:10:53 +02:00
|
|
|
context.status = Status::Runnable;
|
2016-08-31 00:23:51 +02:00
|
|
|
context.running = true;
|
2016-08-20 22:32:45 +02:00
|
|
|
CONTEXT_ID.store(context.id, Ordering::SeqCst);
|
|
|
|
}
|
|
|
|
|
2016-08-18 16:30:45 +02:00
|
|
|
/// Initialize contexts, called if needed
|
|
|
|
fn init_contexts() -> RwLock<ContextList> {
|
2016-08-20 21:43:35 +02:00
|
|
|
RwLock::new(ContextList::new())
|
2016-08-18 16:30:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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()
|
|
|
|
}
|