1fa7767c5Sopenharmony_ci/*
2fa7767c5Sopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3fa7767c5Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fa7767c5Sopenharmony_ci * you may not use this file except in compliance with the License.
5fa7767c5Sopenharmony_ci * You may obtain a copy of the License at
6fa7767c5Sopenharmony_ci *
7fa7767c5Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fa7767c5Sopenharmony_ci *
9fa7767c5Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fa7767c5Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fa7767c5Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fa7767c5Sopenharmony_ci * See the License for the specific language governing permissions and
13fa7767c5Sopenharmony_ci * limitations under the License.
14fa7767c5Sopenharmony_ci */
15fa7767c5Sopenharmony_ci
16fa7767c5Sopenharmony_ci#define HST_LOG_TAG "ConditionVariable"
17fa7767c5Sopenharmony_ci
18fa7767c5Sopenharmony_ci#include "osal/task/condition_variable.h"
19fa7767c5Sopenharmony_ci
20fa7767c5Sopenharmony_cinamespace OHOS {
21fa7767c5Sopenharmony_cinamespace Media {
22fa7767c5Sopenharmony_ciConditionVariable::ConditionVariable() noexcept : condInited_(true)
23fa7767c5Sopenharmony_ci{
24fa7767c5Sopenharmony_ci}
25fa7767c5Sopenharmony_ci
26fa7767c5Sopenharmony_ciConditionVariable::~ConditionVariable() noexcept
27fa7767c5Sopenharmony_ci{
28fa7767c5Sopenharmony_ci}
29fa7767c5Sopenharmony_ci
30fa7767c5Sopenharmony_civoid ConditionVariable::NotifyOne() noexcept
31fa7767c5Sopenharmony_ci{
32fa7767c5Sopenharmony_ci    cond_.notify_one();
33fa7767c5Sopenharmony_ci}
34fa7767c5Sopenharmony_ci
35fa7767c5Sopenharmony_civoid ConditionVariable::NotifyAll() noexcept
36fa7767c5Sopenharmony_ci{
37fa7767c5Sopenharmony_ci    cond_.notify_all();
38fa7767c5Sopenharmony_ci}
39fa7767c5Sopenharmony_ci
40fa7767c5Sopenharmony_civoid ConditionVariable::Wait(AutoLock& lock) noexcept
41fa7767c5Sopenharmony_ci{
42fa7767c5Sopenharmony_ci    cond_.wait(lock);
43fa7767c5Sopenharmony_ci}
44fa7767c5Sopenharmony_ci
45fa7767c5Sopenharmony_civoid ConditionVariable::Wait(AutoLock& lock, std::function<bool()> pred) noexcept
46fa7767c5Sopenharmony_ci{
47fa7767c5Sopenharmony_ci    while (!pred()) {
48fa7767c5Sopenharmony_ci        Wait(lock);
49fa7767c5Sopenharmony_ci    }
50fa7767c5Sopenharmony_ci}
51fa7767c5Sopenharmony_ci
52fa7767c5Sopenharmony_cibool ConditionVariable::WaitFor(AutoLock& lock, int64_t timeoutMs)
53fa7767c5Sopenharmony_ci{
54fa7767c5Sopenharmony_ci    auto status = cond_.wait_for(lock, std::chrono::milliseconds(timeoutMs));
55fa7767c5Sopenharmony_ci    if (status == ffrt::cv_status::no_timeout) {
56fa7767c5Sopenharmony_ci        return true;
57fa7767c5Sopenharmony_ci    } else {
58fa7767c5Sopenharmony_ci        return false;
59fa7767c5Sopenharmony_ci    }
60fa7767c5Sopenharmony_ci}
61fa7767c5Sopenharmony_ci
62fa7767c5Sopenharmony_cibool ConditionVariable::WaitFor(AutoLock& lock, int64_t timeoutMs, std::function<bool()> pred)
63fa7767c5Sopenharmony_ci{
64fa7767c5Sopenharmony_ci    return cond_.wait_for(lock, std::chrono::milliseconds(timeoutMs), pred);
65fa7767c5Sopenharmony_ci}
66fa7767c5Sopenharmony_ci} // namespace Media
67fa7767c5Sopenharmony_ci} // namespace OHOS