From d6362da0ac0245b188b4dbac066ca8fdf3ec81f6 Mon Sep 17 00:00:00 2001 From: Yifan Wu Date: Fri, 20 Jan 2023 01:45:20 +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 f96cd915..7ab64f02 100644 --- a/os/src/sync/condvar.rs +++ b/os/src/sync/condvar.rs @@ -1,5 +1,5 @@ use crate::sync::{Mutex, UPSafeCell}; -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 Condvar { @@ -24,7 +24,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 be58f795..59692cf9 100644 --- a/os/src/sync/mutex.rs +++ b/os/src/sync/mutex.rs @@ -1,6 +1,6 @@ use super::UPSafeCell; 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 7f3870f8..07d77c4e 100644 --- a/os/src/sync/semaphore.rs +++ b/os/src/sync/semaphore.rs @@ -1,5 +1,5 @@ use crate::sync::UPSafeCell; -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 bdeda5d2..dbfce483 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::UPSafeCell; use alloc::collections::{BTreeMap, VecDeque}; use alloc::sync::Arc; @@ -42,6 +42,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 remove_task(task: Arc) { TASK_MANAGER.exclusive_access().remove(task); } diff --git a/os/src/task/mod.rs b/os/src/task/mod.rs index a9cb3457..987d13e7 100644 --- a/os/src/task/mod.rs +++ b/os/src/task/mod.rs @@ -19,7 +19,7 @@ use crate::timer::remove_timer; pub use context::TaskContext; pub use id::{kstack_alloc, pid_alloc, KernelStack, PidHandle, IDLE_PID}; -pub use manager::{add_task, remove_task, pid2process, remove_from_pid2process}; +pub use manager::{add_task, wakeup_task, remove_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 720caf76..88ce0236 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::UPSafeCell; -use crate::task::{add_task, TaskControlBlock}; +use crate::task::{wakeup_task, TaskControlBlock}; use alloc::collections::BinaryHeap; use alloc::sync::Arc; use lazy_static::*; @@ -76,7 +76,7 @@ pub fn check_timer() { let mut timers = TIMERS.exclusive_access(); 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;