From 9c3d8ee3c9dd7c5a1c6368489a891b0f3db07871 Mon Sep 17 00:00:00 2001 From: yufeng <321353225@qq.com> Date: Fri, 19 Apr 2024 16:05:45 +0800 Subject: [PATCH] feat: Seperate exector from kernel --- Cargo.lock | 1 + Cargo.toml | 2 +- byteos.toml | 13 ++++ crates/executor/Cargo.toml | 13 ---- crates/executor/src/executor.rs | 132 -------------------------------- crates/executor/src/lib.rs | 75 ------------------ crates/executor/src/ops.rs | 31 -------- crates/executor/src/task.rs | 35 --------- crates/executor/src/thread.rs | 8 -- kernel/Cargo.toml | 2 +- 10 files changed, 16 insertions(+), 296 deletions(-) delete mode 100644 crates/executor/Cargo.toml delete mode 100644 crates/executor/src/executor.rs delete mode 100644 crates/executor/src/lib.rs delete mode 100644 crates/executor/src/ops.rs delete mode 100644 crates/executor/src/task.rs delete mode 100644 crates/executor/src/thread.rs diff --git a/Cargo.lock b/Cargo.lock index badec2a3..a21e3b18 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -225,6 +225,7 @@ checksum = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650" [[package]] name = "executor" version = "0.1.0" +source = "git+https://github.com/Byte-OS/executor.git#c845ad7faa5d1849f7bfb000cf77d6b820c54a19" dependencies = [ "arch", "crossbeam-queue", diff --git a/Cargo.toml b/Cargo.toml index e16fc741..f986bb09 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,5 +4,5 @@ debug = true [workspace] -members = ["crates/*", "kernel"] +members = ["kernel"] resolver = "2" diff --git a/byteos.toml b/byteos.toml index b666e6e7..84891164 100644 --- a/byteos.toml +++ b/byteos.toml @@ -6,6 +6,10 @@ run = "make ARCH=riscv64 justrun" board = "qemu" driver = "kvirtio,kgoldfish-rtc,ns16550a" + [bin.riscv64-qemu.env] + HEAP_SIZE = "0x0180_0000" + MOUNT_IMG_PATH = "/home/yufeng/Code/ByteOS/mount.img" + # build for x86_64-qemu [bin.x86_64-qemu] target = "x86_64-unknown-none" @@ -16,6 +20,7 @@ run = "make ARCH=x86_64 justrun" [bin.x86_64-qemu.env] HEAP_SIZE = "0x0200_0000" + MOUNT_IMG_PATH = "/home/yufeng/Code/ByteOS/mount.img" # build for aarch64-qemu [bin.aarch64-qemu] @@ -25,6 +30,10 @@ run = "make ARCH=aarch64 justrun" board = "qemu" driver = "kvirtio,kgoldfish-rtc,ns16550a" + [bin.aarch64-qemu.env] + HEAP_SIZE = "0x0200_0000" + MOUNT_IMG_PATH = "/home/yufeng/Code/ByteOS/mount.img" + # build for loongarch64-qemu [bin.loongarch64-qemu] target = "loongarch64-unknown-none" @@ -32,3 +41,7 @@ run = "make ARCH=loongarch64 justrun" [bin.loongarch64-qemu.configs] board = "qemu" driver = "kramdisk,kgoldfish-rtc,ns16550a" + + [bin.loongarch64-qemu.env] + HEAP_SIZE = "0x0200_0000" + MOUNT_IMG_PATH = "/home/yufeng/Code/ByteOS/mount.img" diff --git a/crates/executor/Cargo.toml b/crates/executor/Cargo.toml deleted file mode 100644 index 4e6cfdbf..00000000 --- a/crates/executor/Cargo.toml +++ /dev/null @@ -1,13 +0,0 @@ -[package] -name = "executor" -version = "0.1.0" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -crossbeam-queue = { version = "0.3.8", default-features = false, features = ["alloc"] } -arch = { git = "https://github.com/Byte-OS/arch.git" } -sync = { git = "https://github.com/Byte-OS/sync.git" } -log = "0.4" -downcast-rs = { version = "1.2.0", default-features = false } diff --git a/crates/executor/src/executor.rs b/crates/executor/src/executor.rs deleted file mode 100644 index de300e05..00000000 --- a/crates/executor/src/executor.rs +++ /dev/null @@ -1,132 +0,0 @@ -use alloc::{ - boxed::Box, - collections::{BTreeMap, VecDeque}, - sync::Arc, - task::Wake, -}; -use core::{ - any::Any, - future::Future, - pin::Pin, - task::{Context, Poll}, -}; -use crossbeam_queue::SegQueue; -use sync::Mutex; - -pub type DowncastTask = dyn Any + Sync + Send + 'static; - -pub trait AsyncTask: Any + Send + Sync { - fn get_task_id(&self) -> TaskId; - fn before_run(&self); - fn as_any(self: Arc) -> Arc; -} - -pub struct TaskFutureItem(pub PinedFuture); - -unsafe impl Send for TaskFutureItem {} -unsafe impl Sync for TaskFutureItem {} - -pub type TaskId = usize; -type PinedFuture = Pin>>; -pub static CURRENT_TASK: Mutex>> = Mutex::new(None); - -pub static FUTURE_LIST: Mutex> = Mutex::new(BTreeMap::new()); -pub static TASK_QUEUE: Mutex>> = Mutex::new(VecDeque::new()); -/// wake queue, not use at current. -pub static WAKE_QUEUE: SegQueue = SegQueue::new(); -pub struct Executor; - -impl Executor { - pub fn new() -> Self { - Executor - } - - pub fn spawn(&mut self, task: Arc) { - TASK_QUEUE.lock().push_back(task) - } - - pub fn run(&mut self) { - loop { - if TASK_QUEUE.lock().len() == 0 { - break; - } - self.run_ready_task(); - self.hlt_if_idle(); - } - } - - fn run_ready_task(&mut self) { - let task = TASK_QUEUE.lock().pop_front(); - if let Some(task) = &task { - task.before_run(); - - *CURRENT_TASK.lock() = Some(task.clone()); - // let waker = self.create_waker(task.as_ref()).into(); - // Create Waker - let waker = Arc::new(Waker { - task_id: task.get_task_id(), - }) - .into(); - let mut context = Context::from_waker(&waker); - - let future = FUTURE_LIST.lock().remove(&task.get_task_id()); - - if let Some(mut future) = future { - match future.0.as_mut().poll(&mut context) { - Poll::Ready(()) => {} // task done - Poll::Pending => TASK_QUEUE.lock().push_back(task.clone()), - } - FUTURE_LIST.lock().insert(task.get_task_id(), future); - } - } - } - - /// Executes the `hlt` instruction if there are no ready tasks - fn hlt_if_idle(&self) { - // let len = TASK_QUEUE.lock().len(); - // if len != 0 { - // arch::wfi(); - // } - - // log::error!("hlt if idle"); - // arch::wfi(); - // log::error!("end"); - // log::error!("hlt if idle end: {}", TASK_QUEUE.lock().len()); - } -} - -pub struct Waker { - task_id: TaskId, -} - -impl Wake for Waker { - fn wake(self: Arc) { - self.wake_by_ref(); - } - - fn wake_by_ref(self: &Arc) { - WAKE_QUEUE.push(self.task_id); - } -} - -/// Alloc a task id. -pub fn task_id_alloc() -> TaskId { - static TASK_ID: Mutex = Mutex::new(0); - let mut task_id = TASK_ID.lock(); - *task_id += 1; - *task_id -} - -#[inline] -pub fn current_task() -> Arc { - CURRENT_TASK.lock().as_ref().map(|x| x.clone()).unwrap() -} - -#[inline] -pub fn current_downcast_task() -> Arc { - CURRENT_TASK - .lock() - .as_ref() - .map(|x| x.clone().as_any()) - .unwrap() -} diff --git a/crates/executor/src/lib.rs b/crates/executor/src/lib.rs deleted file mode 100644 index dec458b1..00000000 --- a/crates/executor/src/lib.rs +++ /dev/null @@ -1,75 +0,0 @@ -#![no_std] -#![feature(extract_if)] - -extern crate alloc; - -mod executor; -mod ops; -pub mod task; -pub mod thread; - -use core::task::Poll; -use core::{future::Future, pin::Pin, task::Context}; - -use alloc::boxed::Box; -pub use executor::*; -pub use ops::*; - -pub struct Select { - inner: Option<(A, B)>, -} - -impl Unpin for Select {} - -pub fn select(future1: A, future2: B) -> Select -where - A: Future + Unpin, - B: Future + Unpin, -{ - Select { - inner: Some((future1, future2)), - } -} - -fn poll_unpin(future: A, cx: &mut Context<'_>) -> Poll { - Box::pin(future).as_mut().poll(cx) -} - -pub enum Either { - /// First branch of the type - Left(/* #[pin] */ A), - /// Second branch of the type - Right(/* #[pin] */ B), -} - -impl Future for Select -where - A: Future + Unpin, - B: Future + Unpin, -{ - type Output = Either<(A::Output, B), (B::Output, A)>; - - fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - /// When compiled with `-C opt-level=z`, this function will help the compiler eliminate the `None` branch, where - /// `Option::unwrap` does not. - #[inline(always)] - fn unwrap_option(value: Option) -> T { - match value { - None => unreachable!(), - Some(value) => value, - } - } - - let (a, b) = self.inner.as_mut().expect("cannot poll Select twice"); - - if let Poll::Ready(val) = poll_unpin(a, cx) { - return Poll::Ready(Either::Left((val, unwrap_option(self.inner.take()).1))); - } - - if let Poll::Ready(val) = poll_unpin(b, cx) { - return Poll::Ready(Either::Right((val, unwrap_option(self.inner.take()).0))); - } - - Poll::Pending - } -} diff --git a/crates/executor/src/ops.rs b/crates/executor/src/ops.rs deleted file mode 100644 index 85c9dfad..00000000 --- a/crates/executor/src/ops.rs +++ /dev/null @@ -1,31 +0,0 @@ -use core::{ - future::Future, - pin::Pin, - task::{Context, Poll}, -}; - -pub struct Yield(bool); - -impl Yield { - pub const fn new() -> Self { - Self(false) - } -} - -impl Future for Yield { - type Output = (); - - fn poll(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll { - match self.0 { - true => Poll::Ready(()), - false => { - self.0 = true; - Poll::Pending - } - } - } -} - -pub async fn yield_now() { - Yield::new().await; -} diff --git a/crates/executor/src/task.rs b/crates/executor/src/task.rs deleted file mode 100644 index df38d25d..00000000 --- a/crates/executor/src/task.rs +++ /dev/null @@ -1,35 +0,0 @@ -use alloc::sync::Arc; -use downcast_rs::{impl_downcast, DowncastSync}; - -use crate::TaskId; - -/// Default is kernel task -pub const TYPE_KERNEL_TASK: u8 = 0; - -// TODO: Use AsyncTask instead of AsyncTask Trait. -#[allow(dead_code)] -pub struct AsyncTask { - /// Task id - task_id: TaskId, - /// Check the task type, help us to handle the situation that - /// kernel task and monolithic task coexistence - task_type: u8, - /// Task extended data - pub extend: Arc, -} - -/// Blank Task Extend. -/// Usually used in the kernel task. -/// But if you want to implement the unikernel -/// You should use another Task Extend -pub struct BlankTaskExtend; - -/// implement task extend to blank -impl TaskExtend for BlankTaskExtend {} - -pub trait TaskExtend: DowncastSync { - /// blank before run - fn before_run(&self) {} -} - -impl_downcast!(sync TaskExtend); diff --git a/crates/executor/src/thread.rs b/crates/executor/src/thread.rs deleted file mode 100644 index e807ba04..00000000 --- a/crates/executor/src/thread.rs +++ /dev/null @@ -1,8 +0,0 @@ -use alloc::sync::Arc; - -use crate::{AsyncTask, TASK_QUEUE}; - -#[inline] -pub fn spawn(task: Arc) { - TASK_QUEUE.lock().push_back(task); -} diff --git a/kernel/Cargo.toml b/kernel/Cargo.toml index bd486dfb..b3d40c56 100644 --- a/kernel/Cargo.toml +++ b/kernel/Cargo.toml @@ -23,7 +23,7 @@ hal = { git = "https://github.com/Byte-OS/hal.git" } arch = { git = "https://github.com/Byte-OS/arch.git" } fs = { git = "https://github.com/Byte-OS/fs.git" } fdt = "0.1.5" -executor = { path = "../crates/executor" } +executor = { git = "https://github.com/Byte-OS/executor.git" } xmas-elf = "0.9.0" sync = { git = "https://github.com/Byte-OS/sync.git" } bitflags = "2.0.2"