Converting pids into a new type ContextId
Keeping pid (and file descriptor, and scheme id, ...) as usize is a footgun. Let's remove it.
This commit is contained in:
parent
d16515ea12
commit
37a34ab7f7
10 changed files with 52 additions and 44 deletions
|
@ -9,6 +9,10 @@ use context::memory::{Grant, Memory, SharedMemory, Tls};
|
|||
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 +26,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 +46,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 +79,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,
|
||||
|
|
|
@ -17,7 +17,7 @@ pub struct RegKey {
|
|||
|
||||
#[derive(PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct ProcessKey {
|
||||
context_id: usize,
|
||||
context_id: context::context::ContextId,
|
||||
fd: usize,
|
||||
}
|
||||
|
||||
|
|
|
@ -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