• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "thread.h"
17 
18 #if defined(ENABLE_TASKPOOL_FFRT)
19 #include "ffrt_inner.h"
20 #include "c/executor_task.h"
21 #endif
22 #include "task_manager.h"
23 
24 namespace Commonlibrary::Concurrent::TaskPoolModule {
Thread()25 Thread::Thread() : tid_() {}
26 
27 #ifdef ENABLE_ETS_UTILS_STACKSIZE_LOW
28 static constexpr uint64_t STACK_SIZE = 1 * 512 * 1024;
29 #else
30 static constexpr uint64_t STACK_SIZE = 8 * 1024 * 1024;
31 #endif
32 
Start()33 bool Thread::Start()
34 {
35     if (TaskManager::GetInstance().EnableFfrt()) {
36 #if defined(ENABLE_TASKPOOL_FFRT)
37         ffrt::task_attr task_attr;
38         (void)ffrt_task_attr_init(&task_attr);
39         ffrt_task_attr_set_name(&task_attr, "OS_TaskWorker");
40         ffrt_task_attr_set_qos(&task_attr, ffrt_qos_user_initiated);
41         ffrt_task_attr_set_local(&task_attr, true);
42         ffrt_task_attr_set_stack_size(&task_attr, STACK_SIZE);
43         auto task = [this]() {
44             Thread* thread = reinterpret_cast<Thread*>(this);
45             thread->Run();
46         };
47         ffrt::submit(task, {}, {}, task_attr);
48 #endif
49         return 0;
50     } else {
51         int ret = uv_thread_create(&tid_, [](void* arg) {
52 #if defined IOS_PLATFORM || defined MAC_PLATFORM
53             pthread_setname_np("OS_TaskWorker");
54 #else
55             pthread_setname_np(pthread_self(), "OS_TaskWorker");
56 #endif
57             Thread* thread = reinterpret_cast<Thread*>(arg);
58             thread->Run();
59         }, this);
60         return ret != 0;
61     }
62 }
63 }  // namespace Commonlibrary::Concurrent::TaskPoolModule