Skip to content

Commit 7c7aee3

Browse files
committed
Document task_runtime module
1 parent 96b079b commit 7c7aee3

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/task_runtime.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! An experimental runtime for an async-await style task system.
2+
13
use crate::mpsc_queue::{PopResult, Queue};
24
use alloc::{
35
collections::BTreeMap,
@@ -14,6 +16,8 @@ use futures::{
1416
task::{LocalSpawn, Poll, Spawn, SpawnError},
1517
};
1618

19+
/// An executor that schedules tasks round-robin, and executes an idle_task
20+
/// if no task is ready to execute.
1721
pub struct Executor {
1822
tasks: BTreeMap<TaskId, Pin<Box<LocalFutureObj<'static, ()>>>>,
1923
woken_tasks: Arc<Queue<TaskId>>,
@@ -35,6 +39,7 @@ impl LocalSpawn for Executor {
3539
}
3640

3741
impl Executor {
42+
/// Creates a new executor.
3843
pub fn new() -> Self {
3944
Executor {
4045
tasks: BTreeMap::new(),
@@ -51,6 +56,9 @@ impl Executor {
5156
self.woken_tasks.push(id);
5257
}
5358

59+
/// Sets the specified task as idle task.
60+
///
61+
/// It will be polled whenever there is no ready-to-run task in the queue.
5462
pub fn set_idle_task<Fut>(&mut self, future: Fut)
5563
where
5664
Fut: Future<Output = !> + 'static,
@@ -59,6 +67,8 @@ impl Executor {
5967
self.idle_task = Some(future_obj);
6068
}
6169

70+
/// Poll all tasks that are ready to run, until no ready tasks exist. Then poll the idle task
71+
/// once and return.
6272
pub fn run(&mut self) {
6373
match self.woken_tasks.pop() {
6474
PopResult::Data(task_id) => {
@@ -119,14 +129,24 @@ impl Wake for NoOpWaker {
119129
fn wake(_arc_self: &Arc<Self>) {}
120130
}
121131

122-
// TODO document, check behavior
132+
/// This stream can be used by tasks that want to run when the CPU is idle.
133+
///
134+
/// It works by alternately returning `Poll::Ready` and `Poll::Pending` from `poll_next`, starting
135+
/// with `Poll::Pending`. When returning `Poll::Pending` it sends the Waker to the
136+
/// `idle_waker_sink` (passed on construction). The idle task polls the other end of this sink and
137+
/// wakes all received tasks when it runs.
138+
// TODO is the behavior correct?
123139
#[derive(Debug, Clone)]
124140
pub struct IdleStream {
125141
idle: bool,
126142
idle_waker_sink: mpsc::UnboundedSender<LocalWaker>,
127143
}
128144

129145
impl IdleStream {
146+
/// Creates a new IdleStream with the passed sending end of an idle stream.
147+
///
148+
/// The idle task should wake the tasks received from the receiving end
149+
/// of the idle stream, thereby waking the tasks on idle.
130150
pub fn new(idle_waker_sink: mpsc::UnboundedSender<LocalWaker>) -> Self {
131151
IdleStream {
132152
idle_waker_sink,

0 commit comments

Comments
 (0)