1 // Copyright 2023 The Pigweed Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 4 // use this file except in compliance with the License. You may obtain a copy of 5 // the License at 6 // 7 // https://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, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations under 13 // the License. 14 15 #pragma once 16 #include "chre/platform/condition_variable.h" 17 #include "pw_chrono/system_clock.h" 18 19 namespace chre { 20 ConditionVariable()21inline ConditionVariable::ConditionVariable() {} 22 ~ConditionVariable()23inline ConditionVariable::~ConditionVariable() {} 24 notify_one()25inline void ConditionVariable::notify_one() { notification_.release(); } 26 wait(Mutex & mutex)27inline void ConditionVariable::wait(Mutex& mutex) { 28 mutex.unlock(); 29 notification_.acquire(); 30 mutex.lock(); 31 } 32 wait_for(Mutex & mutex,Nanoseconds timeout)33inline bool ConditionVariable::wait_for(Mutex& mutex, Nanoseconds timeout) { 34 mutex.unlock(); 35 auto pw_timeout = pw::chrono::SystemClock::for_at_least( 36 std::chrono::nanoseconds(timeout.toRawNanoseconds())); 37 bool did_acquire = notification_.try_acquire_for(pw_timeout); 38 mutex.lock(); 39 return did_acquire; 40 } 41 42 } // namespace chre 43