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 super::blocking::raw::MatterRawMutex;
21use super::signal::Signal;
22
23/// A notification primitive that allows for notifying a single waiter.
24pub struct Notification<M = MatterRawMutex>(Signal<Option<()>, M>);
25
26impl<M> Default for Notification<M>
27where
28 M: RawMutex,
29{
30 fn default() -> Self {
31 Self::new()
32 }
33}
34
35impl<M> Notification<M>
36where
37 M: RawMutex,
38{
39 /// Create a new `Notification`.
40 pub const fn new() -> Self {
41 Self(Signal::new(None))
42 }
43
44 /// Notify the waiter.
45 pub fn notify(&self) {
46 self.0.signal(());
47 }
48
49 /// Wait for the notification.
50 pub async fn wait(&self) {
51 self.0.wait_signalled().await;
52 }
53}