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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use crate::{
bindings,
rtos::Task,
util::{owner::Owner, shared_set::*},
};
pub struct Event(SharedSet<Task>);
impl Event {
#[inline]
pub fn new() -> Self {
Event(SharedSet::new())
}
pub fn notify(&self) {
for t in self.0.iter() {
unsafe { bindings::task_notify(t.0) };
}
}
#[inline]
pub fn task_count(&self) -> usize {
self.0.len()
}
}
impl Default for Event {
fn default() -> Self {
Self::new()
}
}
pub struct EventHandle<O: Owner<Event>>(Option<SharedSetHandle<Task, EventHandleOwner<O>>>);
impl<O: Owner<Event>> EventHandle<O> {
pub fn is_done(&self) -> bool {
self.with_owner(|_| ()).is_none()
}
pub fn with_owner<U>(&self, f: impl FnOnce(&O) -> U) -> Option<U> {
Some(f(&self.0.as_ref()?.owner().0))
}
pub fn clear(&mut self) {
self.0.take();
}
}
struct EventHandleOwner<O: Owner<Event>>(O);
impl<O: Owner<Event>> Owner<SharedSet<Task>> for EventHandleOwner<O> {
#[inline]
fn with<U>(&self, f: impl FnOnce(&mut SharedSet<Task>) -> U) -> Option<U> {
self.0.with(|e| f(&mut e.0))
}
}
#[inline]
pub fn handle_event<O: Owner<Event>>(owner: O) -> EventHandle<O> {
EventHandle(insert(EventHandleOwner(owner), Task::current()))
}