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 _REF_FUNCTION_HEADER_H_ 17 #define _REF_FUNCTION_HEADER_H_ 18 19 #include <atomic> 20 #include "dfx/log/ffrt_log_api.h" 21 #include "c/type_def.h" 22 23 namespace ffrt { 24 enum FuncHeaderStatus { 25 NOT_SUBMIT = 1, 26 ALREADY_SUBMIT = 2, 27 }; 28 29 void DestroyFunctionWrapper(ffrt_function_header_t* f, ffrt_function_kind_t kind); 30 31 class RefFunctionHeader { 32 public: RefFunctionHeader(ffrt_function_header_t * header)33 RefFunctionHeader(ffrt_function_header_t* header) : functionHeader_(header) {} IncDeleteRef()34 bool IncDeleteRef() 35 { 36 uint16_t exp = NOT_SUBMIT; 37 uint16_t des = ALREADY_SUBMIT; 38 return rc.compare_exchange_strong(exp, des, std::memory_order_relaxed); 39 } DecDeleteRef()40 void DecDeleteRef() 41 { 42 auto v = rc.fetch_sub(1); 43 if (v == 1) { 44 delete this; 45 } 46 } 47 48 ffrt_function_header_t* functionHeader_ = nullptr; 49 std::atomic_uint16_t rc = 1; 50 private: ~RefFunctionHeader()51 ~RefFunctionHeader() 52 { 53 ffrt::DestroyFunctionWrapper(functionHeader_, ffrt_function_kind_general); 54 } 55 }; 56 } 57 #endif