Next Generation WASM Microkernel Operating System
1// Copyright 2025 Jonas Kruckenberg
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8use arrayvec::ArrayVec;
9use core::task::Waker;
10
11const NUM_WAKERS: usize = 32;
12
13pub struct WakeBatch {
14 inner: ArrayVec<Waker, NUM_WAKERS>,
15}
16
17impl Default for WakeBatch {
18 fn default() -> Self {
19 Self::new()
20 }
21}
22
23impl WakeBatch {
24 pub const fn new() -> Self {
25 Self {
26 inner: ArrayVec::new_const(),
27 }
28 }
29
30 /// Adds a [`Waker`] to the batch, returning `true` if the batch needs to be flushed because it
31 /// is full.
32 pub fn add_waker(&mut self, waker: Waker) -> bool {
33 self.inner.push(waker);
34 self.inner.is_full()
35 }
36
37 pub fn wake_all(&mut self) {
38 for waker in self.inner.drain(..) {
39 waker.wake();
40 }
41 }
42
43 pub fn len(&self) -> usize {
44 self.inner.len()
45 }
46
47 pub fn is_empty(&self) -> bool {
48 self.inner.is_empty()
49 }
50}