• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define HST_LOG_TAG "ConditionVariable"
17 
18 #include "foundation/osal/thread/condition_variable.h"
19 #include <ratio>
20 #include "foundation/log.h"
21 
22 namespace {
TmToNs(struct timespec tm)23 inline long long TmToNs(struct timespec tm)
24 {
25     return tm.tv_sec * std::giga::num + tm.tv_nsec;
26 }
27 
NsToTm(long long ns)28 inline struct timespec NsToTm(long long ns)
29 {
30     return {ns / std::giga::num, static_cast<long>(ns % std::giga::num)};
31 }
32 }
33 
34 namespace OHOS {
35 namespace Media {
36 namespace OSAL {
ConditionVariable()37 ConditionVariable::ConditionVariable() noexcept : condInited_(true)
38 {
39     pthread_condattr_t attr;
40     pthread_condattr_init(&attr);
41 #ifndef __LITEOS_M__
42 #ifdef USING_CLOCK_REALTIME
43     pthread_condattr_setclock(&attr, CLOCK_REALTIME);
44 #else
45     pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
46 #endif
47 #endif
48     int rtv = pthread_cond_init(&cond_, &attr);
49     if (rtv != 0) {
50         condInited_ = false;
51         MEDIA_LOG_E("failed to init pthread cond variable");
52     }
53     pthread_condattr_destroy(&attr);
54 }
55 
~ConditionVariable()56 ConditionVariable::~ConditionVariable() noexcept
57 {
58     if (condInited_) {
59         pthread_cond_destroy(&cond_);
60     }
61 }
62 
NotifyOne()63 void ConditionVariable::NotifyOne() noexcept
64 {
65     int ret = pthread_cond_signal(&cond_);
66     if (ret != 0) {
67         MEDIA_LOG_E("NotifyOne failed with errno = " PUBLIC_LOG_D32, ret);
68     }
69 }
70 
NotifyAll()71 void ConditionVariable::NotifyAll() noexcept
72 {
73     int ret = pthread_cond_broadcast(&cond_);
74     if (ret != 0) {
75         MEDIA_LOG_E("NotifyAll failed with errno = " PUBLIC_LOG_D32, ret);
76     }
77 }
78 
Wait(ScopedLock & lock)79 void ConditionVariable::Wait(ScopedLock& lock) noexcept
80 {
81     pthread_cond_wait(&cond_, &(lock.mutex_->nativeHandle_));
82 }
83 
WaitFor(ScopedLock & lock,int timeoutMs)84 bool ConditionVariable::WaitFor(ScopedLock& lock, int timeoutMs)
85 {
86     if (timeoutMs < 0) {
87         MEDIA_LOG_E("ConditionVariable WaitUntil invalid timeoutMs: " PUBLIC_LOG_D32, timeoutMs);
88         return false;
89     }
90     struct timespec timeout = {0, 0};
91 #ifdef USING_CLOCK_REALTIME
92     clock_gettime(CLOCK_REALTIME, &timeout);
93 #else
94     clock_gettime(CLOCK_MONOTONIC, &timeout);
95 #endif
96     timeout = NsToTm(TmToNs(timeout) + timeoutMs * std::mega::num);
97     return pthread_cond_timedwait(&cond_, &(lock.mutex_->nativeHandle_),
98         &timeout) == 0;
99 }
100 } // namespace OSAL
101 } // namespace Media
102 } // namespace OHOS
103