forked from arceos-org/arceos
-
Notifications
You must be signed in to change notification settings - Fork 24
feat(executor): added async task #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
sususu5
wants to merge
8
commits into
Starry-OS:dev
Choose a base branch
from
sususu5:feat-async-executor
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e282dcc
wip
AsakuraMizu 67943f3
feat(executor): added async task
sususu5 b7f8180
fix(executor): improved return value
sususu5 abb0073
fix(cargo): restored Cargo.lock
sususu5 9376630
fix(executor): fixed race condition in run_until_idle
sususu5 d2d68ab
feat(executor): added per-cpu ready queue
sususu5 290b4a3
refactor(executor): improved block_on function and async waker
sususu5 42518ad
feat(executor): added enqueued flag
sususu5 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,205 @@ | ||||||||||||||
| use alloc::{boxed::Box, collections::VecDeque, sync::Arc, task::Wake}; | ||||||||||||||
| use core::{ | ||||||||||||||
| future::Future, | ||||||||||||||
| pin::Pin, | ||||||||||||||
| sync::atomic::{AtomicBool, AtomicUsize, Ordering}, | ||||||||||||||
| task::{Context, Poll, Waker}, | ||||||||||||||
| }; | ||||||||||||||
|
|
||||||||||||||
| use kernel_guard::NoPreemptIrqSave; | ||||||||||||||
| use kspin::SpinNoIrq; | ||||||||||||||
| use lazyinit::LazyInit; | ||||||||||||||
|
|
||||||||||||||
| use crate::{current_run_queue, select_run_queue, TaskId, WeakAxTaskRef}; | ||||||||||||||
|
|
||||||||||||||
| pub struct AxExecutor { | ||||||||||||||
| queue: SpinNoIrq<VecDeque<Arc<AsyncTask>>>, | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| impl AxExecutor { | ||||||||||||||
| pub fn new() -> Self { | ||||||||||||||
| Self { | ||||||||||||||
| queue: SpinNoIrq::new(VecDeque::new()), | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| pub fn add_task(&self, task: Arc<AsyncTask>) { | ||||||||||||||
| self.queue.lock().push_back(task); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| pub fn pop_task(&self) -> Option<Arc<AsyncTask>> { | ||||||||||||||
| self.queue.lock().pop_front() | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| pub fn is_empty(&self) -> bool { | ||||||||||||||
| self.queue.lock().is_empty() | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| impl Default for AxExecutor { | ||||||||||||||
| fn default() -> Self { | ||||||||||||||
| Self::new() | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| #[percpu::def_percpu] | ||||||||||||||
| static READY_QUEUE: LazyInit<Arc<AxExecutor>> = LazyInit::new(); | ||||||||||||||
|
|
||||||||||||||
| #[percpu::def_percpu] | ||||||||||||||
| static WAKE_COUNT: AtomicUsize = AtomicUsize::new(0); | ||||||||||||||
|
|
||||||||||||||
| #[percpu::def_percpu] | ||||||||||||||
| static BLOCKED_TASK: SpinNoIrq<Option<WeakAxTaskRef>> = SpinNoIrq::new(None); | ||||||||||||||
|
|
||||||||||||||
| pub(crate) fn init() { | ||||||||||||||
| READY_QUEUE.with_current(|q| { | ||||||||||||||
| q.init_once(Arc::new(AxExecutor::new())); | ||||||||||||||
| }); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| pub fn set_blocked_task(task: WeakAxTaskRef) { | ||||||||||||||
| BLOCKED_TASK.with_current(|t| { | ||||||||||||||
| *t.lock() = Some(task); | ||||||||||||||
| }); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| pub fn clear_blocked_task() { | ||||||||||||||
| BLOCKED_TASK.with_current(|t| { | ||||||||||||||
| *t.lock() = None; | ||||||||||||||
| }); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| fn wake_blocked_task() { | ||||||||||||||
| BLOCKED_TASK.with_current(|t| { | ||||||||||||||
| if let Some(weak) = t.lock().as_ref() { | ||||||||||||||
| if let Some(task) = weak.upgrade() { | ||||||||||||||
| select_run_queue::<NoPreemptIrqSave>(&task).unblock_task(task, false); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| }); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /// An asynchronous task that wraps a future. | ||||||||||||||
| pub struct AsyncTask { | ||||||||||||||
| id: TaskId, | ||||||||||||||
| future: SpinNoIrq<Pin<Box<dyn Future<Output = ()> + Send + 'static>>>, | ||||||||||||||
| executor: Arc<AxExecutor>, | ||||||||||||||
| enqueued: AtomicBool, | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| impl AsyncTask { | ||||||||||||||
| pub fn new( | ||||||||||||||
| future: impl Future<Output = ()> + Send + 'static, | ||||||||||||||
| executor: Arc<AxExecutor>, | ||||||||||||||
| ) -> Arc<Self> { | ||||||||||||||
| Arc::new(Self { | ||||||||||||||
| id: TaskId::new(), | ||||||||||||||
| future: SpinNoIrq::new(Box::pin(future)), | ||||||||||||||
| executor, | ||||||||||||||
| enqueued: AtomicBool::new(false), | ||||||||||||||
| }) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| pub fn id(&self) -> TaskId { | ||||||||||||||
| self.id | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| pub(crate) fn poll(self: &Arc<Self>) -> Poll<()> { | ||||||||||||||
| self.enqueued.store(false, Ordering::Release); | ||||||||||||||
| let waker = Waker::from(self.clone()); | ||||||||||||||
| let mut cx = Context::from_waker(&waker); | ||||||||||||||
| let mut future = self.future.lock(); | ||||||||||||||
| future.as_mut().poll(&mut cx) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| fn enqueue(self: &Arc<Self>) -> bool { | ||||||||||||||
| if self | ||||||||||||||
| .enqueued | ||||||||||||||
| .compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire) | ||||||||||||||
| .is_ok() | ||||||||||||||
| { | ||||||||||||||
| self.executor.add_task(self.clone()); | ||||||||||||||
| true | ||||||||||||||
| } else { | ||||||||||||||
| false | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| impl Wake for AsyncTask { | ||||||||||||||
| fn wake(self: Arc<Self>) { | ||||||||||||||
| self.wake_by_ref(); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| fn wake_by_ref(self: &Arc<Self>) { | ||||||||||||||
| if self.enqueue() { | ||||||||||||||
| WAKE_COUNT.with_current(|c| c.fetch_add(1, Ordering::Release)); | ||||||||||||||
| wake_blocked_task(); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| pub fn spawn<F>(future: F) | ||||||||||||||
| where | ||||||||||||||
| F: Future<Output = ()> + Send + 'static, | ||||||||||||||
| { | ||||||||||||||
| let executor = READY_QUEUE.with_current(|q| q.clone()); | ||||||||||||||
| let task = AsyncTask::new(future, executor.clone()); | ||||||||||||||
| if task.enqueue() { | ||||||||||||||
| WAKE_COUNT.with_current(|c| c.fetch_add(1, Ordering::Release)); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| pub fn run_once() -> Option<Poll<()>> { | ||||||||||||||
| if let Some(task) = READY_QUEUE.with_current(|q| q.pop_task()) { | ||||||||||||||
| Some(task.poll()) | ||||||||||||||
| } else { | ||||||||||||||
| None | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| pub fn run_for(max_steps: usize) -> bool { | ||||||||||||||
| let mut ran = false; | ||||||||||||||
| for _ in 0..max_steps { | ||||||||||||||
| if run_once().is_none() { | ||||||||||||||
| break; | ||||||||||||||
| } | ||||||||||||||
| ran = true; | ||||||||||||||
| } | ||||||||||||||
| ran | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| pub fn wake_count() -> usize { | ||||||||||||||
| WAKE_COUNT.with_current(|c| c.load(Ordering::Acquire)) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| pub fn is_empty() -> bool { | ||||||||||||||
| READY_QUEUE.with_current(|q| q.is_empty()) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| pub fn run_until_idle() { | ||||||||||||||
|
||||||||||||||
| pub fn run_until_idle() { | |
| pub fn run_until_idle() { | |
| // Number of consecutive spin iterations before yielding the CPU. | |
| // 64 is a small power-of-two chosen as a compromise: it allows short-lived | |
| // bursts of wakeups to be handled without an expensive scheduler yield, | |
| // while still bounding the time spent busy-waiting when the system is idle. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,6 +41,7 @@ cfg_if::cfg_if! { | |
| #[macro_use] | ||
| mod run_queue; | ||
| mod task; | ||
| pub mod executor; | ||
| mod api; | ||
| mod wait_queue; | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This new
find_areamethod is identical to the existingfind_area_mutmethod on line 113 (which also callsself.areas.find(vaddr)). The only difference is thatfind_area_mutreturns a mutable reference. This creates duplicate logic. Consider whether this new method is necessary, or if callers should usefind_area_mutand simply not mutate the result.