]> git.sesse.net Git - casparcg/blob - tbb/include/tbb/tbb_stddef.h
2.0. Updated tbb library.
[casparcg] / tbb / include / tbb / tbb_stddef.h
1 /*
2     Copyright 2005-2011 Intel Corporation.  All Rights Reserved.
3
4     This file is part of Threading Building Blocks.
5
6     Threading Building Blocks is free software; you can redistribute it
7     and/or modify it under the terms of the GNU General Public License
8     version 2 as published by the Free Software Foundation.
9
10     Threading Building Blocks is distributed in the hope that it will be
11     useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12     of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with Threading Building Blocks; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
19     As a special exception, you may use this file as part of a free software
20     library without restriction.  Specifically, if other files instantiate
21     templates or use macros or inline functions from this file, or you compile
22     this file and link it with other files to produce an executable, this
23     file does not by itself cause the resulting executable to be covered by
24     the GNU General Public License.  This exception does not however
25     invalidate any other reasons why the executable file might be covered by
26     the GNU General Public License.
27 */
28
29 #ifndef __TBB_tbb_stddef_H
30 #define __TBB_tbb_stddef_H
31
32 // Marketing-driven product version
33 #define TBB_VERSION_MAJOR 3
34 #define TBB_VERSION_MINOR 0
35
36 // Engineering-focused interface version
37 #define TBB_INTERFACE_VERSION 5008
38 #define TBB_INTERFACE_VERSION_MAJOR TBB_INTERFACE_VERSION/1000
39
40 // The oldest major interface version still supported
41 // To be used in SONAME, manifests, etc.
42 #define TBB_COMPATIBLE_INTERFACE_VERSION 2
43
44 #define __TBB_STRING_AUX(x) #x
45 #define __TBB_STRING(x) __TBB_STRING_AUX(x)
46
47 // We do not need defines below for resource processing on windows
48 #if !defined RC_INVOKED
49
50 // Define groups for Doxygen documentation
51 /**
52  * @defgroup algorithms         Algorithms
53  * @defgroup containers         Containers
54  * @defgroup memory_allocation  Memory Allocation
55  * @defgroup synchronization    Synchronization
56  * @defgroup timing             Timing
57  * @defgroup task_scheduling    Task Scheduling
58  */
59
60 // Simple text that is displayed on the main page of Doxygen documentation.
61 /**
62  * \mainpage Main Page
63  *
64  * Click the tabs above for information about the
65  * - <a href="./modules.html">Modules</a> (groups of functionality) implemented by the library 
66  * - <a href="./annotated.html">Classes</a> provided by the library
67  * - <a href="./files.html">Files</a> constituting the library.
68  * .
69  * Please note that significant part of TBB functionality is implemented in the form of
70  * template functions, descriptions of which are not accessible on the <a href="./annotated.html">Classes</a>
71  * tab. Use <a href="./modules.html">Modules</a> or <a href="./namespacemembers.html">Namespace/Namespace Members</a>
72  * tabs to find them.
73  *
74  * Additional pieces of information can be found here
75  * - \subpage concepts
76  * .
77  */
78
79 /** \page concepts TBB concepts
80     
81     A concept is a set of requirements to a type, which are necessary and sufficient
82     for the type to model a particular behavior or a set of behaviors. Some concepts 
83     are specific to a particular algorithm (e.g. algorithm body), while other ones 
84     are common to several algorithms (e.g. range concept). 
85
86     All TBB algorithms make use of different classes implementing various concepts.
87     Implementation classes are supplied by the user as type arguments of template 
88     parameters and/or as objects passed as function call arguments. The library 
89     provides predefined  implementations of some concepts (e.g. several kinds of 
90     \ref range_req "ranges"), while other ones must always be implemented by the user. 
91     
92     TBB defines a set of minimal requirements each concept must conform to. Here is 
93     the list of different concepts hyperlinked to the corresponding requirements specifications:
94     - \subpage range_req
95     - \subpage parallel_do_body_req
96     - \subpage parallel_for_body_req
97     - \subpage parallel_reduce_body_req
98     - \subpage parallel_scan_body_req
99     - \subpage parallel_sort_iter_req
100 **/
101
102 // Define preprocessor symbols used to determine architecture
103 #if _WIN32||_WIN64
104 #   if defined(_M_X64)||defined(__x86_64__)  // the latter for MinGW support
105 #       define __TBB_x86_64 1
106 #   elif defined(_M_IA64)
107 #       define __TBB_ipf 1
108 #   elif defined(_M_IX86)||defined(__i386__) // the latter for MinGW support
109 #       define __TBB_x86_32 1
110 #   endif
111 #else /* Assume generic Unix */
112 #   if !__linux__ && !__APPLE__
113 #       define __TBB_generic_os 1
114 #   endif
115 #   if __x86_64__
116 #       define __TBB_x86_64 1
117 #   elif __ia64__
118 #       define __TBB_ipf 1
119 #   elif __i386__||__i386  // __i386 is for Sun OS
120 #       define __TBB_x86_32 1
121 #   else
122 #       define __TBB_generic_arch 1
123 #   endif
124 #endif
125
126 // tbb_config.h should be included the first since it contains macro definitions used in other headers
127 #include "tbb_config.h"
128
129 #if _MSC_VER >=1400
130     #define __TBB_EXPORTED_FUNC   __cdecl
131     #define __TBB_EXPORTED_METHOD __thiscall
132 #else
133     #define __TBB_EXPORTED_FUNC
134     #define __TBB_EXPORTED_METHOD
135 #endif
136
137 #include <cstddef>      /* Need size_t and ptrdiff_t */
138
139 #if _MSC_VER
140     #define __TBB_tbb_windef_H
141     #include "internal/_tbb_windef.h"
142     #undef __TBB_tbb_windef_H
143 #else
144     #include <stdint.h>
145 #endif
146
147 //! The namespace tbb contains all components of the library.
148 namespace tbb {
149
150 #if _MSC_VER
151     namespace internal {
152         typedef __int8 int8_t;
153         typedef __int16 int16_t;
154         typedef __int32 int32_t;
155         typedef __int64 int64_t;
156         typedef unsigned __int8 uint8_t;
157         typedef unsigned __int16 uint16_t;
158         typedef unsigned __int32 uint32_t;
159         typedef unsigned __int64 uint64_t;
160     } // namespace internal
161 #else /* Posix */
162     namespace internal {
163         using ::int8_t;
164         using ::int16_t;
165         using ::int32_t;
166         using ::int64_t;
167         using ::uint8_t;
168         using ::uint16_t;
169         using ::uint32_t;
170         using ::uint64_t;
171     } // namespace internal
172 #endif /* Posix */
173
174     using std::size_t;
175     using std::ptrdiff_t;
176
177     //! Type for an assertion handler
178     typedef void(*assertion_handler_type)( const char* filename, int line, const char* expression, const char * comment );
179
180 #if TBB_USE_ASSERT
181
182     //! Assert that x is true.
183     /** If x is false, print assertion failure message.  
184         If the comment argument is not NULL, it is printed as part of the failure message.  
185         The comment argument has no other effect. */
186     #define __TBB_ASSERT(predicate,message) ((predicate)?((void)0):tbb::assertion_failure(__FILE__,__LINE__,#predicate,message))
187     #define __TBB_ASSERT_EX __TBB_ASSERT
188
189     //! Set assertion handler and return previous value of it.
190     assertion_handler_type __TBB_EXPORTED_FUNC set_assertion_handler( assertion_handler_type new_handler );
191
192     //! Process an assertion failure.
193     /** Normally called from __TBB_ASSERT macro.
194         If assertion handler is null, print message for assertion failure and abort.
195         Otherwise call the assertion handler. */
196     void __TBB_EXPORTED_FUNC assertion_failure( const char* filename, int line, const char* expression, const char* comment );
197
198 #else /* !TBB_USE_ASSERT */
199
200     //! No-op version of __TBB_ASSERT.
201     #define __TBB_ASSERT(predicate,comment) ((void)0)
202     //! "Extended" version is useful to suppress warnings if a variable is only used with an assert
203     #define __TBB_ASSERT_EX(predicate,comment) ((void)(1 && (predicate)))
204
205 #endif /* !TBB_USE_ASSERT */
206
207 //! The function returns the interface version of the TBB shared library being used.
208 /**
209  * The version it returns is determined at runtime, not at compile/link time.
210  * So it can be different than the value of TBB_INTERFACE_VERSION obtained at compile time.
211  */
212 extern "C" int __TBB_EXPORTED_FUNC TBB_runtime_interface_version();
213
214 //! Dummy type that distinguishes splitting constructor from copy constructor.
215 /**
216  * See description of parallel_for and parallel_reduce for example usages.
217  * @ingroup algorithms
218  */
219 class split {
220 };
221
222 /**
223  * @cond INTERNAL
224  * @brief Identifiers declared inside namespace internal should never be used directly by client code.
225  */
226 namespace internal {
227
228 //! Compile-time constant that is upper bound on cache line/sector size.
229 /** It should be used only in situations where having a compile-time upper 
230     bound is more useful than a run-time exact answer.
231     @ingroup memory_allocation */
232 const size_t NFS_MaxLineSize = 128;
233
234 /** Label for data that may be accessed from different threads, and that may eventually become wrapped
235     in a formal atomic type.
236     
237     Note that no problems have yet been observed relating to the definition currently being empty,
238     even if at least "volatile" would seem to be in order to avoid data sometimes temporarily hiding
239     in a register (although "volatile" as a "poor man's atomic" lacks several other features of a proper
240     atomic, some of which are now provided instead through specialized functions).
241
242     Note that usage is intentionally compatible with a definition as qualifier "volatile",
243     both as a way to have the compiler help enforce use of the label and to quickly rule out
244     one potential issue.
245
246     Note however that, with some architecture/compiler combinations, e.g. on Itanium, "volatile" 
247     also has non-portable memory semantics that are needlessly expensive for "relaxed" operations.
248
249     Note that this must only be applied to data that will not change bit patterns when cast to/from
250     an integral type of the same length; tbb::atomic must be used instead for, e.g., floating-point types.
251
252     TODO: apply wherever relevant **/
253 #define __TBB_atomic // intentionally empty, see above
254
255 template<class T, int S>
256 struct padded_base : T {
257     char pad[NFS_MaxLineSize - sizeof(T) % NFS_MaxLineSize];
258 };
259 template<class T> struct padded_base<T, 0> : T {};
260
261 //! Pads type T to fill out to a multiple of cache line size.
262 template<class T>
263 struct padded : padded_base<T, sizeof(T)> {};
264
265 //! Extended variant of the standard offsetof macro
266 /** The standard offsetof macro is not sufficient for TBB as it can be used for
267     POD-types only. The constant 0x1000 (not NULL) is necessary to appease GCC. **/
268 #define __TBB_offsetof(class_name, member_name) \
269     ((ptrdiff_t)&(reinterpret_cast<class_name*>(0x1000)->member_name) - 0x1000)
270
271 //! Returns address of the object containing a member with the given name and address
272 #define __TBB_get_object_ref(class_name, member_name, member_addr) \
273     (*reinterpret_cast<class_name*>((char*)member_addr - __TBB_offsetof(class_name, member_name)))
274
275 //! Throws std::runtime_error with what() returning error_code description prefixed with aux_info
276 void __TBB_EXPORTED_FUNC handle_perror( int error_code, const char* aux_info );
277
278 #if TBB_USE_EXCEPTIONS
279     #define __TBB_TRY try
280     #define __TBB_CATCH(e) catch(e)
281     #define __TBB_THROW(e) throw e
282     #define __TBB_RETHROW() throw
283 #else /* !TBB_USE_EXCEPTIONS */
284     inline bool __TBB_false() { return false; }
285     #define __TBB_TRY
286     #define __TBB_CATCH(e) if ( tbb::internal::__TBB_false() )
287     #define __TBB_THROW(e) ((void)0)
288     #define __TBB_RETHROW() ((void)0)
289 #endif /* !TBB_USE_EXCEPTIONS */
290
291 //! Report a runtime warning.
292 void __TBB_EXPORTED_FUNC runtime_warning( const char* format, ... );
293
294 #if TBB_USE_ASSERT
295 static void* const poisoned_ptr = reinterpret_cast<void*>(-1);
296
297 //! Set p to invalid pointer value.
298 template<typename T>
299 inline void poison_pointer( T*& p ) { p = reinterpret_cast<T*>(poisoned_ptr); }
300
301 /** Expected to be used in assertions only, thus no empty form is defined. **/
302 template<typename T>
303 inline bool is_poisoned( T* p ) { return p == reinterpret_cast<T*>(poisoned_ptr); }
304 #else
305 template<typename T>
306 inline void poison_pointer( T* ) {/*do nothing*/}
307 #endif /* !TBB_USE_ASSERT */
308
309 //! Cast pointer from U* to T.
310 /** This method should be used sparingly as a last resort for dealing with 
311     situations that inherently break strict ISO C++ aliasing rules. */
312 template<typename T, typename U> 
313 inline T punned_cast( U* ptr ) {
314     uintptr_t x = reinterpret_cast<uintptr_t>(ptr);
315     return reinterpret_cast<T>(x);
316 }
317
318 //! Base class for types that should not be assigned.
319 class no_assign {
320     // Deny assignment
321     void operator=( const no_assign& );
322 public:
323 #if __GNUC__
324     //! Explicitly define default construction, because otherwise gcc issues gratuitous warning.
325     no_assign() {}
326 #endif /* __GNUC__ */
327 };
328
329 //! Base class for types that should not be copied or assigned.
330 class no_copy: no_assign {
331     //! Deny copy construction
332     no_copy( const no_copy& );
333 public:
334     //! Allow default construction
335     no_copy() {}
336 };
337
338 //! Class for determining type of std::allocator<T>::value_type.
339 template<typename T>
340 struct allocator_type {
341     typedef T value_type;
342 };
343
344 #if _MSC_VER
345 //! Microsoft std::allocator has non-standard extension that strips const from a type. 
346 template<typename T>
347 struct allocator_type<const T> {
348     typedef T value_type;
349 };
350 #endif
351
352 // Struct to be used as a version tag for inline functions.
353 /** Version tag can be necessary to prevent loader on Linux from using the wrong 
354     symbol in debug builds (when inline functions are compiled as out-of-line). **/
355 struct version_tag_v3 {};
356
357 typedef version_tag_v3 version_tag;
358
359 } // internal
360 //! @endcond
361
362 } // tbb
363
364 #endif /* RC_INVOKED */
365 #endif /* __TBB_tbb_stddef_H */