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.

425 lines
16 KiB

  1. /*
  2. Copyright 2005-2013 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_tbb_stddef_H
  24. #define __TBB_tbb_stddef_H
  25. // Marketing-driven product version
  26. #define TBB_VERSION_MAJOR 4
  27. #define TBB_VERSION_MINOR 1
  28. // Engineering-focused interface version
  29. #define TBB_INTERFACE_VERSION 6103
  30. #define TBB_INTERFACE_VERSION_MAJOR TBB_INTERFACE_VERSION/1000
  31. // The oldest major interface version still supported
  32. // To be used in SONAME, manifests, etc.
  33. #define TBB_COMPATIBLE_INTERFACE_VERSION 2
  34. #define __TBB_STRING_AUX(x) #x
  35. #define __TBB_STRING(x) __TBB_STRING_AUX(x)
  36. // We do not need defines below for resource processing on windows
  37. #if !defined RC_INVOKED
  38. // Define groups for Doxygen documentation
  39. /**
  40. * @defgroup algorithms Algorithms
  41. * @defgroup containers Containers
  42. * @defgroup memory_allocation Memory Allocation
  43. * @defgroup synchronization Synchronization
  44. * @defgroup timing Timing
  45. * @defgroup task_scheduling Task Scheduling
  46. */
  47. // Simple text that is displayed on the main page of Doxygen documentation.
  48. /**
  49. * \mainpage Main Page
  50. *
  51. * Click the tabs above for information about the
  52. * - <a href="./modules.html">Modules</a> (groups of functionality) implemented by the library
  53. * - <a href="./annotated.html">Classes</a> provided by the library
  54. * - <a href="./files.html">Files</a> constituting the library.
  55. * .
  56. * Please note that significant part of TBB functionality is implemented in the form of
  57. * template functions, descriptions of which are not accessible on the <a href="./annotated.html">Classes</a>
  58. * tab. Use <a href="./modules.html">Modules</a> or <a href="./namespacemembers.html">Namespace/Namespace Members</a>
  59. * tabs to find them.
  60. *
  61. * Additional pieces of information can be found here
  62. * - \subpage concepts
  63. * .
  64. */
  65. /** \page concepts TBB concepts
  66. A concept is a set of requirements to a type, which are necessary and sufficient
  67. for the type to model a particular behavior or a set of behaviors. Some concepts
  68. are specific to a particular algorithm (e.g. algorithm body), while other ones
  69. are common to several algorithms (e.g. range concept).
  70. All TBB algorithms make use of different classes implementing various concepts.
  71. Implementation classes are supplied by the user as type arguments of template
  72. parameters and/or as objects passed as function call arguments. The library
  73. provides predefined implementations of some concepts (e.g. several kinds of
  74. \ref range_req "ranges"), while other ones must always be implemented by the user.
  75. TBB defines a set of minimal requirements each concept must conform to. Here is
  76. the list of different concepts hyperlinked to the corresponding requirements specifications:
  77. - \subpage range_req
  78. - \subpage parallel_do_body_req
  79. - \subpage parallel_for_body_req
  80. - \subpage parallel_reduce_body_req
  81. - \subpage parallel_scan_body_req
  82. - \subpage parallel_sort_iter_req
  83. **/
  84. // tbb_config.h should be included the first since it contains macro definitions used in other headers
  85. #include "tbb_config.h"
  86. #if _MSC_VER >=1400
  87. #define __TBB_EXPORTED_FUNC __cdecl
  88. #define __TBB_EXPORTED_METHOD __thiscall
  89. #else
  90. #define __TBB_EXPORTED_FUNC
  91. #define __TBB_EXPORTED_METHOD
  92. #endif
  93. #if __INTEL_COMPILER || _MSC_VER
  94. #define __TBB_NOINLINE(decl) __declspec(noinline) decl
  95. #elif __GNUC__
  96. #define __TBB_NOINLINE(decl) decl __attribute__ ((noinline))
  97. #else
  98. #define __TBB_NOINLINE(decl) decl
  99. #endif
  100. #include <cstddef> /* Need size_t and ptrdiff_t */
  101. #if _MSC_VER
  102. #define __TBB_tbb_windef_H
  103. #include "internal/_tbb_windef.h"
  104. #undef __TBB_tbb_windef_H
  105. #endif
  106. #if !defined(_MSC_VER) || _MSC_VER>=1600
  107. #include <stdint.h>
  108. #endif
  109. //! Type for an assertion handler
  110. typedef void(*assertion_handler_type)( const char* filename, int line, const char* expression, const char * comment );
  111. #if TBB_USE_ASSERT
  112. #define __TBB_ASSERT_NS(predicate,message,ns) ((predicate)?((void)0) : ns::assertion_failure(__FILE__,__LINE__,#predicate,message))
  113. //! Assert that x is true.
  114. /** If x is false, print assertion failure message.
  115. If the comment argument is not NULL, it is printed as part of the failure message.
  116. The comment argument has no other effect. */
  117. #if __TBBMALLOC_BUILD
  118. namespace rml { namespace internal {
  119. #define __TBB_ASSERT(predicate,message) __TBB_ASSERT_NS(predicate,message,rml::internal)
  120. #else
  121. namespace tbb {
  122. #define __TBB_ASSERT(predicate,message) __TBB_ASSERT_NS(predicate,message,tbb)
  123. #endif
  124. #define __TBB_ASSERT_EX __TBB_ASSERT
  125. //! Set assertion handler and return previous value of it.
  126. assertion_handler_type __TBB_EXPORTED_FUNC set_assertion_handler( assertion_handler_type new_handler );
  127. //! Process an assertion failure.
  128. /** Normally called from __TBB_ASSERT macro.
  129. If assertion handler is null, print message for assertion failure and abort.
  130. Otherwise call the assertion handler. */
  131. void __TBB_EXPORTED_FUNC assertion_failure( const char* filename, int line, const char* expression, const char* comment );
  132. #if __TBBMALLOC_BUILD
  133. }} // namespace rml::internal
  134. #else
  135. } // namespace tbb
  136. #endif
  137. #else /* !TBB_USE_ASSERT */
  138. //! No-op version of __TBB_ASSERT.
  139. #define __TBB_ASSERT(predicate,comment) ((void)0)
  140. //! "Extended" version is useful to suppress warnings if a variable is only used with an assert
  141. #define __TBB_ASSERT_EX(predicate,comment) ((void)(1 && (predicate)))
  142. #endif /* !TBB_USE_ASSERT */
  143. //! The namespace tbb contains all components of the library.
  144. namespace tbb {
  145. #if _MSC_VER && _MSC_VER<1600
  146. namespace internal {
  147. typedef __int8 int8_t;
  148. typedef __int16 int16_t;
  149. typedef __int32 int32_t;
  150. typedef __int64 int64_t;
  151. typedef unsigned __int8 uint8_t;
  152. typedef unsigned __int16 uint16_t;
  153. typedef unsigned __int32 uint32_t;
  154. typedef unsigned __int64 uint64_t;
  155. } // namespace internal
  156. #else /* Posix */
  157. namespace internal {
  158. using ::int8_t;
  159. using ::int16_t;
  160. using ::int32_t;
  161. using ::int64_t;
  162. using ::uint8_t;
  163. using ::uint16_t;
  164. using ::uint32_t;
  165. using ::uint64_t;
  166. } // namespace internal
  167. #endif /* Posix */
  168. using std::size_t;
  169. using std::ptrdiff_t;
  170. //! The function returns the interface version of the TBB shared library being used.
  171. /**
  172. * The version it returns is determined at runtime, not at compile/link time.
  173. * So it can be different than the value of TBB_INTERFACE_VERSION obtained at compile time.
  174. */
  175. extern "C" int __TBB_EXPORTED_FUNC TBB_runtime_interface_version();
  176. //! Dummy type that distinguishes splitting constructor from copy constructor.
  177. /**
  178. * See description of parallel_for and parallel_reduce for example usages.
  179. * @ingroup algorithms
  180. */
  181. class split {
  182. };
  183. /**
  184. * @cond INTERNAL
  185. * @brief Identifiers declared inside namespace internal should never be used directly by client code.
  186. */
  187. namespace internal {
  188. //! Compile-time constant that is upper bound on cache line/sector size.
  189. /** It should be used only in situations where having a compile-time upper
  190. bound is more useful than a run-time exact answer.
  191. @ingroup memory_allocation */
  192. const size_t NFS_MaxLineSize = 128;
  193. /** Label for data that may be accessed from different threads, and that may eventually become wrapped
  194. in a formal atomic type.
  195. Note that no problems have yet been observed relating to the definition currently being empty,
  196. even if at least "volatile" would seem to be in order to avoid data sometimes temporarily hiding
  197. in a register (although "volatile" as a "poor man's atomic" lacks several other features of a proper
  198. atomic, some of which are now provided instead through specialized functions).
  199. Note that usage is intentionally compatible with a definition as qualifier "volatile",
  200. both as a way to have the compiler help enforce use of the label and to quickly rule out
  201. one potential issue.
  202. Note however that, with some architecture/compiler combinations, e.g. on IA-64, "volatile"
  203. also has non-portable memory semantics that are needlessly expensive for "relaxed" operations.
  204. Note that this must only be applied to data that will not change bit patterns when cast to/from
  205. an integral type of the same length; tbb::atomic must be used instead for, e.g., floating-point types.
  206. TODO: apply wherever relevant **/
  207. #define __TBB_atomic // intentionally empty, see above
  208. template<class T, int S>
  209. struct padded_base : T {
  210. char pad[NFS_MaxLineSize - sizeof(T) % NFS_MaxLineSize];
  211. };
  212. template<class T> struct padded_base<T, 0> : T {};
  213. //! Pads type T to fill out to a multiple of cache line size.
  214. template<class T>
  215. struct padded : padded_base<T, sizeof(T)> {};
  216. //! Extended variant of the standard offsetof macro
  217. /** The standard offsetof macro is not sufficient for TBB as it can be used for
  218. POD-types only. The constant 0x1000 (not NULL) is necessary to appease GCC. **/
  219. #define __TBB_offsetof(class_name, member_name) \
  220. ((ptrdiff_t)&(reinterpret_cast<class_name*>(0x1000)->member_name) - 0x1000)
  221. //! Returns address of the object containing a member with the given name and address
  222. #define __TBB_get_object_ref(class_name, member_name, member_addr) \
  223. (*reinterpret_cast<class_name*>((char*)member_addr - __TBB_offsetof(class_name, member_name)))
  224. //! Throws std::runtime_error with what() returning error_code description prefixed with aux_info
  225. void __TBB_EXPORTED_FUNC handle_perror( int error_code, const char* aux_info );
  226. #if TBB_USE_EXCEPTIONS
  227. #define __TBB_TRY try
  228. #define __TBB_CATCH(e) catch(e)
  229. #define __TBB_THROW(e) throw e
  230. #define __TBB_RETHROW() throw
  231. #else /* !TBB_USE_EXCEPTIONS */
  232. inline bool __TBB_false() { return false; }
  233. #define __TBB_TRY
  234. #define __TBB_CATCH(e) if ( tbb::internal::__TBB_false() )
  235. #define __TBB_THROW(e) ((void)0)
  236. #define __TBB_RETHROW() ((void)0)
  237. #endif /* !TBB_USE_EXCEPTIONS */
  238. //! Report a runtime warning.
  239. void __TBB_EXPORTED_FUNC runtime_warning( const char* format, ... );
  240. #if TBB_USE_ASSERT
  241. static void* const poisoned_ptr = reinterpret_cast<void*>(-1);
  242. //! Set p to invalid pointer value.
  243. template<typename T>
  244. inline void poison_pointer( T*& p ) { p = reinterpret_cast<T*>(poisoned_ptr); }
  245. /** Expected to be used in assertions only, thus no empty form is defined. **/
  246. template<typename T>
  247. inline bool is_poisoned( T* p ) { return p == reinterpret_cast<T*>(poisoned_ptr); }
  248. #else
  249. template<typename T>
  250. inline void poison_pointer( T* ) {/*do nothing*/}
  251. #endif /* !TBB_USE_ASSERT */
  252. //! Cast pointer from U* to T.
  253. /** This method should be used sparingly as a last resort for dealing with
  254. situations that inherently break strict ISO C++ aliasing rules. */
  255. template<typename T, typename U>
  256. inline T punned_cast( U* ptr ) {
  257. uintptr_t x = reinterpret_cast<uintptr_t>(ptr);
  258. return reinterpret_cast<T>(x);
  259. }
  260. //! Base class for types that should not be assigned.
  261. class no_assign {
  262. // Deny assignment
  263. void operator=( const no_assign& );
  264. public:
  265. #if __GNUC__
  266. //! Explicitly define default construction, because otherwise gcc issues gratuitous warning.
  267. no_assign() {}
  268. #endif /* __GNUC__ */
  269. };
  270. //! Base class for types that should not be copied or assigned.
  271. class no_copy: no_assign {
  272. //! Deny copy construction
  273. no_copy( const no_copy& );
  274. public:
  275. //! Allow default construction
  276. no_copy() {}
  277. };
  278. //! Class for determining type of std::allocator<T>::value_type.
  279. template<typename T>
  280. struct allocator_type {
  281. typedef T value_type;
  282. };
  283. #if _MSC_VER
  284. //! Microsoft std::allocator has non-standard extension that strips const from a type.
  285. template<typename T>
  286. struct allocator_type<const T> {
  287. typedef T value_type;
  288. };
  289. #endif
  290. //! A template to select either 32-bit or 64-bit constant as compile time, depending on machine word size.
  291. template <unsigned u, unsigned long long ull >
  292. struct select_size_t_constant {
  293. //Explicit cast is needed to avoid compiler warnings about possible truncation.
  294. //The value of the right size, which is selected by ?:, is anyway not truncated or promoted.
  295. static const size_t value = (size_t)((sizeof(size_t)==sizeof(u)) ? u : ull);
  296. };
  297. //! A function to check if passed in pointer is aligned on a specific border
  298. template<typename T>
  299. inline bool is_aligned(T* pointer, uintptr_t alignment) {
  300. return 0==((uintptr_t)pointer & (alignment-1));
  301. }
  302. //! A function to check if passed integer is a power of 2
  303. template<typename integer_type>
  304. inline bool is_power_of_two(integer_type arg) {
  305. return arg && (0 == (arg & (arg - 1)));
  306. }
  307. //! A function to compute arg modulo divisor where divisor is a power of 2.
  308. template<typename argument_integer_type, typename divisor_integer_type>
  309. inline argument_integer_type modulo_power_of_two(argument_integer_type arg, divisor_integer_type divisor) {
  310. // Divisor is assumed to be a power of two (which is valid for current uses).
  311. __TBB_ASSERT( is_power_of_two(divisor), "Divisor should be a power of two" );
  312. return (arg & (divisor - 1));
  313. }
  314. //! A function to determine if "arg is a multiplication of a number and a power of 2".
  315. // i.e. for strictly positive i and j, with j a power of 2,
  316. // determines whether i==j<<k for some nonnegative k (so i==j yields true).
  317. template<typename argument_integer_type, typename divisor_integer_type>
  318. inline bool is_power_of_two_factor(argument_integer_type arg, divisor_integer_type divisor) {
  319. // Divisor is assumed to be a power of two (which is valid for current uses).
  320. __TBB_ASSERT( is_power_of_two(divisor), "Divisor should be a power of two" );
  321. return 0 == (arg & (arg - divisor));
  322. }
  323. // Struct to be used as a version tag for inline functions.
  324. /** Version tag can be necessary to prevent loader on Linux from using the wrong
  325. symbol in debug builds (when inline functions are compiled as out-of-line). **/
  326. struct version_tag_v3 {};
  327. typedef version_tag_v3 version_tag;
  328. } // internal
  329. //! @endcond
  330. } // tbb
  331. namespace tbb { namespace internal {
  332. template <bool condition>
  333. struct STATIC_ASSERTION_FAILED;
  334. template <>
  335. struct STATIC_ASSERTION_FAILED<false> { enum {value=1};};
  336. template<>
  337. struct STATIC_ASSERTION_FAILED<true>; //intentionally left undefined to cause compile time error
  338. }} // namespace tbb { namespace internal {
  339. #if __TBB_STATIC_ASSERT_PRESENT
  340. #define __TBB_STATIC_ASSERT(condition,msg) static_assert(condition,msg)
  341. #else
  342. //please note condition is intentionally inverted to get a bit more understandable error msg
  343. #define __TBB_STATIC_ASSERT_IMPL1(condition,msg,line) \
  344. enum {static_assert_on_line_##line = tbb::internal::STATIC_ASSERTION_FAILED<!(condition)>::value}
  345. #define __TBB_STATIC_ASSERT_IMPL(condition,msg,line) __TBB_STATIC_ASSERT_IMPL1(condition,msg,line)
  346. //! Verify at compile time that passed in condition is hold
  347. #define __TBB_STATIC_ASSERT(condition,msg) __TBB_STATIC_ASSERT_IMPL(condition,msg,__LINE__)
  348. #endif
  349. #endif /* RC_INVOKED */
  350. #endif /* __TBB_tbb_stddef_H */