88 lines
2.2 KiB
Rust
88 lines
2.2 KiB
Rust
![]() |
use alloc::boxed::Box;
|
||
|
use collections::Vec;
|
||
|
|
||
|
use arch;
|
||
|
use super::file::File;
|
||
|
use super::memory::Memory;
|
||
|
|
||
|
/// A context, which identifies either a process or a thread
|
||
|
#[derive(Debug)]
|
||
|
pub struct Context {
|
||
|
/// The ID of this context
|
||
|
pub id: usize,
|
||
|
//TODO: Status enum
|
||
|
/// Running or not
|
||
|
pub running: bool,
|
||
|
/// Blocked or not
|
||
|
pub blocked: bool,
|
||
|
/// Exited or not`
|
||
|
pub exited: bool,
|
||
|
/// The architecture specific context
|
||
|
pub arch: arch::context::Context,
|
||
|
/// Kernel stack
|
||
|
pub kstack: Option<Box<[u8]>>,
|
||
|
/// Executable image
|
||
|
pub image: Vec<Memory>,
|
||
|
/// User heap
|
||
|
pub heap: Option<Memory>,
|
||
|
/// User stack
|
||
|
pub stack: Option<Memory>,
|
||
|
/// The open files in the scheme
|
||
|
pub files: Vec<Option<File>>
|
||
|
}
|
||
|
|
||
|
impl Context {
|
||
|
/// Create a new context
|
||
|
pub fn new(id: usize) -> Context {
|
||
|
Context {
|
||
|
id: id,
|
||
|
running: false,
|
||
|
blocked: true,
|
||
|
exited: false,
|
||
|
arch: arch::context::Context::new(),
|
||
|
kstack: None,
|
||
|
image: Vec::new(),
|
||
|
heap: None,
|
||
|
stack: None,
|
||
|
files: Vec::new()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// Add a file to the lowest available slot.
|
||
|
/// Return the file descriptor number or None if no slot was found
|
||
|
pub fn add_file(&mut self, file: File) -> Option<usize> {
|
||
|
for (i, mut file_option) in self.files.iter_mut().enumerate() {
|
||
|
if file_option.is_none() {
|
||
|
*file_option = Some(file);
|
||
|
return Some(i);
|
||
|
}
|
||
|
}
|
||
|
let len = self.files.len();
|
||
|
if len < super::CONTEXT_MAX_FILES {
|
||
|
self.files.push(Some(file));
|
||
|
Some(len)
|
||
|
} else {
|
||
|
None
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// Get a file
|
||
|
pub fn get_file(&self, i: usize) -> Option<File> {
|
||
|
if i < self.files.len() {
|
||
|
self.files[i]
|
||
|
} else {
|
||
|
None
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// Remove a file
|
||
|
// TODO: adjust files vector to smaller size if possible
|
||
|
pub fn remove_file(&mut self, i: usize) -> Option<File> {
|
||
|
if i < self.files.len() {
|
||
|
self.files[i].take()
|
||
|
} else {
|
||
|
None
|
||
|
}
|
||
|
}
|
||
|
}
|