Skip to main content

dope_runtime/executor/
ctx.rs

1use std::{future::Future, ptr::NonNull};
2
3use dope_core::driver::{CompletionBackend, CurrentSlot};
4
5use super::JoinHandle;
6use super::TimerHeap;
7use super::task_queue::TaskQueue;
8
9#[derive(Clone, Copy)]
10pub struct RuntimeHandles {
11    pub(crate) exec: NonNull<TaskQueue>,
12    pub(crate) timers: NonNull<TimerHeap>,
13}
14
15pub struct RuntimeCtx<B: CompletionBackend> {
16    slot: CurrentSlot<B>,
17}
18
19impl<B: CompletionBackend> Clone for RuntimeCtx<B> {
20    fn clone(&self) -> Self {
21        *self
22    }
23}
24
25impl<B: CompletionBackend> Copy for RuntimeCtx<B> {}
26
27impl<B: CompletionBackend> RuntimeCtx<B> {
28    #[inline(always)]
29    pub(super) fn from_slot(slot: CurrentSlot<B>) -> Self {
30        Self { slot }
31    }
32
33    #[inline(always)]
34    pub fn driver(&self) -> &B::Driver {
35        unsafe { self.slot.driver().as_ref() }
36    }
37
38    #[inline(always)]
39    #[allow(clippy::mut_from_ref)]
40    pub fn driver_mut(&self) -> &mut B::Driver {
41        unsafe { &mut *self.slot.driver().as_ptr() }
42    }
43
44    #[inline(always)]
45    fn extra(&self) -> &RuntimeHandles {
46        unsafe { &*(self.slot.extra() as *const RuntimeHandles) }
47    }
48
49    pub fn spawn<F>(&self, fut: F) -> JoinHandle<F::Output>
50    where
51        F: Future + 'static,
52        F::Output: 'static,
53    {
54        unsafe { self.extra().exec.as_ref() }.spawn(fut)
55    }
56
57    pub(crate) fn timers(&self) -> NonNull<TimerHeap> {
58        self.extra().timers
59    }
60}