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 : #ifndef HCCLV2_INNER_NET_DEVICE_MANAGER_H
12 : #define HCCLV2_INNER_NET_DEVICE_MANAGER_H
13 :
14 : #include <mutex>
15 : #include <shared_mutex>
16 : #include <unordered_map>
17 : #include "port.h"
18 : #include "orion_adapter_hccp.h"
19 : #include "ip_address.h"
20 : #include "inner_net_dev.h"
21 : #include "net_device.h"
22 :
23 : namespace Hccl {
24 : class InnerNetDevManager {
25 : public:
26 : // 获取单例实例(线程安全)
27 : static InnerNetDevManager& GetInstance();
28 :
29 : // 删除拷贝构造和赋值运算符,防止实例复制
30 : InnerNetDevManager(const InnerNetDevManager&) = delete;
31 : InnerNetDevManager& operator=(const InnerNetDevManager&) = delete;
32 :
33 : // 添加设备(增加引用计数)
34 : HcclResult AddDevice(const NetDevInfo& info, HcclNetDevice*& device);
35 :
36 : // 删除设备(减少引用计数,计数为0时删除)
37 : HcclResult DeleteDevice(HcclNetDevice* device);
38 :
39 : // 删除设备(减少引用计数,计数为0时删除)
40 : HcclResult RemoveDevice(const NetDevInfo& info);
41 :
42 : private:
43 : // 私有构造函数和析构函数
44 2 : InnerNetDevManager() = default;
45 2 : ~InnerNetDevManager() = default;
46 :
47 : std::unordered_map<NetDevInfo, unique_ptr<InnerNetDev>> netDevMap_;
48 : std::unordered_map<NetDevInfo, uint32_t> netDevCnt_;
49 :
50 : mutable std::shared_mutex mtx_{};
51 : };
52 :
53 : } // namespace Hccl
54 :
55 : #endif // HCCLV2_INNER_NET_DEVICE_MANAGER_H
|