rs_matter/utils/sync/notification.rs
1/*
2 *
3 * Copyright (c) 2024-2026 Project CHIP Authors
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18use embassy_sync::blocking_mutex::raw::RawMutex;
19
20use crate::utils::init::{init, Init};
21
22use super::blocking::raw::MatterRawMutex;
23use super::signal::Signal;
24
25/// A notification primitive that allows for notifying a single waiter.
26pub struct Notification<M = MatterRawMutex> {
27 signal: Signal<Option<()>, M>,
28}
29
30impl<M> Default for Notification<M>
31where
32 M: RawMutex,
33{
34 fn default() -> Self {
35 Self::new()
36 }
37}
38
39impl<M> Notification<M>
40where
41 M: RawMutex,
42{
43 /// Create a new `Notification`.
44 pub const fn new() -> Self {
45 Self {
46 signal: Signal::new(None),
47 }
48 }
49
50 pub fn init() -> impl Init<Self> {
51 init!(Self {
52 signal <- Signal::init(None),
53 })
54 }
55
56 /// Notify the waiter.
57 pub fn notify(&self) {
58 self.signal.signal(());
59 }
60
61 /// Wait for the notification.
62 pub async fn wait(&self) {
63 self.signal.wait_signalled().await;
64 }
65}