Commit 3a673ddc authored by Nifou's avatar Nifou

Create the structures `Pid` and `Tid` and remove all warnings

parent 7d998171
......@@ -78,7 +78,7 @@ impl Syscall {
crate::memory::switch_to_kernel_page_table();
// Get the current thread's process
let mut current_process = PROCESSOR.inner().scheduler.current().process;
let current_process = PROCESSOR.inner().scheduler.current().process;
processes.get_mut(&current_process).unwrap().memory.map(0x300000, 0x300000 + len as u64, Flags::from(prot), Flags::PRESENT);
processes.get_mut(&current_process).unwrap().memory.switch_page_table();
......@@ -106,7 +106,7 @@ impl Syscall {
crate::memory::switch_to_kernel_page_table();
// Get the current thread's process
let mut current_process = PROCESSOR.inner().scheduler.current().process;
let current_process = PROCESSOR.inner().scheduler.current().process;
processes.get_mut(&current_process).unwrap().memory.map(0x10000000, 0x10004000, Flags::PRESENT | Flags::WRITABLE, Flags::PRESENT);
processes.get_mut(&current_process).unwrap().memory.switch_page_table();
......
......@@ -14,6 +14,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses.
*/
//! A trait which make the addition of other scheduler algorithms easier
use super::thread::Thread;
/// A trait which defines the common functions used in a scheduler algorithm
......
......@@ -32,7 +32,8 @@ pub const FX_AREA_SIZE: usize = 512;
/// The context of a process (its registers and its stack).
pub struct Context {
regs: Registers,
pub sse_state: u64,
sse_state: u64,
}
impl Context {
......
......@@ -14,6 +14,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses.
*/
//! Definition of a Process which regroups a set of resources (memory, ...)
use core::sync::atomic::{AtomicUsize, Ordering};
use alloc::prelude::v1::Vec;
use x86_64::structures::paging::page_table::PageTableFlags as Flags;
......@@ -23,7 +24,7 @@ use super::{
memory::ProcessMemory,
info::ProcessInfo,
processor::{PROCESSES, PROCESSOR},
thread::Thread,
thread::{Thread, Tid},
};
/// The address of the user stack
......@@ -34,6 +35,10 @@ pub const STACK_SIZE: usize = 65536;
static NEXT_PID: AtomicUsize = AtomicUsize::new(0);
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
/// A process ID
pub struct Pid(usize);
#[derive(PartialEq, Debug, Clone, Copy)]
/// The state of the process (killed or alive)
pub enum ProcessState {
......@@ -48,7 +53,7 @@ pub enum ProcessState {
/// A process which regroups resources and more
pub struct Process {
/// The process ID
pub pid: usize,
pub pid: Pid,
/// The state of this process
pub state: ProcessState,
......@@ -60,12 +65,12 @@ pub struct Process {
pub info: ProcessInfo,
/// Threads of the process
pub threads: Vec<usize>,
pub threads: Vec<Tid>,
}
impl Process {
/// Create a new process
pub fn new(rip: u64, mut info: ProcessInfo) -> usize {
pub fn new(rip: u64, mut info: ProcessInfo) -> Pid {
// We can use a static address for the stack because the content of it will be switched
// with the switch of page tables
let mut processes = PROCESSES.write();
......@@ -73,7 +78,7 @@ impl Process {
let rsp = info.push_at(STACK_ADDR as usize) as u64;
let mut process = Self {
pid: NEXT_PID.fetch_add(1, Ordering::Relaxed),
pid: Pid(NEXT_PID.fetch_add(1, Ordering::Relaxed)),
state: ProcessState::Alive,
memory: ProcessMemory::new(),
info,
......
......@@ -14,6 +14,11 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses.
*/
//! This file provides a global PROCESSOR which can be used in the kernel
//!
//! To add a new type of scheduler algorithm, you just need to create a new file in the `scheduler`
//! directory, to implement the trait `SchedulerAlgo` on a struct and to create a feature to switch
//! between your scheduler algorithm and others.
use lazy_static::lazy_static;
use core::cell::UnsafeCell;
use alloc::{
......@@ -23,26 +28,32 @@ use alloc::{
use spin::RwLock;
use super::algo::SchedulerAlgo;
use super::process::Process;
use super::process::{Process, Pid};
use super::scheduler::round_robin::RoundRobinScheduler;
lazy_static! {
/// A global Processor
pub static ref PROCESSOR: Processor = Processor::new(Box::new(RoundRobinScheduler::new()));
}
lazy_static! {
pub static ref PROCESSES: RwLock<BTreeMap<usize, Process>> = RwLock::new(BTreeMap::new());
/// A global array which regroups all processes and their PIDs
pub static ref PROCESSES: RwLock<BTreeMap<Pid, Process>> = RwLock::new(BTreeMap::new());
}
/// The inner of the Processor
pub struct ProcessorInner {
/// The scheduler which choose the next thread
/// The scheduler which chooses the next thread
pub scheduler: Box<dyn SchedulerAlgo>,
/// Whether or not it is the first time that the processor is running
first_time: bool,
}
/// A Processor which includes a context dispatcher (to change the context of the thread) and a
/// scheduler (to choose the next thread)
pub struct Processor {
/// The inner of the Processor (used to get a mutable reference of the Processor)
pub inner: UnsafeCell<ProcessorInner>,
}
......@@ -50,12 +61,14 @@ unsafe impl Sync for Processor {}
unsafe impl Send for Processor {}
impl Processor {
/// Create a new Processor
pub fn new(scheduler: Box<dyn SchedulerAlgo>) -> Self {
Self {
inner: UnsafeCell::new(ProcessorInner::new(scheduler)),
}
}
/// Get a mutable reference of the inner of the Processor
pub fn inner(&self) -> &mut ProcessorInner {
unsafe { &mut *self.inner.get() }
}
......@@ -168,6 +181,7 @@ impl Processor {
}
impl ProcessorInner {
/// Create a new ProcessorInner
pub fn new(scheduler: Box<dyn SchedulerAlgo>) -> Self {
Self {
scheduler,
......
......@@ -14,4 +14,5 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses.
*/
//! This directory regroups some alogithms for scheduler
pub mod round_robin;
......@@ -14,6 +14,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses.
*/
//! The [Round Robin](https://en.wikipedia.org/wiki/Round-robin_scheduling) scheduling algorithm
use alloc::collections::vec_deque::VecDeque;
use crate::tasking::{
......@@ -21,11 +22,13 @@ use crate::tasking::{
thread::Thread,
};
/// A [Round Robin](https://en.wikipedia.org/wiki/Round-robin_scheduling) sheduler
pub struct RoundRobinScheduler {
threads: VecDeque<Thread>,
}
impl RoundRobinScheduler {
/// Create a new Round Robin scheduler
pub fn new() -> Self {
Self {
threads: VecDeque::new(),
......
......@@ -14,17 +14,25 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses.
*/
//! Definition of a thread which will be chosen (by the scheduler) and executed
use core::sync::atomic::{AtomicUsize, Ordering};
use super::context::Context;
use super::{
context::Context,
process::Pid,
};
static NEXT_TID: AtomicUsize = AtomicUsize::new(0);
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
/// A thread ID
pub struct Tid(usize);
#[derive(Debug, Clone)]
/// A thread which is chosen by the scheduler
pub struct Thread {
/// The thread ID
pub tid: usize,
pub tid: Tid,
/// The context of the thread
pub ctx: Context,
......@@ -33,14 +41,14 @@ pub struct Thread {
pub first_time: bool,
/// The process which have this thread
pub process: usize,
pub process: Pid,
}
impl Thread {
/// Create a new Thread
pub fn new(process: usize, rip: u64, rsp: u64) -> Self {
pub fn new(process: Pid, rip: u64, rsp: u64) -> Self {
Self {
tid: NEXT_TID.fetch_add(1, Ordering::Relaxed),
tid: Tid(NEXT_TID.fetch_add(1, Ordering::Relaxed)),
ctx: Context::new(rip, rsp),
first_time: true,
process,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment