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 "hccp_tlv_hdc_manager.h"
12 : #include "orion_adapter_rts.h"
13 : #include "socket_handle_manager.h"
14 :
15 : namespace Hccl {
16 :
17 1 : HccpTlvHdcManager::HccpTlvHdcManager() { tlvHandleMap.resize(MAX_DEVICE_NUM); }
18 :
19 986 : HccpTlvHdcManager& HccpTlvHdcManager::GetInstance()
20 : {
21 986 : static HccpTlvHdcManager HccpTlvHdcManager;
22 986 : return HccpTlvHdcManager;
23 : }
24 :
25 960 : void* HccpTlvHdcManager::GetTlvHandle(s32 deviceLogicId)
26 : {
27 960 : if (tlvHandleMap[deviceLogicId] == nullptr) {
28 4 : Init(deviceLogicId);
29 : }
30 960 : return tlvHandleMap[deviceLogicId];
31 : }
32 :
33 5 : void HccpTlvHdcManager::Init(s32 deviceLogicId)
34 : {
35 5 : std::unique_lock<std::mutex> lock(managerMutex);
36 5 : if (instances.count(deviceLogicId) != 0) {
37 1 : return;
38 : }
39 :
40 4 : HRaTlvInitConfig cfg;
41 4 : cfg.phyId = HrtGetDevicePhyIdByIndex(deviceLogicId);
42 4 : cfg.mode = HrtNetworkMode::HDC;
43 4 : cfg.version = 1;
44 4 : tlvHandleMap[deviceLogicId] = HrtRaTlvInit(cfg);
45 :
46 4 : instances.insert(deviceLogicId);
47 5 : }
48 :
49 1 : void HccpTlvHdcManager::DestroyAll()
50 : {
51 1 : std::unique_lock<std::mutex> lock(managerMutex);
52 5 : for (auto deviceLogicId : instances) {
53 12 : HCCL_INFO("HccpTlvHdcManager deinit");
54 :
55 4 : void* tlv_handle = tlvHandleMap[deviceLogicId];
56 4 : if (tlv_handle == nullptr) {
57 0 : continue;
58 : }
59 4 : HrtRaTlvDeInit(tlv_handle); // 要保证DestroyAll只有析构函数调用
60 :
61 4 : tlvHandleMap[deviceLogicId] = nullptr;
62 : }
63 1 : instances.clear();
64 1 : }
65 :
66 1 : HccpTlvHdcManager::~HccpTlvHdcManager() { DECTOR_TRY_CATCH("HccpTlvHdcManager", DestroyAll()); }
67 :
68 : } // namespace Hccl
|