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_strategy.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 30 class CPUWorker : public WorkerThread { 31 public: CPUWorker(const QoS & qos,CpuWorkerOps && ops,void * worker_mgr)32 CPUWorker(const QoS& qos, CpuWorkerOps&& ops, void* worker_mgr) : WorkerThread(qos), ops(ops) 33 { 34 this->worker_mgr = worker_mgr; 35 localFifo.Init(LOCAL_QUEUE_SIZE); 36 #ifdef FFRT_PTHREAD_ENABLE 37 Start(CPUWorker::WrapDispatch, this); 38 #else 39 Start(CPUWorker::Dispatch, this); 40 #endif 41 } 42 43 CpuWorkerOps ops; 44 SpmcQueue localFifo; 45 void* priority_task = nullptr; 46 unsigned int tick = 0; 47 unsigned int global_interval = 60; 48 unsigned int budget = 10; 49 50 public: 51 /* strategy options for worklooper function */ 52 static void WorkerLooperDefault(WorkerThread* p); 53 static void Run(CPUEUTask* task, CoRoutineEnv* coRoutineEnv, CPUWorker* worker); 54 55 private: 56 static void* WrapDispatch(void* worker); 57 static void Dispatch(CPUWorker* worker); 58 static void Run(ffrt_executor_task_t* task, ffrt_qos_t qos); 59 static void RunTask(ffrt_executor_task_t* curtask, CPUWorker* worker); 60 static void RunTask(ffrt_executor_task_t* curtask, CPUWorker* worker, ExecuteCtx* ctx, CoRoutineEnv* coRoutineEnv); 61 static void RunTaskLifo(ffrt_executor_task_t* task, CPUWorker* worker); 62 static void* GetTask(CPUWorker* worker); 63 static PollerRet TryPoll(CPUWorker* worker, int timeout); 64 static bool LocalEmpty(CPUWorker* worker); 65 }; 66 } // namespace ffrt 67 #endif 68