forked from limingth/arceos
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlib.rs
45 lines (37 loc) · 1009 Bytes
/
lib.rs
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
#![no_std]
#![feature(allocator_api)]
use alloc::{collections::btree_map::BTreeMap, sync::Arc, vec, vec::Vec};
use events::{EventData, EventHandler, Events};
use lazy_static::lazy_static;
use spinlock::SpinNoIrq;
extern crate alloc;
pub mod events;
lazy_static! {
static ref EVENT_BUS: SpinNoIrq<EventBus> = SpinNoIrq::new(EventBus::new());
}
struct EventBus {
bus: BTreeMap<Events, Vec<Arc<dyn EventHandler>>>,
}
impl EventBus {
fn new() -> Self {
Self {
bus: BTreeMap::new(),
}
}
}
pub fn post_event(event: Events, mut data: EventData) -> bool {
EVENT_BUS
.lock()
.bus
.get(&event)
.map(|handlers| !handlers.iter().any(|handler| !handler.handle(&mut data)))
.unwrap_or(false)
}
pub fn register_handler(event: Events, handler: &Arc<dyn EventHandler>) {
EVENT_BUS
.lock()
.bus
.entry(event)
.and_modify(|v| v.push(handler.clone()))
.or_insert(vec![handler.clone()]);
}