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 #include <unistd.h>
20
21 namespace OHOS::Rosen {
22 namespace {
23 constexpr uint32_t WATCHDOG_TIMEVAL = 30000;
24 constexpr uint32_t WATCHDOG_DELAY_TIME = 600000;
25 }
26
Instance()27 HgmTaskHandleThread& HgmTaskHandleThread::Instance()
28 {
29 static HgmTaskHandleThread instance;
30 return instance;
31 }
32
HgmTaskHandleThread()33 HgmTaskHandleThread::HgmTaskHandleThread() : runner_(AppExecFwk::EventRunner::Create("HgmTaskHandleThread"))
34 {
35 handler_ = std::make_shared<AppExecFwk::EventHandler>(runner_);
36 if (handler_ != nullptr) {
37 auto task = [] () {
38 auto& hgmTaskHandleThread = HgmTaskHandleThread::Instance();
39 int ret = HiviewDFX::Watchdog::GetInstance().AddThread(
40 "HgmTaskHandle", hgmTaskHandleThread.handler_, WATCHDOG_TIMEVAL);
41 if (ret != 0) {
42 HGM_LOGW("Add watchdog thread failed");
43 }
44 };
45 handler_->PostTask(task, WATCHDOG_DELAY_TIME);
46 }
47 }
48
CreateHandler()49 std::shared_ptr<AppExecFwk::EventHandler> HgmTaskHandleThread::CreateHandler()
50 {
51 return std::make_shared<AppExecFwk::EventHandler>(runner_);
52 }
53
PostTask(const std::function<void ()> & task,int64_t delayTime)54 void HgmTaskHandleThread::PostTask(const std::function<void()>& task, int64_t delayTime)
55 {
56 if (handler_) {
57 handler_->PostTask(task, delayTime, AppExecFwk::EventQueue::Priority::IMMEDIATE);
58 }
59 }
60
PostSyncTask(const std::function<void ()> & task)61 bool HgmTaskHandleThread::PostSyncTask(const std::function<void()>& task)
62 {
63 if (handler_) {
64 return handler_->PostSyncTask(task, AppExecFwk::EventQueue::Priority::VIP);
65 }
66 return false;
67 }
68
PostEvent(std::string eventId,const std::function<void ()> & task,int64_t delayTime)69 void HgmTaskHandleThread::PostEvent(std::string eventId, const std::function<void()>& task, int64_t delayTime)
70 {
71 if (handler_) {
72 handler_->PostTask(task, eventId, delayTime);
73 }
74 }
75
RemoveEvent(std::string eventId)76 void HgmTaskHandleThread::RemoveEvent(std::string eventId)
77 {
78 if (handler_) {
79 handler_->RemoveTask(eventId);
80 }
81 }
82
DetectMultiThreadingCalls()83 void HgmTaskHandleThread::DetectMultiThreadingCalls()
84 {
85 if (auto newTid = gettid(); curThreadId_ != newTid) {
86 // -1 means default curThreadId
87 if (curThreadId_ != -1) {
88 HGM_LOGE("Concurrent access tid1: %{public}d tid2: %{public}d", curThreadId_, newTid);
89 }
90 curThreadId_ = newTid;
91 }
92 }
93 }
94