tbb42_20130725oss/ 0000755 0000764 0000764 00000000000 12200165353 012574 5 ustar vtune vtune tbb42_20130725oss/include/ 0000755 0000764 0000764 00000000000 12200165231 014212 5 ustar vtune vtune tbb42_20130725oss/include/index.html 0000644 0000764 0000764 00000001117 12200165225 016212 0 ustar vtune vtune
Overview
Include files for Intel® Threading Building Blocks (Intel® TBB).
Directories
- tbb
- Include files for Intel TBB classes and functions.
Up to parent directory
Copyright © 2005-2013 Intel Corporation. All Rights Reserved.
Intel is a registered trademark or trademark of Intel Corporation
or its subsidiaries in the United States and other countries.
* Other names and brands may be claimed as the property of others.
tbb42_20130725oss/include/tbb/ 0000755 0000764 0000764 00000000000 12200165246 014767 5 ustar vtune vtune tbb42_20130725oss/include/tbb/parallel_for_each.h 0000664 0000764 0000764 00000005630 12200165244 020566 0 ustar vtune vtune /*
Copyright 2005-2013 Intel Corporation. All Rights Reserved.
This file is part of Threading Building Blocks.
Threading Building Blocks is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
Threading Building Blocks is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Threading Building Blocks; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
As a special exception, you may use this file as part of a free software
library without restriction. Specifically, if other files instantiate
templates or use macros or inline functions from this file, or you compile
this file and link it with other files to produce an executable, this
file does not by itself cause the resulting executable to be covered by
the GNU General Public License. This exception does not however
invalidate any other reasons why the executable file might be covered by
the GNU General Public License.
*/
#ifndef __TBB_parallel_for_each_H
#define __TBB_parallel_for_each_H
#include "parallel_do.h"
namespace tbb {
//! @cond INTERNAL
namespace internal {
// The class calls user function in operator()
template
class parallel_for_each_body : internal::no_assign {
const Function &my_func;
public:
parallel_for_each_body(const Function &_func) : my_func(_func) {}
parallel_for_each_body(const parallel_for_each_body &_caller) : my_func(_caller.my_func) {}
void operator() ( typename std::iterator_traits::reference value ) const {
my_func(value);
}
};
} // namespace internal
//! @endcond
/** \name parallel_for_each
**/
//@{
//! Calls function f for all items from [first, last) interval using user-supplied context
/** @ingroup algorithms */
#if __TBB_TASK_GROUP_CONTEXT
template
void parallel_for_each(InputIterator first, InputIterator last, const Function& f, task_group_context &context) {
internal::parallel_for_each_body body(f);
tbb::parallel_do (first, last, body, context);
}
#endif /* __TBB_TASK_GROUP_CONTEXT */
//! Uses default context
template
void parallel_for_each(InputIterator first, InputIterator last, const Function& f) {
internal::parallel_for_each_body body(f);
tbb::parallel_do (first, last, body);
}
//@}
} // namespace
#endif /* __TBB_parallel_for_each_H */
tbb42_20130725oss/include/tbb/tbb_machine.h 0000664 0000764 0000764 00000117414 12200165244 017403 0 ustar vtune vtune /*
Copyright 2005-2013 Intel Corporation. All Rights Reserved.
This file is part of Threading Building Blocks.
Threading Building Blocks is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
Threading Building Blocks is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Threading Building Blocks; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
As a special exception, you may use this file as part of a free software
library without restriction. Specifically, if other files instantiate
templates or use macros or inline functions from this file, or you compile
this file and link it with other files to produce an executable, this
file does not by itself cause the resulting executable to be covered by
the GNU General Public License. This exception does not however
invalidate any other reasons why the executable file might be covered by
the GNU General Public License.
*/
#ifndef __TBB_machine_H
#define __TBB_machine_H
/** This header provides basic platform abstraction layer by hooking up appropriate
architecture/OS/compiler specific headers from the /include/tbb/machine directory.
If a plug-in header does not implement all the required APIs, it must specify
the missing ones by setting one or more of the following macros:
__TBB_USE_GENERIC_PART_WORD_CAS
__TBB_USE_GENERIC_PART_WORD_FETCH_ADD
__TBB_USE_GENERIC_PART_WORD_FETCH_STORE
__TBB_USE_GENERIC_FETCH_ADD
__TBB_USE_GENERIC_FETCH_STORE
__TBB_USE_GENERIC_DWORD_FETCH_ADD
__TBB_USE_GENERIC_DWORD_FETCH_STORE
__TBB_USE_GENERIC_HALF_FENCED_LOAD_STORE
__TBB_USE_GENERIC_FULL_FENCED_LOAD_STORE
__TBB_USE_GENERIC_RELAXED_LOAD_STORE
__TBB_USE_FETCHSTORE_AS_FULL_FENCED_STORE
In this case tbb_machine.h will add missing functionality based on a minimal set
of APIs that are required to be implemented by all plug-n headers as described
further.
Note that these generic implementations may be sub-optimal for a particular
architecture, and thus should be relied upon only after careful evaluation
or as the last resort.
Additionally __TBB_64BIT_ATOMICS can be set to 0 on a 32-bit architecture to
indicate that the port is not going to support double word atomics. It may also
be set to 1 explicitly, though normally this is not necessary as tbb_machine.h
will set it automatically.
__TBB_ENDIANNESS macro can be defined by the implementation as well.
It is used only if __TBB_USE_GENERIC_PART_WORD_CAS is set (or for testing),
and must specify the layout of aligned 16-bit and 32-bit data anywhere within a process
(while the details of unaligned 16-bit or 32-bit data or of 64-bit data are irrelevant).
The layout must be the same at all relevant memory locations within the current process;
in case of page-specific endianness, one endianness must be kept "out of sight".
Possible settings, reflecting hardware and possibly O.S. convention, are:
- __TBB_ENDIAN_BIG for big-endian data,
- __TBB_ENDIAN_LITTLE for little-endian data,
- __TBB_ENDIAN_DETECT for run-time detection iff exactly one of the above,
- __TBB_ENDIAN_UNSUPPORTED to prevent undefined behavior if none of the above.
Prerequisites for each architecture port
----------------------------------------
The following functions and macros have no generic implementation. Therefore they must be
implemented in each machine architecture specific header either as a conventional
function or as a functional macro.
__TBB_WORDSIZE
This is the size of machine word in bytes, i.e. for 32 bit systems it
should be defined to 4.
__TBB_Yield()
Signals OS that the current thread is willing to relinquish the remainder
of its time quantum.
__TBB_full_memory_fence()
Must prevent all memory operations from being reordered across it (both
by hardware and compiler). All such fences must be totally ordered (or
sequentially consistent).
__TBB_machine_cmpswp4( volatile void *ptr, int32_t value, int32_t comparand )
Must be provided if __TBB_USE_FENCED_ATOMICS is not set.
__TBB_machine_cmpswp8( volatile void *ptr, int32_t value, int64_t comparand )
Must be provided for 64-bit architectures if __TBB_USE_FENCED_ATOMICS is not set,
and for 32-bit architectures if __TBB_64BIT_ATOMICS is set
__TBB_machine_(...), where
= {cmpswp, fetchadd, fetchstore}
= {1, 2, 4, 8}
= {full_fence, acquire, release, relaxed}
Must be provided if __TBB_USE_FENCED_ATOMICS is set.
__TBB_control_consistency_helper()
Bridges the memory-semantics gap between architectures providing only
implicit C++0x "consume" semantics (like Power Architecture) and those
also implicitly obeying control dependencies (like IA-64 architecture).
It must be used only in conditional code where the condition is itself
data-dependent, and will then make subsequent code behave as if the
original data dependency were acquired.
It needs only a compiler fence where implied by the architecture
either specifically (like IA-64 architecture) or because generally stronger
"acquire" semantics are enforced (like x86).
It is always valid, though potentially suboptimal, to replace
control with acquire on the load and then remove the helper.
__TBB_acquire_consistency_helper(), __TBB_release_consistency_helper()
Must be provided if __TBB_USE_GENERIC_HALF_FENCED_LOAD_STORE is set.
Enforce acquire and release semantics in generic implementations of fenced
store and load operations. Depending on the particular architecture/compiler
combination they may be a hardware fence, a compiler fence, both or nothing.
**/
#include "tbb_stddef.h"
namespace tbb {
namespace internal {
////////////////////////////////////////////////////////////////////////////////
// Overridable helpers declarations
//
// A machine/*.h file may choose to define these templates, otherwise it must
// request default implementation by setting appropriate __TBB_USE_GENERIC_XXX macro(s).
//
template
struct machine_load_store;
template
struct machine_load_store_relaxed;
template
struct machine_load_store_seq_cst;
//
// End of overridable helpers declarations
////////////////////////////////////////////////////////////////////////////////
template struct atomic_selector;
template<> struct atomic_selector<1> {
typedef int8_t word;
inline static word fetch_store ( volatile void* location, word value );
};
template<> struct atomic_selector<2> {
typedef int16_t word;
inline static word fetch_store ( volatile void* location, word value );
};
template<> struct atomic_selector<4> {
#if _MSC_VER && !_WIN64
// Work-around that avoids spurious /Wp64 warnings
typedef intptr_t word;
#else
typedef int32_t word;
#endif
inline static word fetch_store ( volatile void* location, word value );
};
template<> struct atomic_selector<8> {
typedef int64_t word;
inline static word fetch_store ( volatile void* location, word value );
};
}} // namespaces internal, tbb
#define __TBB_MACHINE_DEFINE_STORE8_GENERIC_FENCED(M) \
inline void __TBB_machine_generic_store8##M(volatile void *ptr, int64_t value) { \
for(;;) { \
int64_t result = *(int64_t *)ptr; \
if( __TBB_machine_cmpswp8##M(ptr,value,result)==result ) break; \
} \
} \
#define __TBB_MACHINE_DEFINE_LOAD8_GENERIC_FENCED(M) \
inline int64_t __TBB_machine_generic_load8##M(const volatile void *ptr) { \
/* Comparand and new value may be anything, they only must be equal, and */ \
/* the value should have a low probability to be actually found in 'location'.*/ \
const int64_t anyvalue = 2305843009213693951LL; \
return __TBB_machine_cmpswp8##M(const_cast(ptr),anyvalue,anyvalue); \
} \
// The set of allowed values for __TBB_ENDIANNESS (see above for details)
#define __TBB_ENDIAN_UNSUPPORTED -1
#define __TBB_ENDIAN_LITTLE 0
#define __TBB_ENDIAN_BIG 1
#define __TBB_ENDIAN_DETECT 2
#if _WIN32||_WIN64
#ifdef _MANAGED
#pragma managed(push, off)
#endif
#if __MINGW64__ || __MINGW32__
extern "C" __declspec(dllimport) int __stdcall SwitchToThread( void );
#define __TBB_Yield() SwitchToThread()
#if (TBB_USE_GCC_BUILTINS && __TBB_GCC_BUILTIN_ATOMICS_PRESENT)
#include "machine/gcc_generic.h"
#elif __MINGW64__
#include "machine/linux_intel64.h"
#elif __MINGW32__
#include "machine/linux_ia32.h"
#endif
#elif (TBB_USE_ICC_BUILTINS && __TBB_ICC_BUILTIN_ATOMICS_PRESENT)
#include "machine/icc_generic.h"
#elif defined(_M_IX86) && !defined(__TBB_WIN32_USE_CL_BUILTINS)
#include "machine/windows_ia32.h"
#elif defined(_M_X64)
#include "machine/windows_intel64.h"
#elif defined(_XBOX)
#include "machine/xbox360_ppc.h"
#elif defined(_M_ARM) || defined(__TBB_WIN32_USE_CL_BUILTINS)
#include "machine/msvc_armv7.h"
#endif
#ifdef _MANAGED
#pragma managed(pop)
#endif
#elif __TBB_DEFINE_MIC
#include "machine/mic_common.h"
//TODO: check if ICC atomic intrinsics are available for MIC
#include "machine/linux_intel64.h"
#elif __linux__ || __FreeBSD__ || __NetBSD__
#if (TBB_USE_GCC_BUILTINS && __TBB_GCC_BUILTIN_ATOMICS_PRESENT)
#include "machine/gcc_generic.h"
#elif (TBB_USE_ICC_BUILTINS && __TBB_ICC_BUILTIN_ATOMICS_PRESENT)
#include "machine/icc_generic.h"
#elif __i386__
#include "machine/linux_ia32.h"
#elif __x86_64__
#include "machine/linux_intel64.h"
#elif __ia64__
#include "machine/linux_ia64.h"
#elif __powerpc__
#include "machine/mac_ppc.h"
#elif __arm__
#include "machine/gcc_armv7.h"
#elif __TBB_GCC_BUILTIN_ATOMICS_PRESENT
#include "machine/gcc_generic.h"
#endif
#include "machine/linux_common.h"
#elif __APPLE__
//TODO: TBB_USE_GCC_BUILTINS is not used for Mac, Sun, Aix
#if (TBB_USE_ICC_BUILTINS && __TBB_ICC_BUILTIN_ATOMICS_PRESENT)
#include "machine/icc_generic.h"
#elif __i386__
#include "machine/linux_ia32.h"
#elif __x86_64__
#include "machine/linux_intel64.h"
#elif __POWERPC__
#include "machine/mac_ppc.h"
#endif
#include "machine/macos_common.h"
#elif _AIX
#include "machine/ibm_aix51.h"
#elif __sun || __SUNPRO_CC
#define __asm__ asm
#define __volatile__ volatile
#if __i386 || __i386__
#include "machine/linux_ia32.h"
#elif __x86_64__
#include "machine/linux_intel64.h"
#elif __sparc
#include "machine/sunos_sparc.h"
#endif
#include
#define __TBB_Yield() sched_yield()
#endif /* OS selection */
#ifndef __TBB_64BIT_ATOMICS
#define __TBB_64BIT_ATOMICS 1
#endif
//TODO: replace usage of these functions with usage of tbb::atomic, and then remove them
//TODO: map functions with W suffix to use cast to tbb::atomic and according op, i.e. as_atomic().op()
// Special atomic functions
#if __TBB_USE_FENCED_ATOMICS
#define __TBB_machine_cmpswp1 __TBB_machine_cmpswp1full_fence
#define __TBB_machine_cmpswp2 __TBB_machine_cmpswp2full_fence
#define __TBB_machine_cmpswp4 __TBB_machine_cmpswp4full_fence
#define __TBB_machine_cmpswp8 __TBB_machine_cmpswp8full_fence
#if __TBB_WORDSIZE==8
#define __TBB_machine_fetchadd8 __TBB_machine_fetchadd8full_fence
#define __TBB_machine_fetchstore8 __TBB_machine_fetchstore8full_fence
#define __TBB_FetchAndAddWrelease(P,V) __TBB_machine_fetchadd8release(P,V)
#define __TBB_FetchAndIncrementWacquire(P) __TBB_machine_fetchadd8acquire(P,1)
#define __TBB_FetchAndDecrementWrelease(P) __TBB_machine_fetchadd8release(P,(-1))
#else
#define __TBB_machine_fetchadd4 __TBB_machine_fetchadd4full_fence
#define __TBB_machine_fetchstore4 __TBB_machine_fetchstore4full_fence
#define __TBB_FetchAndAddWrelease(P,V) __TBB_machine_fetchadd4release(P,V)
#define __TBB_FetchAndIncrementWacquire(P) __TBB_machine_fetchadd4acquire(P,1)
#define __TBB_FetchAndDecrementWrelease(P) __TBB_machine_fetchadd4release(P,(-1))
#endif /* __TBB_WORDSIZE==4 */
#else /* !__TBB_USE_FENCED_ATOMICS */
#define __TBB_FetchAndAddWrelease(P,V) __TBB_FetchAndAddW(P,V)
#define __TBB_FetchAndIncrementWacquire(P) __TBB_FetchAndAddW(P,1)
#define __TBB_FetchAndDecrementWrelease(P) __TBB_FetchAndAddW(P,(-1))
#endif /* !__TBB_USE_FENCED_ATOMICS */
#if __TBB_WORDSIZE==4
#define __TBB_CompareAndSwapW(P,V,C) __TBB_machine_cmpswp4(P,V,C)
#define __TBB_FetchAndAddW(P,V) __TBB_machine_fetchadd4(P,V)
#define __TBB_FetchAndStoreW(P,V) __TBB_machine_fetchstore4(P,V)
#elif __TBB_WORDSIZE==8
#if __TBB_USE_GENERIC_DWORD_LOAD_STORE || __TBB_USE_GENERIC_DWORD_FETCH_ADD || __TBB_USE_GENERIC_DWORD_FETCH_STORE
#error These macros should only be used on 32-bit platforms.
#endif
#define __TBB_CompareAndSwapW(P,V,C) __TBB_machine_cmpswp8(P,V,C)
#define __TBB_FetchAndAddW(P,V) __TBB_machine_fetchadd8(P,V)
#define __TBB_FetchAndStoreW(P,V) __TBB_machine_fetchstore8(P,V)
#else /* __TBB_WORDSIZE != 8 */
#error Unsupported machine word size.
#endif /* __TBB_WORDSIZE */
#ifndef __TBB_Pause
inline void __TBB_Pause(int32_t) {
__TBB_Yield();
}
#endif
namespace tbb {
//! Sequentially consistent full memory fence.
inline void atomic_fence () { __TBB_full_memory_fence(); }
namespace internal {
//! Class that implements exponential backoff.
/** See implementation of spin_wait_while_eq for an example. */
class atomic_backoff : no_copy {
//! Time delay, in units of "pause" instructions.
/** Should be equal to approximately the number of "pause" instructions
that take the same time as an context switch. */
static const int32_t LOOPS_BEFORE_YIELD = 16;
int32_t count;
public:
// In many cases, an object of this type is initialized eagerly on hot path,
// as in for(atomic_backoff b; ; b.pause()) { /*loop body*/ }
// For this reason, the construction cost must be very small!
atomic_backoff() : count(1) {}
// This constructor pauses immediately; do not use on hot paths!
atomic_backoff( bool ) : count(1) { pause(); }
//! Pause for a while.
void pause() {
if( count<=LOOPS_BEFORE_YIELD ) {
__TBB_Pause(count);
// Pause twice as long the next time.
count*=2;
} else {
// Pause is so long that we might as well yield CPU to scheduler.
__TBB_Yield();
}
}
// pause for a few times and then return false immediately.
bool bounded_pause() {
if( count<=LOOPS_BEFORE_YIELD ) {
__TBB_Pause(count);
// Pause twice as long the next time.
count*=2;
return true;
} else {
return false;
}
}
void reset() {
count = 1;
}
};
//! Spin WHILE the value of the variable is equal to a given value
/** T and U should be comparable types. */
template
void spin_wait_while_eq( const volatile T& location, U value ) {
atomic_backoff backoff;
while( location==value ) backoff.pause();
}
//! Spin UNTIL the value of the variable is equal to a given value
/** T and U should be comparable types. */
template
void spin_wait_until_eq( const volatile T& location, const U value ) {
atomic_backoff backoff;
while( location!=value ) backoff.pause();
}
////////////////////////////////////////////////////////////////////////////////
// Generic compare-and-swap applied to only a part of a machine word.
//
#ifndef __TBB_ENDIANNESS
#define __TBB_ENDIANNESS __TBB_ENDIAN_DETECT
#endif
#if __TBB_USE_GENERIC_PART_WORD_CAS && __TBB_ENDIANNESS==__TBB_ENDIAN_UNSUPPORTED
#error Generic implementation of part-word CAS may not be used with __TBB_ENDIAN_UNSUPPORTED
#endif
#if __TBB_ENDIANNESS!=__TBB_ENDIAN_UNSUPPORTED
//
// This function is the only use of __TBB_ENDIANNESS.
// The following restrictions/limitations apply for this operation:
// - T must be an integer type of at most 4 bytes for the casts and calculations to work
// - T must also be less than 4 bytes to avoid compiler warnings when computing mask
// (and for the operation to be useful at all, so no workaround is applied)
// - the architecture must consistently use either little-endian or big-endian (same for all locations)
//
// TODO: static_assert for the type requirements stated above
template
inline T __TBB_MaskedCompareAndSwap (volatile T * const ptr, const T value, const T comparand ) {
struct endianness{ static bool is_big_endian(){
#if __TBB_ENDIANNESS==__TBB_ENDIAN_DETECT
const uint32_t probe = 0x03020100;
return (((const char*)(&probe))[0]==0x03);
#elif __TBB_ENDIANNESS==__TBB_ENDIAN_BIG || __TBB_ENDIANNESS==__TBB_ENDIAN_LITTLE
return __TBB_ENDIANNESS==__TBB_ENDIAN_BIG;
#else
#error Unexpected value of __TBB_ENDIANNESS
#endif
}};
const uint32_t byte_offset = (uint32_t) ((uintptr_t)ptr & 0x3);
volatile uint32_t * const aligned_ptr = (uint32_t*)((uintptr_t)ptr - byte_offset );
// location of T within uint32_t for a C++ shift operation
const uint32_t bits_to_shift = 8*(endianness::is_big_endian() ? (4 - sizeof(T) - (byte_offset)) : byte_offset);
const uint32_t mask = (((uint32_t)1<<(sizeof(T)*8)) - 1 )<> bits_to_shift);
}
else continue; // CAS failed but the bits of interest were not changed
}
}
#endif // __TBB_ENDIANNESS!=__TBB_ENDIAN_UNSUPPORTED
////////////////////////////////////////////////////////////////////////////////
template
inline T __TBB_CompareAndSwapGeneric (volatile void *ptr, T value, T comparand );
template<>
inline uint8_t __TBB_CompareAndSwapGeneric <1,uint8_t> (volatile void *ptr, uint8_t value, uint8_t comparand ) {
#if __TBB_USE_GENERIC_PART_WORD_CAS
return __TBB_MaskedCompareAndSwap((volatile uint8_t *)ptr,value,comparand);
#else
return __TBB_machine_cmpswp1(ptr,value,comparand);
#endif
}
template<>
inline uint16_t __TBB_CompareAndSwapGeneric <2,uint16_t> (volatile void *ptr, uint16_t value, uint16_t comparand ) {
#if __TBB_USE_GENERIC_PART_WORD_CAS
return __TBB_MaskedCompareAndSwap((volatile uint16_t *)ptr,value,comparand);
#else
return __TBB_machine_cmpswp2(ptr,value,comparand);
#endif
}
template<>
inline uint32_t __TBB_CompareAndSwapGeneric <4,uint32_t> (volatile void *ptr, uint32_t value, uint32_t comparand ) {
// Cast shuts up /Wp64 warning
return (uint32_t)__TBB_machine_cmpswp4(ptr,value,comparand);
}
#if __TBB_64BIT_ATOMICS
template<>
inline uint64_t __TBB_CompareAndSwapGeneric <8,uint64_t> (volatile void *ptr, uint64_t value, uint64_t comparand ) {
return __TBB_machine_cmpswp8(ptr,value,comparand);
}
#endif
template
inline T __TBB_FetchAndAddGeneric (volatile void *ptr, T addend) {
T result;
for( atomic_backoff b;;b.pause() ) {
result = *reinterpret_cast(ptr);
// __TBB_CompareAndSwapGeneric presumed to have full fence.
if( __TBB_CompareAndSwapGeneric ( ptr, result+addend, result )==result )
break;
}
return result;
}
template
inline T __TBB_FetchAndStoreGeneric (volatile void *ptr, T value) {
T result;
for( atomic_backoff b;;b.pause() ) {
result = *reinterpret_cast(ptr);
// __TBB_CompareAndSwapGeneric presumed to have full fence.
if( __TBB_CompareAndSwapGeneric ( ptr, value, result )==result )
break;
}
return result;
}
#if __TBB_USE_GENERIC_PART_WORD_CAS
#define __TBB_machine_cmpswp1 tbb::internal::__TBB_CompareAndSwapGeneric<1,uint8_t>
#define __TBB_machine_cmpswp2 tbb::internal::__TBB_CompareAndSwapGeneric<2,uint16_t>
#endif
#if __TBB_USE_GENERIC_FETCH_ADD || __TBB_USE_GENERIC_PART_WORD_FETCH_ADD
#define __TBB_machine_fetchadd1 tbb::internal::__TBB_FetchAndAddGeneric<1,uint8_t>
#define __TBB_machine_fetchadd2 tbb::internal::__TBB_FetchAndAddGeneric<2,uint16_t>
#endif
#if __TBB_USE_GENERIC_FETCH_ADD
#define __TBB_machine_fetchadd4 tbb::internal::__TBB_FetchAndAddGeneric<4,uint32_t>
#endif
#if __TBB_USE_GENERIC_FETCH_ADD || __TBB_USE_GENERIC_DWORD_FETCH_ADD
#define __TBB_machine_fetchadd8 tbb::internal::__TBB_FetchAndAddGeneric<8,uint64_t>
#endif
#if __TBB_USE_GENERIC_FETCH_STORE || __TBB_USE_GENERIC_PART_WORD_FETCH_STORE
#define __TBB_machine_fetchstore1 tbb::internal::__TBB_FetchAndStoreGeneric<1,uint8_t>
#define __TBB_machine_fetchstore2 tbb::internal::__TBB_FetchAndStoreGeneric<2,uint16_t>
#endif
#if __TBB_USE_GENERIC_FETCH_STORE
#define __TBB_machine_fetchstore4 tbb::internal::__TBB_FetchAndStoreGeneric<4,uint32_t>
#endif
#if __TBB_USE_GENERIC_FETCH_STORE || __TBB_USE_GENERIC_DWORD_FETCH_STORE
#define __TBB_machine_fetchstore8 tbb::internal::__TBB_FetchAndStoreGeneric<8,uint64_t>
#endif
#if __TBB_USE_FETCHSTORE_AS_FULL_FENCED_STORE
#define __TBB_MACHINE_DEFINE_ATOMIC_SELECTOR_FETCH_STORE(S) \
atomic_selector::word atomic_selector::fetch_store ( volatile void* location, word value ) { \
return __TBB_machine_fetchstore##S( location, value ); \
}
__TBB_MACHINE_DEFINE_ATOMIC_SELECTOR_FETCH_STORE(1)
__TBB_MACHINE_DEFINE_ATOMIC_SELECTOR_FETCH_STORE(2)
__TBB_MACHINE_DEFINE_ATOMIC_SELECTOR_FETCH_STORE(4)
__TBB_MACHINE_DEFINE_ATOMIC_SELECTOR_FETCH_STORE(8)
#undef __TBB_MACHINE_DEFINE_ATOMIC_SELECTOR_FETCH_STORE
#endif /* __TBB_USE_FETCHSTORE_AS_FULL_FENCED_STORE */
#if __TBB_USE_GENERIC_DWORD_LOAD_STORE
/*TODO: find a more elegant way to handle function names difference*/
#if ! __TBB_USE_FENCED_ATOMICS
/* This name forwarding is needed for generic implementation of
* load8/store8 defined below (via macro) to pick the right CAS function*/
#define __TBB_machine_cmpswp8full_fence __TBB_machine_cmpswp8
#endif
__TBB_MACHINE_DEFINE_LOAD8_GENERIC_FENCED(full_fence)
__TBB_MACHINE_DEFINE_STORE8_GENERIC_FENCED(full_fence)
#if ! __TBB_USE_FENCED_ATOMICS
#undef __TBB_machine_cmpswp8full_fence
#endif
#define __TBB_machine_store8 tbb::internal::__TBB_machine_generic_store8full_fence
#define __TBB_machine_load8 tbb::internal::__TBB_machine_generic_load8full_fence
#endif /* __TBB_USE_GENERIC_DWORD_LOAD_STORE */
#if __TBB_USE_GENERIC_HALF_FENCED_LOAD_STORE
/** Fenced operations use volatile qualifier to prevent compiler from optimizing
them out, and on architectures with weak memory ordering to induce compiler
to generate code with appropriate acquire/release semantics.
On architectures like IA32, Intel64 (and likely Sparc TSO) volatile has
no effect on code gen, and consistency helpers serve as a compiler fence (the
latter being true for IA64/gcc as well to fix a bug in some gcc versions).
This code assumes that the generated instructions will operate atomically,
which typically requires a type that can be moved in a single instruction,
cooperation from the compiler for effective use of such an instruction,
and appropriate alignment of the data. **/
template
struct machine_load_store {
static T load_with_acquire ( const volatile T& location ) {
T to_return = location;
__TBB_acquire_consistency_helper();
return to_return;
}
static void store_with_release ( volatile T &location, T value ) {
__TBB_release_consistency_helper();
location = value;
}
};
//in general, plain load and store of 32bit compiler is not atomic for 64bit types
#if __TBB_WORDSIZE==4 && __TBB_64BIT_ATOMICS
template
struct machine_load_store {
static T load_with_acquire ( const volatile T& location ) {
return (T)__TBB_machine_load8( (const volatile void*)&location );
}
static void store_with_release ( volatile T& location, T value ) {
__TBB_machine_store8( (volatile void*)&location, (int64_t)value );
}
};
#endif /* __TBB_WORDSIZE==4 && __TBB_64BIT_ATOMICS */
#endif /* __TBB_USE_GENERIC_HALF_FENCED_LOAD_STORE */
#if __TBB_USE_GENERIC_SEQUENTIAL_CONSISTENCY_LOAD_STORE
template
struct machine_load_store_seq_cst {
static T load ( const volatile T& location ) {
__TBB_full_memory_fence();
return machine_load_store::load_with_acquire( location );
}
#if __TBB_USE_FETCHSTORE_AS_FULL_FENCED_STORE
static void store ( volatile T &location, T value ) {
atomic_selector::fetch_store( (volatile void*)&location, (typename atomic_selector::word)value );
}
#else /* !__TBB_USE_FETCHSTORE_AS_FULL_FENCED_STORE */
static void store ( volatile T &location, T value ) {
machine_load_store::store_with_release( location, value );
__TBB_full_memory_fence();
}
#endif /* !__TBB_USE_FETCHSTORE_AS_FULL_FENCED_STORE */
};
#if __TBB_WORDSIZE==4 && __TBB_64BIT_ATOMICS
/** The implementation does not use functions __TBB_machine_load8/store8 as they
are not required to be sequentially consistent. **/
template
struct machine_load_store_seq_cst {
static T load ( const volatile T& location ) {
// Comparand and new value may be anything, they only must be equal, and
// the value should have a low probability to be actually found in 'location'.
const int64_t anyvalue = 2305843009213693951LL;
return __TBB_machine_cmpswp8( (volatile void*)const_cast(&location), anyvalue, anyvalue );
}
static void store ( volatile T &location, T value ) {
int64_t result = (volatile int64_t&)location;
while ( __TBB_machine_cmpswp8((volatile void*)&location, (int64_t)value, result) != result )
result = (volatile int64_t&)location;
}
};
#endif /* __TBB_WORDSIZE==4 && __TBB_64BIT_ATOMICS */
#endif /*__TBB_USE_GENERIC_SEQUENTIAL_CONSISTENCY_LOAD_STORE */
#if __TBB_USE_GENERIC_RELAXED_LOAD_STORE
// Relaxed operations add volatile qualifier to prevent compiler from optimizing them out.
/** Volatile should not incur any additional cost on IA32, Intel64, and Sparc TSO
architectures. However on architectures with weak memory ordering compiler may
generate code with acquire/release semantics for operations on volatile data. **/
template
struct machine_load_store_relaxed {
static inline T load ( const volatile T& location ) {
return location;
}
static inline void store ( volatile T& location, T value ) {
location = value;
}
};
#if __TBB_WORDSIZE==4 && __TBB_64BIT_ATOMICS
template
struct machine_load_store_relaxed {
static inline T load ( const volatile T& location ) {
return (T)__TBB_machine_load8( (const volatile void*)&location );
}
static inline void store ( volatile T& location, T value ) {
__TBB_machine_store8( (volatile void*)&location, (int64_t)value );
}
};
#endif /* __TBB_WORDSIZE==4 && __TBB_64BIT_ATOMICS */
#endif /* __TBB_USE_GENERIC_RELAXED_LOAD_STORE */
#undef __TBB_WORDSIZE //this macro is forbidden to use outside of atomic machinery
template
inline T __TBB_load_with_acquire(const volatile T &location) {
return machine_load_store::load_with_acquire( location );
}
template
inline void __TBB_store_with_release(volatile T& location, V value) {
machine_load_store::store_with_release( location, T(value) );
}
//! Overload that exists solely to avoid /Wp64 warnings.
inline void __TBB_store_with_release(volatile size_t& location, size_t value) {
machine_load_store::store_with_release( location, value );
}
template
inline T __TBB_load_full_fence(const volatile T &location) {
return machine_load_store_seq_cst::load( location );
}
template
inline void __TBB_store_full_fence(volatile T& location, V value) {
machine_load_store_seq_cst::store( location, T(value) );
}
//! Overload that exists solely to avoid /Wp64 warnings.
inline void __TBB_store_full_fence(volatile size_t& location, size_t value) {
machine_load_store_seq_cst::store( location, value );
}
template
inline T __TBB_load_relaxed (const volatile T& location) {
return machine_load_store_relaxed::load( const_cast(location) );
}
template
inline void __TBB_store_relaxed ( volatile T& location, V value ) {
machine_load_store_relaxed::store( const_cast(location), T(value) );
}
//! Overload that exists solely to avoid /Wp64 warnings.
inline void __TBB_store_relaxed ( volatile size_t& location, size_t value ) {
machine_load_store_relaxed::store( const_cast(location), value );
}
// Macro __TBB_TypeWithAlignmentAtLeastAsStrict(T) should be a type with alignment at least as
// strict as type T. The type should have a trivial default constructor and destructor, so that
// arrays of that type can be declared without initializers.
// It is correct (but perhaps a waste of space) if __TBB_TypeWithAlignmentAtLeastAsStrict(T) expands
// to a type bigger than T.
// The default definition here works on machines where integers are naturally aligned and the
// strictest alignment is 64.
#ifndef __TBB_TypeWithAlignmentAtLeastAsStrict
#if __TBB_ATTRIBUTE_ALIGNED_PRESENT
#define __TBB_DefineTypeWithAlignment(PowerOf2) \
struct __TBB_machine_type_with_alignment_##PowerOf2 { \
uint32_t member[PowerOf2/sizeof(uint32_t)]; \
} __attribute__((aligned(PowerOf2)));
#define __TBB_alignof(T) __alignof__(T)
#elif __TBB_DECLSPEC_ALIGN_PRESENT
#define __TBB_DefineTypeWithAlignment(PowerOf2) \
__declspec(align(PowerOf2)) \
struct __TBB_machine_type_with_alignment_##PowerOf2 { \
uint32_t member[PowerOf2/sizeof(uint32_t)]; \
};
#define __TBB_alignof(T) __alignof(T)
#else /* A compiler with unknown syntax for data alignment */
#error Must define __TBB_TypeWithAlignmentAtLeastAsStrict(T)
#endif
/* Now declare types aligned to useful powers of two */
// TODO: Is __TBB_DefineTypeWithAlignment(8) needed on 32 bit platforms?
__TBB_DefineTypeWithAlignment(16)
__TBB_DefineTypeWithAlignment(32)
__TBB_DefineTypeWithAlignment(64)
typedef __TBB_machine_type_with_alignment_64 __TBB_machine_type_with_strictest_alignment;
// Primary template is a declaration of incomplete type so that it fails with unknown alignments
template struct type_with_alignment;
// Specializations for allowed alignments
template<> struct type_with_alignment<1> { char member; };
template<> struct type_with_alignment<2> { uint16_t member; };
template<> struct type_with_alignment<4> { uint32_t member; };
template<> struct type_with_alignment<8> { uint64_t member; };
template<> struct type_with_alignment<16> {__TBB_machine_type_with_alignment_16 member; };
template<> struct type_with_alignment<32> {__TBB_machine_type_with_alignment_32 member; };
template<> struct type_with_alignment<64> {__TBB_machine_type_with_alignment_64 member; };
#if __TBB_ALIGNOF_NOT_INSTANTIATED_TYPES_BROKEN
//! Work around for bug in GNU 3.2 and MSVC compilers.
/** Bug is that compiler sometimes returns 0 for __alignof(T) when T has not yet been instantiated.
The work-around forces instantiation by forcing computation of sizeof(T) before __alignof(T). */
template
struct work_around_alignment_bug {
static const size_t alignment = __TBB_alignof(T);
};
#define __TBB_TypeWithAlignmentAtLeastAsStrict(T) tbb::internal::type_with_alignment::alignment>
#else
#define __TBB_TypeWithAlignmentAtLeastAsStrict(T) tbb::internal::type_with_alignment<__TBB_alignof(T)>
#endif /* __TBB_ALIGNOF_NOT_INSTANTIATED_TYPES_BROKEN */
#endif /* __TBB_TypeWithAlignmentAtLeastAsStrict */
// Template class here is to avoid instantiation of the static data for modules that don't use it
template
struct reverse {
static const T byte_table[256];
};
// An efficient implementation of the reverse function utilizes a 2^8 lookup table holding the bit-reversed
// values of [0..2^8 - 1]. Those values can also be computed on the fly at a slightly higher cost.
template
const T reverse::byte_table[256] = {
0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, 0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0,
0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8, 0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8,
0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4, 0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4,
0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC, 0x1C, 0x9C, 0x5C, 0xDC, 0x3C, 0xBC, 0x7C, 0xFC,
0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2, 0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2, 0x72, 0xF2,
0x0A, 0x8A, 0x4A, 0xCA, 0x2A, 0xAA, 0x6A, 0xEA, 0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA,
0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0x66, 0xE6, 0x16, 0x96, 0x56, 0xD6, 0x36, 0xB6, 0x76, 0xF6,
0x0E, 0x8E, 0x4E, 0xCE, 0x2E, 0xAE, 0x6E, 0xEE, 0x1E, 0x9E, 0x5E, 0xDE, 0x3E, 0xBE, 0x7E, 0xFE,
0x01, 0x81, 0x41, 0xC1, 0x21, 0xA1, 0x61, 0xE1, 0x11, 0x91, 0x51, 0xD1, 0x31, 0xB1, 0x71, 0xF1,
0x09, 0x89, 0x49, 0xC9, 0x29, 0xA9, 0x69, 0xE9, 0x19, 0x99, 0x59, 0xD9, 0x39, 0xB9, 0x79, 0xF9,
0x05, 0x85, 0x45, 0xC5, 0x25, 0xA5, 0x65, 0xE5, 0x15, 0x95, 0x55, 0xD5, 0x35, 0xB5, 0x75, 0xF5,
0x0D, 0x8D, 0x4D, 0xCD, 0x2D, 0xAD, 0x6D, 0xED, 0x1D, 0x9D, 0x5D, 0xDD, 0x3D, 0xBD, 0x7D, 0xFD,
0x03, 0x83, 0x43, 0xC3, 0x23, 0xA3, 0x63, 0xE3, 0x13, 0x93, 0x53, 0xD3, 0x33, 0xB3, 0x73, 0xF3,
0x0B, 0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB, 0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB,
0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7, 0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7,
0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF, 0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF
};
} // namespace internal
} // namespace tbb
// Preserving access to legacy APIs
using tbb::internal::__TBB_load_with_acquire;
using tbb::internal::__TBB_store_with_release;
// Mapping historically used names to the ones expected by atomic_load_store_traits
#define __TBB_load_acquire __TBB_load_with_acquire
#define __TBB_store_release __TBB_store_with_release
#ifndef __TBB_Log2
inline intptr_t __TBB_Log2( uintptr_t x ) {
if( x==0 ) return -1;
intptr_t result = 0;
#if !defined(_M_ARM)
uintptr_t tmp;
if( sizeof(x)>4 && (tmp = ((uint64_t)x)>>32) ) { x=tmp; result += 32; }
#endif
if( uintptr_t tmp = x>>16 ) { x=tmp; result += 16; }
if( uintptr_t tmp = x>>8 ) { x=tmp; result += 8; }
if( uintptr_t tmp = x>>4 ) { x=tmp; result += 4; }
if( uintptr_t tmp = x>>2 ) { x=tmp; result += 2; }
return (x&2)? result+1: result;
}
#endif
#ifndef __TBB_AtomicOR
inline void __TBB_AtomicOR( volatile void *operand, uintptr_t addend ) {
for( tbb::internal::atomic_backoff b;;b.pause() ) {
uintptr_t tmp = *(volatile uintptr_t *)operand;
uintptr_t result = __TBB_CompareAndSwapW(operand, tmp|addend, tmp);
if( result==tmp ) break;
}
}
#endif
#ifndef __TBB_AtomicAND
inline void __TBB_AtomicAND( volatile void *operand, uintptr_t addend ) {
for( tbb::internal::atomic_backoff b;;b.pause() ) {
uintptr_t tmp = *(volatile uintptr_t *)operand;
uintptr_t result = __TBB_CompareAndSwapW(operand, tmp&addend, tmp);
if( result==tmp ) break;
}
}
#endif
#if __TBB_PREFETCHING
#ifndef __TBB_cl_prefetch
#error This platform does not define cache management primitives required for __TBB_PREFETCHING
#endif
#ifndef __TBB_cl_evict
#define __TBB_cl_evict(p)
#endif
#endif
#ifndef __TBB_Flag
typedef unsigned char __TBB_Flag;
#endif
typedef __TBB_atomic __TBB_Flag __TBB_atomic_flag;
#ifndef __TBB_TryLockByte
inline bool __TBB_TryLockByte( __TBB_atomic_flag &flag ) {
return __TBB_machine_cmpswp1(&flag,1,0)==0;
}
#endif
#ifndef __TBB_LockByte
inline __TBB_Flag __TBB_LockByte( __TBB_atomic_flag& flag ) {
tbb::internal::atomic_backoff backoff;
while( !__TBB_TryLockByte(flag) ) backoff.pause();
return 0;
}
#endif
#ifndef __TBB_UnlockByte
#define __TBB_UnlockByte(addr) __TBB_store_with_release((addr),0)
#endif
// lock primitives with TSX
#if ( __TBB_x86_32 || __TBB_x86_64 ) /* only on ia32/intel64 */
inline void __TBB_TryLockByteElidedCancel() { __TBB_machine_try_lock_elided_cancel(); }
inline bool __TBB_TryLockByteElided( __TBB_atomic_flag& flag ) {
bool res = __TBB_machine_try_lock_elided( &flag )!=0;
// to avoid the "lemming" effect, we need to abort the transaction
// if __TBB_machine_try_lock_elided returns false (i.e., someone else
// has acquired the mutex non-speculatively).
if( !res ) __TBB_TryLockByteElidedCancel();
return res;
}
inline void __TBB_LockByteElided( __TBB_atomic_flag& flag )
{
for(;;) {
tbb::internal::spin_wait_while_eq( flag, 1 );
if( __TBB_machine_try_lock_elided( &flag ) )
return;
// Another thread acquired the lock "for real".
// To avoid the "lemming" effect, we abort the transaction.
__TBB_TryLockByteElidedCancel();
}
}
inline void __TBB_UnlockByteElided( __TBB_atomic_flag& flag ) {
__TBB_machine_unlock_elided( &flag );
}
#endif
#ifndef __TBB_ReverseByte
inline unsigned char __TBB_ReverseByte(unsigned char src) {
return tbb::internal::reverse::byte_table[src];
}
#endif
template
T __TBB_ReverseBits(T src) {
T dst;
unsigned char *original = (unsigned char *) &src;
unsigned char *reversed = (unsigned char *) &dst;
for( int i = sizeof(T)-1; i >= 0; i-- )
reversed[i] = __TBB_ReverseByte( original[sizeof(T)-i-1] );
return dst;
}
#endif /* __TBB_machine_H */
tbb42_20130725oss/include/tbb/concurrent_unordered_set.h 0000664 0000764 0000764 00000026752 12200165244 022260 0 ustar vtune vtune /*
Copyright 2005-2013 Intel Corporation. All Rights Reserved.
This file is part of Threading Building Blocks.
Threading Building Blocks is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
Threading Building Blocks is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Threading Building Blocks; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
As a special exception, you may use this file as part of a free software
library without restriction. Specifically, if other files instantiate
templates or use macros or inline functions from this file, or you compile
this file and link it with other files to produce an executable, this
file does not by itself cause the resulting executable to be covered by
the GNU General Public License. This exception does not however
invalidate any other reasons why the executable file might be covered by
the GNU General Public License.
*/
/* Container implementations in this header are based on PPL implementations
provided by Microsoft. */
#ifndef __TBB_concurrent_unordered_set_H
#define __TBB_concurrent_unordered_set_H
#include "internal/_concurrent_unordered_impl.h"
namespace tbb
{
namespace interface5 {
// Template class for hash set traits
template
class concurrent_unordered_set_traits
{
protected:
typedef Key value_type;
typedef Key key_type;
typedef Hash_compare hash_compare;
typedef typename Allocator::template rebind::other allocator_type;
enum { allow_multimapping = Allow_multimapping };
concurrent_unordered_set_traits() : my_hash_compare() {}
concurrent_unordered_set_traits(const hash_compare& hc) : my_hash_compare(hc) {}
typedef hash_compare value_compare;
static const Key& get_key(const value_type& value) {
return value;
}
hash_compare my_hash_compare; // the comparator predicate for keys
};
template , typename Key_equality = std::equal_to, typename Allocator = tbb::tbb_allocator >
class concurrent_unordered_set : public internal::concurrent_unordered_base< concurrent_unordered_set_traits, Allocator, false> >
{
// Base type definitions
typedef internal::hash_compare hash_compare;
typedef internal::concurrent_unordered_base< concurrent_unordered_set_traits > base_type;
typedef concurrent_unordered_set_traits, Allocator, false> traits_type;
using traits_type::my_hash_compare;
#if __TBB_EXTRA_DEBUG
public:
#endif
using traits_type::allow_multimapping;
public:
using base_type::end;
using base_type::find;
using base_type::insert;
// Type definitions
typedef Key key_type;
typedef typename base_type::value_type value_type;
typedef Key mapped_type;
typedef Hasher hasher;
typedef Key_equality key_equal;
typedef hash_compare key_compare;
typedef typename base_type::allocator_type allocator_type;
typedef typename base_type::pointer pointer;
typedef typename base_type::const_pointer const_pointer;
typedef typename base_type::reference reference;
typedef typename base_type::const_reference const_reference;
typedef typename base_type::size_type size_type;
typedef typename base_type::difference_type difference_type;
typedef typename base_type::iterator iterator;
typedef typename base_type::const_iterator const_iterator;
typedef typename base_type::iterator local_iterator;
typedef typename base_type::const_iterator const_local_iterator;
// Construction/destruction/copying
explicit concurrent_unordered_set(size_type n_of_buckets = 8, const hasher& a_hasher = hasher(),
const key_equal& a_keyeq = key_equal(), const allocator_type& a = allocator_type())
: base_type(n_of_buckets, key_compare(a_hasher, a_keyeq), a)
{
}
concurrent_unordered_set(const Allocator& a) : base_type(8, key_compare(), a)
{
}
template
concurrent_unordered_set(Iterator first, Iterator last, size_type n_of_buckets = 8, const hasher& a_hasher = hasher(),
const key_equal& a_keyeq = key_equal(), const allocator_type& a = allocator_type())
: base_type(n_of_buckets, key_compare(a_hasher, a_keyeq), a)
{
for (; first != last; ++first)
base_type::insert(*first);
}
#if __TBB_INITIALIZER_LISTS_PRESENT
//! Constructor from initializer_list
concurrent_unordered_set(std::initializer_list const& il, size_type n_of_buckets = 8, const hasher& a_hasher = hasher(),
const key_equal& a_keyeq = key_equal(), const allocator_type& a = allocator_type())
: base_type(n_of_buckets, key_compare(a_hasher, a_keyeq), a)
{
this->insert(il.begin(),il.end());
}
#endif //# __TBB_INITIALIZER_LISTS_PRESENT
concurrent_unordered_set(const concurrent_unordered_set& table) : base_type(table)
{
}
concurrent_unordered_set(const concurrent_unordered_set& table, const Allocator& a)
: base_type(table, a)
{
}
concurrent_unordered_set& operator=(const concurrent_unordered_set& table)
{
base_type::operator=(table);
return (*this);
}
#if __TBB_INITIALIZER_LISTS_PRESENT
//! assignment operator from initializer_list
concurrent_unordered_set& operator=(std::initializer_list const& il)
{
base_type::operator=(il);
return (*this);
}
#endif //# __TBB_INITIALIZER_LISTS_PRESENT
iterator unsafe_erase(const_iterator where)
{
return base_type::unsafe_erase(where);
}
size_type unsafe_erase(const key_type& key)
{
return base_type::unsafe_erase(key);
}
iterator unsafe_erase(const_iterator first, const_iterator last)
{
return base_type::unsafe_erase(first, last);
}
void swap(concurrent_unordered_set& table)
{
base_type::swap(table);
}
// Observers
hasher hash_function() const
{
return my_hash_compare.my_hash_object;
}
key_equal key_eq() const
{
return my_hash_compare.my_key_compare_object;
}
};
template , typename Key_equality = std::equal_to,
typename Allocator = tbb::tbb_allocator >
class concurrent_unordered_multiset :
public internal::concurrent_unordered_base< concurrent_unordered_set_traits, Allocator, true> >
{
public:
// Base type definitions
typedef internal::hash_compare hash_compare;
typedef concurrent_unordered_set_traits traits_type;
typedef internal::concurrent_unordered_base< traits_type > base_type;
using traits_type::allow_multimapping;
using traits_type::my_hash_compare;
// Type definitions
typedef Key key_type;
typedef typename base_type::value_type value_type;
typedef Key mapped_type;
typedef Hasher hasher;
typedef Key_equality key_equal;
typedef hash_compare key_compare;
typedef typename base_type::allocator_type allocator_type;
typedef typename base_type::pointer pointer;
typedef typename base_type::const_pointer const_pointer;
typedef typename base_type::reference reference;
typedef typename base_type::const_reference const_reference;
typedef typename base_type::size_type size_type;
typedef typename base_type::difference_type difference_type;
typedef typename base_type::iterator iterator;
typedef typename base_type::const_iterator const_iterator;
typedef typename base_type::iterator local_iterator;
typedef typename base_type::const_iterator const_local_iterator;
// Construction/destruction/copying
explicit concurrent_unordered_multiset(size_type n_of_buckets = 8,
const hasher& _Hasher = hasher(), const key_equal& _Key_equality = key_equal(),
const allocator_type& a = allocator_type())
: base_type(n_of_buckets, key_compare(_Hasher, _Key_equality), a)
{
}
concurrent_unordered_multiset(const Allocator& a) : base_type(8, key_compare(), a)
{
}
template
concurrent_unordered_multiset(Iterator first, Iterator last, size_type n_of_buckets = 8,
const hasher& _Hasher = hasher(), const key_equal& _Key_equality = key_equal(),
const allocator_type& a = allocator_type())
: base_type(n_of_buckets, key_compare(_Hasher, _Key_equality), a)
{
for (; first != last; ++first)
{
base_type::insert(*first);
}
}
#if __TBB_INITIALIZER_LISTS_PRESENT
//! Constructor from initializer_list
concurrent_unordered_multiset(std::initializer_list const& il, size_type n_of_buckets = 8, const hasher& a_hasher = hasher(),
const key_equal& a_keyeq = key_equal(), const allocator_type& a = allocator_type())
: base_type(n_of_buckets, key_compare(a_hasher, a_keyeq), a)
{
this->insert(il.begin(),il.end());
}
#endif //# __TBB_INITIALIZER_LISTS_PRESENT
concurrent_unordered_multiset(const concurrent_unordered_multiset& table) : base_type(table)
{
}
concurrent_unordered_multiset(const concurrent_unordered_multiset& table, const Allocator& a) : base_type(table, a)
{
}
concurrent_unordered_multiset& operator=(const concurrent_unordered_multiset& table)
{
base_type::operator=(table);
return (*this);
}
#if __TBB_INITIALIZER_LISTS_PRESENT
//! assignment operator from initializer_list
concurrent_unordered_multiset& operator=(std::initializer_list const& il)
{
base_type::operator=(il);
return (*this);
}
#endif //# __TBB_INITIALIZER_LISTS_PRESENT
// Modifiers
std::pair insert(const value_type& value)
{
return base_type::insert(value);
}
iterator insert(const_iterator where, const value_type& value)
{
return base_type::insert(where, value);
}
template
void insert(Iterator first, Iterator last)
{
base_type::insert(first, last);
}
iterator unsafe_erase(const_iterator where)
{
return base_type::unsafe_erase(where);
}
size_type unsafe_erase(const key_type& key)
{
return base_type::unsafe_erase(key);
}
iterator unsafe_erase(const_iterator first, const_iterator last)
{
return base_type::unsafe_erase(first, last);
}
void swap(concurrent_unordered_multiset& table)
{
base_type::swap(table);
}
// Observers
hasher hash_function() const
{
return my_hash_compare.my_hash_object;
}
key_equal key_eq() const
{
return my_hash_compare.my_key_compare_object;
}
};
} // namespace interface5
using interface5::concurrent_unordered_set;
using interface5::concurrent_unordered_multiset;
} // namespace tbb
#endif// __TBB_concurrent_unordered_set_H
tbb42_20130725oss/include/tbb/tick_count.h 0000664 0000764 0000764 00000012232 12200165244 017302 0 ustar vtune vtune /*
Copyright 2005-2013 Intel Corporation. All Rights Reserved.
This file is part of Threading Building Blocks.
Threading Building Blocks is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
Threading Building Blocks is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Threading Building Blocks; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
As a special exception, you may use this file as part of a free software
library without restriction. Specifically, if other files instantiate
templates or use macros or inline functions from this file, or you compile
this file and link it with other files to produce an executable, this
file does not by itself cause the resulting executable to be covered by
the GNU General Public License. This exception does not however
invalidate any other reasons why the executable file might be covered by
the GNU General Public License.
*/
#ifndef __TBB_tick_count_H
#define __TBB_tick_count_H
#include "tbb_stddef.h"
#if _WIN32||_WIN64
#include "machine/windows_api.h"
#elif __linux__
#include
#else /* generic Unix */
#include
#endif /* (choice of OS) */
namespace tbb {
//! Absolute timestamp
/** @ingroup timing */
class tick_count {
public:
//! Relative time interval.
class interval_t {
long long value;
explicit interval_t( long long value_ ) : value(value_) {}
public:
//! Construct a time interval representing zero time duration
interval_t() : value(0) {};
//! Construct a time interval representing sec seconds time duration
explicit interval_t( double sec );
//! Return the length of a time interval in seconds
double seconds() const;
friend class tbb::tick_count;
//! Extract the intervals from the tick_counts and subtract them.
friend interval_t operator-( const tick_count& t1, const tick_count& t0 );
//! Add two intervals.
friend interval_t operator+( const interval_t& i, const interval_t& j ) {
return interval_t(i.value+j.value);
}
//! Subtract two intervals.
friend interval_t operator-( const interval_t& i, const interval_t& j ) {
return interval_t(i.value-j.value);
}
//! Accumulation operator
interval_t& operator+=( const interval_t& i ) {value += i.value; return *this;}
//! Subtraction operator
interval_t& operator-=( const interval_t& i ) {value -= i.value; return *this;}
private:
static long long ticks_per_second(){
#if _WIN32||_WIN64
LARGE_INTEGER qpfreq;
int rval = QueryPerformanceFrequency(&qpfreq);
__TBB_ASSERT_EX(rval, "QueryPerformanceFrequency returned zero");
return static_cast(qpfreq.QuadPart);
#elif __linux__
return static_cast(1E9);
#else /* generic Unix */
return static_cast(1E6);
#endif /* (choice of OS) */
}
};
//! Construct an absolute timestamp initialized to zero.
tick_count() : my_count(0) {};
//! Return current time.
static tick_count now();
//! Subtract two timestamps to get the time interval between
friend interval_t operator-( const tick_count& t1, const tick_count& t0 );
//! Return the resolution of the clock in seconds per tick.
static double resolution() { return 1.0 / interval_t::ticks_per_second(); }
private:
long long my_count;
};
inline tick_count tick_count::now() {
tick_count result;
#if _WIN32||_WIN64
LARGE_INTEGER qpcnt;
int rval = QueryPerformanceCounter(&qpcnt);
__TBB_ASSERT_EX(rval, "QueryPerformanceCounter failed");
result.my_count = qpcnt.QuadPart;
#elif __linux__
struct timespec ts;
int status = clock_gettime( CLOCK_REALTIME, &ts );
__TBB_ASSERT_EX( status==0, "CLOCK_REALTIME not supported" );
result.my_count = static_cast(1000000000UL)*static_cast(ts.tv_sec) + static_cast(ts.tv_nsec);
#else /* generic Unix */
struct timeval tv;
int status = gettimeofday(&tv, NULL);
__TBB_ASSERT_EX( status==0, "gettimeofday failed" );
result.my_count = static_cast(1000000)*static_cast(tv.tv_sec) + static_cast(tv.tv_usec);
#endif /*(choice of OS) */
return result;
}
inline tick_count::interval_t::interval_t( double sec ) {
value = static_cast(sec*interval_t::ticks_per_second());
}
inline tick_count::interval_t operator-( const tick_count& t1, const tick_count& t0 ) {
return tick_count::interval_t( t1.my_count-t0.my_count );
}
inline double tick_count::interval_t::seconds() const {
return value*tick_count::resolution();
}
} // namespace tbb
#endif /* __TBB_tick_count_H */
tbb42_20130725oss/include/tbb/mutex.h 0000664 0000764 0000764 00000015164 12200165244 016311 0 ustar vtune vtune /*
Copyright 2005-2013 Intel Corporation. All Rights Reserved.
This file is part of Threading Building Blocks.
Threading Building Blocks is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
Threading Building Blocks is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Threading Building Blocks; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
As a special exception, you may use this file as part of a free software
library without restriction. Specifically, if other files instantiate
templates or use macros or inline functions from this file, or you compile
this file and link it with other files to produce an executable, this
file does not by itself cause the resulting executable to be covered by
the GNU General Public License. This exception does not however
invalidate any other reasons why the executable file might be covered by
the GNU General Public License.
*/
#ifndef __TBB_mutex_H
#define __TBB_mutex_H
#if _WIN32||_WIN64
#include "machine/windows_api.h"
#else
#include
#endif /* _WIN32||_WIN64 */
#include
#include "aligned_space.h"
#include "tbb_stddef.h"
#include "tbb_profiling.h"
namespace tbb {
//! Wrapper around the platform's native reader-writer lock.
/** For testing purposes only.
@ingroup synchronization */
class mutex {
public:
//! Construct unacquired mutex.
mutex() {
#if TBB_USE_ASSERT || TBB_USE_THREADING_TOOLS
internal_construct();
#else
#if _WIN32||_WIN64
InitializeCriticalSectionEx(&impl, 4000, 0);
#else
int error_code = pthread_mutex_init(&impl,NULL);
if( error_code )
tbb::internal::handle_perror(error_code,"mutex: pthread_mutex_init failed");
#endif /* _WIN32||_WIN64*/
#endif /* TBB_USE_ASSERT */
};
~mutex() {
#if TBB_USE_ASSERT
internal_destroy();
#else
#if _WIN32||_WIN64
DeleteCriticalSection(&impl);
#else
pthread_mutex_destroy(&impl);
#endif /* _WIN32||_WIN64 */
#endif /* TBB_USE_ASSERT */
};
class scoped_lock;
friend class scoped_lock;
//! The scoped locking pattern
/** It helps to avoid the common problem of forgetting to release lock.
It also nicely provides the "node" for queuing locks. */
class scoped_lock : internal::no_copy {
public:
//! Construct lock that has not acquired a mutex.
scoped_lock() : my_mutex(NULL) {};
//! Acquire lock on given mutex.
scoped_lock( mutex& mutex ) {
acquire( mutex );
}
//! Release lock (if lock is held).
~scoped_lock() {
if( my_mutex )
release();
}
//! Acquire lock on given mutex.
void acquire( mutex& mutex ) {
#if TBB_USE_ASSERT
internal_acquire(mutex);
#else
mutex.lock();
my_mutex = &mutex;
#endif /* TBB_USE_ASSERT */
}
//! Try acquire lock on given mutex.
bool try_acquire( mutex& mutex ) {
#if TBB_USE_ASSERT
return internal_try_acquire (mutex);
#else
bool result = mutex.try_lock();
if( result )
my_mutex = &mutex;
return result;
#endif /* TBB_USE_ASSERT */
}
//! Release lock
void release() {
#if TBB_USE_ASSERT
internal_release ();
#else
my_mutex->unlock();
my_mutex = NULL;
#endif /* TBB_USE_ASSERT */
}
private:
//! The pointer to the current mutex to work
mutex* my_mutex;
//! All checks from acquire using mutex.state were moved here
void __TBB_EXPORTED_METHOD internal_acquire( mutex& m );
//! All checks from try_acquire using mutex.state were moved here
bool __TBB_EXPORTED_METHOD internal_try_acquire( mutex& m );
//! All checks from release using mutex.state were moved here
void __TBB_EXPORTED_METHOD internal_release();
friend class mutex;
};
// Mutex traits
static const bool is_rw_mutex = false;
static const bool is_recursive_mutex = false;
static const bool is_fair_mutex = false;
// ISO C++0x compatibility methods
//! Acquire lock
void lock() {
#if TBB_USE_ASSERT
aligned_space tmp;
new(tmp.begin()) scoped_lock(*this);
#else
#if _WIN32||_WIN64
EnterCriticalSection(&impl);
#else
pthread_mutex_lock(&impl);
#endif /* _WIN32||_WIN64 */
#endif /* TBB_USE_ASSERT */
}
//! Try acquiring lock (non-blocking)
/** Return true if lock acquired; false otherwise. */
bool try_lock() {
#if TBB_USE_ASSERT
aligned_space tmp;
scoped_lock& s = *tmp.begin();
s.my_mutex = NULL;
return s.internal_try_acquire(*this);
#else
#if _WIN32||_WIN64
return TryEnterCriticalSection(&impl)!=0;
#else
return pthread_mutex_trylock(&impl)==0;
#endif /* _WIN32||_WIN64 */
#endif /* TBB_USE_ASSERT */
}
//! Release lock
void unlock() {
#if TBB_USE_ASSERT
aligned_space tmp;
scoped_lock& s = *tmp.begin();
s.my_mutex = this;
s.internal_release();
#else
#if _WIN32||_WIN64
LeaveCriticalSection(&impl);
#else
pthread_mutex_unlock(&impl);
#endif /* _WIN32||_WIN64 */
#endif /* TBB_USE_ASSERT */
}
//! Return native_handle
#if _WIN32||_WIN64
typedef LPCRITICAL_SECTION native_handle_type;
#else
typedef pthread_mutex_t* native_handle_type;
#endif
native_handle_type native_handle() { return (native_handle_type) &impl; }
enum state_t {
INITIALIZED=0x1234,
DESTROYED=0x789A,
HELD=0x56CD
};
private:
#if _WIN32||_WIN64
CRITICAL_SECTION impl;
enum state_t state;
#else
pthread_mutex_t impl;
#endif /* _WIN32||_WIN64 */
//! All checks from mutex constructor using mutex.state were moved here
void __TBB_EXPORTED_METHOD internal_construct();
//! All checks from mutex destructor using mutex.state were moved here
void __TBB_EXPORTED_METHOD internal_destroy();
#if _WIN32||_WIN64
public:
//! Set the internal state
void set_state( state_t to ) { state = to; }
#endif
};
__TBB_DEFINE_PROFILING_SET_NAME(mutex)
} // namespace tbb
#endif /* __TBB_mutex_H */
tbb42_20130725oss/include/tbb/blocked_range3d.h 0000664 0000764 0000764 00000010545 12200165244 020153 0 ustar vtune vtune /*
Copyright 2005-2013 Intel Corporation. All Rights Reserved.
This file is part of Threading Building Blocks.
Threading Building Blocks is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
Threading Building Blocks is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Threading Building Blocks; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
As a special exception, you may use this file as part of a free software
library without restriction. Specifically, if other files instantiate
templates or use macros or inline functions from this file, or you compile
this file and link it with other files to produce an executable, this
file does not by itself cause the resulting executable to be covered by
the GNU General Public License. This exception does not however
invalidate any other reasons why the executable file might be covered by
the GNU General Public License.
*/
#ifndef __TBB_blocked_range3d_H
#define __TBB_blocked_range3d_H
#include "tbb_stddef.h"
#include "blocked_range.h"
namespace tbb {
//! A 3-dimensional range that models the Range concept.
/** @ingroup algorithms */
template
class blocked_range3d {
public:
//! Type for size of an iteration range
typedef blocked_range page_range_type;
typedef blocked_range row_range_type;
typedef blocked_range col_range_type;
private:
page_range_type my_pages;
row_range_type my_rows;
col_range_type my_cols;
public:
blocked_range3d( PageValue page_begin, PageValue page_end,
RowValue row_begin, RowValue row_end,
ColValue col_begin, ColValue col_end ) :
my_pages(page_begin,page_end),
my_rows(row_begin,row_end),
my_cols(col_begin,col_end)
{
}
blocked_range3d( PageValue page_begin, PageValue page_end, typename page_range_type::size_type page_grainsize,
RowValue row_begin, RowValue row_end, typename row_range_type::size_type row_grainsize,
ColValue col_begin, ColValue col_end, typename col_range_type::size_type col_grainsize ) :
my_pages(page_begin,page_end,page_grainsize),
my_rows(row_begin,row_end,row_grainsize),
my_cols(col_begin,col_end,col_grainsize)
{
}
//! True if range is empty
bool empty() const {
// Yes, it is a logical OR here, not AND.
return my_pages.empty() || my_rows.empty() || my_cols.empty();
}
//! True if range is divisible into two pieces.
bool is_divisible() const {
return my_pages.is_divisible() || my_rows.is_divisible() || my_cols.is_divisible();
}
blocked_range3d( blocked_range3d& r, split ) :
my_pages(r.my_pages),
my_rows(r.my_rows),
my_cols(r.my_cols)
{
if( my_pages.size()*double(my_rows.grainsize()) < my_rows.size()*double(my_pages.grainsize()) ) {
if ( my_rows.size()*double(my_cols.grainsize()) < my_cols.size()*double(my_rows.grainsize()) ) {
my_cols.my_begin = col_range_type::do_split(r.my_cols);
} else {
my_rows.my_begin = row_range_type::do_split(r.my_rows);
}
} else {
if ( my_pages.size()*double(my_cols.grainsize()) < my_cols.size()*double(my_pages.grainsize()) ) {
my_cols.my_begin = col_range_type::do_split(r.my_cols);
} else {
my_pages.my_begin = page_range_type::do_split(r.my_pages);
}
}
}
//! The pages of the iteration space
const page_range_type& pages() const {return my_pages;}
//! The rows of the iteration space
const row_range_type& rows() const {return my_rows;}
//! The columns of the iteration space
const col_range_type& cols() const {return my_cols;}
};
} // namespace tbb
#endif /* __TBB_blocked_range3d_H */
tbb42_20130725oss/include/tbb/concurrent_queue.h 0000664 0000764 0000764 00000034571 12200165244 020540 0 ustar vtune vtune /*
Copyright 2005-2013 Intel Corporation. All Rights Reserved.
This file is part of Threading Building Blocks.
Threading Building Blocks is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
Threading Building Blocks is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Threading Building Blocks; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
As a special exception, you may use this file as part of a free software
library without restriction. Specifically, if other files instantiate
templates or use macros or inline functions from this file, or you compile
this file and link it with other files to produce an executable, this
file does not by itself cause the resulting executable to be covered by
the GNU General Public License. This exception does not however
invalidate any other reasons why the executable file might be covered by
the GNU General Public License.
*/
#ifndef __TBB_concurrent_queue_H
#define __TBB_concurrent_queue_H
#include "internal/_concurrent_queue_impl.h"
namespace tbb {
namespace strict_ppl {
//! A high-performance thread-safe non-blocking concurrent queue.
/** Multiple threads may each push and pop concurrently.
Assignment construction is not allowed.
@ingroup containers */
template >
class concurrent_queue: public internal::concurrent_queue_base_v3 {
template friend class internal::concurrent_queue_iterator;
//! Allocator type
typedef typename A::template rebind::other page_allocator_type;
page_allocator_type my_allocator;
//! Allocates a block of size n (bytes)
/*override*/ virtual void *allocate_block( size_t n ) {
void *b = reinterpret_cast(my_allocator.allocate( n ));
if( !b )
internal::throw_exception(internal::eid_bad_alloc);
return b;
}
//! Deallocates block created by allocate_block.
/*override*/ virtual void deallocate_block( void *b, size_t n ) {
my_allocator.deallocate( reinterpret_cast(b), n );
}
public:
//! Element type in the queue.
typedef T value_type;
//! Reference type
typedef T& reference;
//! Const reference type
typedef const T& const_reference;
//! Integral type for representing size of the queue.
typedef size_t size_type;
//! Difference type for iterator
typedef ptrdiff_t difference_type;
//! Allocator type
typedef A allocator_type;
//! Construct empty queue
explicit concurrent_queue(const allocator_type& a = allocator_type()) :
my_allocator( a )
{
}
//! [begin,end) constructor
template
concurrent_queue( InputIterator begin, InputIterator end, const allocator_type& a = allocator_type()) :
my_allocator( a )
{
for( ; begin != end; ++begin )
this->internal_push(&*begin);
}
//! Copy constructor
concurrent_queue( const concurrent_queue& src, const allocator_type& a = allocator_type()) :
internal::concurrent_queue_base_v3(), my_allocator( a )
{
this->assign( src );
}
//! Destroy queue
~concurrent_queue();
//! Enqueue an item at tail of queue.
void push( const T& source ) {
this->internal_push( &source );
}
//! Attempt to dequeue an item from head of queue.
/** Does not wait for item to become available.
Returns true if successful; false otherwise. */
bool try_pop( T& result ) {
return this->internal_try_pop( &result );
}
//! Return the number of items in the queue; thread unsafe
size_type unsafe_size() const {return this->internal_size();}
//! Equivalent to size()==0.
bool empty() const {return this->internal_empty();}
//! Clear the queue. not thread-safe.
void clear() ;
//! Return allocator object
allocator_type get_allocator() const { return this->my_allocator; }
typedef internal::concurrent_queue_iterator iterator;
typedef internal::concurrent_queue_iterator const_iterator;
//------------------------------------------------------------------------
// The iterators are intended only for debugging. They are slow and not thread safe.
//------------------------------------------------------------------------
iterator unsafe_begin() {return iterator(*this);}
iterator unsafe_end() {return iterator();}
const_iterator unsafe_begin() const {return const_iterator(*this);}
const_iterator unsafe_end() const {return const_iterator();}
} ;
template
concurrent_queue::~concurrent_queue() {
clear();
this->internal_finish_clear();
}
template
void concurrent_queue::clear() {
while( !empty() ) {
T value;
this->internal_try_pop(&value);
}
}
} // namespace strict_ppl
//! A high-performance thread-safe blocking concurrent bounded queue.
/** This is the pre-PPL TBB concurrent queue which supports boundedness and blocking semantics.
Note that method names agree with the PPL-style concurrent queue.
Multiple threads may each push and pop concurrently.
Assignment construction is not allowed.
@ingroup containers */
template >
class concurrent_bounded_queue: public internal::concurrent_queue_base_v3 {
template friend class internal::concurrent_queue_iterator;
//! Allocator type
typedef typename A::template rebind::other page_allocator_type;
page_allocator_type my_allocator;
typedef typename concurrent_queue_base_v3::padded_page padded_page;
//! Class used to ensure exception-safety of method "pop"
class destroyer: internal::no_copy {
T& my_value;
public:
destroyer( T& value ) : my_value(value) {}
~destroyer() {my_value.~T();}
};
T& get_ref( page& p, size_t index ) {
__TBB_ASSERT( index(static_cast(&p))->last)[index];
}
/*override*/ virtual void copy_item( page& dst, size_t index, const void* src ) {
new( &get_ref(dst,index) ) T(*static_cast(src));
}
/*override*/ virtual void copy_page_item( page& dst, size_t dindex, const page& src, size_t sindex ) {
new( &get_ref(dst,dindex) ) T( get_ref( const_cast(src), sindex ) );
}
/*override*/ virtual void assign_and_destroy_item( void* dst, page& src, size_t index ) {
T& from = get_ref(src,index);
destroyer d(from);
*static_cast(dst) = from;
}
/*override*/ virtual page *allocate_page() {
size_t n = sizeof(padded_page) + (items_per_page-1)*sizeof(T);
page *p = reinterpret_cast(my_allocator.allocate( n ));
if( !p )
internal::throw_exception(internal::eid_bad_alloc);
return p;
}
/*override*/ virtual void deallocate_page( page *p ) {
size_t n = sizeof(padded_page) + (items_per_page-1)*sizeof(T);
my_allocator.deallocate( reinterpret_cast(p), n );
}
public:
//! Element type in the queue.
typedef T value_type;
//! Allocator type
typedef A allocator_type;
//! Reference type
typedef T& reference;
//! Const reference type
typedef const T& const_reference;
//! Integral type for representing size of the queue.
/** Note that the size_type is a signed integral type.
This is because the size can be negative if there are pending pops without corresponding pushes. */
typedef std::ptrdiff_t size_type;
//! Difference type for iterator
typedef std::ptrdiff_t difference_type;
//! Construct empty queue
explicit concurrent_bounded_queue(const allocator_type& a = allocator_type()) :
concurrent_queue_base_v3( sizeof(T) ), my_allocator( a )
{
}
//! Copy constructor
concurrent_bounded_queue( const concurrent_bounded_queue& src, const allocator_type& a = allocator_type()) :
concurrent_queue_base_v3( sizeof(T) ), my_allocator( a )
{
assign( src );
}
//! [begin,end) constructor
template
concurrent_bounded_queue( InputIterator begin, InputIterator end, const allocator_type& a = allocator_type()) :
concurrent_queue_base_v3( sizeof(T) ), my_allocator( a )
{
for( ; begin != end; ++begin )
internal_push_if_not_full(&*begin);
}
//! Destroy queue
~concurrent_bounded_queue();
//! Enqueue an item at tail of queue.
void push( const T& source ) {
internal_push( &source );
}
//! Dequeue item from head of queue.
/** Block until an item becomes available, and then dequeue it. */
void pop( T& destination ) {
internal_pop( &destination );
}
#if TBB_USE_EXCEPTIONS
//! Abort all pending queue operations
void abort() {
internal_abort();
}
#endif
//! Enqueue an item at tail of queue if queue is not already full.
/** Does not wait for queue to become not full.
Returns true if item is pushed; false if queue was already full. */
bool try_push( const T& source ) {
return internal_push_if_not_full( &source );
}
//! Attempt to dequeue an item from head of queue.
/** Does not wait for item to become available.
Returns true if successful; false otherwise. */
bool try_pop( T& destination ) {
return internal_pop_if_present( &destination );
}
//! Return number of pushes minus number of pops.
/** Note that the result can be negative if there are pops waiting for the
corresponding pushes. The result can also exceed capacity() if there
are push operations in flight. */
size_type size() const {return internal_size();}
//! Equivalent to size()<=0.
bool empty() const {return internal_empty();}
//! Maximum number of allowed elements
size_type capacity() const {
return my_capacity;
}
//! Set the capacity
/** Setting the capacity to 0 causes subsequent try_push operations to always fail,
and subsequent push operations to block forever. */
void set_capacity( size_type new_capacity ) {
internal_set_capacity( new_capacity, sizeof(T) );
}
//! return allocator object
allocator_type get_allocator() const { return this->my_allocator; }
//! clear the queue. not thread-safe.
void clear() ;
typedef internal::concurrent_queue_iterator iterator;
typedef internal::concurrent_queue_iterator const_iterator;
//------------------------------------------------------------------------
// The iterators are intended only for debugging. They are slow and not thread safe.
//------------------------------------------------------------------------
iterator unsafe_begin() {return iterator(*this);}
iterator unsafe_end() {return iterator();}
const_iterator unsafe_begin() const {return const_iterator(*this);}
const_iterator unsafe_end() const {return const_iterator();}
};
template
concurrent_bounded_queue::~concurrent_bounded_queue() {
clear();
internal_finish_clear();
}
template
void concurrent_bounded_queue::clear() {
while( !empty() ) {
T value;
internal_pop_if_present(&value);
}
}
namespace deprecated {
//! A high-performance thread-safe blocking concurrent bounded queue.
/** This is the pre-PPL TBB concurrent queue which support boundedness and blocking semantics.
Note that method names agree with the PPL-style concurrent queue.
Multiple threads may each push and pop concurrently.
Assignment construction is not allowed.
@ingroup containers */
template >
class concurrent_queue: public concurrent_bounded_queue {
#if !__TBB_TEMPLATE_FRIENDS_BROKEN
template friend class internal::concurrent_queue_iterator;
#endif
public:
//! Construct empty queue
explicit concurrent_queue(const A& a = A()) :
concurrent_bounded_queue( a )
{
}
//! Copy constructor
concurrent_queue( const concurrent_queue& src, const A& a = A()) :
concurrent_bounded_queue( src, a )
{
}
//! [begin,end) constructor
template
concurrent_queue( InputIterator b /*begin*/, InputIterator e /*end*/, const A& a = A()) :
concurrent_bounded_queue( b, e, a )
{
}
//! Enqueue an item at tail of queue if queue is not already full.
/** Does not wait for queue to become not full.
Returns true if item is pushed; false if queue was already full. */
bool push_if_not_full( const T& source ) {
return this->try_push( source );
}
//! Attempt to dequeue an item from head of queue.
/** Does not wait for item to become available.
Returns true if successful; false otherwise.
@deprecated Use try_pop()
*/
bool pop_if_present( T& destination ) {
return this->try_pop( destination );
}
typedef typename concurrent_bounded_queue::iterator iterator;
typedef typename concurrent_bounded_queue::const_iterator const_iterator;
//
//------------------------------------------------------------------------
// The iterators are intended only for debugging. They are slow and not thread safe.
//------------------------------------------------------------------------
iterator begin() {return this->unsafe_begin();}
iterator end() {return this->unsafe_end();}
const_iterator begin() const {return this->unsafe_begin();}
const_iterator end() const {return this->unsafe_end();}
};
}
#if TBB_DEPRECATED
using deprecated::concurrent_queue;
#else
using strict_ppl::concurrent_queue;
#endif
} // namespace tbb
#endif /* __TBB_concurrent_queue_H */
tbb42_20130725oss/include/tbb/critical_section.h 0000664 0000764 0000764 00000010563 12200165244 020463 0 ustar vtune vtune /*
Copyright 2005-2013 Intel Corporation. All Rights Reserved.
This file is part of Threading Building Blocks.
Threading Building Blocks is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
Threading Building Blocks is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Threading Building Blocks; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
As a special exception, you may use this file as part of a free software
library without restriction. Specifically, if other files instantiate
templates or use macros or inline functions from this file, or you compile
this file and link it with other files to produce an executable, this
file does not by itself cause the resulting executable to be covered by
the GNU General Public License. This exception does not however
invalidate any other reasons why the executable file might be covered by
the GNU General Public License.
*/
#ifndef _TBB_CRITICAL_SECTION_H_
#define _TBB_CRITICAL_SECTION_H_
#if _WIN32||_WIN64
#include "machine/windows_api.h"
#else
#include
#include
#endif // _WIN32||WIN64
#include "tbb_stddef.h"
#include "tbb_thread.h"
#include "tbb_exception.h"
#include "tbb_profiling.h"
namespace tbb {
namespace internal {
class critical_section_v4 : internal::no_copy {
#if _WIN32||_WIN64
CRITICAL_SECTION my_impl;
#else
pthread_mutex_t my_impl;
#endif
tbb_thread::id my_tid;
public:
void __TBB_EXPORTED_METHOD internal_construct();
critical_section_v4() {
#if _WIN32||_WIN64
InitializeCriticalSectionEx( &my_impl, 4000, 0 );
#else
pthread_mutex_init(&my_impl, NULL);
#endif
internal_construct();
}
~critical_section_v4() {
__TBB_ASSERT(my_tid == tbb_thread::id(), "Destroying a still-held critical section");
#if _WIN32||_WIN64
DeleteCriticalSection(&my_impl);
#else
pthread_mutex_destroy(&my_impl);
#endif
}
class scoped_lock : internal::no_copy {
private:
critical_section_v4 &my_crit;
public:
scoped_lock( critical_section_v4& lock_me) :my_crit(lock_me) {
my_crit.lock();
}
~scoped_lock() {
my_crit.unlock();
}
};
void lock() {
tbb_thread::id local_tid = this_tbb_thread::get_id();
if(local_tid == my_tid) throw_exception( eid_improper_lock );
#if _WIN32||_WIN64
EnterCriticalSection( &my_impl );
#else
int rval = pthread_mutex_lock(&my_impl);
__TBB_ASSERT_EX(!rval, "critical_section::lock: pthread_mutex_lock failed");
#endif
__TBB_ASSERT(my_tid == tbb_thread::id(), NULL);
my_tid = local_tid;
}
bool try_lock() {
bool gotlock;
tbb_thread::id local_tid = this_tbb_thread::get_id();
if(local_tid == my_tid) return false;
#if _WIN32||_WIN64
gotlock = TryEnterCriticalSection( &my_impl ) != 0;
#else
int rval = pthread_mutex_trylock(&my_impl);
// valid returns are 0 (locked) and [EBUSY]
__TBB_ASSERT(rval == 0 || rval == EBUSY, "critical_section::trylock: pthread_mutex_trylock failed");
gotlock = rval == 0;
#endif
if(gotlock) {
my_tid = local_tid;
}
return gotlock;
}
void unlock() {
__TBB_ASSERT(this_tbb_thread::get_id() == my_tid, "thread unlocking critical_section is not thread that locked it");
my_tid = tbb_thread::id();
#if _WIN32||_WIN64
LeaveCriticalSection( &my_impl );
#else
int rval = pthread_mutex_unlock(&my_impl);
__TBB_ASSERT_EX(!rval, "critical_section::unlock: pthread_mutex_unlock failed");
#endif
}
static const bool is_rw_mutex = false;
static const bool is_recursive_mutex = false;
static const bool is_fair_mutex = true;
}; // critical_section_v4
} // namespace internal
typedef internal::critical_section_v4 critical_section;
__TBB_DEFINE_PROFILING_SET_NAME(critical_section)
} // namespace tbb
#endif // _TBB_CRITICAL_SECTION_H_
tbb42_20130725oss/include/tbb/queuing_mutex.h 0000664 0000764 0000764 00000010110 12200165244 020030 0 ustar vtune vtune /*
Copyright 2005-2013 Intel Corporation. All Rights Reserved.
This file is part of Threading Building Blocks.
Threading Building Blocks is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
Threading Building Blocks is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Threading Building Blocks; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
As a special exception, you may use this file as part of a free software
library without restriction. Specifically, if other files instantiate
templates or use macros or inline functions from this file, or you compile
this file and link it with other files to produce an executable, this
file does not by itself cause the resulting executable to be covered by
the GNU General Public License. This exception does not however
invalidate any other reasons why the executable file might be covered by
the GNU General Public License.
*/
#ifndef __TBB_queuing_mutex_H
#define __TBB_queuing_mutex_H
#include "tbb_config.h"
#if !TBB_USE_EXCEPTIONS && _MSC_VER
// Suppress "C++ exception handler used, but unwind semantics are not enabled" warning in STL headers
#pragma warning (push)
#pragma warning (disable: 4530)
#endif
#include
#if !TBB_USE_EXCEPTIONS && _MSC_VER
#pragma warning (pop)
#endif
#include "atomic.h"
#include "tbb_profiling.h"
namespace tbb {
//! Queuing mutex with local-only spinning.
/** @ingroup synchronization */
class queuing_mutex {
public:
//! Construct unacquired mutex.
queuing_mutex() {
q_tail = NULL;
#if TBB_USE_THREADING_TOOLS
internal_construct();
#endif
}
//! The scoped locking pattern
/** It helps to avoid the common problem of forgetting to release lock.
It also nicely provides the "node" for queuing locks. */
class scoped_lock: internal::no_copy {
//! Initialize fields to mean "no lock held".
void initialize() {
mutex = NULL;
#if TBB_USE_ASSERT
internal::poison_pointer(next);
#endif /* TBB_USE_ASSERT */
}
public:
//! Construct lock that has not acquired a mutex.
/** Equivalent to zero-initialization of *this. */
scoped_lock() {initialize();}
//! Acquire lock on given mutex.
scoped_lock( queuing_mutex& m ) {
initialize();
acquire(m);
}
//! Release lock (if lock is held).
~scoped_lock() {
if( mutex ) release();
}
//! Acquire lock on given mutex.
void __TBB_EXPORTED_METHOD acquire( queuing_mutex& m );
//! Acquire lock on given mutex if free (i.e. non-blocking)
bool __TBB_EXPORTED_METHOD try_acquire( queuing_mutex& m );
//! Release lock.
void __TBB_EXPORTED_METHOD release();
private:
//! The pointer to the mutex owned, or NULL if not holding a mutex.
queuing_mutex* mutex;
//! The pointer to the next competitor for a mutex
scoped_lock *next;
//! The local spin-wait variable
/** Inverted (0 - blocked, 1 - acquired the mutex) for the sake of
zero-initialization. Defining it as an entire word instead of
a byte seems to help performance slightly. */
uintptr_t going;
};
void __TBB_EXPORTED_METHOD internal_construct();
// Mutex traits
static const bool is_rw_mutex = false;
static const bool is_recursive_mutex = false;
static const bool is_fair_mutex = true;
private:
//! The last competitor requesting the lock
atomic q_tail;
};
__TBB_DEFINE_PROFILING_SET_NAME(queuing_mutex)
} // namespace tbb
#endif /* __TBB_queuing_mutex_H */
tbb42_20130725oss/include/tbb/atomic.h 0000664 0000764 0000764 00000052241 12200165244 016420 0 ustar vtune vtune /*
Copyright 2005-2013 Intel Corporation. All Rights Reserved.
This file is part of Threading Building Blocks.
Threading Building Blocks is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
Threading Building Blocks is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Threading Building Blocks; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
As a special exception, you may use this file as part of a free software
library without restriction. Specifically, if other files instantiate
templates or use macros or inline functions from this file, or you compile
this file and link it with other files to produce an executable, this
file does not by itself cause the resulting executable to be covered by
the GNU General Public License. This exception does not however
invalidate any other reasons why the executable file might be covered by
the GNU General Public License.
*/
#ifndef __TBB_atomic_H
#define __TBB_atomic_H
#include
#if _MSC_VER
#define __TBB_LONG_LONG __int64
#else
#define __TBB_LONG_LONG long long
#endif /* _MSC_VER */
#include "tbb_machine.h"
#if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
// Workaround for overzealous compiler warnings
#pragma warning (push)
#pragma warning (disable: 4244 4267 4512)
#endif
namespace tbb {
//! Specifies memory semantics.
enum memory_semantics {
//! Sequential consistency
full_fence,
//! Acquire
acquire,
//! Release
release,
//! No ordering
relaxed
};
//! @cond INTERNAL
namespace internal {
#if __TBB_ATTRIBUTE_ALIGNED_PRESENT
#define __TBB_DECL_ATOMIC_FIELD(t,f,a) t f __attribute__ ((aligned(a)));
#elif __TBB_DECLSPEC_ALIGN_PRESENT
#define __TBB_DECL_ATOMIC_FIELD(t,f,a) __declspec(align(a)) t f;
#else
#error Do not know syntax for forcing alignment.
#endif
template
struct atomic_rep; // Primary template declared, but never defined.
template<>
struct atomic_rep<1> { // Specialization
typedef int8_t word;
};
template<>
struct atomic_rep<2> { // Specialization
typedef int16_t word;
};
template<>
struct atomic_rep<4> { // Specialization
#if _MSC_VER && !_WIN64
// Work-around that avoids spurious /Wp64 warnings
typedef intptr_t word;
#else
typedef int32_t word;
#endif
};
#if __TBB_64BIT_ATOMICS
template<>
struct atomic_rep<8> { // Specialization
typedef int64_t word;
};
#endif
template
struct aligned_storage;
//the specializations are needed to please MSVC syntax of __declspec(align()) which accept _literal_ constants only
#if __TBB_ATOMIC_CTORS
#define ATOMIC_STORAGE_PARTIAL_SPECIALIZATION(S) \
template \
struct aligned_storage { \
__TBB_DECL_ATOMIC_FIELD(value_type,my_value,S) \
aligned_storage() = default ; \
constexpr aligned_storage(value_type value):my_value(value){} \
}; \
#else
#define ATOMIC_STORAGE_PARTIAL_SPECIALIZATION(S) \
template \
struct aligned_storage { \
__TBB_DECL_ATOMIC_FIELD(value_type,my_value,S) \
}; \
#endif
template
struct aligned_storage {
value_type my_value;
#if __TBB_ATOMIC_CTORS
aligned_storage() = default ;
constexpr aligned_storage(value_type value):my_value(value){}
#endif
};
ATOMIC_STORAGE_PARTIAL_SPECIALIZATION(2)
ATOMIC_STORAGE_PARTIAL_SPECIALIZATION(4)
#if __TBB_64BIT_ATOMICS
ATOMIC_STORAGE_PARTIAL_SPECIALIZATION(8)
#endif
template
struct atomic_traits; // Primary template declared, but not defined.
#define __TBB_DECL_FENCED_ATOMIC_PRIMITIVES(S,M) \
template<> struct atomic_traits { \
typedef atomic_rep::word word; \
inline static word compare_and_swap( volatile void* location, word new_value, word comparand ) { \
return __TBB_machine_cmpswp##S##M(location,new_value,comparand); \
} \
inline static word fetch_and_add( volatile void* location, word addend ) { \
return __TBB_machine_fetchadd##S##M(location,addend); \
} \
inline static word fetch_and_store( volatile void* location, word value ) { \
return __TBB_machine_fetchstore##S##M(location,value); \
} \
};
#define __TBB_DECL_ATOMIC_PRIMITIVES(S) \
template \
struct atomic_traits { \
typedef atomic_rep::word word; \
inline static word compare_and_swap( volatile void* location, word new_value, word comparand ) { \
return __TBB_machine_cmpswp##S(location,new_value,comparand); \
} \
inline static word fetch_and_add( volatile void* location, word addend ) { \
return __TBB_machine_fetchadd##S(location,addend); \
} \
inline static word fetch_and_store( volatile void* location, word value ) { \
return __TBB_machine_fetchstore##S(location,value); \
} \
};
template
struct atomic_load_store_traits; // Primary template declaration
#define __TBB_DECL_ATOMIC_LOAD_STORE_PRIMITIVES(M) \
template<> struct atomic_load_store_traits { \
template \
inline static T load( const volatile T& location ) { \
return __TBB_load_##M( location ); \
} \
template \
inline static void store( volatile T& location, T value ) { \
__TBB_store_##M( location, value ); \
} \
}
#if __TBB_USE_FENCED_ATOMICS
__TBB_DECL_FENCED_ATOMIC_PRIMITIVES(1,full_fence)
__TBB_DECL_FENCED_ATOMIC_PRIMITIVES(2,full_fence)
__TBB_DECL_FENCED_ATOMIC_PRIMITIVES(4,full_fence)
__TBB_DECL_FENCED_ATOMIC_PRIMITIVES(1,acquire)
__TBB_DECL_FENCED_ATOMIC_PRIMITIVES(2,acquire)
__TBB_DECL_FENCED_ATOMIC_PRIMITIVES(4,acquire)
__TBB_DECL_FENCED_ATOMIC_PRIMITIVES(1,release)
__TBB_DECL_FENCED_ATOMIC_PRIMITIVES(2,release)
__TBB_DECL_FENCED_ATOMIC_PRIMITIVES(4,release)
__TBB_DECL_FENCED_ATOMIC_PRIMITIVES(1,relaxed)
__TBB_DECL_FENCED_ATOMIC_PRIMITIVES(2,relaxed)
__TBB_DECL_FENCED_ATOMIC_PRIMITIVES(4,relaxed)
#if __TBB_64BIT_ATOMICS
__TBB_DECL_FENCED_ATOMIC_PRIMITIVES(8,full_fence)
__TBB_DECL_FENCED_ATOMIC_PRIMITIVES(8,acquire)
__TBB_DECL_FENCED_ATOMIC_PRIMITIVES(8,release)
__TBB_DECL_FENCED_ATOMIC_PRIMITIVES(8,relaxed)
#endif
#else /* !__TBB_USE_FENCED_ATOMICS */
__TBB_DECL_ATOMIC_PRIMITIVES(1)
__TBB_DECL_ATOMIC_PRIMITIVES(2)
__TBB_DECL_ATOMIC_PRIMITIVES(4)
#if __TBB_64BIT_ATOMICS
__TBB_DECL_ATOMIC_PRIMITIVES(8)
#endif
#endif /* !__TBB_USE_FENCED_ATOMICS */
__TBB_DECL_ATOMIC_LOAD_STORE_PRIMITIVES(full_fence);
__TBB_DECL_ATOMIC_LOAD_STORE_PRIMITIVES(acquire);
__TBB_DECL_ATOMIC_LOAD_STORE_PRIMITIVES(release);
__TBB_DECL_ATOMIC_LOAD_STORE_PRIMITIVES(relaxed);
//! Additive inverse of 1 for type T.
/** Various compilers issue various warnings if -1 is used with various integer types.
The baroque expression below avoids all the warnings (we hope). */
#define __TBB_MINUS_ONE(T) (T(T(0)-T(1)))
//! Base class that provides basic functionality for atomic without fetch_and_add.
/** Works for any type T that has the same size as an integral type, has a trivial constructor/destructor,
and can be copied/compared by memcpy/memcmp. */
template
struct atomic_impl {
protected:
aligned_storage my_storage;
private:
//TODO: rechecks on recent versions of gcc if union is still the _only_ way to do a conversion without warnings
//! Union type used to convert type T to underlying integral type.
template
union converter {
typedef typename atomic_rep::word bits_type;
converter(){}
converter(value_type a_value) : value(a_value) {}
value_type value;
bits_type bits;
};
template
static typename converter::bits_type to_bits(value_t value){
return converter(value).bits;
}
template
static value_t to_value(typename converter::bits_type bits){
converter u;
u.bits = bits;
return u.value;
}
template
union ptr_converter; //Primary template declared, but never defined.
template
union ptr_converter {
ptr_converter(){}
ptr_converter(value_t* a_value) : value(a_value) {}
value_t* value;
uintptr_t bits;
};
//TODO: check if making to_bits accepting reference (thus unifying it with to_bits_ref)
//does not hurt performance
template
static typename converter::bits_type & to_bits_ref(value_t& value){
//TODO: this #ifdef is temporary workaround, as union conversion seems to fail
//on suncc for 64 bit types for 32 bit target
#if !__SUNPRO_CC
return *(typename converter::bits_type*)ptr_converter(&value).bits;
#else
return *(typename converter::bits_type*)(&value);
#endif
}
public:
typedef T value_type;
#if __TBB_ATOMIC_CTORS
atomic_impl() = default ;
constexpr atomic_impl(value_type value):my_storage(value){}
#endif
template
value_type fetch_and_store( value_type value ) {
return to_value(
internal::atomic_traits::fetch_and_store( &my_storage.my_value, to_bits(value) )
);
}
value_type fetch_and_store( value_type value ) {
return fetch_and_store(value);
}
template
value_type compare_and_swap( value_type value, value_type comparand ) {
return to_value(
internal::atomic_traits::compare_and_swap( &my_storage.my_value, to_bits(value), to_bits(comparand) )
);
}
value_type compare_and_swap( value_type value, value_type comparand ) {
return compare_and_swap(value,comparand);
}
operator value_type() const volatile { // volatile qualifier here for backwards compatibility
return to_value(
__TBB_load_with_acquire( to_bits_ref(my_storage.my_value) )
);
}
template
value_type load () const {
return to_value(
internal::atomic_load_store_traits::load( to_bits_ref(my_storage.my_value) )
);
}
value_type load () const {
return load();
}
template
void store ( value_type value ) {
internal::atomic_load_store_traits::store( to_bits_ref(my_storage.my_value), to_bits(value));
}
void store ( value_type value ) {
store( value );
}
protected:
value_type store_with_release( value_type rhs ) {
//TODO: unify with store
__TBB_store_with_release( to_bits_ref(my_storage.my_value), to_bits(rhs) );
return rhs;
}
};
//! Base class that provides basic functionality for atomic with fetch_and_add.
/** I is the underlying type.
D is the difference type.
StepType should be char if I is an integral type, and T if I is a T*. */
template
struct atomic_impl_with_arithmetic: atomic_impl {
public:
typedef I value_type;
#if __TBB_ATOMIC_CTORS
atomic_impl_with_arithmetic() = default ;
constexpr atomic_impl_with_arithmetic(value_type value): atomic_impl(value){}
#endif
template
value_type fetch_and_add( D addend ) {
return value_type(internal::atomic_traits::fetch_and_add( &this->my_storage.my_value, addend*sizeof(StepType) ));
}
value_type fetch_and_add( D addend ) {
return fetch_and_add(addend);
}
template
value_type fetch_and_increment() {
return fetch_and_add(1);
}
value_type fetch_and_increment() {
return fetch_and_add(1);
}
template
value_type fetch_and_decrement() {
return fetch_and_add(__TBB_MINUS_ONE(D));
}
value_type fetch_and_decrement() {
return fetch_and_add(__TBB_MINUS_ONE(D));
}
public:
value_type operator+=( D value ) {
return fetch_and_add(value)+value;
}
value_type operator-=( D value ) {
// Additive inverse of value computed using binary minus,
// instead of unary minus, for sake of avoiding compiler warnings.
return operator+=(D(0)-value);
}
value_type operator++() {
return fetch_and_add(1)+1;
}
value_type operator--() {
return fetch_and_add(__TBB_MINUS_ONE(D))-1;
}
value_type operator++(int) {
return fetch_and_add(1);
}
value_type operator--(int) {
return fetch_and_add(__TBB_MINUS_ONE(D));
}
};
} /* Internal */
//! @endcond
//! Primary template for atomic.
/** See the Reference for details.
@ingroup synchronization */
template
struct atomic: internal::atomic_impl {
#if __TBB_ATOMIC_CTORS
atomic() = default;
constexpr atomic(T arg): internal::atomic_impl(arg) {}
#endif
T operator=( T rhs ) {
// "this" required here in strict ISO C++ because store_with_release is a dependent name
return this->store_with_release(rhs);
}
atomic& operator=( const atomic& rhs ) {this->store_with_release(rhs); return *this;}
};
#if __TBB_ATOMIC_CTORS
#define __TBB_DECL_ATOMIC(T) \
template<> struct atomic: internal::atomic_impl_with_arithmetic { \
atomic() = default; \
constexpr atomic(T arg): internal::atomic_impl_with_arithmetic(arg) {} \
\
T operator=( T rhs ) {return store_with_release(rhs);} \
atomic& operator=( const atomic& rhs ) {store_with_release(rhs); return *this;} \
};
#else
#define __TBB_DECL_ATOMIC(T) \
template<> struct atomic: internal::atomic_impl_with_arithmetic { \
T operator=( T rhs ) {return store_with_release(rhs);} \
atomic& operator=( const atomic& rhs ) {store_with_release(rhs); return *this;} \
};
#endif
#if __TBB_64BIT_ATOMICS
//TODO: consider adding non-default (and atomic) copy constructor for 32bit platform
__TBB_DECL_ATOMIC(__TBB_LONG_LONG)
__TBB_DECL_ATOMIC(unsigned __TBB_LONG_LONG)
#else
// test_atomic will verify that sizeof(long long)==8
#endif
__TBB_DECL_ATOMIC(long)
__TBB_DECL_ATOMIC(unsigned long)
#if _MSC_VER && !_WIN64
#if __TBB_ATOMIC_CTORS
/* Special version of __TBB_DECL_ATOMIC that avoids gratuitous warnings from cl /Wp64 option.
It is identical to __TBB_DECL_ATOMIC(unsigned) except that it replaces operator=(T)
with an operator=(U) that explicitly converts the U to a T. Types T and U should be
type synonyms on the platform. Type U should be the wider variant of T from the
perspective of /Wp64. */
#define __TBB_DECL_ATOMIC_ALT(T,U) \
template<> struct atomic: internal::atomic_impl_with_arithmetic { \
atomic() = default ; \
constexpr atomic(T arg): internal::atomic_impl_with_arithmetic(arg) {} \
T operator=( U rhs ) {return store_with_release(T(rhs));} \
atomic& operator=( const atomic& rhs ) {store_with_release(rhs); return *this;} \
};
#else
#define __TBB_DECL_ATOMIC_ALT(T,U) \
template<> struct atomic: internal::atomic_impl_with_arithmetic { \
T operator=( U rhs ) {return store_with_release(T(rhs));} \
atomic& operator=( const atomic& rhs ) {store_with_release(rhs); return *this;} \
};
#endif
__TBB_DECL_ATOMIC_ALT(unsigned,size_t)
__TBB_DECL_ATOMIC_ALT(int,ptrdiff_t)
#else
__TBB_DECL_ATOMIC(unsigned)
__TBB_DECL_ATOMIC(int)
#endif /* _MSC_VER && !_WIN64 */
__TBB_DECL_ATOMIC(unsigned short)
__TBB_DECL_ATOMIC(short)
__TBB_DECL_ATOMIC(char)
__TBB_DECL_ATOMIC(signed char)
__TBB_DECL_ATOMIC(unsigned char)
#if !_MSC_VER || defined(_NATIVE_WCHAR_T_DEFINED)
__TBB_DECL_ATOMIC(wchar_t)
#endif /* _MSC_VER||!defined(_NATIVE_WCHAR_T_DEFINED) */
//! Specialization for atomic with arithmetic and operator->.
template