runtime_loader.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. Copyright (c) 2005-2020 Intel Corporation
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #include "internal/_deprecated_header_message_guard.h"
  14. #if !defined(__TBB_show_deprecation_message_runtime_loader_H) && defined(__TBB_show_deprecated_header_message)
  15. #define __TBB_show_deprecation_message_runtime_loader_H
  16. #pragma message("TBB Warning: tbb/runtime_loader.h is deprecated. For details, please see Deprecated Features appendix in the TBB reference manual.")
  17. #endif
  18. #if defined(__TBB_show_deprecated_header_message)
  19. #undef __TBB_show_deprecated_header_message
  20. #endif
  21. #ifndef __TBB_runtime_loader_H
  22. #define __TBB_runtime_loader_H
  23. #define __TBB_runtime_loader_H_include_area
  24. #include "internal/_warning_suppress_enable_notice.h"
  25. #if ! TBB_PREVIEW_RUNTIME_LOADER
  26. #error Set TBB_PREVIEW_RUNTIME_LOADER to include runtime_loader.h
  27. #endif
  28. #include "tbb_stddef.h"
  29. #include <climits>
  30. #if _MSC_VER
  31. #if ! __TBB_NO_IMPLICIT_LINKAGE
  32. #ifdef _DEBUG
  33. #pragma comment( linker, "/nodefaultlib:tbb_debug.lib" )
  34. #pragma comment( linker, "/defaultlib:tbbproxy_debug.lib" )
  35. #else
  36. #pragma comment( linker, "/nodefaultlib:tbb.lib" )
  37. #pragma comment( linker, "/defaultlib:tbbproxy.lib" )
  38. #endif
  39. #endif
  40. #endif
  41. namespace tbb {
  42. namespace interface6 {
  43. //! Load TBB at runtime.
  44. /*!
  45. \b Usage:
  46. In source code:
  47. \code
  48. #include "tbb/runtime_loader.h"
  49. char const * path[] = { "<install dir>/lib/ia32", NULL };
  50. tbb::runtime_loader loader( path );
  51. // Now use TBB.
  52. \endcode
  53. Link with \c tbbproxy.lib (or \c libtbbproxy.a) instead of \c tbb.lib (\c libtbb.dylib,
  54. \c libtbb.so).
  55. TBB library will be loaded at runtime from \c <install dir>/lib/ia32 directory.
  56. \b Attention:
  57. All \c runtime_loader objects (in the same module, i.e. exe or dll) share some global state.
  58. The most noticeable piece of global state is loaded TBB library.
  59. There are some implications:
  60. - Only one TBB library can be loaded per module.
  61. - If one object has already loaded TBB library, another object will not load TBB.
  62. If the loaded TBB library is suitable for the second object, both will use TBB
  63. cooperatively, otherwise the second object will report an error.
  64. - \c runtime_loader objects will not work (correctly) in parallel due to absence of
  65. synchronization.
  66. */
  67. class __TBB_DEPRECATED_IN_VERBOSE_MODE runtime_loader : tbb::internal::no_copy {
  68. public:
  69. //! Error mode constants.
  70. enum error_mode {
  71. em_status, //!< Save status of operation and continue.
  72. em_throw, //!< Throw an exception of tbb::runtime_loader::error_code type.
  73. em_abort //!< Print message to \c stderr and call \c abort().
  74. }; // error_mode
  75. //! Error codes.
  76. enum error_code {
  77. ec_ok, //!< No errors.
  78. ec_bad_call, //!< Invalid function call (e. g. load() called when TBB is already loaded).
  79. ec_bad_arg, //!< Invalid argument passed.
  80. ec_bad_lib, //!< Invalid library found (e. g. \c TBB_runtime_version symbol not found).
  81. ec_bad_ver, //!< TBB found but version is not suitable.
  82. ec_no_lib //!< No suitable TBB library found.
  83. }; // error_code
  84. //! Initialize object but do not load TBB.
  85. runtime_loader( error_mode mode = em_abort );
  86. //! Initialize object and load TBB.
  87. /*!
  88. See load() for details.
  89. If error mode is \c em_status, call status() to check whether TBB was loaded or not.
  90. */
  91. runtime_loader(
  92. char const * path[], //!< List of directories to search TBB in.
  93. int min_ver = TBB_INTERFACE_VERSION, //!< Minimal suitable version of TBB.
  94. int max_ver = INT_MAX, //!< Maximal suitable version of TBB.
  95. error_mode mode = em_abort //!< Error mode for this object.
  96. );
  97. //! Destroy object.
  98. ~runtime_loader();
  99. //! Load TBB.
  100. /*!
  101. The method searches the directories specified in \c path[] array for the TBB library.
  102. When the library is found, it is loaded and its version is checked. If the version is
  103. not suitable, the library is unloaded, and the search continues.
  104. \b Note:
  105. For security reasons, avoid using relative directory names. For example, never load
  106. TBB from current (\c "."), parent (\c "..") or any other relative directory (like
  107. \c "lib" ). Use only absolute directory names (e. g. "/usr/local/lib").
  108. For the same security reasons, avoid using system default directories (\c "") on
  109. Windows. (See http://www.microsoft.com/technet/security/advisory/2269637.mspx for
  110. details.)
  111. Neglecting these rules may cause your program to execute 3-rd party malicious code.
  112. \b Errors:
  113. - \c ec_bad_call - TBB already loaded by this object.
  114. - \c ec_bad_arg - \p min_ver and/or \p max_ver negative or zero,
  115. or \p min_ver > \p max_ver.
  116. - \c ec_bad_ver - TBB of unsuitable version already loaded by another object.
  117. - \c ec_no_lib - No suitable library found.
  118. */
  119. error_code
  120. load(
  121. char const * path[], //!< List of directories to search TBB in.
  122. int min_ver = TBB_INTERFACE_VERSION, //!< Minimal suitable version of TBB.
  123. int max_ver = INT_MAX //!< Maximal suitable version of TBB.
  124. );
  125. //! Report status.
  126. /*!
  127. If error mode is \c em_status, the function returns status of the last operation.
  128. */
  129. error_code status();
  130. private:
  131. error_mode const my_mode;
  132. error_code my_status;
  133. bool my_loaded;
  134. }; // class runtime_loader
  135. } // namespace interface6
  136. using interface6::runtime_loader;
  137. } // namespace tbb
  138. #include "internal/_warning_suppress_disable_notice.h"
  139. #undef __TBB_runtime_loader_H_include_area
  140. #endif /* __TBB_runtime_loader_H */