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_CO_ROUTINE_HPP 17484543d1Sopenharmony_ci#define FFRT_CO_ROUTINE_HPP 18484543d1Sopenharmony_ci#include <atomic> 19484543d1Sopenharmony_ci#include <functional> 20484543d1Sopenharmony_ci#include <thread> 21484543d1Sopenharmony_ci#include "co2_context.h" 22484543d1Sopenharmony_ci 23484543d1Sopenharmony_ci#if defined(__aarch64__) 24484543d1Sopenharmony_ciconstexpr size_t STACK_MAGIC = 0x7BCDABCDABCDABCD; 25484543d1Sopenharmony_ci#elif defined(__arm__) 26484543d1Sopenharmony_ciconstexpr size_t STACK_MAGIC = 0x7BCDABCD; 27484543d1Sopenharmony_ci#elif defined(__x86_64__) 28484543d1Sopenharmony_ciconstexpr size_t STACK_MAGIC = 0x7BCDABCDABCDABCD; 29484543d1Sopenharmony_ci#elif defined(__riscv) && __riscv_xlen == 64 30484543d1Sopenharmony_ciconstexpr size_t STACK_MAGIC = 0x7BCDABCDABCDABCD; 31484543d1Sopenharmony_ci#endif 32484543d1Sopenharmony_ci 33484543d1Sopenharmony_ci#ifndef FFRT_STACK_SIZE 34484543d1Sopenharmony_ci#define FFRT_STACK_SIZE (1 << 20) 35484543d1Sopenharmony_ci#endif 36484543d1Sopenharmony_ci 37484543d1Sopenharmony_cinamespace ffrt { 38484543d1Sopenharmony_ciclass CPUEUTask; 39484543d1Sopenharmony_cistruct WaitEntry; 40484543d1Sopenharmony_ci} // namespace ffrt 41484543d1Sopenharmony_cistruct CoRoutine; 42484543d1Sopenharmony_ci 43484543d1Sopenharmony_cienum class CoStatus { 44484543d1Sopenharmony_ci CO_UNINITIALIZED, 45484543d1Sopenharmony_ci CO_NOT_FINISH, 46484543d1Sopenharmony_ci CO_RUNNING, 47484543d1Sopenharmony_ci}; 48484543d1Sopenharmony_ci 49484543d1Sopenharmony_cienum class CoStackProtectType { 50484543d1Sopenharmony_ci CO_STACK_WEAK_PROTECT, 51484543d1Sopenharmony_ci CO_STACK_STRONG_PROTECT 52484543d1Sopenharmony_ci}; 53484543d1Sopenharmony_ci 54484543d1Sopenharmony_cienum class BlockType { 55484543d1Sopenharmony_ci BLOCK_COROUTINE, 56484543d1Sopenharmony_ci BLOCK_THREAD 57484543d1Sopenharmony_ci}; 58484543d1Sopenharmony_ci 59484543d1Sopenharmony_ciconstexpr uint64_t STACK_SIZE = FFRT_STACK_SIZE; 60484543d1Sopenharmony_ciconstexpr uint64_t MIN_STACK_SIZE = 32 * 1024; 61484543d1Sopenharmony_ci 62484543d1Sopenharmony_ciusing CoCtx = struct co2_context; 63484543d1Sopenharmony_ci 64484543d1Sopenharmony_cistruct CoRoutineEnv { 65484543d1Sopenharmony_ci CoRoutine* runningCo = nullptr; 66484543d1Sopenharmony_ci CoCtx schCtx; 67484543d1Sopenharmony_ci const std::function<bool(ffrt::CPUEUTask*)>* pending = nullptr; 68484543d1Sopenharmony_ci}; 69484543d1Sopenharmony_ci 70484543d1Sopenharmony_cistruct StackMem { 71484543d1Sopenharmony_ci uint64_t size; 72484543d1Sopenharmony_ci size_t magic; 73484543d1Sopenharmony_ci uint8_t stk[8]; 74484543d1Sopenharmony_ci}; 75484543d1Sopenharmony_ci 76484543d1Sopenharmony_cistruct CoRoutine { 77484543d1Sopenharmony_ci std::atomic_int status; 78484543d1Sopenharmony_ci CoRoutineEnv* thEnv; 79484543d1Sopenharmony_ci ffrt::CPUEUTask* task; 80484543d1Sopenharmony_ci CoCtx ctx; 81484543d1Sopenharmony_ci uint64_t allocatedSize; // CoRoutine allocated size 82484543d1Sopenharmony_ci bool isTaskDone = false; 83484543d1Sopenharmony_ci StackMem stkMem; 84484543d1Sopenharmony_ci}; 85484543d1Sopenharmony_ci 86484543d1Sopenharmony_cistruct CoStackAttr { 87484543d1Sopenharmony_cipublic: 88484543d1Sopenharmony_ci explicit CoStackAttr(uint64_t coSize = STACK_SIZE, CoStackProtectType coType = 89484543d1Sopenharmony_ci CoStackProtectType::CO_STACK_WEAK_PROTECT) 90484543d1Sopenharmony_ci { 91484543d1Sopenharmony_ci size = coSize; 92484543d1Sopenharmony_ci type = coType; 93484543d1Sopenharmony_ci } 94484543d1Sopenharmony_ci ~CoStackAttr() {} 95484543d1Sopenharmony_ci uint64_t size; 96484543d1Sopenharmony_ci CoStackProtectType type; 97484543d1Sopenharmony_ci 98484543d1Sopenharmony_ci static inline CoStackAttr* Instance(uint64_t coSize = STACK_SIZE, 99484543d1Sopenharmony_ci CoStackProtectType coType = CoStackProtectType::CO_STACK_WEAK_PROTECT) 100484543d1Sopenharmony_ci { 101484543d1Sopenharmony_ci static CoStackAttr inst(coSize, coType); 102484543d1Sopenharmony_ci return &inst; 103484543d1Sopenharmony_ci } 104484543d1Sopenharmony_ci}; 105484543d1Sopenharmony_ci 106484543d1Sopenharmony_ciclass CoRoutineFactory { 107484543d1Sopenharmony_cipublic: 108484543d1Sopenharmony_ci using CowakeCB = std::function<void (ffrt::CPUEUTask*, bool)>; 109484543d1Sopenharmony_ci 110484543d1Sopenharmony_ci static CoRoutineFactory &Instance(); 111484543d1Sopenharmony_ci 112484543d1Sopenharmony_ci static void CoWakeFunc(ffrt::CPUEUTask* task, bool timeOut) 113484543d1Sopenharmony_ci { 114484543d1Sopenharmony_ci return Instance().cowake_(task, timeOut); 115484543d1Sopenharmony_ci } 116484543d1Sopenharmony_ci 117484543d1Sopenharmony_ci static void RegistCb(const CowakeCB &cowake) 118484543d1Sopenharmony_ci { 119484543d1Sopenharmony_ci Instance().cowake_ = cowake; 120484543d1Sopenharmony_ci } 121484543d1Sopenharmony_ciprivate: 122484543d1Sopenharmony_ci CowakeCB cowake_; 123484543d1Sopenharmony_ci}; 124484543d1Sopenharmony_ci 125484543d1Sopenharmony_civoid CoStackFree(void); 126484543d1Sopenharmony_civoid CoWorkerExit(void); 127484543d1Sopenharmony_ci 128484543d1Sopenharmony_ciint CoStart(ffrt::CPUEUTask* task); 129484543d1Sopenharmony_civoid CoYield(void); 130484543d1Sopenharmony_ci 131484543d1Sopenharmony_civoid CoWait(const std::function<bool(ffrt::CPUEUTask*)>& pred); 132484543d1Sopenharmony_civoid CoWake(ffrt::CPUEUTask* task, bool timeOut); 133484543d1Sopenharmony_ci 134484543d1Sopenharmony_ci#ifdef FFRT_TASK_LOCAL_ENABLE 135484543d1Sopenharmony_civoid TaskTsdDeconstruct(ffrt::CPUEUTask* task); 136484543d1Sopenharmony_ci#endif 137484543d1Sopenharmony_ci 138484543d1Sopenharmony_ci#endif 139