Next Generation WASM Microkernel Operating System
1// Copyright 2025 Jonas Kruckenberg
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8use crate::loom::sync::atomic::{AtomicU64, Ordering};
9use core::fmt;
10
11/// An opaque ID that uniquely identifies a task relative to all other currently
12/// running tasks.
13///
14/// # Notes
15///
16/// - Task IDs are unique relative to other *currently running* tasks. When a
17/// task completes, the same ID may be used for another task.
18/// - Task IDs are *not* sequential, and do not indicate the order in which
19/// tasks are spawned, what runtime a task is spawned on, or any other data.
20#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
21pub struct Id(u64);
22
23impl Id {
24 pub const fn stub() -> Self {
25 Self(0)
26 }
27
28 pub(crate) fn next() -> Self {
29 #[cfg(loom)]
30 crate::loom::lazy_static! {
31 static ref NEXT_ID: AtomicU64 = AtomicU64::new(1);
32 }
33 #[cfg(not(loom))]
34 static NEXT_ID: AtomicU64 = AtomicU64::new(1);
35
36 let id = NEXT_ID.fetch_add(1, Ordering::Relaxed);
37 Self(id)
38 }
39
40 pub(crate) fn as_u64(self) -> u64 {
41 self.0
42 }
43
44 pub fn is_stub(self) -> bool {
45 self.0 == 0
46 }
47}
48
49impl fmt::Display for Id {
50 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51 self.0.fmt(f)
52 }
53}