2016-08-15 02:16:56 +02:00
|
|
|
//! Context management
|
2016-09-23 00:14:45 +02:00
|
|
|
use alloc::boxed::Box;
|
2016-11-13 15:36:52 +01:00
|
|
|
use core::sync::atomic::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-11-13 15:36:52 +01:00
|
|
|
pub use context::context::ContextId;
|
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;
|
|
|
|
|
2016-09-24 01:54:39 +02:00
|
|
|
/// Event handling
|
|
|
|
pub mod event;
|
|
|
|
|
2016-09-16 18:44:52 +02:00
|
|
|
/// 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
|
2016-11-03 14:36:12 +01:00
|
|
|
pub const CONTEXT_MAX_CONTEXTS: usize = usize::max_value() - 1;
|
2016-08-20 21:43:35 +02:00
|
|
|
|
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]
|
2016-11-13 15:36:52 +01:00
|
|
|
static CONTEXT_ID: context::AtomicContextId = context::AtomicContextId::default();
|
2016-08-20 21:43:35 +02:00
|
|
|
|
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-23 00:14:45 +02:00
|
|
|
let mut fx = unsafe { Box::from_raw(::alloc::heap::allocate(512, 16) as *mut [u8; 512]) };
|
|
|
|
for b in fx.iter_mut() {
|
|
|
|
*b = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
context.arch.set_fx(fx.as_ptr() as usize);
|
|
|
|
context.kfx = Some(fx);
|
2016-09-16 19:10:53 +02:00
|
|
|
context.status = Status::Runnable;
|
2016-08-31 00:23:51 +02:00
|
|
|
context.running = true;
|
2016-10-31 17:49:00 +01:00
|
|
|
context.cpu_id = Some(::cpu_id());
|
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()
|
|
|
|
}
|
2016-09-17 17:23:36 +02:00
|
|
|
|
2016-11-13 15:36:52 +01:00
|
|
|
pub fn context_id() -> context::ContextId {
|
2016-09-17 17:23:36 +02:00
|
|
|
CONTEXT_ID.load(Ordering::SeqCst)
|
|
|
|
}
|