Skip to main content

dope_core/driver/route/token/
mod.rs

1use core::{hash, marker, mem, num};
2
3use o3::base::marker::ThreadBound;
4
5use crate::driver::{
6    route,
7    route::{Epoch, Route, indices, indices::SlotIndex, kind, table},
8};
9
10pub(super) mod control;
11
12const fn metadata(route: Route, kind: kind::Code, slot: SlotIndex) -> u64 {
13    ((route.raw() as u64) << route::SHIFT)
14        | ((kind.raw() as u64) << route::KIND_SHIFT)
15        | slot.raw() as u64
16}
17
18#[derive(Clone, Copy, PartialEq, Eq, Debug)]
19#[repr(C)]
20pub struct Token {
21    pub(super) epoch: num::NonZero<u64>,
22    pub(super) thread: ThreadBound,
23    pub(super) meta: u64,
24}
25
26impl hash::Hash for Token {
27    fn hash<H: hash::Hasher>(&self, state: &mut H) {
28        hash::Hash::hash(&self.epoch.get(), state);
29        hash::Hash::hash(&self.meta, state);
30    }
31}
32
33impl Token {
34    pub(crate) const fn from_flight(
35        epoch: route::Epoch,
36        slot: indices::SlotIndex,
37        kind: kind::Code,
38        route: route::Route,
39    ) -> Self {
40        let (epoch, thread) = epoch.into_parts();
41        Self {
42            epoch,
43            thread,
44            meta: metadata(route, kind, slot),
45        }
46    }
47
48    pub const fn route(self) -> route::Route {
49        route::Route::from_bits((self.meta >> route::SHIFT) as u8)
50    }
51
52    pub const fn kind(self) -> Result<kind::Kind, kind::Unknown> {
53        let raw = self.kind_raw();
54        match raw {
55            0 => Ok(kind::Kind::Unspecified),
56            1 => Ok(kind::Kind::Accept),
57            2 => Ok(kind::Kind::Recv),
58            3 => Ok(kind::Kind::Send),
59            5 => Ok(kind::Kind::Socket),
60            6 => Ok(kind::Kind::Connect),
61            7 => Ok(kind::Kind::Shutdown),
62            11 => Ok(kind::Kind::Open),
63            19 => Ok(kind::Kind::OneShot),
64            21 => Ok(kind::Kind::TaskQueue),
65            25 => Ok(kind::Kind::Tuning),
66            28 => Ok(kind::Kind::Write),
67            29 => Ok(kind::Kind::Sync),
68            30 => Ok(kind::Kind::Read),
69            unknown => Err(kind::Unknown::new(unknown)),
70        }
71    }
72
73    pub(crate) const fn kind_raw(self) -> u8 {
74        (self.meta >> route::KIND_SHIFT) as u8
75    }
76
77    pub const fn slot(self) -> indices::SlotIndex {
78        SlotIndex::from_field(self.meta)
79    }
80
81    pub const fn epoch(self) -> route::Epoch {
82        Epoch::new(self.epoch, self.thread)
83    }
84
85    pub(super) const fn encode<Tag, C>(parts: table::Parts<Tag>) -> Self
86    where
87        Tag: route::Select<C>,
88        C: kind::Class,
89    {
90        Self::from_flight(parts.epoch(), parts.slot(), C::CODE, Tag::ROUTE)
91    }
92
93    #[doc(hidden)]
94    pub const fn from_parts<Tag: route::Tag>(parts: table::Parts<Tag>) -> Self {
95        Self::from_flight(
96            parts.epoch(),
97            parts.slot(),
98            <Tag::Class as kind::Class>::CODE,
99            Tag::ROUTE,
100        )
101    }
102}
103
104#[derive(Clone, Copy, Debug)]
105pub(crate) struct Brand<'d, Tag> {
106    pub(super) driver: crate::driver::Invariant<'d>,
107    tag: marker::PhantomData<*mut Tag>,
108}
109
110impl<'d, Tag> Brand<'d, Tag> {
111    pub(crate) const fn new(driver: crate::driver::Invariant<'d>) -> Self {
112        Self {
113            driver,
114            tag: marker::PhantomData,
115        }
116    }
117}
118
119const _: () = {
120    assert!(mem::size_of::<Token>() == 2 * mem::size_of::<u64>());
121    assert!(mem::size_of::<Option<Token>>() == mem::size_of::<Token>());
122};