1 /*
2  * Copyright (c) 2021-2023 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 #define HST_LOG_TAG "ConditionVariable"
17 
18 #include "osal/task/condition_variable.h"
19 
20 namespace OHOS {
21 namespace Media {
ConditionVariable()22 ConditionVariable::ConditionVariable() noexcept : condInited_(true)
23 {
24 }
25 
~ConditionVariable()26 ConditionVariable::~ConditionVariable() noexcept
27 {
28 }
29 
NotifyOne()30 void ConditionVariable::NotifyOne() noexcept
31 {
32     cond_.notify_one();
33 }
34 
NotifyAll()35 void ConditionVariable::NotifyAll() noexcept
36 {
37     cond_.notify_all();
38 }
39 
Wait(AutoLock & lock)40 void ConditionVariable::Wait(AutoLock& lock) noexcept
41 {
42     cond_.wait(lock);
43 }
44 
Wait(AutoLock & lock,std::function<bool ()> pred)45 void ConditionVariable::Wait(AutoLock& lock, std::function<bool()> pred) noexcept
46 {
47     while (!pred()) {
48         Wait(lock);
49     }
50 }
51 
WaitFor(AutoLock & lock,int64_t timeoutMs)52 bool ConditionVariable::WaitFor(AutoLock& lock, int64_t timeoutMs)
53 {
54     auto status = cond_.wait_for(lock, std::chrono::milliseconds(timeoutMs));
55     if (status == ffrt::cv_status::no_timeout) {
56         return true;
57     } else {
58         return false;
59     }
60 }
61 
WaitFor(AutoLock & lock,int64_t timeoutMs,std::function<bool ()> pred)62 bool ConditionVariable::WaitFor(AutoLock& lock, int64_t timeoutMs, std::function<bool()> pred)
63 {
64     return cond_.wait_for(lock, std::chrono::milliseconds(timeoutMs), pred);
65 }
66 } // namespace Media
67 } // namespace OHOS