2 Copyright 2005-2011 Intel Corporation. All Rights Reserved.
4 This file is part of Threading Building Blocks.
6 Threading Building Blocks is free software; you can redistribute it
7 and/or modify it under the terms of the GNU General Public License
8 version 2 as published by the Free Software Foundation.
10 Threading Building Blocks is distributed in the hope that it will be
11 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with Threading Building Blocks; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 As a special exception, you may use this file as part of a free software
20 library without restriction. Specifically, if other files instantiate
21 templates or use macros or inline functions from this file, or you compile
22 this file and link it with other files to produce an executable, this
23 file does not by itself cause the resulting executable to be covered by
24 the GNU General Public License. This exception does not however
25 invalidate any other reasons why the executable file might be covered by
26 the GNU General Public License.
29 #ifndef __TBB_profiling_H
30 #define __TBB_profiling_H
32 // Check if the tools support is enabled
33 #if (_WIN32||_WIN64||__linux__) && !__MINGW32__ && TBB_USE_THREADING_TOOLS
36 #include <stdlib.h> /* mbstowcs_s */
38 #include "tbb_stddef.h"
43 void __TBB_EXPORTED_FUNC itt_set_sync_name_v3( void *obj, const wchar_t* name );
44 inline size_t multibyte_to_widechar( wchar_t* wcs, const char* mbs, size_t bufsize) {
47 mbstowcs_s( &len, wcs, bufsize, mbs, _TRUNCATE );
48 return len; // mbstowcs_s counts null terminator
50 size_t len = mbstowcs( wcs, mbs, bufsize );
51 if(wcs && len!=size_t(-1) )
52 wcs[len<bufsize-1? len: bufsize-1] = wchar_t('\0');
53 return len+1; // mbstowcs does not count null terminator
57 void __TBB_EXPORTED_FUNC itt_set_sync_name_v3( void *obj, const char* name );
59 } // namespace internal
62 //! Macro __TBB_DEFINE_PROFILING_SET_NAME(T) defines "set_name" methods for sync objects of type T
63 /** Should be used in the "tbb" namespace only.
64 Don't place semicolon after it to avoid compiler warnings. **/
66 #define __TBB_DEFINE_PROFILING_SET_NAME(sync_object_type) \
67 namespace profiling { \
68 inline void set_name( sync_object_type& obj, const wchar_t* name ) { \
69 tbb::internal::itt_set_sync_name_v3( &obj, name ); \
71 inline void set_name( sync_object_type& obj, const char* name ) { \
72 size_t len = tbb::internal::multibyte_to_widechar(NULL, name, 0); \
73 wchar_t *wname = new wchar_t[len]; \
74 tbb::internal::multibyte_to_widechar(wname, name, len); \
75 set_name( obj, wname ); \
80 #define __TBB_DEFINE_PROFILING_SET_NAME(sync_object_type) \
81 namespace profiling { \
82 inline void set_name( sync_object_type& obj, const char* name ) { \
83 tbb::internal::itt_set_sync_name_v3( &obj, name ); \
88 #else /* no tools support */
91 #define __TBB_DEFINE_PROFILING_SET_NAME(sync_object_type) \
92 namespace profiling { \
93 inline void set_name( sync_object_type&, const wchar_t* ) {} \
94 inline void set_name( sync_object_type&, const char* ) {} \
97 #define __TBB_DEFINE_PROFILING_SET_NAME(sync_object_type) \
98 namespace profiling { \
99 inline void set_name( sync_object_type&, const char* ) {} \
103 #endif /* no tools support */
106 // Need these to work regardless of tools support
110 enum notify_type {prepare=0, cancel, acquired, releasing};
111 const uintptr_t NUM_NOTIFY_TYPES = 4; // set to # elements in enum above
113 void __TBB_EXPORTED_FUNC call_itt_notify_v5(int t, void *ptr);
114 void __TBB_EXPORTED_FUNC itt_store_pointer_with_release_v3(void *dst, void *src);
115 void* __TBB_EXPORTED_FUNC itt_load_pointer_with_acquire_v3(const void *src);
116 void* __TBB_EXPORTED_FUNC itt_load_pointer_v3( const void* src );
118 // two template arguments are to workaround /Wp64 warning with tbb::atomic specialized for unsigned type
119 template <typename T, typename U>
120 inline void itt_store_word_with_release(tbb::atomic<T>& dst, U src) {
121 #if TBB_USE_THREADING_TOOLS
122 // This assertion should be replaced with static_assert
123 __TBB_ASSERT(sizeof(T) == sizeof(void *), "Type must be word-sized.");
124 itt_store_pointer_with_release_v3(&dst, (void *)uintptr_t(src));
127 #endif // TBB_USE_THREADING_TOOLS
130 template <typename T>
131 inline T itt_load_word_with_acquire(const tbb::atomic<T>& src) {
132 #if TBB_USE_THREADING_TOOLS
133 // This assertion should be replaced with static_assert
134 __TBB_ASSERT(sizeof(T) == sizeof(void *), "Type must be word-sized.");
135 #if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
136 // Workaround for overzealous compiler warnings
137 #pragma warning (push)
138 #pragma warning (disable: 4311)
140 T result = (T)itt_load_pointer_with_acquire_v3(&src);
141 #if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
142 #pragma warning (pop)
147 #endif // TBB_USE_THREADING_TOOLS
150 template <typename T>
151 inline void itt_store_word_with_release(T& dst, T src) {
152 #if TBB_USE_THREADING_TOOLS
153 // This assertion should be replaced with static_assert
154 __TBB_ASSERT(sizeof(T) == sizeof(void *), "Type must be word-sized.");
155 itt_store_pointer_with_release_v3(&dst, (void *)src);
157 __TBB_store_with_release(dst, src);
158 #endif // TBB_USE_THREADING_TOOLS
161 template <typename T>
162 inline T itt_load_word_with_acquire(const T& src) {
163 #if TBB_USE_THREADING_TOOLS
164 // This assertion should be replaced with static_assert
165 __TBB_ASSERT(sizeof(T) == sizeof(void *), "Type must be word-sized");
166 return (T)itt_load_pointer_with_acquire_v3(&src);
168 return __TBB_load_with_acquire(src);
169 #endif // TBB_USE_THREADING_TOOLS
172 template <typename T>
173 inline void itt_hide_store_word(T& dst, T src) {
174 #if TBB_USE_THREADING_TOOLS
175 // This assertion should be replaced with static_assert
176 __TBB_ASSERT(sizeof(T) == sizeof(void *), "Type must be word-sized");
177 itt_store_pointer_with_release_v3(&dst, (void *)src);
183 template <typename T>
184 inline T itt_hide_load_word(const T& src) {
185 #if TBB_USE_THREADING_TOOLS
186 // This assertion should be replaced with static_assert
187 __TBB_ASSERT(sizeof(T) == sizeof(void *), "Type must be word-sized.");
188 return (T)itt_load_pointer_v3(&src);
194 #if TBB_USE_THREADING_TOOLS
195 inline void call_itt_notify(notify_type t, void *ptr) {
196 call_itt_notify_v5((int)t, ptr);
199 inline void call_itt_notify(notify_type /*t*/, void * /*ptr*/) {}
200 #endif // TBB_USE_THREADING_TOOLS
202 } // namespace internal
205 #endif /* __TBB_profiling_H */