28
28
#include < mxnet/engine.h>
29
29
#include " ./engine/openmp.h"
30
30
#include " ./operator/custom/custom-inl.h"
31
- #include " ./common/library.h"
32
31
#if MXNET_USE_OPENCV
33
32
#include < opencv2/opencv.hpp>
34
33
#endif // MXNET_USE_OPENCV
35
34
#include " common/utils.h"
36
35
#include " engine/openmp.h"
37
36
38
- #if defined(_WIN32) || defined(_WIN64) || defined(__WINDOWS__)
39
- #include < windows.h>
40
- #else
41
- #include < dlfcn.h>
42
- #endif
43
37
44
38
#if defined(_WIN32) || defined(_WIN64) || defined(__WINDOWS__)
39
+ #include < windows.h>
45
40
/* !
46
41
* \brief Retrieve the system error message for the last-error code
47
42
* \param err string that gets the error message
@@ -58,9 +53,10 @@ void win_err(char **err) {
58
53
reinterpret_cast <char *>(err),
59
54
0 , NULL );
60
55
}
56
+ #else
57
+ #include < dlfcn.h>
61
58
#endif
62
59
63
-
64
60
namespace mxnet {
65
61
66
62
#if MXNET_USE_SIGNAL_HANDLER && DMLC_LOG_STACK_TRACE
@@ -106,6 +102,91 @@ LibraryInitializer::~LibraryInitializer() {
106
102
close_open_libs ();
107
103
}
108
104
105
+ bool LibraryInitializer::lib_is_loaded (const std::string& path) const {
106
+ return loaded_libs.count (path) > 0 ;
107
+ }
108
+
109
+ /* !
110
+ * \brief Loads the dynamic shared library file
111
+ * \param path library file location
112
+ * \return handle a pointer for the loaded library, throws dmlc::error if library can't be loaded
113
+ */
114
+ void * LibraryInitializer::lib_load (const char * path) {
115
+ void *handle = nullptr ;
116
+ // check if library was already loaded
117
+ if (!lib_is_loaded (path)) {
118
+ // if not, load it
119
+ #if defined(_WIN32) || defined(_WIN64) || defined(__WINDOWS__)
120
+ handle = LoadLibrary (path);
121
+ if (!handle) {
122
+ char *err_msg = nullptr ;
123
+ win_err (&err_msg);
124
+ LOG (FATAL) << " Error loading library: '" << path << " '\n " << err_msg;
125
+ LocalFree (err_msg);
126
+ return nullptr ;
127
+ }
128
+ #else
129
+ handle = dlopen (path, RTLD_LAZY);
130
+ if (!handle) {
131
+ LOG (FATAL) << " Error loading library: '" << path << " '\n " << dlerror ();
132
+ return nullptr ;
133
+ }
134
+ #endif // _WIN32 or _WIN64 or __WINDOWS__
135
+ // then store the pointer to the library
136
+ loaded_libs[path] = handle;
137
+ } else {
138
+ loaded_libs.at (path);
139
+ }
140
+ return handle;
141
+ }
142
+
143
+ /* !
144
+ * \brief Closes the loaded dynamic shared library file
145
+ * \param handle library file handle
146
+ */
147
+ void LibraryInitializer::lib_close (void * handle) {
148
+ std::string libpath;
149
+ for (const auto & l: loaded_libs) {
150
+ if (l.second == handle) {
151
+ libpath = l.first ;
152
+ break ;
153
+ }
154
+ }
155
+ CHECK (!libpath.empty ());
156
+ #if defined(_WIN32) || defined(_WIN64) || defined(__WINDOWS__)
157
+ FreeLibrary ((HMODULE)handle);
158
+ #else
159
+ if (dlclose (handle)) {
160
+ LOG (WARNING) << " LibraryInitializer::lib_close: couldn't close library at address: " << handle
161
+ << " loaded from: '" << libpath << " ': " << dlerror ();
162
+ }
163
+ #endif // _WIN32 or _WIN64 or __WINDOWS__
164
+ loaded_libs.erase (libpath);
165
+ }
166
+
167
+ /* !
168
+ * \brief Obtains address of given function in the loaded library
169
+ * \param handle pointer for the loaded library
170
+ * \param func function pointer that gets output address
171
+ * \param name function name to be fetched
172
+ */
173
+ void LibraryInitializer::get_sym (void * handle, void ** func, char * name) {
174
+ #if defined(_WIN32) || defined(_WIN64) || defined(__WINDOWS__)
175
+ *func = GetProcAddress ((HMODULE)handle, name);
176
+ if (!(*func)) {
177
+ char *err_msg = nullptr ;
178
+ win_err (&err_msg);
179
+ LOG (FATAL) << " Error getting function '" << name << " ' from library\n " << err_msg;
180
+ LocalFree (err_msg);
181
+ }
182
+ #else
183
+ *func = dlsym (handle, name);
184
+ if (!(*func)) {
185
+ LOG (FATAL) << " Error getting function '" << name << " ' from library\n " << dlerror ();
186
+ }
187
+ #endif // _WIN32 or _WIN64 or __WINDOWS__
188
+ }
189
+
109
190
bool LibraryInitializer::was_forked () const {
110
191
return common::current_process_id () != original_pid_;
111
192
}
@@ -153,15 +234,11 @@ void LibraryInitializer::install_signal_handlers() {
153
234
}
154
235
155
236
void LibraryInitializer::close_open_libs () {
156
- for (auto const & lib : loaded_libs) {
157
- close_lib (lib .second );
237
+ for (const auto & l : loaded_libs) {
238
+ lib_close (l .second );
158
239
}
159
240
}
160
241
161
- void LibraryInitializer::dynlib_defer_close (const std::string &path, void *handle) {
162
- loaded_libs.emplace (path, handle);
163
- }
164
-
165
242
/* *
166
243
* Perform static initialization
167
244
*/
0 commit comments