1 /*
2  * Copyright (c) 2021-2022 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 "none_io_waiter.h"
17 
18 #include <chrono>
19 
20 #include "event_handler_utils.h"
21 
22 DEFINE_HILOG_LABEL("NoneIoWaiter");
23 
24 namespace OHOS {
25 namespace AppExecFwk {
26 namespace {
27 const int32_t HOURS_PER_DAY = 24;
28 const int32_t DAYS_PER_YEAR = 365;
29 const int32_t HOURS_PER_YEAR = HOURS_PER_DAY * DAYS_PER_YEAR;
30 }  // unnamed namespace
31 
32 // Nothing to do, but used to fix a codex warning.
~NoneIoWaiter()33 NoneIoWaiter::~NoneIoWaiter()
34 {}
35 
WaitFor(std::unique_lock<std::mutex> & lock,int64_t nanoseconds)36 bool NoneIoWaiter::WaitFor(std::unique_lock<std::mutex> &lock, int64_t nanoseconds)
37 {
38     ++waitingCount_;
39     if (nanoseconds < 0) {
40         condition_.wait(lock, [this] { return this->pred_; });
41     } else {
42         /*
43          * Fix a problem in some versions of STL.
44          * Parameter 'nanoseconds' is too large to cause overflow by adding 'now'.
45          * So limit it to no more than one year.
46          */
47         static const auto oneYear = std::chrono::hours(HOURS_PER_YEAR);
48         auto duration = std::chrono::nanoseconds(nanoseconds);
49         (void)condition_.wait_for(lock, (duration > oneYear) ? oneYear : duration, [this] { return this->pred_; });
50     }
51     --waitingCount_;
52     pred_ = false;
53     return true;
54 }
55 
NotifyOne()56 void NoneIoWaiter::NotifyOne()
57 {
58     if (waitingCount_ > 0) {
59         pred_ = true;
60         condition_.notify_one();
61     }
62 }
63 
NotifyAll()64 void NoneIoWaiter::NotifyAll()
65 {
66     if (waitingCount_ > 0) {
67         pred_ = true;
68         condition_.notify_all();
69     }
70 }
71 
SupportListeningFileDescriptor() const72 bool NoneIoWaiter::SupportListeningFileDescriptor() const
73 {
74     return false;
75 }
76 
AddFileDescriptor(int32_t,uint32_t)77 bool NoneIoWaiter::AddFileDescriptor(int32_t, uint32_t)
78 {
79     HILOGW("AddFileDescriptor: Function is not supported !!!");
80     return false;
81 }
82 
RemoveFileDescriptor(int32_t)83 void NoneIoWaiter::RemoveFileDescriptor(int32_t)
84 {
85     HILOGW("RemoveFileDescriptor: Function is not supported !!!");
86 }
87 
SetFileDescriptorEventCallback(const IoWaiter::FileDescriptorEventCallback &)88 void NoneIoWaiter::SetFileDescriptorEventCallback(const IoWaiter::FileDescriptorEventCallback &)
89 {
90     HILOGW("SetFileDescriptorEventCallback: Function is not supported !!!");
91 }
92 }  // namespace AppExecFwk
93 }  // namespace OHOS
94