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 "ccu_instance_mgr.h"
12 :
13 : #include "ccu_log.h"
14 : #include "hccl_common.h"
15 :
16 : namespace hcomm {
17 :
18 132 : CcuInstanceMgr::~CcuInstanceMgr()
19 : {
20 132 : if (!initializedFlag_) {
21 132 : return;
22 : }
23 :
24 0 : (void)Deinit();
25 132 : }
26 :
27 126 : CcuInstanceMgr &CcuInstanceMgr::GetInstance(const int32_t deviceLogicId)
28 : {
29 258 : static CcuInstanceMgr instanceMgrs[MAX_MODULE_DEVICE_NUM + 1];
30 :
31 126 : int32_t devLogicId = deviceLogicId;
32 126 : if (devLogicId < 0 || static_cast<uint32_t>(devLogicId) >= MAX_MODULE_DEVICE_NUM) {
33 0 : HCCL_WARNING("[CcuInstanceMgr][%s] use the backup device, devLogicId[%d] should be "
34 : "less than %u.", __func__, devLogicId, MAX_MODULE_DEVICE_NUM);
35 0 : devLogicId = MAX_MODULE_DEVICE_NUM; // 使用备份设备
36 : }
37 :
38 126 : instanceMgrs[devLogicId].devLogicId_ = devLogicId;
39 126 : return instanceMgrs[devLogicId];
40 : }
41 :
42 0 : CcuResult CcuInstanceMgr::Init()
43 : {
44 0 : std::unique_lock<std::shared_timed_mutex> lock(insMapMutex_);
45 0 : if (initializedFlag_) {
46 0 : return CcuResult::CCU_SUCCESS;
47 : }
48 :
49 0 : initializedFlag_ = true;
50 0 : insMap_.clear();
51 0 : return CcuResult::CCU_SUCCESS;
52 0 : }
53 :
54 0 : CcuResult CcuInstanceMgr::Deinit()
55 : {
56 0 : std::unique_lock<std::shared_timed_mutex> lock(insMapMutex_);
57 0 : insMap_.clear();
58 0 : initializedFlag_ = false;
59 0 : return CcuResult::CCU_SUCCESS;
60 0 : }
61 :
62 27 : CcuResult CcuInstanceMgr::Create(
63 : CcuInstanceType insType, CcuInsHandle &insHandle)
64 : {
65 27 : std::unique_lock<std::shared_timed_mutex> lock(insMapMutex_);
66 :
67 27 : std::unique_ptr<CcuInstance> instance{nullptr};
68 27 : EXCEPTION_CATCH(
69 : instance = std::make_unique<CcuInstance>(insType),
70 : return CcuResult::CCU_E_PTR);
71 :
72 27 : CCU_CHK_RET(instance->Init());
73 :
74 27 : instanceId_ += 1;
75 27 : insMap_.emplace(instanceId_, std::move(instance));
76 27 : insHandle = instanceId_;
77 27 : return CcuResult::CCU_SUCCESS;
78 27 : }
79 :
80 71 : CcuInstance *CcuInstanceMgr::Get(CcuInsHandle insHandle) const
81 : {
82 71 : std::shared_lock<std::shared_timed_mutex> lock(insMapMutex_);
83 71 : auto it = insMap_.find(insHandle);
84 71 : if (it == insMap_.end()) {
85 0 : HCCL_ERROR("[CcuInstanceMgr][%s] handle[%llx] is not existed.",
86 : __func__, insHandle);
87 0 : return nullptr;
88 : }
89 :
90 71 : return it->second.get();
91 71 : }
92 :
93 27 : CcuResult CcuInstanceMgr::Destroy(CcuInsHandle insHandle)
94 : {
95 27 : std::unique_lock<std::shared_timed_mutex> lock(insMapMutex_);
96 27 : auto it = insMap_.find(insHandle);
97 27 : if (it == insMap_.end()) {
98 0 : HCCL_ERROR("[CcuInstanceMgr][%s] handle[%llx] is not existed.",
99 : __func__, insHandle);
100 0 : return CcuResult::CCU_E_NOT_FOUND;
101 : }
102 :
103 27 : insMap_.erase(it);
104 27 : return CcuResult::CCU_SUCCESS;
105 27 : }
106 :
107 : } // namespace hcomm
|