• 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 #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 
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 
SetUp()54 uint32_t EventReactor::SetUp()
55 {
56     if (demultiplexer_ == nullptr) {
57         return TIMER_ERR_INVALID_VALUE;
58     }
59 
60     uint32_t ret = demultiplexer_->StartUp(); // return TIME_ERR_OK, if demultiplexer has been started.
61     if (ret != 0) {
62         UTILS_LOGE("demultiplexer startUp failed.");
63         return ret;
64     }
65 
66     loopReady_ = true;
67     return TIMER_ERR_OK;
68 }
69 
CleanUp()70 void EventReactor::CleanUp()
71 {
72     std::lock_guard<std::recursive_mutex> lock(mutex_);
73     for (auto &itor : timerEventHandlers_) {
74         itor->Uninitialize();
75     }
76 }
77 
RunLoop(int timeout) const78 void EventReactor::RunLoop(int timeout) const
79 {
80     if (demultiplexer_ == nullptr) {
81         UTILS_LOGE("demultiplexer_ is nullptr.");
82         return;
83     }
84 
85     while (loopReady_ && switch_) {
86         demultiplexer_->Polling(timeout);
87     }
88 
89     loopReady_ = false;
90 }
91 
SwitchOn()92 void EventReactor::SwitchOn()
93 {
94     switch_ = true;
95 }
96 
SwitchOff()97 void EventReactor::SwitchOff()
98 {
99     switch_ = false;
100 }
101 
ScheduleTimer(const TimerCallback & cb,uint32_t interval,int & timerFd,bool once)102 uint32_t EventReactor::ScheduleTimer(const TimerCallback& cb, uint32_t interval, int& timerFd, bool once)
103 {
104     std::lock_guard<std::recursive_mutex> lock(mutex_);
105     std::shared_ptr<TimerEventHandler> handler = std::make_shared<TimerEventHandler>(this, interval, once);
106     handler->SetTimerCallback(cb);
107     uint32_t ret = handler->Initialize();
108     if (ret != TIMER_ERR_OK) {
109         UTILS_LOGD("ScheduleTimer %{public}d initialize failed", interval);
110         return ret;
111     }
112 
113     timerFd = handler->GetTimerFd();
114     timerEventHandlers_.push_back(handler);
115     return TIMER_ERR_OK;
116 }
117 
CancelTimer(int timerFd)118 void EventReactor::CancelTimer(int timerFd)
119 {
120     UTILS_LOGD("Cancel timer, timerFd: %{public}d.", timerFd);
121     std::lock_guard<std::recursive_mutex> lock(mutex_);
122     auto itor = timerEventHandlers_.begin();
123     for (; itor != timerEventHandlers_.end(); ++itor) {
124         if ((*itor)->GetTimerFd() == timerFd) {
125             (*itor)->Uninitialize();
126             timerEventHandlers_.erase(itor);
127             return;
128         }
129     }
130 }
131 
132 } // namespace Utils
133 } // namespace OHOS
134