1 /*
2 * Copyright (C) 2024 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 #include "time_wait_helper.h"
17
18 #include "telephony_log_wrapper.h"
19
20 namespace OHOS {
21 namespace Telephony {
22
TimeWaitHelper(int16_t waitTime)23 TimeWaitHelper::TimeWaitHelper(int16_t waitTime)
24 {
25 waitTime_ = waitTime;
26 }
27
~TimeWaitHelper()28 TimeWaitHelper::~TimeWaitHelper()
29 {
30 TELEPHONY_LOGI("~TimeWaitHelper: %{public}d", waitTime_);
31 }
32
NotifyAll()33 void TimeWaitHelper::NotifyAll()
34 {
35 std::unique_lock<ffrt::mutex> lock(mutex_);
36 TELEPHONY_LOGI("TimeWaitHelper: %{public}d NotifyAll", waitTime_);
37 isNotified_ = true;
38 cv_.notify_all();
39 }
40
WaitForResult()41 bool TimeWaitHelper::WaitForResult()
42 {
43 if (!isNotified_) {
44 std::unique_lock<ffrt::mutex> lock(mutex_);
45 auto now = std::chrono::system_clock::now();
46 while (!isNotified_) {
47 if (cv_.wait_until(lock, now + std::chrono::seconds(waitTime_)) == ffrt::cv_status::timeout) {
48 TELEPHONY_LOGE("TimeWaitHelper: %{public}d time out", waitTime_);
49 return false;
50 }
51 }
52 return true;
53 }
54 TELEPHONY_LOGE("TimeWaitHelper: %{public}d isNotified_ is true", waitTime_);
55 return false;
56 }
57 } // namespace Telephony
58 } // namespace OHOS