diff options
| author | Dario Nieuwenhuis <[email protected]> | 2024-10-06 18:56:09 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-10-06 18:56:09 +0000 |
| commit | 631fec8d092b247b02d4279b8087cceb49146575 (patch) | |
| tree | 245cfd1a86c011c74f9299b7415be5d3287af59a /embassy-sync/src | |
| parent | 8f273497453d3ca3f297465b67820d4d36705d11 (diff) | |
| parent | 12e6add058b1bbe69660717bdef3d414a04b8b19 (diff) | |
Merge pull request #3393 from sourcebox/sync-additions
embassy-sync: add clear, len, is_empty and is_full functions to zerocopy_channel
Diffstat (limited to 'embassy-sync/src')
| -rw-r--r-- | embassy-sync/src/zerocopy_channel.rs | 91 |
1 files changed, 88 insertions, 3 deletions
diff --git a/embassy-sync/src/zerocopy_channel.rs b/embassy-sync/src/zerocopy_channel.rs index cfce9a571..fabb69bf6 100644 --- a/embassy-sync/src/zerocopy_channel.rs +++ b/embassy-sync/src/zerocopy_channel.rs | |||
| @@ -53,7 +53,7 @@ impl<'a, M: RawMutex, T> Channel<'a, M, T> { | |||
| 53 | buf: buf.as_mut_ptr(), | 53 | buf: buf.as_mut_ptr(), |
| 54 | phantom: PhantomData, | 54 | phantom: PhantomData, |
| 55 | state: Mutex::new(RefCell::new(State { | 55 | state: Mutex::new(RefCell::new(State { |
| 56 | len, | 56 | capacity: len, |
| 57 | front: 0, | 57 | front: 0, |
| 58 | back: 0, | 58 | back: 0, |
| 59 | full: false, | 59 | full: false, |
| @@ -70,6 +70,28 @@ impl<'a, M: RawMutex, T> Channel<'a, M, T> { | |||
| 70 | pub fn split(&mut self) -> (Sender<'_, M, T>, Receiver<'_, M, T>) { | 70 | pub fn split(&mut self) -> (Sender<'_, M, T>, Receiver<'_, M, T>) { |
| 71 | (Sender { channel: self }, Receiver { channel: self }) | 71 | (Sender { channel: self }, Receiver { channel: self }) |
| 72 | } | 72 | } |
| 73 | |||
| 74 | /// Clears all elements in the channel. | ||
| 75 | pub fn clear(&mut self) { | ||
| 76 | self.state.lock(|s| { | ||
| 77 | s.borrow_mut().clear(); | ||
| 78 | }); | ||
| 79 | } | ||
| 80 | |||
| 81 | /// Returns the number of elements currently in the channel. | ||
| 82 | pub fn len(&self) -> usize { | ||
| 83 | self.state.lock(|s| s.borrow().len()) | ||
| 84 | } | ||
| 85 | |||
| 86 | /// Returns whether the channel is empty. | ||
| 87 | pub fn is_empty(&self) -> bool { | ||
| 88 | self.state.lock(|s| s.borrow().is_empty()) | ||
| 89 | } | ||
| 90 | |||
| 91 | /// Returns whether the channel is full. | ||
| 92 | pub fn is_full(&self) -> bool { | ||
| 93 | self.state.lock(|s| s.borrow().is_full()) | ||
| 94 | } | ||
| 73 | } | 95 | } |
| 74 | 96 | ||
| 75 | /// Send-only access to a [`Channel`]. | 97 | /// Send-only access to a [`Channel`]. |
| @@ -130,6 +152,28 @@ impl<'a, M: RawMutex, T> Sender<'a, M, T> { | |||
| 130 | pub fn send_done(&mut self) { | 152 | pub fn send_done(&mut self) { |
| 131 | self.channel.state.lock(|s| s.borrow_mut().push_done()) | 153 | self.channel.state.lock(|s| s.borrow_mut().push_done()) |
| 132 | } | 154 | } |
| 155 | |||
| 156 | /// Clears all elements in the channel. | ||
| 157 | pub fn clear(&mut self) { | ||
| 158 | self.channel.state.lock(|s| { | ||
| 159 | s.borrow_mut().clear(); | ||
| 160 | }); | ||
| 161 | } | ||
| 162 | |||
| 163 | /// Returns the number of elements currently in the channel. | ||
| 164 | pub fn len(&self) -> usize { | ||
| 165 | self.channel.state.lock(|s| s.borrow().len()) | ||
| 166 | } | ||
| 167 | |||
| 168 | /// Returns whether the channel is empty. | ||
| 169 | pub fn is_empty(&self) -> bool { | ||
| 170 | self.channel.state.lock(|s| s.borrow().is_empty()) | ||
| 171 | } | ||
| 172 | |||
| 173 | /// Returns whether the channel is full. | ||
| 174 | pub fn is_full(&self) -> bool { | ||
| 175 | self.channel.state.lock(|s| s.borrow().is_full()) | ||
| 176 | } | ||
| 133 | } | 177 | } |
| 134 | 178 | ||
| 135 | /// Receive-only access to a [`Channel`]. | 179 | /// Receive-only access to a [`Channel`]. |
| @@ -190,10 +234,33 @@ impl<'a, M: RawMutex, T> Receiver<'a, M, T> { | |||
| 190 | pub fn receive_done(&mut self) { | 234 | pub fn receive_done(&mut self) { |
| 191 | self.channel.state.lock(|s| s.borrow_mut().pop_done()) | 235 | self.channel.state.lock(|s| s.borrow_mut().pop_done()) |
| 192 | } | 236 | } |
| 237 | |||
| 238 | /// Clears all elements in the channel. | ||
| 239 | pub fn clear(&mut self) { | ||
| 240 | self.channel.state.lock(|s| { | ||
| 241 | s.borrow_mut().clear(); | ||
| 242 | }); | ||
| 243 | } | ||
| 244 | |||
| 245 | /// Returns the number of elements currently in the channel. | ||
| 246 | pub fn len(&self) -> usize { | ||
| 247 | self.channel.state.lock(|s| s.borrow().len()) | ||
| 248 | } | ||
| 249 | |||
| 250 | /// Returns whether the channel is empty. | ||
| 251 | pub fn is_empty(&self) -> bool { | ||
| 252 | self.channel.state.lock(|s| s.borrow().is_empty()) | ||
| 253 | } | ||
| 254 | |||
| 255 | /// Returns whether the channel is full. | ||
| 256 | pub fn is_full(&self) -> bool { | ||
| 257 | self.channel.state.lock(|s| s.borrow().is_full()) | ||
| 258 | } | ||
| 193 | } | 259 | } |
| 194 | 260 | ||
| 195 | struct State { | 261 | struct State { |
| 196 | len: usize, | 262 | /// Maximum number of elements the channel can hold. |
| 263 | capacity: usize, | ||
| 197 | 264 | ||
| 198 | /// Front index. Always 0..=(N-1) | 265 | /// Front index. Always 0..=(N-1) |
| 199 | front: usize, | 266 | front: usize, |
| @@ -210,13 +277,31 @@ struct State { | |||
| 210 | 277 | ||
| 211 | impl State { | 278 | impl State { |
| 212 | fn increment(&self, i: usize) -> usize { | 279 | fn increment(&self, i: usize) -> usize { |
| 213 | if i + 1 == self.len { | 280 | if i + 1 == self.capacity { |
| 214 | 0 | 281 | 0 |
| 215 | } else { | 282 | } else { |
| 216 | i + 1 | 283 | i + 1 |
| 217 | } | 284 | } |
| 218 | } | 285 | } |
| 219 | 286 | ||
| 287 | fn clear(&mut self) { | ||
| 288 | self.front = 0; | ||
| 289 | self.back = 0; | ||
| 290 | self.full = false; | ||
| 291 | } | ||
| 292 | |||
| 293 | fn len(&self) -> usize { | ||
| 294 | if !self.full { | ||
| 295 | if self.back >= self.front { | ||
| 296 | self.back - self.front | ||
| 297 | } else { | ||
| 298 | self.capacity + self.back - self.front | ||
| 299 | } | ||
| 300 | } else { | ||
| 301 | self.capacity | ||
| 302 | } | ||
| 303 | } | ||
| 304 | |||
| 220 | fn is_full(&self) -> bool { | 305 | fn is_full(&self) -> bool { |
| 221 | self.full | 306 | self.full |
| 222 | } | 307 | } |
