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_task_handle_thread.h"
17
18 #include <unistd.h>
19
20 #include "ffrt_inner.h"
21 #include "hgm_log.h"
22 #include "xcollie/watchdog.h"
23
24 namespace OHOS::Rosen {
25 namespace {
26 constexpr uint32_t WATCHDOG_TIMEVAL = 30000;
27 constexpr uint32_t WATCHDOG_DELAY_TIME = 600000;
28 constexpr uint64_t MILLISECONDS_TO_MICROSECONDS = 1000;
29 constexpr uint64_t MAX_ALLOWED_DELAY = UINT64_MAX / 1000;
30
ConvertMillisecondsToMicroseconds(int64_t delayTime)31 uint64_t ConvertMillisecondsToMicroseconds(int64_t delayTime)
32 {
33 return (delayTime < 0) ? 0 :
34 std::min(static_cast<uint64_t>(delayTime), MAX_ALLOWED_DELAY) * MILLISECONDS_TO_MICROSECONDS;
35 }
36 }
37
Instance()38 HgmTaskHandleThread& HgmTaskHandleThread::Instance()
39 {
40 static HgmTaskHandleThread instance;
41 return instance;
42 }
43
HgmTaskHandleThread()44 HgmTaskHandleThread::HgmTaskHandleThread()
45 {
46 queue_ = std::make_shared<ffrt::queue>(
47 static_cast<ffrt::queue_type>(ffrt_inner_queue_type_t::ffrt_queue_eventhandler_adapter), "HgmTaskHandleThread",
48 ffrt::queue_attr().qos(ffrt::qos_user_interactive));
49 PostTask([]() { HiviewDFX::Watchdog::GetInstance().InitFfrtWatchdog(); }, WATCHDOG_DELAY_TIME);
50 }
51
PostTask(const std::function<void ()> & task,int64_t delayTime)52 void HgmTaskHandleThread::PostTask(const std::function<void()>& task, int64_t delayTime)
53 {
54 if (queue_) {
55 auto microDelayTime = ConvertMillisecondsToMicroseconds(delayTime);
56 queue_->submit(
57 std::move(task), ffrt::task_attr()
58 .delay(microDelayTime)
59 .priority(static_cast<ffrt_queue_priority_t>(ffrt_inner_queue_priority_immediate)));
60 }
61 }
62
PostSyncTask(const std::function<void ()> & task)63 bool HgmTaskHandleThread::PostSyncTask(const std::function<void()>& task)
64 {
65 if (queue_) {
66 auto handle = queue_->submit_h(std::move(task),
67 ffrt::task_attr().priority(static_cast<ffrt_queue_priority_t>(ffrt_inner_queue_priority_vip)));
68 queue_->wait(handle);
69 return true;
70 }
71 return false;
72 }
73
PostEvent(std::string eventId,const std::function<void ()> & task,int64_t delayTime)74 void HgmTaskHandleThread::PostEvent(std::string eventId, const std::function<void()>& task, int64_t delayTime)
75 {
76 if (queue_) {
77 auto microDelayTime = ConvertMillisecondsToMicroseconds(delayTime);
78 queue_->submit(std::move(task), ffrt::task_attr().name(eventId.c_str()).delay(microDelayTime));
79 }
80 }
81
RemoveEvent(std::string eventId)82 void HgmTaskHandleThread::RemoveEvent(std::string eventId)
83 {
84 if (queue_) {
85 ffrt_queue_t* queue = reinterpret_cast<ffrt_queue_t*>(queue_.get());
86 ffrt_queue_cancel_by_name(*queue, eventId.c_str());
87 }
88 }
89
DetectMultiThreadingCalls()90 void HgmTaskHandleThread::DetectMultiThreadingCalls()
91 {
92 if (auto newTid = gettid(); curThreadId_ != newTid) {
93 // -1 means default curThreadId
94 if (curThreadId_ != -1) {
95 HGM_LOGD("Concurrent access tid1: %{public}d tid2: %{public}d", curThreadId_, newTid);
96 }
97 curThreadId_ = newTid;
98 }
99 }
100 }
101