77 * LICENSE file in the root directory of this source tree.
88 */
99
10- #include < string>
1110#include < executorch/runtime/core/error.h>
11+ #include < string>
1212
1313#ifdef _WIN32
1414#include < windows.h>
1515#else
1616#include < dlfcn.h>
17+ #include < unistd.h>
1718#endif
1819
1920namespace executorch {
@@ -23,52 +24,75 @@ namespace cuda {
2324Result<void *> load_library (const std::string& path) {
2425#ifdef _WIN32
2526 auto lib_handle = LoadLibrary (path.c_str ());
26- if (hModule == NULL ) {
27- ET_LOG (Error, " Failed to load %s with error: %lu" , path.c_str (), GetLastError ());
28- return Error::AccessFailed;
27+ if (lib_handle == NULL ) {
28+ ET_LOG (
29+ Error,
30+ " Failed to load %s with error: %lu" ,
31+ path.c_str (),
32+ GetLastError ());
33+ return Error::AccessFailed;
2934 }
3035
3136#else
3237 void * lib_handle = dlopen (path.c_str (), RTLD_LAZY | RTLD_LOCAL);
33- if (so_handle == nullptr ) {
38+ if (lib_handle == nullptr ) {
3439 ET_LOG (Error, " Failed to load %s with error: %s" , path.c_str (), dlerror ());
3540 return Error::AccessFailed;
3641 }
3742#endif
38- return (void *) lib_handle;
43+ return (void *)lib_handle;
3944}
4045
4146Error close_library (void * lib_handle) {
4247#ifdef _WIN32
43- if (!FreeLibrary ((HModule )lib_handle)) {
44- printf (" FreeLibrary failed with error %lu\n " , GetLastError ());
45- return Error::Internal
46- }
48+ if (!FreeLibrary ((HMODULE )lib_handle)) {
49+ printf (" FreeLibrary failed with error %lu\n " , GetLastError ());
50+ return Error::Internal;
51+ }
4752#else
4853 if (dlclose (lib_handle) != 0 ) {
49- ET_LOG (Error, " dlclose failed: %s\n " , dlerror ());
50- return Error::Internal;
54+ ET_LOG (Error, " dlclose failed: %s\n " , dlerror ());
55+ return Error::Internal;
5156 }
5257#endif
5358 return Error::Ok;
5459}
5560
5661Result<void *> get_function (void * lib_handle, const std::string& fn_name) {
5762#ifdef _WIN32
58- auto fn = GetProcAddress ((HModule )lib_handle, fn_name.c_str ());
63+ auto fn = GetProcAddress ((HMODULE )lib_handle, fn_name.c_str ());
5964 if (!fn) {
60- ET_LOG (Error, " Failed loading symbol %s with error %lu\n " , fn_name.c_str (), GetLastError ());
61- return Error::Internal;
65+ ET_LOG (
66+ Error,
67+ " Failed loading symbol %s with error %lu\n " ,
68+ fn_name.c_str (),
69+ GetLastError ());
70+ return Error::Internal;
6271 }
6372#else
6473 auto fn = dlsym (lib_handle, fn_name.c_str ());
6574 if (fn == nullptr ) {
66- ET_LOG (Error, " Failed loading symbol %s with error %s\n " , fn_name.c_str (), dlerror ());
67- return Error::Internal;
75+ ET_LOG (
76+ Error,
77+ " Failed loading symbol %s with error %s\n " ,
78+ fn_name.c_str (),
79+ dlerror ());
80+ return Error::Internal;
6881 }
6982#endif
7083
71- return (void *)fn; // This I think is technically ub on windows. We should probably explicitly pack the bytes.
84+ return (void *)fn; // This I think is technically ub on windows. We should
85+ // probably explicitly pack the bytes.
86+ }
87+
88+ int32_t get_process_id (void * lib_handle) {
89+ #ifdef _WIN32
90+ return GetCurrentProcessId ();
91+ #else
92+ return getpid ();
93+ #endif
94+ }
95+
7296}
7397
7498} // cuda
0 commit comments