1 /*
2 * Copyright (c) 2023 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 "hgm_log.h"
17 #include "hgm_task_handle_thread.h"
18 #include "xcollie/watchdog.h"
19
20 namespace OHOS::Rosen {
21 namespace {
22 constexpr uint32_t WATCHDOG_TIMEVAL = 5000;
23 }
24
Instance()25 HgmTaskHandleThread& HgmTaskHandleThread::Instance()
26 {
27 static HgmTaskHandleThread instance;
28 return instance;
29 }
30
HgmTaskHandleThread()31 HgmTaskHandleThread::HgmTaskHandleThread() : runner_(AppExecFwk::EventRunner::Create("HgmTaskHandleThread"))
32 {
33 handler_ = std::make_shared<AppExecFwk::EventHandler>(runner_);
34 int ret = HiviewDFX::Watchdog::GetInstance().AddThread("HgmTaskHandle", handler_, WATCHDOG_TIMEVAL);
35 if (ret != 0) {
36 HGM_LOGW("Add watchdog thread failed");
37 }
38 }
39
CreateHandler()40 std::shared_ptr<AppExecFwk::EventHandler> HgmTaskHandleThread::CreateHandler()
41 {
42 return std::make_shared<AppExecFwk::EventHandler>(runner_);
43 }
44
PostTask(const std::function<void ()> & task,int64_t delayTime)45 void HgmTaskHandleThread::PostTask(const std::function<void()>& task, int64_t delayTime)
46 {
47 if (handler_) {
48 handler_->PostTask(task, delayTime, AppExecFwk::EventQueue::Priority::IMMEDIATE);
49 }
50 }
51
PostSyncTask(const std::function<void ()> & task)52 bool HgmTaskHandleThread::PostSyncTask(const std::function<void()>& task)
53 {
54 if (handler_) {
55 return handler_->PostSyncTask(task, AppExecFwk::EventQueue::Priority::IMMEDIATE);
56 }
57 return false;
58 }
59
PostEvent(std::string eventId,const std::function<void ()> & task,int64_t delayTime)60 void HgmTaskHandleThread::PostEvent(std::string eventId, const std::function<void()>& task, int64_t delayTime)
61 {
62 if (handler_) {
63 handler_->PostTask(task, eventId, delayTime);
64 }
65 }
66
RemoveEvent(std::string eventId)67 void HgmTaskHandleThread::RemoveEvent(std::string eventId)
68 {
69 if (handler_) {
70 handler_->RemoveTask(eventId);
71 }
72 }
73 }
74