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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
use core::{fmt, hash};
use super::{
raw, raw_cfg, Cfg, DrainSemaphoreError, GetSemaphoreError, PollSemaphoreError, QueueOrder,
SignalSemaphoreError, WaitSemaphoreError, WaitSemaphoreTimeoutError,
};
use crate::{
time::Duration,
utils::{Init, PhantomInvariant},
};
pub use raw::SemaphoreValue;
define_object! {
#[doc = common_doc_owned_handle!()]
#[doc = include_str!("../common.md")]
pub struct Semaphore<System: _>(System::RawSemaphoreId);
#[doc = include_str!("../common.md")]
pub struct SemaphoreRef<System: raw::KernelSemaphore>(_);
pub type StaticSemaphore<System>;
pub trait SemaphoreHandle {}
pub trait SemaphoreMethods {}
}
impl<System: raw::KernelSemaphore> StaticSemaphore<System> {
pub const fn define() -> SemaphoreDefiner<System> {
SemaphoreDefiner::new()
}
}
#[doc = include_str!("../common.md")]
pub trait SemaphoreMethods: SemaphoreHandle {
#[inline]
fn drain(&self) -> Result<(), DrainSemaphoreError> {
unsafe { <Self::System as raw::KernelSemaphore>::raw_semaphore_drain(self.id()) }
}
#[inline]
fn get(&self) -> Result<SemaphoreValue, GetSemaphoreError> {
unsafe { <Self::System as raw::KernelSemaphore>::raw_semaphore_get(self.id()) }
}
#[inline]
fn signal(&self, count: SemaphoreValue) -> Result<(), SignalSemaphoreError> {
unsafe { <Self::System as raw::KernelSemaphore>::raw_semaphore_signal(self.id(), count) }
}
#[inline]
fn signal_one(&self) -> Result<(), SignalSemaphoreError> {
unsafe { <Self::System as raw::KernelSemaphore>::raw_semaphore_signal_one(self.id()) }
}
#[inline]
fn wait_one(&self) -> Result<(), WaitSemaphoreError> {
unsafe { <Self::System as raw::KernelSemaphore>::raw_semaphore_wait_one(self.id()) }
}
#[inline]
fn wait_one_timeout(&self, timeout: Duration) -> Result<(), WaitSemaphoreTimeoutError> {
unsafe {
<Self::System as raw::KernelSemaphore>::raw_semaphore_wait_one_timeout(
self.id(),
timeout,
)
}
}
#[inline]
fn poll_one(&self) -> Result<(), PollSemaphoreError> {
unsafe { <Self::System as raw::KernelSemaphore>::raw_semaphore_poll_one(self.id()) }
}
}
impl<T: SemaphoreHandle> SemaphoreMethods for T {}
#[must_use = "must call `finish()` to complete registration"]
pub struct SemaphoreDefiner<System: raw::KernelSemaphore> {
_phantom: PhantomInvariant<System>,
initial_value: Option<SemaphoreValue>,
maximum_value: Option<SemaphoreValue>,
queue_order: QueueOrder,
}
impl<System: raw::KernelSemaphore> SemaphoreDefiner<System> {
const fn new() -> Self {
Self {
_phantom: Init::INIT,
initial_value: None,
maximum_value: None,
queue_order: QueueOrder::TaskPriority,
}
}
pub const fn initial(self, initial: SemaphoreValue) -> Self {
assert!(
self.initial_value.is_none(),
"`initial` is already specified"
);
Self {
initial_value: Some(initial),
..self
}
}
pub const fn maximum(self, maximum: SemaphoreValue) -> Self {
assert!(
self.maximum_value.is_none(),
"`maximum` is already specified"
);
Self {
maximum_value: Some(maximum),
..self
}
}
pub const fn queue_order(self, queue_order: QueueOrder) -> Self {
Self {
queue_order,
..self
}
}
pub const fn finish<C: ~const raw_cfg::CfgSemaphore<System = System>>(
self,
c: &mut Cfg<C>,
) -> StaticSemaphore<System> {
let initial_value = self.initial_value.expect("`initial` is not specified");
let maximum_value = self.maximum_value.expect("`maximum` is not specified");
let id = c.raw().semaphore_define(
raw_cfg::SemaphoreDescriptor {
phantom: Init::INIT,
initial: initial_value,
maximum: maximum_value,
queue_order: self.queue_order,
},
(),
);
unsafe { SemaphoreRef::from_id(id) }
}
}