blob: 82839b36d74c65d0e85c1e0e09763b518a31d069 [file] [log] [blame]
Andrew Walbranea9fa482021-03-04 16:11:12 +00001// Copyright 2021, The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Synchronisation utilities.
16
17use std::sync::{Arc, Condvar, Mutex};
18
19/// A flag which one thread can use to notify other threads when a condition becomes true. This is
20/// something like a single-use binary semaphore.
21#[derive(Clone, Debug)]
22pub struct AtomicFlag {
23 state: Arc<(Mutex<bool>, Condvar)>,
24}
25
26impl Default for AtomicFlag {
27 #[allow(clippy::mutex_atomic)]
28 fn default() -> Self {
29 Self { state: Arc::new((Mutex::new(false), Condvar::new())) }
30 }
31}
32
33#[allow(clippy::mutex_atomic)]
34impl AtomicFlag {
35 /// Wait until the flag is set.
36 pub fn wait(&self) {
37 let _flag = self.state.1.wait_while(self.state.0.lock().unwrap(), |flag| !*flag).unwrap();
38 }
39
40 /// Set the flag, and notify all waiting threads.
41 pub fn raise(&self) {
42 let mut flag = self.state.0.lock().unwrap();
43 *flag = true;
44 self.state.1.notify_all();
45 }
46}