Commit bc5494a0 authored by Nifou's avatar Nifou

Remove the struct Processor which is useless

parent d789589f
......@@ -35,7 +35,7 @@ use crate::tasking::{
ProcessInfo,
AuxVecItem,
},
processor::PROCESSES,
scheduler::PROCESSES,
};
#[derive(PartialEq, Debug, Clone, Copy)]
......
......@@ -20,7 +20,7 @@ use core::sync::atomic::Ordering;
use super::id::IrqId;
use super::idt::PICS;
use crate::tasking::processor::{FIRST_TIME, PROCESSOR};
use crate::tasking::scheduler::{FIRST_TIME, SCHEDULER};
/// Loop infinitely
pub fn stop() -> ! {
......@@ -92,7 +92,7 @@ pub extern "x86-interrupt" fn timer_interrupt_handler(stack_frame: &mut Interrup
end(IrqId::TIMER);
// Choose the next thread
if let Some(thread) = PROCESSOR.inner().scheduler.choose() {
if let Some(thread) = SCHEDULER.get().choose() {
// Change the stack to use the new context
unsafe { llvm_asm!("mov rsp, rax" : : "{rax}"(thread.kstack) : : "intel", "volatile"); }
......
......@@ -18,7 +18,7 @@
use bitflags::bitflags;
use x86_64::structures::paging::page_table::PageTableFlags as Flags;
use crate::tasking::processor::{PROCESSOR, PROCESSES};
use crate::tasking::scheduler::{PROCESSES, SCHEDULER};
use super::{Syscall, error::{Result, Error}};
bitflags! {
......@@ -78,7 +78,7 @@ impl Syscall {
crate::memory::switch_to_kernel_page_table();
// Get the current thread's process
let current_process = PROCESSOR.inner().scheduler.current().unwrap().process;
let current_process = SCHEDULER.get().current().unwrap().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 current_process = PROCESSOR.inner().scheduler.current().unwrap().process;
let current_process = SCHEDULER.get().current().unwrap().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();
......
......@@ -15,7 +15,7 @@
* along with this program. If not, see https://www.gnu.org/licenses.
*/
//! Syscalls used for tasking (exit, ...)
use crate::tasking::processor::PROCESSOR;
use crate::tasking::scheduler::SCHEDULER;
use super::{Syscall, error::{Result, Error}};
// --- Signals (from libc) ---
......@@ -66,9 +66,9 @@ impl Syscall {
// Use the kernel's page table
crate::memory::switch_to_kernel_page_table();
PROCESSOR.inner().scheduler.remove_current();
SCHEDULER.get().remove_current();
if let Some(thread) = PROCESSOR.inner().scheduler.choose() {
if let Some(thread) = SCHEDULER.get().choose() {
// Change the stack to use the new context
unsafe { llvm_asm!("mov rsp, rax" : : "{rax}"(thread.kstack) : : "intel", "volatile"); }
......@@ -108,9 +108,9 @@ impl Syscall {
// Use the kernel's page table
crate::memory::switch_to_kernel_page_table();
PROCESSOR.inner().scheduler.remove_current();
SCHEDULER.get().remove_current();
if let Some(thread) = PROCESSOR.inner().scheduler.choose() {
if let Some(thread) = SCHEDULER.get().choose() {
// Change the stack to use the new context
unsafe { llvm_asm!("mov rsp, rax" : : "{rax}"(thread.kstack) : : "intel", "volatile"); }
......
......@@ -30,4 +30,3 @@ pub mod thread;
pub mod info;
pub mod algo;
pub mod scheduler;
pub mod processor;
......@@ -23,7 +23,7 @@ use crate::memory;
use super::{
memory::ProcessMemory,
info::ProcessInfo,
processor::{PROCESSES, PROCESSOR},
scheduler::{PROCESSES, SCHEDULER},
thread::{Thread, Tid},
};
......@@ -99,7 +99,7 @@ impl Process {
let (thread, mappings) = Thread::new(process.pid, rip, rsp, cr3, info.args.len(), info.first_arg_addr);
// Insert the new thread
PROCESSOR.inner().scheduler.add(thread.clone());
SCHEDULER.get().add(thread.clone());
// Add the new thread in the process' threads list
processes.get_mut(&process.pid).unwrap().threads.push(thread.tid);
......
/*
* Copyright (C) 2020 Nicolas Fouquet
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* 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,
sync::atomic::AtomicBool,
};
use alloc::{
prelude::v1::Box,
collections::btree_map::BTreeMap,
};
use spin::RwLock;
use super::algo::SchedulerAlgo;
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! {
/// A global array which regroups all processes and their PIDs
pub static ref PROCESSES: RwLock<BTreeMap<Pid, Process>> = RwLock::new(BTreeMap::new());
}
/// Whether or not it is the first time that the processor is running
pub static FIRST_TIME: AtomicBool = AtomicBool::new(true);
/// The inner of the Processor
pub struct ProcessorInner {
/// The scheduler which chooses the next thread
pub scheduler: Box<dyn SchedulerAlgo>,
}
/// 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>,
}
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() }
}
}
impl ProcessorInner {
/// Create a new ProcessorInner
pub fn new(scheduler: Box<dyn SchedulerAlgo>) -> Self {
Self {
scheduler,
}
}
}
......@@ -15,4 +15,45 @@
* along with this program. If not, see https://www.gnu.org/licenses.
*/
//! This directory regroups some alogithms for scheduler
use lazy_static::lazy_static;
use core::{
cell::UnsafeCell,
sync::atomic::AtomicBool,
};
use alloc::{
prelude::v1::Box,
collections::btree_map::BTreeMap,
};
use spin::RwLock;
use super::algo::SchedulerAlgo;
use super::process::{Process, Pid};
use super::scheduler::round_robin::RoundRobinScheduler;
pub mod round_robin;
lazy_static! {
/// A global Processor
pub static ref SCHEDULER: SchedulerCurrentAlgo = SchedulerCurrentAlgo(UnsafeCell::new(Box::new(RoundRobinScheduler::new())));
}
lazy_static! {
/// A global array which regroups all processes and their PIDs
pub static ref PROCESSES: RwLock<BTreeMap<Pid, Process>> = RwLock::new(BTreeMap::new());
}
/// Whether or not it is the first time that the processor is running
pub static FIRST_TIME: AtomicBool = AtomicBool::new(true);
/// The inner of the Processor
pub struct SchedulerCurrentAlgo(pub UnsafeCell<Box<dyn SchedulerAlgo>>);
impl SchedulerCurrentAlgo {
/// Get a mutable reference of the scheduler
pub fn get(&self) -> &mut Box<dyn SchedulerAlgo> {
unsafe { &mut *self.0.get() }
}
}
unsafe impl Sync for SchedulerCurrentAlgo {}
unsafe impl Send for SchedulerCurrentAlgo {}
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