Skip to main content

kernel_api/threading/
meta.rs

1use alloc::sync::Arc;
2use core::sync::atomic::Ordering;
3use core::task::Poll;
4use futures::task::AtomicWaker;
5use log::warn;
6use crate::address_space::{AddressSpace, Kernel};
7use crate::mapping::{Mapping, Stack};
8use crate::syscall::AsyncMap;
9use crate::syscall::handle::HandleMap;
10use crate::threading::{AtomicThreadState, ThreadId, ThreadState};
11
12// todo: decide what we want `pub` here
13#[derive(Debug)]
14pub struct ThreadMeta {
15	pub name: Arc<str>,
16	pub kernel_stack: Mapping<Stack, Kernel>,
17	pub state: AtomicThreadState,
18	pub thread_id: ThreadId,
19	pub address_space: AddressSpace,
20	pub handles: HandleMap,
21	pub async_map: Arc<AsyncMap>,
22	pub join_waiter: AtomicWaker,
23}
24
25impl ThreadMeta {
26	pub async fn join(self: Arc<Self>) -> isize {
27		core::future::poll_fn(|ctx| {
28			self.join_waiter.register(ctx.waker());
29			match self.state.load(Ordering::SeqCst) {
30				ThreadState::Killed(code) => Poll::Ready(code),
31				_ => Poll::Pending,
32			}
33		}).await
34	}
35}
36
37impl Drop for ThreadMeta {
38	fn drop(&mut self) {
39		warn!("todo: clean up memory for thread `{}`", self.name);
40	}
41}