1 // 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 17 use 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)] 22 pub struct AtomicFlag { 23 state: Arc<(Mutex<bool>, Condvar)>, 24 } 25 26 impl Default for AtomicFlag { 27 #[allow(clippy::mutex_atomic)] default() -> Self28 fn default() -> Self { 29 Self { state: Arc::new((Mutex::new(false), Condvar::new())) } 30 } 31 } 32 33 #[allow(clippy::mutex_atomic)] 34 impl AtomicFlag { 35 /// Wait until the flag is set. wait(&self)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. raise(&self)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 } 47