Compare commits
No commits in common. "957e767dfc30acff5570af8b78ebe2cbbdd9ac02" and "87ba7f2ab4ffd5fb9a056dcf9c19bb711d8f941e" have entirely different histories.
957e767dfc
...
87ba7f2ab4
@ -9,7 +9,6 @@ pub fn gettick() -> u64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub extern "x86-interrupt" fn timer_interrupt_handler(_stack_frame: InterruptStackFrame) {
|
pub extern "x86-interrupt" fn timer_interrupt_handler(_stack_frame: InterruptStackFrame) {
|
||||||
// TODO: thread preemption
|
|
||||||
unsafe {
|
unsafe {
|
||||||
TICKS += 1;
|
TICKS += 1;
|
||||||
PICS.lock()
|
PICS.lock()
|
||||||
|
@ -15,7 +15,6 @@ use futures_util::task::AtomicWaker;
|
|||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
// TODO: put this in a spin mutex instead
|
|
||||||
pub static ref SCHEDULER: AsyncMutex<Scheduler> = AsyncMutex::new(Scheduler::new());
|
pub static ref SCHEDULER: AsyncMutex<Scheduler> = AsyncMutex::new(Scheduler::new());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -24,7 +23,6 @@ pub type Threadt = Arc<RefCell<Thread>>;
|
|||||||
pub const K_THREAD_ID: ThreadId = ThreadId(0); // Kernel main thread identifier
|
pub const K_THREAD_ID: ThreadId = ThreadId(0); // Kernel main thread identifier
|
||||||
|
|
||||||
struct ThreadStream {
|
struct ThreadStream {
|
||||||
// TODO: add the double scheduler queue
|
|
||||||
ids: ArrayQueue<ThreadId>,
|
ids: ArrayQueue<ThreadId>,
|
||||||
waker: AtomicWaker,
|
waker: AtomicWaker,
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use crate::println;
|
use crate::println;
|
||||||
use crate::proc::thread::{resume_k_thread, RUNNING_THREAD};
|
use crate::proc::thread::{resume_k_thread, RUNNING_THREAD};
|
||||||
use crate::proc::scheduler::SCHEDULER;
|
use crate::proc::scheduler::SCHEDULER;
|
||||||
use crate::task::yield_executor::YieldExecutor;
|
use crate::task::executor::EXECUTOR;
|
||||||
use crate::task::Task;
|
use crate::task::Task;
|
||||||
|
|
||||||
pub use ids::*;
|
pub use ids::*;
|
||||||
@ -25,6 +25,8 @@ impl SyscallContext {
|
|||||||
pub async fn run(&mut self) {
|
pub async fn run(&mut self) {
|
||||||
println!("Running async syscall runner for {:?}", self.id);
|
println!("Running async syscall runner for {:?}", self.id);
|
||||||
self.dispatch().await;
|
self.dispatch().await;
|
||||||
|
println!("Syscall {:?} end, unblocking thread", self.id);
|
||||||
|
SCHEDULER.lock().await.unblock(self.thread_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn dispatch(&mut self) {
|
pub async fn dispatch(&mut self) {
|
||||||
@ -49,9 +51,18 @@ pub fn syscall_routine(syscall_id: SyscallId) -> u64 {
|
|||||||
}));
|
}));
|
||||||
|
|
||||||
println!("Spawning async syscall runner");
|
println!("Spawning async syscall runner");
|
||||||
let mut executor = YieldExecutor::new(context.borrow().thread_id);
|
EXECUTOR
|
||||||
executor.spawn(Task::new(syscall_runner(context.clone())));
|
.try_lock()
|
||||||
executor.run();
|
.unwrap()
|
||||||
|
.spawn(Task::new(syscall_runner(context.clone())));
|
||||||
|
|
||||||
|
println!("Blocking thread");
|
||||||
|
SCHEDULER
|
||||||
|
.try_lock()
|
||||||
|
.unwrap()
|
||||||
|
.block(context.borrow().thread_id);
|
||||||
|
println!("Returning to scheduler");
|
||||||
|
resume_k_thread();
|
||||||
|
|
||||||
let res = context.borrow().res;
|
let res = context.borrow().res;
|
||||||
res
|
res
|
||||||
|
@ -1,11 +1,10 @@
|
|||||||
use crate::println;
|
use crate::println;
|
||||||
use crate::proc::scheduler::SCHEDULER;
|
use crate::proc::scheduler::SCHEDULER;
|
||||||
use crate::proc::thread::resume_k_thread;
|
|
||||||
|
|
||||||
use super::SyscallContext;
|
use super::SyscallContext;
|
||||||
|
|
||||||
pub async fn exit(context: &SyscallContext) {
|
pub async fn exit(context: &SyscallContext) {
|
||||||
println!("Running exit(2)");
|
println!("Running exit(2)");
|
||||||
SCHEDULER.lock().await.exit(context.thread_id);
|
let mut scheduler = SCHEDULER.lock().await;
|
||||||
resume_k_thread();
|
scheduler.exit(context.thread_id);
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,6 @@ use core::sync::atomic::{AtomicU64, Ordering};
|
|||||||
use core::task::{Context, Poll};
|
use core::task::{Context, Poll};
|
||||||
|
|
||||||
pub mod executor;
|
pub mod executor;
|
||||||
pub mod yield_executor;
|
|
||||||
pub mod keyboard;
|
pub mod keyboard;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
|
@ -1,119 +0,0 @@
|
|||||||
|
|
||||||
use crate::println;
|
|
||||||
use crate::proc::thread::{ThreadId, resume_k_thread};
|
|
||||||
use crate::proc::scheduler::SCHEDULER;
|
|
||||||
|
|
||||||
use super::{Task, TaskId};
|
|
||||||
|
|
||||||
use alloc::{collections::BTreeMap, sync::Arc, task::Wake};
|
|
||||||
use core::task::{Context, Poll, Waker};
|
|
||||||
use crossbeam_queue::ArrayQueue;
|
|
||||||
|
|
||||||
pub struct YieldExecutor {
|
|
||||||
tasks: BTreeMap<TaskId, Task>,
|
|
||||||
task_queue: Arc<ArrayQueue<TaskId>>,
|
|
||||||
waker_cache: BTreeMap<TaskId, Waker>,
|
|
||||||
thread_id: ThreadId,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl YieldExecutor {
|
|
||||||
pub fn new(thread_id: ThreadId) -> Self {
|
|
||||||
YieldExecutor {
|
|
||||||
tasks: BTreeMap::new(),
|
|
||||||
task_queue: Arc::new(ArrayQueue::new(100)),
|
|
||||||
waker_cache: BTreeMap::new(),
|
|
||||||
thread_id: thread_id,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn run(&mut self) {
|
|
||||||
loop {
|
|
||||||
self.run_ready_tasks();
|
|
||||||
if self.done() {
|
|
||||||
break;
|
|
||||||
} else {
|
|
||||||
println!("Blocking thread");
|
|
||||||
SCHEDULER
|
|
||||||
.try_lock()
|
|
||||||
.unwrap()
|
|
||||||
.block(self.thread_id);
|
|
||||||
println!("Returning to scheduler");
|
|
||||||
resume_k_thread();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn done(&self) -> bool {
|
|
||||||
self.tasks.is_empty()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn spawn(&mut self, task: Task) {
|
|
||||||
let task_id = task.id;
|
|
||||||
if self.tasks.insert(task.id, task).is_some() {
|
|
||||||
panic!("Duplicate task ID");
|
|
||||||
}
|
|
||||||
self.task_queue.push(task_id).expect("Task queue full");
|
|
||||||
}
|
|
||||||
|
|
||||||
fn run_ready_tasks(&mut self) {
|
|
||||||
let Self {
|
|
||||||
tasks,
|
|
||||||
task_queue,
|
|
||||||
waker_cache,
|
|
||||||
thread_id,
|
|
||||||
} = self; // Executor destructuring
|
|
||||||
|
|
||||||
while let Ok(task_id) = task_queue.pop() {
|
|
||||||
let task = match tasks.get_mut(&task_id) {
|
|
||||||
Some(task) => task,
|
|
||||||
None => continue, // Task does not exist anymore
|
|
||||||
};
|
|
||||||
let waker = waker_cache
|
|
||||||
.entry(task_id)
|
|
||||||
.or_insert_with(|| YieldTaskWaker::new(task_id, task_queue.clone(), *thread_id));
|
|
||||||
let mut context = Context::from_waker(waker);
|
|
||||||
match task.poll(&mut context) {
|
|
||||||
Poll::Ready(()) => {
|
|
||||||
// task is done
|
|
||||||
tasks.remove(&task_id);
|
|
||||||
waker_cache.remove(&task_id);
|
|
||||||
}
|
|
||||||
Poll::Pending => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct YieldTaskWaker {
|
|
||||||
task_id: TaskId,
|
|
||||||
task_queue: Arc<ArrayQueue<TaskId>>,
|
|
||||||
thread_id: ThreadId,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl YieldTaskWaker {
|
|
||||||
fn new(task_id: TaskId, task_queue: Arc<ArrayQueue<TaskId>>, thread_id: ThreadId) -> Waker {
|
|
||||||
Waker::from(Arc::new(YieldTaskWaker {
|
|
||||||
task_id,
|
|
||||||
task_queue,
|
|
||||||
thread_id
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn wake_task(&self) {
|
|
||||||
self.task_queue.push(self.task_id).expect("Task queue full");
|
|
||||||
SCHEDULER
|
|
||||||
.try_lock()
|
|
||||||
.unwrap()
|
|
||||||
.unblock(self.thread_id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Wake for YieldTaskWaker {
|
|
||||||
fn wake(self: Arc<Self>) {
|
|
||||||
self.wake_task();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn wake_by_ref(self: &Arc<Self>) {
|
|
||||||
self.wake_task();
|
|
||||||
}
|
|
||||||
}
|
|
@ -11,7 +11,6 @@ use futures_util::task::AtomicWaker;
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct Lock {
|
struct Lock {
|
||||||
lock: Arc<AtomicBool>,
|
lock: Arc<AtomicBool>,
|
||||||
// TODO: Make a queue of wakers
|
|
||||||
waker: Arc<AtomicWaker>,
|
waker: Arc<AtomicWaker>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user