LCOV - code coverage report
Current view: top level - acl/aclrt_impl - init_callback_manager.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 63 65 96.9 %
Date: 2026-08-27 13:24:42 Functions: 8 8 100.0 %

          Line data    Source code
       1             : /**
       2             :  * Copyright (c) 2025 Huawei Technologies Co., Ltd.
       3             :  * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
       4             :  * CANN Open Software License Agreement Version 2.0 (the "License").
       5             :  * Please refer to the License for details. You may not use this file except in compliance with the License.
       6             :  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
       7             :  * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
       8             :  * See LICENSE in the root of the software repository for the full text of the License.
       9             :  */
      10             : 
      11             : #include "init_callback_manager.h"
      12             : #include "acl/acl_base.h"
      13             : #include "acl_rt_impl_base.h"
      14             : 
      15             : namespace acl {
      16         260 : InitCallbackManager &InitCallbackManager::GetInstance()
      17             : {
      18             :     // 单例模式上下文不做判空和捕获异常,内存分配失败这种极端情况让程序正常终止,比引入更复杂的错误处理逻辑更合理
      19             :     // 这里单例在堆上申请且内存不显式释放,是考虑到so卸载顺序的问题,延长单例的生命周期确保不引入异常
      20         260 :     static InitCallbackManager *instance = new InitCallbackManager();
      21         260 :     return *instance;
      22             : }
      23             : 
      24           1 : InitCallbackManager::InitCallbackManager() : mutex_(GetAclInitMutex()) {}
      25             : 
      26          16 : aclError InitCallbackManager::RegInitCallback(aclRegisterCallbackType type, aclInitCallbackFunc cbFunc, void *userData)
      27             : {
      28          16 :     if (cbFunc == nullptr) {
      29           1 :         return ACL_ERROR_INVALID_PARAM;
      30             :     }
      31          30 :     std::lock_guard<std::recursive_mutex> lock(mutex_);
      32          15 :     if (type != ACL_REG_TYPE_OTHER && initCallbackMap_.count(type) != 0U) {
      33           2 :         return ACL_ERROR_INTERNAL_ERROR;
      34             :     }
      35          13 :     initCallbackMap_.insert({type, {cbFunc, userData}});
      36             :     // 已经初始化的情况下,需要立即执行
      37          13 :     if (GetAclInitFlag()) {
      38           4 :         auto &configData = GetConfigPathStr();
      39           4 :         (void)cbFunc(configData.c_str(), configData.size(), userData);
      40             :     }
      41          13 :     return ACL_SUCCESS;
      42             : }
      43             : 
      44          10 : aclError InitCallbackManager::UnRegInitCallback(aclRegisterCallbackType type, aclInitCallbackFunc cbFunc)
      45             : {
      46          10 :     if (cbFunc == nullptr) {
      47           0 :         return ACL_ERROR_INVALID_PARAM;
      48             :     }
      49          20 :     std::lock_guard<std::recursive_mutex> lock(mutex_);
      50          10 :     if (initCallbackMap_.count(type) == 0U) {
      51           3 :         return ACL_ERROR_INTERNAL_ERROR;
      52             :     }
      53           7 :     const auto range = initCallbackMap_.equal_range(type);
      54           8 :     for (auto it = range.first; it != range.second;) {
      55           7 :         if (it->second.first == cbFunc) {
      56           6 :             initCallbackMap_.erase(it);
      57           6 :             return ACL_SUCCESS;
      58             :         }
      59           1 :         ++it;
      60             :     }
      61           1 :     return ACL_ERROR_INTERNAL_ERROR;
      62             : }
      63             : 
      64         110 : aclError InitCallbackManager::NotifyInitCallback(aclRegisterCallbackType type,
      65             :                                                  const char *configStr, size_t len)
      66             : {
      67         220 :     std::lock_guard<std::recursive_mutex> lock(mutex_);
      68         110 :     const auto range = initCallbackMap_.equal_range(type);
      69         111 :     for (auto it = range.first; it != range.second; ++it) {
      70             :         // callback可以保证不为空
      71           8 :         auto ret = it->second.first(configStr, len, it->second.second);
      72           8 :         if (ret != ACL_SUCCESS) {
      73           7 :             return ret;
      74             :         }
      75             :     }
      76         103 :     return ACL_SUCCESS;
      77             : }
      78             : 
      79          18 : aclError InitCallbackManager::RegFinalizeCallback(aclRegisterCallbackType type, aclFinalizeCallbackFunc cbFunc,
      80             :                                                   void *userData)
      81             : {
      82          18 :     if (cbFunc == nullptr) {
      83           1 :         return ACL_ERROR_INVALID_PARAM;
      84             :     }
      85          34 :     std::lock_guard<std::recursive_mutex> lock(mutex_);
      86          17 :     if (type != ACL_REG_TYPE_OTHER && finalizeCallbackMap_.count(type) != 0U) {
      87           2 :         return ACL_ERROR_INTERNAL_ERROR;
      88             :     }
      89          15 :     finalizeCallbackMap_.insert({type, {cbFunc, userData}});
      90          15 :     return ACL_SUCCESS;
      91             : }
      92             : 
      93          14 : aclError InitCallbackManager::UnRegFinalizeCallback(aclRegisterCallbackType type, aclFinalizeCallbackFunc cbFunc)
      94             : {
      95          14 :     if (cbFunc == nullptr) {
      96           0 :         return ACL_ERROR_INVALID_PARAM;
      97             :     }
      98          28 :     std::lock_guard<std::recursive_mutex> lock(mutex_);
      99          14 :     if (finalizeCallbackMap_.count(type) == 0U) {
     100           3 :         return ACL_ERROR_INTERNAL_ERROR;
     101             :     }
     102          11 :     const auto range = finalizeCallbackMap_.equal_range(type);
     103          12 :     for (auto it = range.first; it != range.second;) {
     104          11 :         if (it->second.first == cbFunc) {
     105          10 :             finalizeCallbackMap_.erase(it);
     106          10 :             return ACL_SUCCESS;
     107             :         }
     108           1 :         ++it;
     109             :     }
     110           1 :     return ACL_ERROR_INTERNAL_ERROR;
     111             : }
     112             : 
     113         105 : aclError InitCallbackManager::NotifyFinalizeCallback(aclRegisterCallbackType type)
     114             : {
     115         210 :     std::lock_guard<std::recursive_mutex> lock(mutex_);
     116         105 :     const auto range = finalizeCallbackMap_.equal_range(type);
     117         106 :     for (auto it = range.first; it != range.second; ++it) {
     118             :         // callback可以保证不为空
     119           8 :         auto ret = it->second.first(it->second.second);
     120           8 :         if (ret != ACL_SUCCESS) {
     121           7 :             return ret;
     122             :         }
     123             :     }
     124          98 :     return ACL_SUCCESS;
     125             : }
     126             : }

Generated by: LCOV version 1.14