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 #ifndef FFRT_CPU_WORKER_HPP
17 #define FFRT_CPU_WORKER_HPP
18 
19 #include "eu/worker_thread.h"
20 #include "eu/cpu_manager_interface.h"
21 #include "c/executor_task.h"
22 #include "sync/poller.h"
23 #include "util/spmc_queue.h"
24 #include "tm/cpu_task.h"
25 
26 namespace ffrt {
27 const unsigned int LOCAL_QUEUE_SIZE = 128;
28 const unsigned int STEAL_BUFFER_SIZE = LOCAL_QUEUE_SIZE / 2;
29 class CPUWorker : public WorkerThread {
30 public:
CPUWorker(const QoS& qos, CpuWorkerOps&& ops)31     CPUWorker(const QoS& qos, CpuWorkerOps&& ops) : WorkerThread(qos), ops(ops)
32     {
33         localFifo.Init(LOCAL_QUEUE_SIZE);
34 #ifdef FFRT_PTHREAD_ENABLE
35         Start(CPUWorker::WrapDispatch, this);
36 #else
37         Start(CPUWorker::Dispatch, this);
38 #endif
39     }
40 
41     CpuWorkerOps ops;
42     SpmcQueue localFifo;
43     void* priority_task = nullptr;
44     unsigned int tick = 0;
45     unsigned int global_interval = 60;
46     unsigned int budget = 10;
47 
48 public:
49     /* strategy options for worklooper function */
50     static void WorkerLooperDefault(WorkerThread* p);
51 
52 private:
53     static void* WrapDispatch(void* worker);
54     static void Dispatch(CPUWorker* worker);
55     static void Run(CPUEUTask* task, CPUWorker* worker);
56     static void Run(ffrt_executor_task_t* task, ffrt_qos_t qos);
57     static void RunTask(ffrt_executor_task_t* curtask, CPUWorker* worker);
58     static void RunTaskLifo(ffrt_executor_task_t* task, CPUWorker* worker);
59     static void* GetTask(CPUWorker* worker);
60     static PollerRet TryPoll(CPUWorker* worker, int timeout);
61     static bool LocalEmpty(CPUWorker* worker);
62 };
63 } // namespace ffrt
64 #endif
65