Skip to main content

os/task/
mod.rs

1use alloc::boxed::Box;
2use core::{future::Future, pin::Pin};
3
4pub struct Task {
5    id: TaskId,
6    future: Pin<Box<dyn Future<Output = ()>>>,
7}
8impl Task {
9    pub fn new(future: impl Future<Output = ()> + 'static) -> Task {
10        Task {
11            id: TaskId::new(),
12            future: Box::pin(future),
13        }
14    }
15
16    pub fn id(&self) -> u64 {
17        self.id.as_u64()
18    }
19}
20use core::task::{Context, Poll};
21
22impl Task {
23    fn poll(&mut self, context: &mut Context) -> Poll<()> {
24        self.future.as_mut().poll(context)
25    }
26}
27pub mod keyboard;
28pub mod simple_executor;
29#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
30struct TaskId(u64);
31use core::sync::atomic::{AtomicU64, Ordering};
32
33impl TaskId {
34    fn new() -> Self {
35        static NEXT_ID: AtomicU64 = AtomicU64::new(0);
36        TaskId(NEXT_ID.fetch_add(1, Ordering::Relaxed))
37    }
38
39    fn as_u64(self) -> u64 {
40        self.0
41    }
42}
43
44pub mod executor;
45
46// PTA exercise placeholder — students fill in their thread_create() implementation here
47// STUDENT_CODE_HERE