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 #include "event_reactor.h"
16 #include "event_handler.h"
17 #include "event_demultiplexer.h"
18 #include "timer_event_handler.h"
19 #include "common_timer_errors.h"
20 #include "utils_log.h"
21
22 #include <cstdio>
23 #include <unistd.h>
24 #include <sys/syscall.h>
25
26 namespace OHOS {
27 namespace Utils {
28
EventReactor()29 EventReactor::EventReactor()
30 :loopReady_(false), switch_(false), demultiplexer_(new EventDemultiplexer())
31 {
32 }
33
~EventReactor()34 EventReactor::~EventReactor()
35 {
36 }
37
UpdateEventHandler(EventHandler * handler)38 void EventReactor::UpdateEventHandler(EventHandler* handler)
39 {
40 if ((handler != nullptr) && (handler->GetEventReactor() == this) && (demultiplexer_ != nullptr)) {
41 if (demultiplexer_->UpdateEventHandler(handler) != 0) {
42 UTILS_LOGE("updateEventHandler failed.");
43 }
44 }
45 }
46
SetUp()47 uint32_t EventReactor::SetUp()
48 {
49 if (demultiplexer_ == nullptr) {
50 return TIMER_ERR_INVALID_VALUE;
51 }
52
53 uint32_t ret = demultiplexer_->StartUp(); // return TIME_ERR_OK, if demultiplexer has been started.
54 if (ret != 0) {
55 UTILS_LOGE("demultiplexer startUp failed.");
56 return ret;
57 }
58
59 loopReady_ = true;
60 return TIMER_ERR_OK;
61 }
62
CleanUp()63 void EventReactor::CleanUp()
64 {
65 std::lock_guard<std::recursive_mutex> lock(mutex_);
66 for (auto &itor : timerEventHandlers_) {
67 itor->Uninitialize();
68 }
69 }
70
RunLoop(int timeout) const71 void EventReactor::RunLoop(int timeout) const
72 {
73 if (demultiplexer_ == nullptr) {
74 UTILS_LOGE("demultiplexer_ is nullptr.");
75 return;
76 }
77
78 while (loopReady_ && switch_) {
79 demultiplexer_->Polling(timeout);
80 }
81
82 loopReady_ = false;
83 }
84
SwitchOn()85 void EventReactor::SwitchOn()
86 {
87 switch_ = true;
88 }
89
SwitchOff()90 void EventReactor::SwitchOff()
91 {
92 switch_ = false;
93 }
94
ScheduleTimer(const TimerCallback & cb,uint32_t interval,int & timerFd,bool once)95 uint32_t EventReactor::ScheduleTimer(const TimerCallback& cb, uint32_t interval, int& timerFd, bool once)
96 {
97 std::lock_guard<std::recursive_mutex> lock(mutex_);
98 std::shared_ptr<TimerEventHandler> handler = std::make_shared<TimerEventHandler>(this, interval, once);
99 handler->SetTimerCallback(cb);
100 uint32_t ret = handler->Initialize();
101 if (ret != TIMER_ERR_OK) {
102 UTILS_LOGD("ScheduleTimer %{public}d initialize failed", interval);
103 return ret;
104 }
105
106 timerFd = handler->GetHandle();
107 timerEventHandlers_.push_back(handler);
108 return TIMER_ERR_OK;
109 }
110
CancelTimer(int timerFd)111 void EventReactor::CancelTimer(int timerFd)
112 {
113 UTILS_LOGD("Cancel timer, timerFd: %{public}d.", timerFd);
114 std::lock_guard<std::recursive_mutex> lock(mutex_);
115 auto itor = timerEventHandlers_.begin();
116 for (; itor != timerEventHandlers_.end(); ++itor) {
117 if ((*itor)->GetHandle() == timerFd) {
118 (*itor)->Uninitialize();
119 timerEventHandlers_.erase(itor);
120 return;
121 }
122 }
123 }
124
125 } // namespace Utils
126 } // namespace OHOS
127