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