1 /*
2 * Copyright (c) 2022 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 <cstring>
17 #include "anr_thread.h"
18
19 #include "core/common/task_runner_adapter.h"
20 #include "core/common/task_runner_adapter_factory.h"
21
22 namespace OHOS::Ace {
23
24 namespace {
25 RefPtr<TaskRunnerAdapter> g_anrThread;
26 } // namespace
27
Start()28 void AnrThread::Start()
29 {
30 if (!g_anrThread) {
31 g_anrThread = TaskRunnerAdapterFactory::Create(false, "anr");
32 }
33 }
34
Stop()35 void AnrThread::Stop()
36 {
37 g_anrThread.Reset();
38 }
39
PostTaskToTaskRunner(Task && task,uint32_t delayTime,const std::string & name)40 bool AnrThread::PostTaskToTaskRunner(Task&& task, uint32_t delayTime, const std::string& name)
41 {
42 if (!g_anrThread || !task) {
43 return false;
44 }
45
46 if (delayTime > 0) {
47 g_anrThread->PostDelayedTask(std::move(task), delayTime, name);
48 } else {
49 g_anrThread->PostTask(std::move(task), name);
50 }
51 return true;
52 }
53 } // namespace OHOS::Ace
54