DPC++ Runtime
Runtime libraries for oneAPI DPC++
windows_pi.cpp
Go to the documentation of this file.
1 //==---------------- windows_pi.cpp ----------------------------------------==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 
11 #include <cassert>
12 #include <string>
13 #include <windows.h>
14 #include <winreg.h>
15 
16 namespace sycl {
18 namespace detail {
19 namespace pi {
20 
21 void *loadOsLibrary(const std::string &PluginPath) {
22  // Tells the system to not display the critical-error-handler message box.
23  // Instead, the system sends the error to the calling process.
24  // This is crucial for graceful handling of plugins that couldn't be
25  // loaded, e.g. due to missing native run-times.
26  // TODO: add reporting in case of an error.
27  // NOTE: we restore the old mode to not affect user app behavior.
28  //
29  UINT SavedMode = SetErrorMode(SEM_FAILCRITICALERRORS);
30  // Exclude current directory from DLL search path
31  if (!SetDllDirectoryA("")) {
32  assert(false && "Failed to update DLL search path");
33  }
34  auto Result = (void *)LoadLibraryA(PluginPath.c_str());
35  (void)SetErrorMode(SavedMode);
36  if (!SetDllDirectoryA(nullptr)) {
37  assert(false && "Failed to restore DLL search path");
38  }
39 
40  return Result;
41 }
42 
43 int unloadOsLibrary(void *Library) {
44  // The mock plugin does not have an associated library, so we allow nullptr
45  // here to avoid it trying to free a non-existent library.
46  if (!Library)
47  return 1;
48  return (int)FreeLibrary((HMODULE)Library);
49 }
50 
51 void *getOsLibraryFuncAddress(void *Library, const std::string &FunctionName) {
52  return reinterpret_cast<void *>(
53  GetProcAddress((HMODULE)Library, FunctionName.c_str()));
54 }
55 
56 } // namespace pi
57 } // namespace detail
58 } // __SYCL_INLINE_VER_NAMESPACE(_V1)
59 } // namespace sycl
#define __SYCL_INLINE_VER_NAMESPACE(X)
int unloadOsLibrary(void *Library)
Definition: posix_pi.cpp:32
void * loadOsLibrary(const std::string &Library)
Definition: posix_pi.cpp:20
void * getOsLibraryFuncAddress(void *Library, const std::string &FunctionName)
Definition: posix_pi.cpp:40
---— Error handling, matching OpenCL plugin semantics.
Definition: access.hpp:14