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 :stopped_(true), demultiplexer_(new EventDemultiplexer())
31 {
32 }
33
~EventReactor()34 EventReactor::~EventReactor()
35 {
36 }
37
RemoveEventHandler(EventHandler * handler)38 void EventReactor::RemoveEventHandler(EventHandler* handler)
39 {
40 if ((handler != nullptr) && (handler->GetEventReactor() == this) && (demultiplexer_ != nullptr)) {
41 demultiplexer_->RemoveEventHandler(handler);
42 }
43 }
44
UpdateEventHandler(EventHandler * handler)45 void EventReactor::UpdateEventHandler(EventHandler* handler)
46 {
47 if ((handler != nullptr) && (handler->GetEventReactor() == this) && (demultiplexer_ != nullptr)) {
48 if (demultiplexer_->UpdateEventHandler(handler) != 0) {
49 UTILS_LOGE("updateEventHandler failed.");
50 }
51 }
52 }
53
StartUp()54 uint32_t EventReactor::StartUp()
55 {
56 if (demultiplexer_ == nullptr) {
57 UTILS_LOGE("Looper::startUp failed, demultiplexer is null.");
58 return TIMER_ERR_INVALID_VALUE;
59 }
60
61 uint32_t ret = demultiplexer_->StartUp();
62 if (ret != 0) {
63 UTILS_LOGE("demultiplexer startUp failed.");
64 return ret;
65 }
66
67 stopped_ = false;
68 return TIMER_ERR_OK;
69 }
70
CleanUp()71 void EventReactor::CleanUp()
72 {
73 std::lock_guard<std::recursive_mutex> lock(mutex_);
74 for (auto &itor : timerEventHandlers_) {
75 itor->Uninitialize();
76 }
77 }
78
RunLoop(int timeout) const79 void EventReactor::RunLoop(int timeout) const
80 {
81 if (demultiplexer_ == nullptr) {
82 UTILS_LOGE("demultiplexer_ is nullptr.");
83 return;
84 }
85 while (!stopped_) {
86 demultiplexer_->Polling(timeout);
87 }
88 }
89
StopLoop()90 void EventReactor::StopLoop()
91 {
92 stopped_ = true;
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 if (handler == nullptr) {
100 UTILS_LOGE("ScheduleTimer create TimerEventHandler failed.");
101 return TIMER_ERR_INVALID_VALUE;
102 }
103 handler->SetTimerCallback(cb);
104 uint32_t ret = handler->Initialize();
105 if (ret != TIMER_ERR_OK) {
106 UTILS_LOGE("ScheduleTimer %{public}d initialize failed", interval);
107 return ret;
108 }
109
110 timerFd = handler->GetTimerFd();
111 timerEventHandlers_.push_back(handler);
112 return TIMER_ERR_OK;
113 }
114
CancelTimer(int timerFd)115 void EventReactor::CancelTimer(int timerFd)
116 {
117 UTILS_LOGD("Cancel timer, timerFd: %{public}d.", timerFd);
118 std::lock_guard<std::recursive_mutex> lock(mutex_);
119 auto itor = timerEventHandlers_.begin();
120 for (; itor != timerEventHandlers_.end(); ++itor) {
121 if ((*itor)->GetTimerFd() == timerFd) {
122 (*itor)->Uninitialize();
123 timerEventHandlers_.erase(itor);
124 return;
125 }
126 }
127 }
128
129 } // namespace Utils
130 } // namespace OHOS