From 3f8deaa28b36b1faa6c9ace2d13f25bb8c6638f3 Mon Sep 17 00:00:00 2001 From: ClawSeven Date: Wed, 15 Feb 2023 17:23:27 +0800 Subject: [PATCH] Reimplement scheduler with intrusive linked list --- src/framework/jinux-frame/Cargo.toml | 1 + src/framework/jinux-frame/src/task/mod.rs | 2 +- src/framework/jinux-frame/src/task/task.rs | 10 ++++++++++ src/services/libs/jinux-std/Cargo.toml | 1 + .../libs/jinux-std/src/process/fifo_scheduler.rs | 8 ++++---- 5 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/framework/jinux-frame/Cargo.toml b/src/framework/jinux-frame/Cargo.toml index 69fe57cc..a19b1e2f 100644 --- a/src/framework/jinux-frame/Cargo.toml +++ b/src/framework/jinux-frame/Cargo.toml @@ -18,6 +18,7 @@ uart_16550 = "0.2.0" pod = {path = "../pod"} pod-derive = {path = "../pod-derive"} acpi= "4.1.1" +intrusive-collections = "0.9.5" [dependencies.lazy_static] version = "1.0" diff --git a/src/framework/jinux-frame/src/task/mod.rs b/src/framework/jinux-frame/src/task/mod.rs index 42407029..fbfd8b22 100644 --- a/src/framework/jinux-frame/src/task/mod.rs +++ b/src/framework/jinux-frame/src/task/mod.rs @@ -11,4 +11,4 @@ pub use self::scheduler::{set_scheduler, Scheduler}; pub(crate) use self::task::context_switch; pub(crate) use self::task::TaskContext; pub(crate) use self::task::SWITCH_TO_USER_SPACE_TASK; -pub use self::task::{Task, TaskStatus}; +pub use self::task::{Task, TaskStatus, TaskAdapter}; diff --git a/src/framework/jinux-frame/src/task/task.rs b/src/framework/jinux-frame/src/task/task.rs index aa49a340..d05adf9c 100644 --- a/src/framework/jinux-frame/src/task/task.rs +++ b/src/framework/jinux-frame/src/task/task.rs @@ -11,6 +11,9 @@ use crate::user::{syscall_switch_to_user_space, trap_switch_to_user_space, UserS use crate::vm::{VmAllocOptions, VmFrameVec}; use crate::{prelude::*, UPSafeCell}; +use intrusive_collections::intrusive_adapter; +use intrusive_collections::LinkedListAtomicLink; + use super::processor::{current_task, schedule}; core::arch::global_asm!(include_str!("switch.S")); @@ -66,6 +69,7 @@ lazy_static! { }, exit_code: usize::MAX, kstack: KernelStack::new(), + link: LinkedListAtomicLink::new(), }; task.task_inner.exclusive_access().task_status = TaskStatus::Runnable; task.task_inner.exclusive_access().ctx.rip = context_switch_to_user_space as usize; @@ -98,8 +102,12 @@ pub struct Task { exit_code: usize, /// kernel stack, note that the top is SyscallFrame/TrapFrame kstack: KernelStack, + link: LinkedListAtomicLink, } +// TaskAdapter struct is implemented for building relationships between doubly linked list and Task struct +intrusive_adapter!(pub TaskAdapter = Arc: Task { link: LinkedListAtomicLink }); + pub(crate) struct TaskInner { pub task_status: TaskStatus, pub ctx: TaskContext, @@ -166,6 +174,7 @@ impl Task { }, exit_code: 0, kstack: KernelStack::new(), + link: LinkedListAtomicLink::new(), }; result.task_inner.exclusive_access().task_status = TaskStatus::Runnable; @@ -210,6 +219,7 @@ impl Task { }, exit_code: 0, kstack: KernelStack::new(), + link: LinkedListAtomicLink::new(), }; result.task_inner.exclusive_access().task_status = TaskStatus::Runnable; diff --git a/src/services/libs/jinux-std/Cargo.toml b/src/services/libs/jinux-std/Cargo.toml index f1a8b7f6..9f24b695 100644 --- a/src/services/libs/jinux-std/Cargo.toml +++ b/src/services/libs/jinux-std/Cargo.toml @@ -17,6 +17,7 @@ jinux-rights-proc = {path="../jinux-rights-proc"} jinux-util = {path="../jinux-util"} virtio-input-decoder = "0.1.4" ascii = { version = "1.1", default-features = false, features = ["alloc"] } +intrusive-collections = "0.9.5" # parse elf file xmas-elf = "0.8.0" diff --git a/src/services/libs/jinux-std/src/process/fifo_scheduler.rs b/src/services/libs/jinux-std/src/process/fifo_scheduler.rs index 21a07e33..820a10a1 100644 --- a/src/services/libs/jinux-std/src/process/fifo_scheduler.rs +++ b/src/services/libs/jinux-std/src/process/fifo_scheduler.rs @@ -1,16 +1,16 @@ use crate::prelude::*; -use jinux_frame::task::{set_scheduler, Scheduler, Task}; +use jinux_frame::task::{set_scheduler, Scheduler, Task, TaskAdapter}; -pub const TASK_INIT_CAPABILITY: usize = 16; +use intrusive_collections::LinkedList; pub struct FifoScheduler { - tasks: Mutex>>, + tasks: Mutex>, } impl FifoScheduler { pub fn new() -> Self { Self { - tasks: Mutex::new(VecDeque::with_capacity(TASK_INIT_CAPABILITY)), + tasks: Mutex::new(LinkedList::new(TaskAdapter::new())), } } }