Skip to main content

kernel_api/threading/
mod.rs

1//! Provides an interface to the scheduler and threading subsystem
2
3mod state;
4
5use core::borrow::Borrow;
6use core::ptr::NonNull;
7pub use state::*;
8use crate::ptr::TaggedNonNull;
9
10type TaskRefInner = TaggedNonNull<u64>;
11
12/// A cheap to copy handle to a task.
13#[derive(Copy, Clone, Debug, Ord, PartialOrd, PartialEq, Eq, Hash)]
14pub struct TaskRef {
15	tagged_ref: TaskRefInner,
16}
17
18// SAFETY: constructor only allows pointers to `Sync` types so `Send` references are sound
19unsafe impl Send for TaskRef {}
20// SAFETY: constructor only allows pointers to `Sync` types so `Sync` references are sound
21unsafe impl Sync for TaskRef {}
22
23impl TaskRef {
24	pub const MAX_GENERATION: usize = (1 << TaskRefInner::TAG_HIGH_BITS) - 1;
25
26	#[doc(hidden)]
27	pub unsafe fn new<T: Sync>(r: &'static T, generation: usize) -> Self {
28		Self {
29			tagged_ref: TaskRefInner::new(
30				NonNull::from_ref(r).cast(),
31				generation << TaskRefInner::TAG_LOW_BITS,
32			),
33		}
34	}
35
36	#[doc(hidden)]
37	pub unsafe fn from_raw(ptr: TaskRefInner) -> Self {
38		Self {
39			tagged_ref: ptr,
40		}
41	}
42
43	#[doc(hidden)]
44	pub fn tag_raw(self) -> usize {
45		self.tagged_ref.tag()
46	}
47
48	pub fn as_ptr(self) -> NonNull<u64> { self.tagged_ref.as_ptr() }
49	pub fn generation(self) -> usize { self.tagged_ref.tag() >> TaskRefInner::TAG_LOW_BITS }
50	pub fn addr_eq(self, other: TaskRef) -> bool {
51		TaskRefInner::addr_eq(&self.tagged_ref, &other.tagged_ref)
52	}
53
54	pub fn into_raw(self) -> TaggedNonNull<u64> { self.tagged_ref }
55}