You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

188 lines
6.7 KiB

  1. /*
  2. Copyright 2005-2014 Intel Corporation. All Rights Reserved.
  3. This file is part of Threading Building Blocks.
  4. Threading Building Blocks is free software; you can redistribute it
  5. and/or modify it under the terms of the GNU General Public License
  6. version 2 as published by the Free Software Foundation.
  7. Threading Building Blocks is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  9. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with Threading Building Blocks; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  14. As a special exception, you may use this file as part of a free software
  15. library without restriction. Specifically, if other files instantiate
  16. templates or use macros or inline functions from this file, or you compile
  17. this file and link it with other files to produce an executable, this
  18. file does not by itself cause the resulting executable to be covered by
  19. the GNU General Public License. This exception does not however
  20. invalidate any other reasons why the executable file might be covered by
  21. the GNU General Public License.
  22. */
  23. #ifndef __TBB_runtime_loader_H
  24. #define __TBB_runtime_loader_H
  25. #if ! TBB_PREVIEW_RUNTIME_LOADER
  26. #error Set TBB_PREVIEW_RUNTIME_LOADER to include runtime_loader.h
  27. #endif
  28. #include "tbb/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. syncronization.
  66. */
  67. class 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. #endif /* __TBB_runtime_loader_H */