• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "eu/worker_thread.h"
17 #include <algorithm>
18 #include <unistd.h>
19 #include <sys/syscall.h>
20 #include "dfx/log/ffrt_log_api.h"
21 #include "eu/execute_unit.h"
22 #include "eu/qos_interface.h"
23 #include "qos.h"
24 #include "util/name_manager.h"
25 
26 namespace ffrt {
WorkerThread(const QoS & qos)27 WorkerThread::WorkerThread(const QoS& qos) : exited(false), idle(false), tid(-1), qos(qos)
28 {
29 #ifdef FFRT_PTHREAD_ENABLE
30     pthread_attr_init(&attr_);
31     size_t stackSize = ExecuteUnit::Instance().GetGroupCtl()[qos()].workerStackSize;
32     if (stackSize > 0) {
33         pthread_attr_setstacksize(&attr_, stackSize);
34     }
35 #endif
36 }
37 
NativeConfig()38 void WorkerThread::NativeConfig()
39 {
40     pid_t pid = syscall(SYS_gettid);
41     this->tid = pid;
42 }
43 
WorkerSetup(WorkerThread * wthread)44 void WorkerThread::WorkerSetup(WorkerThread* wthread)
45 {
46     static int threadIndex[QoS::Max()] = {0};
47     std::string qosStr = std::to_string(qos());
48     std::string threadName = std::string(WORKER_THREAD_NAME_PREFIX) + qosStr +
49         std::string(WORKER_THREAD_SYMBOL) + std::to_string(threadIndex[qos()]++);
50     if (qosStr == "") {
51         FFRT_LOGE("ffrt threadName qos[%d] index[%d]", qos(), threadIndex[qos()]);
52     }
53     pthread_setname_np(wthread->GetThread(), threadName.c_str());
54     SetThreadAttr(wthread, qos);
55 }
56 
SetThreadAttr(WorkerThread * thread,const QoS & qos)57 void SetThreadAttr(WorkerThread* thread, const QoS& qos)
58 {
59     if (qos() <= qos_max) {
60         FFRTQosApplyForOther(qos(), thread->Id());
61         FFRT_LOGD("qos apply tid[%d] level[%d]\n", thread->Id(), qos());
62     }
63 }
64 }; // namespace ffrt
65