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 #ifndef HISTREAMER_FOUNDATION_OSAL_CONDITION_VARIABLE_H
17 #define HISTREAMER_FOUNDATION_OSAL_CONDITION_VARIABLE_H
18 
19 #include <functional>
20 #include "osal/task/autolock.h"
21 
22 namespace OHOS {
23 namespace Media {
24 class ConditionVariable {
25 public:
26     ConditionVariable() noexcept;
27 
28     virtual ~ConditionVariable() noexcept;
29 
30     ConditionVariable(const ConditionVariable& other) = delete;
31 
32     ConditionVariable operator=(const ConditionVariable& other) = delete;
33 
34     void __attribute__((no_sanitize("cfi"))) NotifyOne() noexcept;
35 
36     void NotifyAll() noexcept;
37 
38     void Wait(AutoLock& lock) noexcept;
39 
40     void Wait(AutoLock& lock, std::function<bool()> pred) noexcept;
41 
42     bool WaitFor(AutoLock& lock, int64_t timeoutMs);
43 
44     bool WaitFor(AutoLock& lock, int64_t timeoutMs, std::function<bool()> pred);
45 
46 private:
47 #ifdef MEDIA_FOUNDATION_FFRT
48     ffrt::condition_variable cond_;
49 #else
50     pthread_cond_t cond_;
51 #endif
52     bool condInited_;
53 };
54 } // namespace Media
55 } // namespace OHOS
56 
57 #endif // HISTREAMER_FOUNDATION_OSAL_CONDITION_VARIABLE_H
58