axipi/
event.rs

1use alloc::{boxed::Box, sync::Arc};
2
3/// A callback function that will be called when an [`IpiEvent`] is received and handled.
4pub struct Callback(Box<dyn FnOnce()>);
5
6impl Callback {
7    /// Create a new [`Callback`] with the given function.
8    pub fn new<F: FnOnce() + 'static>(callback: F) -> Self {
9        Self(Box::new(callback))
10    }
11
12    /// Call the callback function.
13    pub fn call(self) {
14        (self.0)()
15    }
16}
17
18impl<T: FnOnce() + 'static> From<T> for Callback {
19    fn from(callback: T) -> Self {
20        Self::new(callback)
21    }
22}
23
24/// A [`Callback`] that can be called multiple times. It's used for multicast IPI events.
25#[derive(Clone)]
26pub struct MulticastCallback(Arc<dyn Fn()>);
27
28impl MulticastCallback {
29    /// Create a new [`MulticastCallback`] with the given function.
30    pub fn new<F: Fn() + 'static>(callback: F) -> Self {
31        Self(Arc::new(callback))
32    }
33
34    /// Convert the [`MulticastCallback`] into a [`Callback`].
35    pub fn into_unicast(self) -> Callback {
36        Callback(Box::new(move || (self.0)()))
37    }
38
39    /// Call the callback function.
40    pub fn call(self) {
41        (self.0)()
42    }
43}
44
45impl<T: Fn() + 'static> From<T> for MulticastCallback {
46    fn from(callback: T) -> Self {
47        Self::new(callback)
48    }
49}
50
51/// An IPI event that is sent from a source CPU to the target CPU.
52pub struct IpiEvent {
53    /// The source CPU ID that sent the IPI event.
54    pub src_cpu_id: usize,
55    /// The callback function that will be called when the IPI event is handled.
56    pub callback: Callback,
57}