forked from arceos-org/arceos
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathevent.rs
More file actions
60 lines (50 loc) · 1.62 KB
/
Copy pathevent.rs
File metadata and controls
60 lines (50 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use alloc::{boxed::Box, sync::Arc};
use core::sync::atomic::AtomicBool;
/// A callback function that will be called when an [`IpiEvent`] is received and handled.
pub struct Callback(Box<dyn FnOnce()>);
impl Callback {
/// Create a new [`Callback`] with the given function.
pub fn new<F: FnOnce() + 'static>(callback: F) -> Self {
Self(Box::new(callback))
}
/// Call the callback function.
pub fn call(self) {
(self.0)()
}
}
impl<T: FnOnce() + 'static> From<T> for Callback {
fn from(callback: T) -> Self {
Self::new(callback)
}
}
/// A [`Callback`] that can be called multiple times. It's used for multicast IPI events.
#[derive(Clone)]
pub struct MulticastCallback(Arc<dyn Fn()>);
impl MulticastCallback {
/// Create a new [`MulticastCallback`] with the given function.
pub fn new<F: Fn() + 'static>(callback: F) -> Self {
Self(Arc::new(callback))
}
/// Convert the [`MulticastCallback`] into a [`Callback`].
pub fn into_unicast(self) -> Callback {
Callback(Box::new(move || (self.0)()))
}
/// Call the callback function.
pub fn call(self) {
(self.0)()
}
}
impl<T: Fn() + 'static> From<T> for MulticastCallback {
fn from(callback: T) -> Self {
Self::new(callback)
}
}
/// An IPI event that is sent from a source CPU to the target CPU.
pub struct IpiEvent {
pub name: &'static str,
/// The source CPU ID that sent the IPI event.
pub src_cpu_id: usize,
/// The callback function that will be called when the IPI event is handled.
pub callback: Callback,
pub done: Option<Arc<AtomicBool>>,
}