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 DLRTS_FUNCTION_H
12 : #define DLRTS_FUNCTION_H
13 :
14 : #include <mutex>
15 : #include "hccl_dl.h"
16 : #include "log.h"
17 :
18 : template<const char *soName>
19 : class DlRtsFunction {
20 : public:
21 : explicit DlRtsFunction() = default;
22 129 : virtual ~DlRtsFunction()
23 : {
24 129 : if (handle_ != nullptr) {
25 2 : HcclDlclose(handle_);
26 2 : handle_ = nullptr;
27 : }
28 129 : }
29 :
30 13 : template<const char *funcName> void *Handle()
31 : {
32 13 : Init();
33 13 : if (handle_ == nullptr) {
34 7 : return nullptr;
35 : }
36 6 : return HcclDlsym(handle_, funcName);
37 : }
38 : private:
39 13 : void Init()
40 : {
41 13 : std::lock_guard<std::mutex> lock(handleMutex_);
42 13 : if (isInit_) {
43 4 : return;
44 : }
45 9 : HCCL_INFO("open so: %s", soName);
46 9 : handle_ = HcclDlopen(soName, RTLD_NOW);
47 9 : const char* errMsg = dlerror();
48 9 : CHK_PRT_RET(handle_ == nullptr, HCCL_ERROR("dlopen [%s] failed, %s", soName,\
49 : (errMsg == nullptr) ? "please check the file exist or permission denied." : errMsg),);
50 2 : isInit_ = true;
51 13 : }
52 : void *handle_ = nullptr;
53 : std::mutex handleMutex_;
54 : bool isInit_ = false;
55 : };
56 : #endif
|