evering_threaded/
op.rs

1use std::time::Duration;
2
3use evering::driver::OpId;
4use evering::op::{Cancellation, Completable};
5
6use crate::runtime::RuntimeHandle;
7
8#[derive(Debug)]
9pub(crate) struct Sqe {
10    pub id: OpId,
11    pub data: SqeData,
12}
13
14#[derive(Debug)]
15pub(crate) struct Rqe {
16    pub id: OpId,
17    pub data: RqeData,
18}
19
20#[derive(Debug)]
21pub(crate) enum SqeData {
22    Exit,
23    Ping { delay: Duration },
24}
25
26#[derive(Debug)]
27pub(crate) enum RqeData {
28    Exited,
29    Pong { token: u64 },
30}
31
32pub(crate) struct Ping;
33
34unsafe impl Completable for Ping {
35    type Output = u64;
36    type Driver = RuntimeHandle;
37    fn complete(self, _drv: &RuntimeHandle, payload: RqeData) -> Self::Output {
38        let RqeData::Pong { token } = payload else {
39            unreachable!()
40        };
41        token
42    }
43    fn cancel(self, _drv: &RuntimeHandle) -> Cancellation {
44        Cancellation::noop()
45    }
46}
47
48pub async fn ping(delay: Duration) -> u64 {
49    RuntimeHandle::submit(Ping, |id, _| Sqe {
50        id,
51        data: SqeData::Ping { delay },
52    })
53    .await
54}
55
56pub(crate) struct Exit;
57
58unsafe impl Completable for Exit {
59    type Output = ();
60    type Driver = RuntimeHandle;
61    fn complete(self, _drv: &RuntimeHandle, payload: RqeData) -> Self::Output {
62        let RqeData::Exited = payload else {
63            unreachable!()
64        };
65    }
66    fn cancel(self, _drv: &RuntimeHandle) -> Cancellation {
67        Cancellation::noop()
68    }
69}
70
71pub async fn exit() {
72    RuntimeHandle::submit(Exit, |id, _| Sqe {
73        id,
74        data: SqeData::Exit,
75    })
76    .await
77}