1/*
2 Copyright 2005-2013 Intel Corporation. All Rights Reserved.
3
4 This file is part of Threading Building Blocks.
5
6 Threading Building Blocks is free software; you can redistribute it
7 and/or modify it under the terms of the GNU General Public License
8 version 2 as published by the Free Software Foundation.
9
10 Threading Building Blocks is distributed in the hope that it will be
11 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with Threading Building Blocks; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19 As a special exception, you may use this file as part of a free software
20 library without restriction. Specifically, if other files instantiate
21 templates or use macros or inline functions from this file, or you compile
22 this file and link it with other files to produce an executable, this
23 file does not by itself cause the resulting executable to be covered by
24 the GNU General Public License. This exception does not however
25 invalidate any other reasons why the executable file might be covered by
26 the GNU General Public License.
27*/
28
29#ifndef __TBB_profiling_H
30#define __TBB_profiling_H
31
32// Check if the tools support is enabled
33#if (_WIN32||_WIN64||__linux__) && !__MINGW32__ && TBB_USE_THREADING_TOOLS
34
35#if _WIN32||_WIN64
36#include <stdlib.h> /* mbstowcs_s */
37#endif
38#include "tbb_stddef.h"
39
40namespace tbb {
41 namespace internal {
42#if _WIN32||_WIN64
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) {
45#if _MSC_VER>=1400
46 size_t len;
47 mbstowcs_s( &len, wcs, bufsize, mbs, _TRUNCATE );
48 return len; // mbstowcs_s counts null terminator
49#else
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
54#endif
55 }
56#else
57 void __TBB_EXPORTED_FUNC itt_set_sync_name_v3( void *obj, const char* name );
58#endif
59 } // namespace internal
60} // namespace tbb
61
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. **/
65#if _WIN32||_WIN64
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 ); \
70 } \
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 ); \
76 delete[] wname; \
77 } \
78 }
79#else /* !WIN */
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 ); \
84 } \
85 }
86#endif /* !WIN */
87
88#else /* no tools support */
89
90#if _WIN32||_WIN64
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* ) {} \
95 }
96#else /* !WIN */
97 #define __TBB_DEFINE_PROFILING_SET_NAME(sync_object_type) \
98 namespace profiling { \
99 inline void set_name( sync_object_type&, const char* ) {} \
100 }
101#endif /* !WIN */
102
103#endif /* no tools support */
104
105#include "atomic.h"
106// Need these to work regardless of tools support
107namespace tbb {
108 namespace internal {
109
110 enum notify_type {prepare=0, cancel, acquired, releasing};
111 const uintptr_t NUM_NOTIFY_TYPES = 4; // set to # elements in enum above
112
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 );
117
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));
125#else
126 dst = src;
127#endif // TBB_USE_THREADING_TOOLS
128 }
129
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)
139#endif
140 T result = (T)itt_load_pointer_with_acquire_v3(&src);
141#if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
142 #pragma warning (pop)
143#endif
144 return result;
145#else
146 return src;
147#endif // TBB_USE_THREADING_TOOLS
148 }
149
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);
156#else
157 __TBB_store_with_release(dst, src);
158#endif // TBB_USE_THREADING_TOOLS
159 }
160
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);
167#else
168 return __TBB_load_with_acquire(src);
169#endif // TBB_USE_THREADING_TOOLS
170 }
171
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);
178#else
179 dst = src;
180#endif
181 }
182
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);
189#else
190 return src;
191#endif
192 }
193
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);
197 }
198#else
199 inline void call_itt_notify(notify_type /*t*/, void * /*ptr*/) {}
200#endif // TBB_USE_THREADING_TOOLS
201
202 } // namespace internal
203} // namespace tbb
204
205#endif /* __TBB_profiling_H */
206