1484543d1Sopenharmony_ci/* 2484543d1Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 3484543d1Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4484543d1Sopenharmony_ci * you may not use this file except in compliance with the License. 5484543d1Sopenharmony_ci * You may obtain a copy of the License at 6484543d1Sopenharmony_ci * 7484543d1Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8484543d1Sopenharmony_ci * 9484543d1Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10484543d1Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11484543d1Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12484543d1Sopenharmony_ci * See the License for the specific language governing permissions and 13484543d1Sopenharmony_ci * limitations under the License. 14484543d1Sopenharmony_ci */ 15484543d1Sopenharmony_ci 16484543d1Sopenharmony_ci#ifndef FFRT_CPU_WORKER_HPP 17484543d1Sopenharmony_ci#define FFRT_CPU_WORKER_HPP 18484543d1Sopenharmony_ci 19484543d1Sopenharmony_ci#include "eu/worker_thread.h" 20484543d1Sopenharmony_ci#include "eu/cpu_manager_interface.h" 21484543d1Sopenharmony_ci#include "c/executor_task.h" 22484543d1Sopenharmony_ci#include "sync/poller.h" 23484543d1Sopenharmony_ci#include "util/spmc_queue.h" 24484543d1Sopenharmony_ci#include "tm/cpu_task.h" 25484543d1Sopenharmony_ci 26484543d1Sopenharmony_cinamespace ffrt { 27484543d1Sopenharmony_ciconst unsigned int LOCAL_QUEUE_SIZE = 128; 28484543d1Sopenharmony_ciconst unsigned int STEAL_BUFFER_SIZE = LOCAL_QUEUE_SIZE / 2; 29484543d1Sopenharmony_ciclass CPUWorker : public WorkerThread { 30484543d1Sopenharmony_cipublic: 31484543d1Sopenharmony_ci CPUWorker(const QoS& qos, CpuWorkerOps&& ops) : WorkerThread(qos), ops(ops) 32484543d1Sopenharmony_ci { 33484543d1Sopenharmony_ci localFifo.Init(LOCAL_QUEUE_SIZE); 34484543d1Sopenharmony_ci#ifdef FFRT_PTHREAD_ENABLE 35484543d1Sopenharmony_ci Start(CPUWorker::WrapDispatch, this); 36484543d1Sopenharmony_ci#else 37484543d1Sopenharmony_ci Start(CPUWorker::Dispatch, this); 38484543d1Sopenharmony_ci#endif 39484543d1Sopenharmony_ci } 40484543d1Sopenharmony_ci 41484543d1Sopenharmony_ci CpuWorkerOps ops; 42484543d1Sopenharmony_ci SpmcQueue localFifo; 43484543d1Sopenharmony_ci void* priority_task = nullptr; 44484543d1Sopenharmony_ci unsigned int tick = 0; 45484543d1Sopenharmony_ci unsigned int global_interval = 60; 46484543d1Sopenharmony_ci unsigned int budget = 10; 47484543d1Sopenharmony_ci 48484543d1Sopenharmony_cipublic: 49484543d1Sopenharmony_ci /* strategy options for worklooper function */ 50484543d1Sopenharmony_ci static void WorkerLooperDefault(WorkerThread* p); 51484543d1Sopenharmony_ci 52484543d1Sopenharmony_ciprivate: 53484543d1Sopenharmony_ci static void* WrapDispatch(void* worker); 54484543d1Sopenharmony_ci static void Dispatch(CPUWorker* worker); 55484543d1Sopenharmony_ci static void Run(CPUEUTask* task, CPUWorker* worker); 56484543d1Sopenharmony_ci static void Run(ffrt_executor_task_t* task, ffrt_qos_t qos); 57484543d1Sopenharmony_ci static void RunTask(ffrt_executor_task_t* curtask, CPUWorker* worker); 58484543d1Sopenharmony_ci static void RunTaskLifo(ffrt_executor_task_t* task, CPUWorker* worker); 59484543d1Sopenharmony_ci static void* GetTask(CPUWorker* worker); 60484543d1Sopenharmony_ci static PollerRet TryPoll(CPUWorker* worker, int timeout); 61484543d1Sopenharmony_ci static bool LocalEmpty(CPUWorker* worker); 62484543d1Sopenharmony_ci}; 63484543d1Sopenharmony_ci} // namespace ffrt 64484543d1Sopenharmony_ci#endif 65