From 8c09e0537ecd865ce87482359814428ce2aa0acf Mon Sep 17 00:00:00 2001 From: Yifan Wu Date: Fri, 20 Jan 2023 01:54:13 +0800 Subject: [PATCH] add_task->wakeup_task --- os/src/sync/condvar.rs | 4 ++-- os/src/sync/mutex.rs | 4 ++-- os/src/sync/semaphore.rs | 4 ++-- os/src/task/manager.rs | 9 ++++++++- os/src/task/mod.rs | 2 +- os/src/timer.rs | 4 ++-- 6 files changed, 17 insertions(+), 10 deletions(-) diff --git a/os/src/sync/condvar.rs b/os/src/sync/condvar.rs index 714782c2..b8ad07b7 100644 --- a/os/src/sync/condvar.rs +++ b/os/src/sync/condvar.rs @@ -1,6 +1,6 @@ use crate::sync::{Mutex, UPIntrFreeCell}; use crate::task::{ - add_task, block_current_and_run_next, block_current_task, current_task, TaskContext, + wakeup_task, block_current_and_run_next, block_current_task, current_task, TaskContext, TaskControlBlock, }; use alloc::{collections::VecDeque, sync::Arc}; @@ -27,7 +27,7 @@ impl Condvar { pub fn signal(&self) { let mut inner = self.inner.exclusive_access(); if let Some(task) = inner.wait_queue.pop_front() { - add_task(task); + wakeup_task(task); } } diff --git a/os/src/sync/mutex.rs b/os/src/sync/mutex.rs index 463638b3..2b2db108 100644 --- a/os/src/sync/mutex.rs +++ b/os/src/sync/mutex.rs @@ -1,6 +1,6 @@ use super::UPIntrFreeCell; use crate::task::TaskControlBlock; -use crate::task::{add_task, current_task}; +use crate::task::{wakeup_task, current_task}; use crate::task::{block_current_and_run_next, suspend_current_and_run_next}; use alloc::{collections::VecDeque, sync::Arc}; @@ -80,7 +80,7 @@ impl Mutex for MutexBlocking { let mut mutex_inner = self.inner.exclusive_access(); assert!(mutex_inner.locked); if let Some(waking_task) = mutex_inner.wait_queue.pop_front() { - add_task(waking_task); + wakeup_task(waking_task); } else { mutex_inner.locked = false; } diff --git a/os/src/sync/semaphore.rs b/os/src/sync/semaphore.rs index 354db29f..cb62c201 100644 --- a/os/src/sync/semaphore.rs +++ b/os/src/sync/semaphore.rs @@ -1,5 +1,5 @@ use crate::sync::UPIntrFreeCell; -use crate::task::{add_task, block_current_and_run_next, current_task, TaskControlBlock}; +use crate::task::{wakeup_task, block_current_and_run_next, current_task, TaskControlBlock}; use alloc::{collections::VecDeque, sync::Arc}; pub struct Semaphore { @@ -28,7 +28,7 @@ impl Semaphore { inner.count += 1; if inner.count <= 0 { if let Some(task) = inner.wait_queue.pop_front() { - add_task(task); + wakeup_task(task); } } } diff --git a/os/src/task/manager.rs b/os/src/task/manager.rs index 168ba32e..7672cf53 100644 --- a/os/src/task/manager.rs +++ b/os/src/task/manager.rs @@ -1,4 +1,4 @@ -use super::{ProcessControlBlock, TaskControlBlock}; +use super::{ProcessControlBlock, TaskControlBlock, TaskStatus}; use crate::sync::UPIntrFreeCell; use alloc::collections::{BTreeMap, VecDeque}; use alloc::sync::Arc; @@ -34,6 +34,13 @@ pub fn add_task(task: Arc) { TASK_MANAGER.exclusive_access().add(task); } +pub fn wakeup_task(task: Arc) { + let mut task_inner = task.inner_exclusive_access(); + task_inner.task_status = TaskStatus::Ready; + drop(task_inner); + add_task(task); +} + pub fn fetch_task() -> Option> { TASK_MANAGER.exclusive_access().fetch() } diff --git a/os/src/task/mod.rs b/os/src/task/mod.rs index 9ed25ae6..c8354805 100644 --- a/os/src/task/mod.rs +++ b/os/src/task/mod.rs @@ -18,7 +18,7 @@ use switch::__switch; pub use context::TaskContext; pub use id::{kstack_alloc, pid_alloc, KernelStack, PidHandle, IDLE_PID}; -pub use manager::{add_task, pid2process, remove_from_pid2process}; +pub use manager::{add_task, wakeup_task, pid2process, remove_from_pid2process}; pub use processor::{ current_kstack_top, current_process, current_task, current_trap_cx, current_trap_cx_user_va, current_user_token, run_tasks, schedule, take_current_task, diff --git a/os/src/timer.rs b/os/src/timer.rs index 7be2d012..83c969d4 100644 --- a/os/src/timer.rs +++ b/os/src/timer.rs @@ -3,7 +3,7 @@ use core::cmp::Ordering; use crate::config::CLOCK_FREQ; use crate::sbi::set_timer; use crate::sync::UPIntrFreeCell; -use crate::task::{add_task, TaskControlBlock}; +use crate::task::{wakeup_task, TaskControlBlock}; use alloc::collections::BinaryHeap; use alloc::sync::Arc; use lazy_static::*; @@ -64,7 +64,7 @@ pub fn check_timer() { TIMERS.exclusive_session(|timers| { while let Some(timer) = timers.peek() { if timer.expire_ms <= current_ms { - add_task(Arc::clone(&timer.task)); + wakeup_task(Arc::clone(&timer.task)); timers.pop(); } else { break;