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