You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
8.5 KiB

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