1 /* 2 * Copyright (c) 2021-2021 Huawei Device Co., Ltd. 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 16 #ifndef HISTREAMER_FOUNDATION_OSAL_CONDITION_VARIABLE_H 17 #define HISTREAMER_FOUNDATION_OSAL_CONDITION_VARIABLE_H 18 19 #include <cerrno> 20 #include <ctime> 21 #include "foundation/osal/thread/scoped_lock.h" 22 23 namespace OHOS { 24 namespace Media { 25 namespace OSAL { 26 class ConditionVariable { 27 public: 28 ConditionVariable() noexcept; 29 30 virtual ~ConditionVariable() noexcept; 31 32 ConditionVariable(const ConditionVariable& other) = delete; 33 34 ConditionVariable operator=(const ConditionVariable& other) = delete; 35 36 void NotifyOne() noexcept; 37 38 void NotifyAll() noexcept; 39 40 void Wait(ScopedLock& lock) noexcept; 41 42 template <typename Predicate> Wait(ScopedLock & lock,Predicate pred)43 void Wait(ScopedLock& lock, Predicate pred) noexcept 44 { 45 while (!pred()) { 46 Wait(lock); 47 } 48 } 49 50 bool WaitFor(ScopedLock& lock, int timeoutMs); 51 52 template <typename Predicate> WaitFor(ScopedLock & lock,int timeoutMs,Predicate pred)53 bool WaitFor(ScopedLock& lock, int timeoutMs, Predicate pred) 54 { 55 if (timeoutMs < 0) { 56 return false; 57 } 58 struct timespec timeout = {0, 0}; 59 #ifdef USING_CLOCK_REALTIME 60 clock_gettime(CLOCK_REALTIME, &timeout); 61 #else 62 clock_gettime(CLOCK_MONOTONIC, &timeout); 63 #endif 64 timeout.tv_sec += timeoutMs / 1000; // 1000 65 timeout.tv_nsec += (timeoutMs % 1000) * 1000000; // 1000 1000000 66 int status = 0; 67 while (!pred() && (status == 0)) { 68 status = pthread_cond_timedwait(&cond_, &(lock.mutex_->nativeHandle_), 69 &timeout); 70 if (status == ETIMEDOUT) { 71 return pred(); 72 } 73 } 74 return status == 0; 75 } 76 77 private: 78 bool condInited_; 79 pthread_cond_t cond_{}; 80 }; 81 } // namespace OSAL 82 } // namespace Media 83 } // namespace OHOS 84 85 #endif // HISTREAMER_FOUNDATION_OSAL_CONDITION_VARIABLE_H 86