tbb_profiling.h

00001 /*
00002     Copyright 2005-2011 Intel Corporation.  All Rights Reserved.
00003 
00004     The source code contained or described herein and all documents related
00005     to the source code ("Material") are owned by Intel Corporation or its
00006     suppliers or licensors.  Title to the Material remains with Intel
00007     Corporation or its suppliers and licensors.  The Material is protected
00008     by worldwide copyright laws and treaty provisions.  No part of the
00009     Material may be used, copied, reproduced, modified, published, uploaded,
00010     posted, transmitted, distributed, or disclosed in any way without
00011     Intel's prior express written permission.
00012 
00013     No license under any patent, copyright, trade secret or other
00014     intellectual property right is granted to or conferred upon you by
00015     disclosure or delivery of the Materials, either expressly, by
00016     implication, inducement, estoppel or otherwise.  Any license under such
00017     intellectual property rights must be express and approved by Intel in
00018     writing.
00019 */
00020 
00021 #ifndef __TBB_profiling_H
00022 #define __TBB_profiling_H
00023 
00024 // Check if the tools support is enabled
00025 #if (_WIN32||_WIN64||__linux__) && !__MINGW32__ && TBB_USE_THREADING_TOOLS
00026 
00027 #if _WIN32||_WIN64
00028 #include <stdlib.h>  /* mbstowcs_s */
00029 #endif
00030 #include "tbb_stddef.h"
00031 
00032 namespace tbb {
00033     namespace internal {
00034 #if _WIN32||_WIN64
00035         void __TBB_EXPORTED_FUNC itt_set_sync_name_v3( void *obj, const wchar_t* name ); 
00036         inline size_t multibyte_to_widechar( wchar_t* wcs, const char* mbs, size_t bufsize) {
00037 #if _MSC_VER>=1400
00038             size_t len;
00039             mbstowcs_s( &len, wcs, bufsize, mbs, _TRUNCATE );
00040             return len;   // mbstowcs_s counts null terminator
00041 #else
00042             size_t len = mbstowcs( wcs, mbs, bufsize );
00043             if(wcs && len!=size_t(-1) )
00044                 wcs[len<bufsize-1? len: bufsize-1] = wchar_t('\0');
00045             return len+1; // mbstowcs does not count null terminator
00046 #endif
00047         }
00048 #else
00049         void __TBB_EXPORTED_FUNC itt_set_sync_name_v3( void *obj, const char* name ); 
00050 #endif
00051     } // namespace internal
00052 } // namespace tbb
00053 
00055 
00057 #if _WIN32||_WIN64
00058     #define __TBB_DEFINE_PROFILING_SET_NAME(sync_object_type)    \
00059         namespace profiling {                                                       \
00060             inline void set_name( sync_object_type& obj, const wchar_t* name ) {    \
00061                 tbb::internal::itt_set_sync_name_v3( &obj, name );                  \
00062             }                                                                       \
00063             inline void set_name( sync_object_type& obj, const char* name ) {       \
00064                 size_t len = tbb::internal::multibyte_to_widechar(NULL, name, 0);   \
00065                 wchar_t *wname = new wchar_t[len];                                  \
00066                 tbb::internal::multibyte_to_widechar(wname, name, len);             \
00067                 set_name( obj, wname );                                             \
00068                 delete[] wname;                                                     \
00069             }                                                                       \
00070         }
00071 #else /* !WIN */
00072     #define __TBB_DEFINE_PROFILING_SET_NAME(sync_object_type)    \
00073         namespace profiling {                                                       \
00074             inline void set_name( sync_object_type& obj, const char* name ) {       \
00075                 tbb::internal::itt_set_sync_name_v3( &obj, name );                  \
00076             }                                                                       \
00077         }
00078 #endif /* !WIN */
00079 
00080 #else /* no tools support */
00081 
00082 #if _WIN32||_WIN64
00083     #define __TBB_DEFINE_PROFILING_SET_NAME(sync_object_type)    \
00084         namespace profiling {                                               \
00085             inline void set_name( sync_object_type&, const wchar_t* ) {}    \
00086             inline void set_name( sync_object_type&, const char* ) {}       \
00087         }
00088 #else /* !WIN */
00089     #define __TBB_DEFINE_PROFILING_SET_NAME(sync_object_type)    \
00090         namespace profiling {                                               \
00091             inline void set_name( sync_object_type&, const char* ) {}       \
00092         }
00093 #endif /* !WIN */
00094 
00095 #endif /* no tools support */
00096 
00097 #include "atomic.h"
00098 // Need these to work regardless of tools support
00099 namespace tbb {
00100     namespace internal {
00101 
00102         enum notify_type {prepare=0, cancel, acquired, releasing};
00103         const uintptr_t NUM_NOTIFY_TYPES = 4; // set to # elements in enum above
00104         
00105         void __TBB_EXPORTED_FUNC call_itt_notify_v5(int t, void *ptr);
00106         void __TBB_EXPORTED_FUNC itt_store_pointer_with_release_v3(void *dst, void *src);
00107         void* __TBB_EXPORTED_FUNC itt_load_pointer_with_acquire_v3(const void *src);
00108         void* __TBB_EXPORTED_FUNC itt_load_pointer_v3( const void* src );
00109 
00110         // two template arguments are to workaround /Wp64 warning with tbb::atomic specialized for unsigned type
00111         template <typename T, typename U>
00112         inline void itt_store_word_with_release(tbb::atomic<T>& dst, U src) {
00113 #if TBB_USE_THREADING_TOOLS
00114             // This assertion should be replaced with static_assert
00115             __TBB_ASSERT(sizeof(T) == sizeof(void *), "Type must be word-sized.");
00116             itt_store_pointer_with_release_v3(&dst, (void *)uintptr_t(src));
00117 #else
00118             dst = src;
00119 #endif // TBB_USE_THREADING_TOOLS
00120         }
00121 
00122         template <typename T>
00123         inline T itt_load_word_with_acquire(const tbb::atomic<T>& src) {
00124 #if TBB_USE_THREADING_TOOLS
00125             // This assertion should be replaced with static_assert
00126             __TBB_ASSERT(sizeof(T) == sizeof(void *), "Type must be word-sized.");
00127 #if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
00128             // Workaround for overzealous compiler warnings 
00129             #pragma warning (push)
00130             #pragma warning (disable: 4311)
00131 #endif
00132             T result = (T)itt_load_pointer_with_acquire_v3(&src);
00133 #if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
00134             #pragma warning (pop)
00135 #endif
00136             return result;
00137 #else
00138             return src;
00139 #endif // TBB_USE_THREADING_TOOLS
00140         }
00141 
00142         template <typename T>
00143         inline void itt_store_word_with_release(T& dst, T src) {
00144 #if TBB_USE_THREADING_TOOLS
00145             // This assertion should be replaced with static_assert
00146             __TBB_ASSERT(sizeof(T) == sizeof(void *), "Type must be word-sized.");
00147             itt_store_pointer_with_release_v3(&dst, (void *)src);
00148 #else
00149             __TBB_store_with_release(dst, src); 
00150 #endif // TBB_USE_THREADING_TOOLS
00151         }
00152 
00153         template <typename T>
00154         inline T itt_load_word_with_acquire(const T& src) {
00155 #if TBB_USE_THREADING_TOOLS
00156             // This assertion should be replaced with static_assert
00157             __TBB_ASSERT(sizeof(T) == sizeof(void *), "Type must be word-sized");
00158             return (T)itt_load_pointer_with_acquire_v3(&src);
00159 #else
00160             return __TBB_load_with_acquire(src);
00161 #endif // TBB_USE_THREADING_TOOLS
00162         }
00163         
00164         template <typename T>
00165         inline void itt_hide_store_word(T& dst, T src) {
00166 #if TBB_USE_THREADING_TOOLS
00167             // This assertion should be replaced with static_assert
00168             __TBB_ASSERT(sizeof(T) == sizeof(void *), "Type must be word-sized");
00169             itt_store_pointer_with_release_v3(&dst, (void *)src);
00170 #else
00171             dst = src;
00172 #endif
00173         }
00174 
00175         template <typename T>
00176         inline T itt_hide_load_word(const T& src) {
00177 #if TBB_USE_THREADING_TOOLS
00178             // This assertion should be replaced with static_assert
00179             __TBB_ASSERT(sizeof(T) == sizeof(void *), "Type must be word-sized.");
00180             return (T)itt_load_pointer_v3(&src);
00181 #else
00182             return src;
00183 #endif
00184         }
00185 
00186 #if TBB_USE_THREADING_TOOLS
00187         inline void call_itt_notify(notify_type t, void *ptr) {
00188             call_itt_notify_v5((int)t, ptr);
00189         }
00190 #else
00191         inline void call_itt_notify(notify_type /*t*/, void * /*ptr*/) {}
00192 #endif // TBB_USE_THREADING_TOOLS
00193 
00194     } // namespace internal
00195 } // namespace tbb
00196 
00197 #endif /* __TBB_profiling_H */

Copyright © 2005-2011 Intel Corporation. All Rights Reserved.

Intel, Pentium, Intel Xeon, Itanium, Intel XScale and VTune are registered trademarks or trademarks 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.