• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #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);
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);
50     }
51     --waitingCount_;
52     return true;
53 }
54 
NotifyOne()55 void NoneIoWaiter::NotifyOne()
56 {
57     if (waitingCount_ > 0) {
58         condition_.notify_one();
59     }
60 }
61 
NotifyAll()62 void NoneIoWaiter::NotifyAll()
63 {
64     if (waitingCount_ > 0) {
65         condition_.notify_all();
66     }
67 }
68 
SupportListeningFileDescriptor() const69 bool NoneIoWaiter::SupportListeningFileDescriptor() const
70 {
71     return false;
72 }
73 
AddFileDescriptor(int32_t,uint32_t)74 bool NoneIoWaiter::AddFileDescriptor(int32_t, uint32_t)
75 {
76     HILOGW("AddFileDescriptor: Function is not supported !!!");
77     return false;
78 }
79 
RemoveFileDescriptor(int32_t)80 void NoneIoWaiter::RemoveFileDescriptor(int32_t)
81 {
82     HILOGW("RemoveFileDescriptor: Function is not supported !!!");
83 }
84 
SetFileDescriptorEventCallback(const IoWaiter::FileDescriptorEventCallback &)85 void NoneIoWaiter::SetFileDescriptorEventCallback(const IoWaiter::FileDescriptorEventCallback &)
86 {
87     HILOGW("SetFileDescriptorEventCallback: Function is not supported !!!");
88 }
89 }  // namespace AppExecFwk
90 }  // namespace OHOS
91