mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-16 14:27:49 +00:00
Create a basic executor based on crossbeam_queue
This commit is contained in:
33
Cargo.lock
generated
33
Cargo.lock
generated
@@ -9,6 +9,12 @@ dependencies = [
|
||||
"nodrop",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d"
|
||||
|
||||
[[package]]
|
||||
name = "bit_field"
|
||||
version = "0.9.0"
|
||||
@@ -26,6 +32,7 @@ name = "blog_os"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"bootloader",
|
||||
"crossbeam-queue",
|
||||
"lazy_static",
|
||||
"linked_list_allocator",
|
||||
"pc-keyboard",
|
||||
@@ -51,12 +58,38 @@ dependencies = [
|
||||
"rustc_version",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "0.1.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
|
||||
|
||||
[[package]]
|
||||
name = "cpuio"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "22b8e308ccfc5acf3b82f79c0eac444cf6114cb2ac67a230ca6c177210068daa"
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-queue"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c695eeca1e7173472a32221542ae469b3e9aac3a4fc81f7696bcad82029493db"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-utils"
|
||||
version = "0.7.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.4.0"
|
||||
|
||||
@@ -26,6 +26,10 @@ linked_list_allocator = "0.6.4"
|
||||
version = "1.0"
|
||||
features = ["spin_no_std"]
|
||||
|
||||
[dependencies.crossbeam-queue]
|
||||
version = "0.2.1"
|
||||
default-features = false
|
||||
features = ["alloc"]
|
||||
|
||||
[package.metadata.bootimage]
|
||||
test-args = [
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#![feature(alloc_error_handler)]
|
||||
#![feature(const_fn)]
|
||||
#![feature(alloc_layout_extra)]
|
||||
#![feature(wake_trait)]
|
||||
#![feature(const_in_array_repeat_expressions)]
|
||||
#![test_runner(crate::test_runner)]
|
||||
#![reexport_test_harness_main = "test_main"]
|
||||
@@ -18,6 +19,7 @@ pub mod gdt;
|
||||
pub mod interrupts;
|
||||
pub mod memory;
|
||||
pub mod serial;
|
||||
pub mod task;
|
||||
pub mod vga_buffer;
|
||||
|
||||
pub fn init() {
|
||||
|
||||
31
src/main.rs
31
src/main.rs
@@ -51,11 +51,22 @@ fn kernel_main(boot_info: &'static BootInfo) -> ! {
|
||||
Rc::strong_count(&cloned_reference)
|
||||
);
|
||||
|
||||
#[cfg(test)]
|
||||
test_main();
|
||||
use blog_os::task::executor::Executor;
|
||||
|
||||
println!("It did not crash!");
|
||||
blog_os::hlt_loop();
|
||||
let mut executor = Executor::new();
|
||||
let spawner = executor.create_spawner();
|
||||
spawner.spawn(bar());
|
||||
|
||||
spawner.spawn(async {
|
||||
#[cfg(test)]
|
||||
test_main();
|
||||
});
|
||||
|
||||
spawner.spawn(async {
|
||||
println!("It did not crash!");
|
||||
});
|
||||
|
||||
executor.run();
|
||||
}
|
||||
|
||||
/// This function is called on panic.
|
||||
@@ -71,3 +82,15 @@ fn panic(info: &PanicInfo) -> ! {
|
||||
fn panic(info: &PanicInfo) -> ! {
|
||||
blog_os::test_panic_handler(info)
|
||||
}
|
||||
|
||||
async fn test() -> u32 {
|
||||
42
|
||||
}
|
||||
|
||||
async fn foo() -> u32 {
|
||||
test().await * 2
|
||||
}
|
||||
|
||||
async fn bar() {
|
||||
println!("foo result: {}", foo().await);
|
||||
}
|
||||
|
||||
102
src/task/executor.rs
Normal file
102
src/task/executor.rs
Normal file
@@ -0,0 +1,102 @@
|
||||
use alloc::{boxed::Box, collections::BTreeMap, sync::Arc, task::Wake};
|
||||
use core::{
|
||||
future::Future,
|
||||
pin::Pin,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
use crossbeam_queue::SegQueue;
|
||||
|
||||
pub type Task = Pin<Box<dyn Future<Output = ()>>>;
|
||||
type TaskQueue = SegQueue<Task>;
|
||||
type TaskId = usize;
|
||||
type WakeQueue = SegQueue<TaskId>;
|
||||
|
||||
pub struct Executor {
|
||||
task_queue: Arc<TaskQueue>,
|
||||
wake_queue: Arc<WakeQueue>,
|
||||
pending_tasks: BTreeMap<TaskId, Task>,
|
||||
}
|
||||
|
||||
impl Executor {
|
||||
pub fn new() -> Self {
|
||||
Executor {
|
||||
task_queue: Arc::new(TaskQueue::new()),
|
||||
wake_queue: Arc::new(WakeQueue::new()),
|
||||
pending_tasks: BTreeMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_spawner(&self) -> Spawner {
|
||||
Spawner {
|
||||
task_queue: self.task_queue.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run(&mut self) -> ! {
|
||||
loop {
|
||||
// wakeup waiting tasks
|
||||
while let Ok(task_id) = self.wake_queue.pop() {
|
||||
let task = self
|
||||
.pending_tasks
|
||||
.remove(&task_id)
|
||||
.expect("woken task not found in pending_tasks");
|
||||
self.task_queue.push(task);
|
||||
}
|
||||
// run ready tasks
|
||||
while let Ok(mut task) = self.task_queue.pop() {
|
||||
let waker = self.create_waker(&task).into();
|
||||
let mut context = Context::from_waker(&waker);
|
||||
match task.as_mut().poll(&mut context) {
|
||||
Poll::Ready(()) => {} // task done
|
||||
Poll::Pending => {
|
||||
// add task to pending_tasks list and wait for wakeup
|
||||
let task_id = Self::task_id(&task);
|
||||
self.pending_tasks.insert(task_id, task);
|
||||
}
|
||||
}
|
||||
}
|
||||
// wait for next interrupt if there is nothing left to do
|
||||
if self.wake_queue.is_empty() {
|
||||
crate::hlt_loop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn task_id(task: &Task) -> TaskId {
|
||||
let future_ref: &dyn Future<Output = ()> = &*task;
|
||||
future_ref as *const _ as *const () as usize
|
||||
}
|
||||
|
||||
fn create_waker(&self, task: &Task) -> Arc<Waker> {
|
||||
Arc::new(Waker {
|
||||
wake_queue: self.wake_queue.clone(),
|
||||
task_id: Self::task_id(task),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Spawner {
|
||||
task_queue: Arc<TaskQueue>,
|
||||
}
|
||||
|
||||
impl Spawner {
|
||||
pub fn spawn(&self, task: impl Future<Output = ()> + 'static) {
|
||||
self.task_queue.push(Box::pin(task))
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Waker {
|
||||
wake_queue: Arc<WakeQueue>,
|
||||
task_id: TaskId,
|
||||
}
|
||||
|
||||
impl Wake for Waker {
|
||||
fn wake(self: Arc<Self>) {
|
||||
self.wake_by_ref();
|
||||
}
|
||||
|
||||
fn wake_by_ref(self: &Arc<Self>) {
|
||||
self.wake_queue.push(self.task_id);
|
||||
}
|
||||
}
|
||||
1
src/task/mod.rs
Normal file
1
src/task/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod executor;
|
||||
Reference in New Issue
Block a user