1 /*
2 * Copyright (c) 2022-2025 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 "dh_timer.h"
17
18 #include <pthread.h>
19
20 #include "anonymous_string.h"
21 #include "constants.h"
22 #include "distributed_hardware_errno.h"
23 #include "distributed_hardware_log.h"
24
25 namespace OHOS {
26 namespace DistributedHardware {
27 namespace {
28 constexpr const char *EVENT_RUN = "EventRun";
29 }
30
31 #undef DH_LOG_TAG
32 #define DH_LOG_TAG "DHTimer"
33
DHTimer(std::string timerId,int32_t delayTimeMs)34 DHTimer::DHTimer(std::string timerId, int32_t delayTimeMs) : timerId_(timerId), delayTimeMs_(delayTimeMs)
35 {
36 DHLOGI("DHTimer ctor!");
37 }
38
~DHTimer()39 DHTimer::~DHTimer()
40 {
41 DHLOGI("DHTimer dtor!");
42 ReleaseTimer();
43 }
44
InitTimer()45 void DHTimer::InitTimer()
46 {
47 DHLOGI("start");
48 std::unique_lock<std::mutex> lock(timerMutex_);
49 if (eventHandler_ == nullptr) {
50 eventHandlerThread_ = std::thread([this]() { this->StartEventRunner(); });
51 timerCond_.wait(lock, [this] { return eventHandler_ != nullptr; });
52 }
53 DHLOGI("end");
54 }
55
ReleaseTimer()56 void DHTimer::ReleaseTimer()
57 {
58 DHLOGI("start");
59 std::lock_guard<std::mutex> lock(timerMutex_);
60 if (eventHandler_ != nullptr) {
61 eventHandler_->RemoveTask(timerId_);
62 if (eventHandler_->GetEventRunner() != nullptr) {
63 eventHandler_->GetEventRunner()->Stop();
64 }
65 }
66 if (eventHandlerThread_.joinable()) {
67 eventHandlerThread_.join();
68 }
69 eventHandler_ = nullptr;
70 DHLOGI("end");
71 }
72
StartEventRunner()73 void DHTimer::StartEventRunner()
74 {
75 DHLOGI("start");
76 int32_t ret = pthread_setname_np(pthread_self(), EVENT_RUN);
77 if (ret != DH_FWK_SUCCESS) {
78 DHLOGE("StartEventRunner setname failed.");
79 }
80 auto busRunner = AppExecFwk::EventRunner::Create(false);
81 if (busRunner == nullptr) {
82 DHLOGE("busRunner is nullptr!");
83 return;
84 }
85 {
86 std::lock_guard<std::mutex> lock(timerMutex_);
87 eventHandler_ = std::make_shared<AppExecFwk::EventHandler>(busRunner);
88 }
89 timerCond_.notify_all();
90 busRunner->Run();
91 DHLOGI("end");
92 }
93
StartTimer()94 void DHTimer::StartTimer()
95 {
96 DHLOGI("start");
97 InitTimer();
98 std::lock_guard<std::mutex> lock(timerMutex_);
99 if (eventHandler_ == nullptr) {
100 DHLOGE("eventHandler is nullptr");
101 return;
102 }
103 auto executeFunc = [this] { Execute(); };
104 eventHandler_->PostTask(executeFunc, timerId_, delayTimeMs_);
105 }
106
StopTimer()107 void DHTimer::StopTimer()
108 {
109 DHLOGI("start");
110 ReleaseTimer();
111 HandleStopTimer();
112 DHLOGI("end");
113 }
114
Execute()115 void DHTimer::Execute()
116 {
117 DHLOGI("start");
118 std::lock_guard<std::mutex> lock(timerMutex_);
119 if (eventHandler_ == nullptr) {
120 DHLOGE("eventHandler is nullptr!");
121 return;
122 }
123 auto executeInnerFunc = [this] { ExecuteInner(); };
124 eventHandler_->PostTask(executeInnerFunc, timerId_, delayTimeMs_);
125 }
126 } // namespace DistributedHardware
127 } // namespace OHOS
128