Line data Source code
1 : /**
2 : * Copyright (c) 2026 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 "device_comm.h"
12 :
13 : #include "tsd_hdc_client.h"
14 : #include "basic_define.h"
15 : #include "tsd_util_func.h"
16 : #include "tsd_log.h"
17 :
18 : namespace tsd {
19 : /**
20 : * @ingroup DeviceComm
21 : * @brief DeviceComm构造函数
22 : */
23 151 : DeviceComm::DeviceComm(const uint32_t devId, const DeviceCommType commType) : deviceId_(devId), commType_(commType) {}
24 :
25 : /**
26 : * @ingroup DeviceComm
27 : * @brief 静态工厂函数,根据 commType 生成具体子类实例并维护单例
28 : * @param [in] devId : 建立连接的Device设备ID
29 : * @param [in] commType : 设备通信类型
30 : * @return DeviceComm单例类实例
31 : */
32 94 : std::shared_ptr<DeviceComm> DeviceComm::GetInstance(const uint32_t devId, const DeviceCommType commType)
33 : {
34 : // 校验Client端,device ID范围[0-128)
35 94 : if (devId >= MAX_DEVNUM_PER_HOST) {
36 2 : TSD_ERROR("deviceId=%u is not supported, not in [0-128]", devId);
37 2 : return nullptr;
38 : }
39 92 : uint64_t curIndex = KeyCompose(devId, commType);
40 92 : std::shared_ptr<DeviceComm> deviceCommPtr = nullptr;
41 : {
42 92 : std::recursive_mutex* deviceCommMutex = MutexForDeviceCommMap();
43 92 : std::map<uint64_t, std::shared_ptr<DeviceComm>>* deviceCommMap = DeviceCommMap();
44 92 : std::unordered_map<uint32_t, CreatorFunc>* creatorMap = CreatorMap();
45 124 : TSD_CHECK(deviceCommMutex != nullptr, nullptr, "Fail to create deviceComm mutex");
46 92 : TSD_CHECK(deviceCommMap != nullptr, nullptr, "Fail to create deviceComm map");
47 92 : TSD_CHECK(creatorMap != nullptr, nullptr, "Fail to create creator map");
48 92 : const std::lock_guard<std::recursive_mutex> lk(*deviceCommMutex);
49 92 : const std::map<uint64_t, std::shared_ptr<DeviceComm>>::iterator iter = deviceCommMap->find(curIndex);
50 92 : if (iter != deviceCommMap->end()) {
51 30 : return iter->second;
52 : }
53 : const std::unordered_map<uint32_t, CreatorFunc>::iterator creatorIter =
54 62 : creatorMap->find(static_cast<uint32_t>(commType));
55 62 : if (creatorIter == creatorMap->end()) {
56 1 : TSD_ERROR("DeviceCommType=%u is not supported", static_cast<uint32_t>(commType));
57 1 : return nullptr;
58 : }
59 61 : deviceCommPtr = creatorIter->second(devId);
60 61 : TSD_CHECK((deviceCommPtr != nullptr), nullptr, "Fail to create deviceCommPtr");
61 60 : deviceCommMap->emplace(curIndex, deviceCommPtr);
62 92 : }
63 60 : return deviceCommPtr;
64 92 : }
65 :
66 19 : bool DeviceComm::Register(DeviceCommType type, CreatorFunc creator)
67 : {
68 19 : std::unordered_map<uint32_t, CreatorFunc>* creatorMap = CreatorMap();
69 19 : TSD_CHECK(creatorMap != nullptr, false, "Fail to create creator map");
70 19 : creatorMap->emplace(static_cast<uint32_t>(type), std::move(creator));
71 19 : return true;
72 : }
73 : } // namespace tsd
|