Merge branch 'cap2' of https://github.com/Yoric/redox into Yoric-cap2
This commit is contained in:
commit
297b6e09fc
18 changed files with 254 additions and 108 deletions
|
@ -6,9 +6,14 @@ use spin::Mutex;
|
|||
use arch;
|
||||
use context::file::File;
|
||||
use context::memory::{Grant, Memory, SharedMemory, Tls};
|
||||
use scheme::FileHandle;
|
||||
use syscall::data::Event;
|
||||
use sync::{WaitMap, WaitQueue};
|
||||
|
||||
/// Unique identifier for a context (i.e. `pid`).
|
||||
use ::core::sync::atomic::AtomicUsize;
|
||||
int_like!(ContextId, AtomicContextId, usize, AtomicUsize);
|
||||
|
||||
/// The status of a context - used for scheduling
|
||||
/// See syscall::process::waitpid and the sync module for examples of usage
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
||||
|
@ -22,9 +27,9 @@ pub enum Status {
|
|||
#[derive(Debug)]
|
||||
pub struct Context {
|
||||
/// The ID of this context
|
||||
pub id: usize,
|
||||
pub id: ContextId,
|
||||
/// The ID of the parent context
|
||||
pub ppid: usize,
|
||||
pub ppid: ContextId,
|
||||
/// The real user id
|
||||
pub ruid: u32,
|
||||
/// The real group id
|
||||
|
@ -42,7 +47,7 @@ pub struct Context {
|
|||
/// Context is halting parent
|
||||
pub vfork: bool,
|
||||
/// Context is being waited on
|
||||
pub waitpid: Arc<WaitMap<usize, usize>>,
|
||||
pub waitpid: Arc<WaitMap<ContextId, usize>>,
|
||||
/// Context should wake up at specified time
|
||||
pub wake: Option<(u64, u64)>,
|
||||
/// The architecture specific context
|
||||
|
@ -75,10 +80,10 @@ pub struct Context {
|
|||
|
||||
impl Context {
|
||||
/// Create a new context
|
||||
pub fn new(id: usize) -> Context {
|
||||
pub fn new(id: ContextId) -> Context {
|
||||
Context {
|
||||
id: id,
|
||||
ppid: 0,
|
||||
ppid: ContextId::from(0),
|
||||
ruid: 0,
|
||||
rgid: 0,
|
||||
euid: 0,
|
||||
|
@ -180,28 +185,28 @@ impl Context {
|
|||
|
||||
/// Add a file to the lowest available slot.
|
||||
/// Return the file descriptor number or None if no slot was found
|
||||
pub fn add_file(&self, file: File) -> Option<usize> {
|
||||
pub fn add_file(&self, file: File) -> Option<FileHandle> {
|
||||
let mut files = self.files.lock();
|
||||
for (i, mut file_option) in files.iter_mut().enumerate() {
|
||||
if file_option.is_none() {
|
||||
*file_option = Some(file);
|
||||
return Some(i);
|
||||
return Some(FileHandle::from(i));
|
||||
}
|
||||
}
|
||||
let len = files.len();
|
||||
if len < super::CONTEXT_MAX_FILES {
|
||||
files.push(Some(file));
|
||||
Some(len)
|
||||
Some(FileHandle::from(len))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Get a file
|
||||
pub fn get_file(&self, i: usize) -> Option<File> {
|
||||
pub fn get_file(&self, i: FileHandle) -> Option<File> {
|
||||
let files = self.files.lock();
|
||||
if i < files.len() {
|
||||
files[i]
|
||||
if i.into() < files.len() {
|
||||
files[i.into()]
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
@ -209,10 +214,10 @@ impl Context {
|
|||
|
||||
/// Remove a file
|
||||
// TODO: adjust files vector to smaller size if possible
|
||||
pub fn remove_file(&self, i: usize) -> Option<File> {
|
||||
pub fn remove_file(&self, i: FileHandle) -> Option<File> {
|
||||
let mut files = self.files.lock();
|
||||
if i < files.len() {
|
||||
files[i].take()
|
||||
if i.into() < files.len() {
|
||||
files[i.into()].take()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ use collections::BTreeMap;
|
|||
use spin::{Once, RwLock, RwLockReadGuard, RwLockWriteGuard};
|
||||
|
||||
use context;
|
||||
use scheme::{FileHandle, SchemeId};
|
||||
use sync::WaitQueue;
|
||||
use syscall::data::Event;
|
||||
|
||||
|
@ -10,14 +11,14 @@ type EventList = Weak<WaitQueue<Event>>;
|
|||
|
||||
#[derive(PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct RegKey {
|
||||
scheme_id: usize,
|
||||
scheme_id: SchemeId,
|
||||
event_id: usize,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct ProcessKey {
|
||||
context_id: usize,
|
||||
fd: usize,
|
||||
context_id: context::context::ContextId,
|
||||
fd: FileHandle,
|
||||
}
|
||||
|
||||
type Registry = BTreeMap<RegKey, BTreeMap<ProcessKey, EventList>>;
|
||||
|
@ -39,7 +40,7 @@ pub fn registry_mut() -> RwLockWriteGuard<'static, Registry> {
|
|||
REGISTRY.call_once(init_registry).write()
|
||||
}
|
||||
|
||||
pub fn register(fd: usize, scheme_id: usize, event_id: usize) -> bool {
|
||||
pub fn register(fd: FileHandle, scheme_id: SchemeId, event_id: usize) -> bool {
|
||||
let (context_id, events) = {
|
||||
let contexts = context::contexts();
|
||||
let context_lock = contexts.current().expect("event::register: No context");
|
||||
|
@ -66,7 +67,7 @@ pub fn register(fd: usize, scheme_id: usize, event_id: usize) -> bool {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn unregister(fd: usize, scheme_id: usize, event_id: usize) {
|
||||
pub fn unregister(fd: FileHandle, scheme_id: SchemeId, event_id: usize) {
|
||||
let mut registry = registry_mut();
|
||||
|
||||
let mut remove = false;
|
||||
|
@ -91,7 +92,7 @@ pub fn unregister(fd: usize, scheme_id: usize, event_id: usize) {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn trigger(scheme_id: usize, event_id: usize, flags: usize, data: usize) {
|
||||
pub fn trigger(scheme_id: SchemeId, event_id: usize, flags: usize, data: usize) {
|
||||
let registry = registry();
|
||||
let key = RegKey {
|
||||
scheme_id: scheme_id,
|
||||
|
@ -101,7 +102,7 @@ pub fn trigger(scheme_id: usize, event_id: usize, flags: usize, data: usize) {
|
|||
for entry in event_lists.iter() {
|
||||
if let Some(event_list) = entry.1.upgrade() {
|
||||
event_list.send(Event {
|
||||
id: (entry.0).fd,
|
||||
id: (entry.0).fd.into(),
|
||||
flags: flags,
|
||||
data: data
|
||||
});
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
//! File struct
|
||||
|
||||
use scheme::SchemeId;
|
||||
|
||||
/// A file
|
||||
//TODO: Close on exec
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub struct File {
|
||||
/// The scheme that this file refers to
|
||||
pub scheme: usize,
|
||||
pub scheme: SchemeId,
|
||||
/// The number the scheme uses to refer to this file
|
||||
pub number: usize,
|
||||
/// If events are on, this is the event ID
|
||||
|
|
|
@ -7,11 +7,11 @@ use spin::RwLock;
|
|||
|
||||
use arch;
|
||||
use syscall::error::{Result, Error, EAGAIN};
|
||||
use super::context::Context;
|
||||
use super::context::{Context, ContextId};
|
||||
|
||||
/// Context list type
|
||||
pub struct ContextList {
|
||||
map: BTreeMap<usize, Arc<RwLock<Context>>>,
|
||||
map: BTreeMap<ContextId, Arc<RwLock<Context>>>,
|
||||
next_id: usize
|
||||
}
|
||||
|
||||
|
@ -25,7 +25,7 @@ impl ContextList {
|
|||
}
|
||||
|
||||
/// Get the nth context.
|
||||
pub fn get(&self, id: usize) -> Option<&Arc<RwLock<Context>>> {
|
||||
pub fn get(&self, id: ContextId) -> Option<&Arc<RwLock<Context>>> {
|
||||
self.map.get(&id)
|
||||
}
|
||||
|
||||
|
@ -34,7 +34,7 @@ impl ContextList {
|
|||
self.map.get(&super::CONTEXT_ID.load(Ordering::SeqCst))
|
||||
}
|
||||
|
||||
pub fn iter(&self) -> ::collections::btree_map::Iter<usize, Arc<RwLock<Context>>> {
|
||||
pub fn iter(&self) -> ::collections::btree_map::Iter<ContextId, Arc<RwLock<Context>>> {
|
||||
self.map.iter()
|
||||
}
|
||||
|
||||
|
@ -44,7 +44,7 @@ impl ContextList {
|
|||
self.next_id = 1;
|
||||
}
|
||||
|
||||
while self.map.contains_key(&self.next_id) {
|
||||
while self.map.contains_key(&ContextId::from(self.next_id)) {
|
||||
self.next_id += 1;
|
||||
}
|
||||
|
||||
|
@ -52,7 +52,7 @@ impl ContextList {
|
|||
return Err(Error::new(EAGAIN));
|
||||
}
|
||||
|
||||
let id = self.next_id;
|
||||
let id = ContextId::from(self.next_id);
|
||||
self.next_id += 1;
|
||||
|
||||
assert!(self.map.insert(id, Arc::new(RwLock::new(Context::new(id)))).is_none());
|
||||
|
@ -85,7 +85,7 @@ impl ContextList {
|
|||
Ok(context_lock)
|
||||
}
|
||||
|
||||
pub fn remove(&mut self, id: usize) -> Option<Arc<RwLock<Context>>> {
|
||||
pub fn remove(&mut self, id: ContextId) -> Option<Arc<RwLock<Context>>> {
|
||||
self.map.remove(&id)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
//! Context management
|
||||
use alloc::boxed::Box;
|
||||
use core::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
|
||||
use core::sync::atomic::Ordering;
|
||||
use spin::{Once, RwLock, RwLockReadGuard, RwLockWriteGuard};
|
||||
|
||||
pub use self::context::{Context, Status};
|
||||
pub use self::list::ContextList;
|
||||
pub use self::switch::switch;
|
||||
pub use context::context::ContextId;
|
||||
|
||||
/// Context struct
|
||||
mod context;
|
||||
|
@ -35,7 +36,7 @@ pub const CONTEXT_MAX_FILES: usize = 65536;
|
|||
static CONTEXTS: Once<RwLock<ContextList>> = Once::new();
|
||||
|
||||
#[thread_local]
|
||||
static CONTEXT_ID: AtomicUsize = ATOMIC_USIZE_INIT;
|
||||
static CONTEXT_ID: context::AtomicContextId = context::AtomicContextId::default();
|
||||
|
||||
pub fn init() {
|
||||
let mut contexts = contexts_mut();
|
||||
|
@ -69,6 +70,6 @@ pub fn contexts_mut() -> RwLockWriteGuard<'static, ContextList> {
|
|||
CONTEXTS.call_once(init_contexts).write()
|
||||
}
|
||||
|
||||
pub fn context_id() -> usize {
|
||||
pub fn context_id() -> context::ContextId {
|
||||
CONTEXT_ID.load(Ordering::SeqCst)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue