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