Intel(R) Threading Building Blocks Doxygen Documentation  version 4.2.3
tbb_misc.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2005-2019 Intel Corporation
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 
17 // Source file for miscellaneous entities that are infrequently referenced by
18 // an executing program.
19 
20 #include "tbb/tbb_stddef.h"
21 #include "tbb_assert_impl.h" // Out-of-line TBB assertion handling routines are instantiated here.
22 #include "tbb/tbb_exception.h"
23 #include "tbb/tbb_machine.h"
24 #include "tbb_misc.h"
25 #include "tbb_version.h"
26 
27 #include <cstdio>
28 #include <cstdlib>
29 #include <stdexcept>
30 #include <cstring>
31 
32 #if _WIN32||_WIN64
34 #endif
35 
36 #define __TBB_STD_RETHROW_EXCEPTION_POSSIBLY_BROKEN \
37  (__GLIBCXX__ && __TBB_GLIBCXX_VERSION>=40700 && __TBB_GLIBCXX_VERSION<60000 \
38  && TBB_USE_EXCEPTIONS && !TBB_USE_CAPTURED_EXCEPTION)
39 
40 #if __TBB_STD_RETHROW_EXCEPTION_POSSIBLY_BROKEN
41 // GCC ABI declarations necessary for a workaround
42 #include <cxxabi.h>
43 #endif
44 
45 namespace tbb {
46 
47 const char* bad_last_alloc::what() const throw() { return "bad allocation in previous or concurrent attempt"; }
48 const char* improper_lock::what() const throw() { return "attempted recursive lock on critical section or non-recursive mutex"; }
49 const char* user_abort::what() const throw() { return "User-initiated abort has terminated this operation"; }
50 const char* invalid_multiple_scheduling::what() const throw() { return "The same task_handle object cannot be executed more than once"; }
51 const char* missing_wait::what() const throw() { return "wait() was not called on the structured_task_group"; }
52 
53 namespace internal {
54 
55 #if TBB_USE_EXCEPTIONS
56  #define DO_THROW(exc, init_args) throw exc init_args;
57 #else /* !TBB_USE_EXCEPTIONS */
58  #define PRINT_ERROR_AND_ABORT(exc_name, msg) \
59  fprintf (stderr, "Exception %s with message %s would've been thrown, " \
60  "if exception handling were not disabled. Aborting.\n", exc_name, msg); \
61  fflush(stderr); \
62  std::abort();
63  #define DO_THROW(exc, init_args) PRINT_ERROR_AND_ABORT(#exc, #init_args)
64 #endif /* !TBB_USE_EXCEPTIONS */
65 
66 
67 /* The "what" should be fairly short, not more than about 128 characters.
68  Because we control all the call sites to handle_perror, it is pointless
69  to bullet-proof it for very long strings.
70 
71  Design note: ADR put this routine off to the side in tbb_misc.cpp instead of
72  Task.cpp because the throw generates a pathetic lot of code, and ADR wanted
73  this large chunk of code to be placed on a cold page. */
74 void handle_perror( int error_code, const char* what ) {
75  char buf[256];
76 #if _MSC_VER
77  #define snprintf _snprintf
78 #endif
79  int written = snprintf(buf, sizeof(buf), "%s: %s", what, strerror( error_code ));
80  // On overflow, the returned value exceeds sizeof(buf) (for GLIBC) or is negative (for MSVC).
81  __TBB_ASSERT_EX( written>0 && written<(int)sizeof(buf), "Error description is too long" );
82  // Ensure that buffer ends in terminator.
83  buf[sizeof(buf)-1] = 0;
84 #if TBB_USE_EXCEPTIONS
85  throw std::runtime_error(buf);
86 #else
87  PRINT_ERROR_AND_ABORT( "runtime_error", buf);
88 #endif /* !TBB_USE_EXCEPTIONS */
89 }
90 
91 #if _WIN32||_WIN64
92 void handle_win_error( int error_code ) {
93  char buf[512];
94 #if !__TBB_WIN8UI_SUPPORT
95  FormatMessageA( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
96  NULL, error_code, 0, buf, sizeof(buf), NULL );
97 #else
98 //TODO: update with right replacement for FormatMessageA
99  sprintf_s((char*)&buf, 512, "error code %d", error_code);
100 #endif
101 #if TBB_USE_EXCEPTIONS
102  throw std::runtime_error(buf);
103 #else
104  PRINT_ERROR_AND_ABORT( "runtime_error", buf);
105 #endif /* !TBB_USE_EXCEPTIONS */
106 }
107 #endif // _WIN32||_WIN64
108 
111 }
112 
114  __TBB_ASSERT ( eid > 0 && eid < eid_max, "Unknown exception ID" );
115  switch ( eid ) {
116  case eid_bad_alloc: DO_THROW(std::bad_alloc, () );
118  case eid_nonpositive_step: DO_THROW(std::invalid_argument, ("Step must be positive") );
119  case eid_out_of_range: DO_THROW(std::out_of_range, ("Index out of requested size range") );
120  case eid_segment_range_error: DO_THROW(std::range_error, ("Index out of allocated segment slots") );
121  case eid_index_range_error: DO_THROW(std::range_error, ("Index is not allocated") );
125  case eid_possible_deadlock: DO_THROW(std::runtime_error, ("Resource deadlock would occur") );
126  case eid_operation_not_permitted: DO_THROW(std::runtime_error, ("Operation not permitted") );
127  case eid_condvar_wait_failed: DO_THROW(std::runtime_error, ("Wait on condition variable failed") );
128  case eid_invalid_load_factor: DO_THROW(std::out_of_range, ("Invalid hash load factor") );
129  case eid_reserved: DO_THROW(std::out_of_range, ("[backward compatibility] Invalid number of buckets") );
130  case eid_invalid_swap: DO_THROW(std::invalid_argument, ("swap() is invalid on non-equal allocators") );
131  case eid_reservation_length_error: DO_THROW(std::length_error, ("reservation size exceeds permitted max size") );
132  case eid_invalid_key: DO_THROW(std::out_of_range, ("invalid key") );
133  case eid_user_abort: DO_THROW( user_abort, () );
134  case eid_bad_tagged_msg_cast: DO_THROW(std::runtime_error, ("Illegal tagged_msg cast") );
135 #if __TBB_SUPPORTS_WORKERS_WAITING_IN_TERMINATE
136  case eid_blocking_thread_join_impossible: DO_THROW(std::runtime_error, ("Blocking terminate failed") );
137 #endif
138  default: break;
139  }
140 #if !TBB_USE_EXCEPTIONS && __APPLE__
141  out_of_range e1("");
142  length_error e2("");
143  range_error e3("");
144  invalid_argument e4("");
145 #endif /* !TBB_USE_EXCEPTIONS && __APPLE__ */
146 }
147 
148 #if __TBB_STD_RETHROW_EXCEPTION_POSSIBLY_BROKEN
149 // Runtime detection and workaround for the GCC bug 62258.
150 // The problem is that std::rethrow_exception() does not increment a counter
151 // of active exceptions, causing std::uncaught_exception() to return a wrong value.
152 // The code is created after, and roughly reflects, the workaround
153 // at https://gcc.gnu.org/bugzilla/attachment.cgi?id=34683
154 
155 void fix_broken_rethrow() {
156  struct gcc_eh_data {
157  void * caughtExceptions;
158  unsigned int uncaughtExceptions;
159  };
160  gcc_eh_data* eh_data = punned_cast<gcc_eh_data*>( abi::__cxa_get_globals() );
161  ++eh_data->uncaughtExceptions;
162 }
163 
165  bool is_broken;
166  __TBB_ASSERT( !std::uncaught_exception(),
167  "gcc_rethrow_exception_broken() must not be called when an exception is active" );
168  try {
169  // Throw, catch, and rethrow an exception
170  try {
171  throw __TBB_GLIBCXX_VERSION;
172  } catch(...) {
173  std::rethrow_exception( std::current_exception() );
174  }
175  } catch(...) {
176  // Check the bug presence
177  is_broken = std::uncaught_exception();
178  }
179  if( is_broken ) fix_broken_rethrow();
180  __TBB_ASSERT( !std::uncaught_exception(), NULL );
181  return is_broken;
182 }
183 #else
185 bool gcc_rethrow_exception_broken() { return false; }
186 #endif /* __TBB_STD_RETHROW_EXCEPTION_POSSIBLY_BROKEN */
187 
189 static const char VersionString[] = "\0" TBB_VERSION_STRINGS;
190 
191 static bool PrintVersionFlag = false;
192 
193 void PrintVersion() {
194  PrintVersionFlag = true;
195  fputs(VersionString+1,stderr);
196 }
197 
198 void PrintExtraVersionInfo( const char* category, const char* format, ... ) {
199  if( PrintVersionFlag ) {
200  char str[1024]; memset(str, 0, 1024);
201  va_list args; va_start(args, format);
202  // Note: correct vsnprintf definition obtained from tbb_assert_impl.h
203  vsnprintf( str, 1024-1, format, args);
204  va_end(args);
205  fprintf(stderr, "TBB: %s\t%s\n", category, str );
206  }
207 }
208 
209 void PrintRMLVersionInfo( void* arg, const char* server_info ) {
210  PrintExtraVersionInfo( server_info, (const char *)arg );
211 }
212 
214 #if _MSC_VER
215 #include <intrin.h> // for __cpuid
216 #endif
218 #if __TBB_TSX_AVAILABLE
219 #if (__INTEL_COMPILER || __GNUC__ || _MSC_VER || __SUNPRO_CC)
220  bool result = false;
221  const int rtm_ebx_mask = 1<<11;
222 #if _MSC_VER
223  int info[4] = {0,0,0,0};
224  const int reg_ebx = 1;
225  __cpuidex(info, 7, 0);
226  result = (info[reg_ebx] & rtm_ebx_mask)!=0;
227 #elif __GNUC__ || __SUNPRO_CC
228  int32_t reg_ebx = 0;
229  int32_t reg_eax = 7;
230  int32_t reg_ecx = 0;
231  __asm__ __volatile__ ( "movl %%ebx, %%esi\n"
232  "cpuid\n"
233  "movl %%ebx, %0\n"
234  "movl %%esi, %%ebx\n"
235  : "=a"(reg_ebx) : "0" (reg_eax), "c" (reg_ecx) : "esi",
236 #if __TBB_x86_64
237  "ebx",
238 #endif
239  "edx"
240  );
241  result = (reg_ebx & rtm_ebx_mask)!=0 ;
242 #endif
243  return result;
244 #else
245  #error Speculation detection not enabled for compiler
246 #endif /* __INTEL_COMPILER || __GNUC__ || _MSC_VER */
247 #else /* __TBB_TSX_AVAILABLE */
248  return false;
249 #endif /* __TBB_TSX_AVAILABLE */
250 }
251 
252 } // namespace internal
253 
255  return TBB_INTERFACE_VERSION;
256 }
257 
258 } // namespace tbb
259 
260 #if !__TBB_RML_STATIC
261 #if __TBB_x86_32
262 
263 #include "tbb/atomic.h"
264 
265 // in MSVC environment, int64_t defined in tbb::internal namespace only (see tbb_stddef.h)
266 #if _MSC_VER
267 using tbb::internal::int64_t;
268 #endif
269 
271 extern "C" void __TBB_machine_store8_slow_perf_warning( volatile void *ptr ) {
272  // Report run-time warning unless we have already recently reported warning for that address.
273  const unsigned n = 4;
274  static tbb::atomic<void*> cache[n];
275  static tbb::atomic<unsigned> k;
276  for( unsigned i=0; i<n; ++i )
277  if( ptr==cache[i] )
278  goto done;
279  cache[(k++)%n] = const_cast<void*>(ptr);
280  tbb::internal::runtime_warning( "atomic store on misaligned 8-byte location %p is slow", ptr );
281 done:;
282 }
283 
285 extern "C" void __TBB_machine_store8_slow( volatile void *ptr, int64_t value ) {
286  for( tbb::internal::atomic_backoff b;;b.pause() ) {
287  int64_t tmp = *(int64_t*)ptr;
288  if( __TBB_machine_cmpswp8(ptr,value,tmp)==tmp )
289  break;
290  }
291 }
292 
293 #endif /* __TBB_x86_32 */
294 #endif /* !__TBB_RML_STATIC */
295 
296 #if __TBB_ipf
297 /* It was found that on IA-64 architecture inlining of __TBB_machine_lockbyte leads
298  to serious performance regression with ICC. So keep it out-of-line.
299  */
300 extern "C" intptr_t __TBB_machine_lockbyte( volatile unsigned char& flag ) {
302  while( !__TBB_TryLockByte(flag) ) backoff.pause();
303  return 0;
304 }
305 #endif
int64_t __TBB_machine_lockbyte(volatile unsigned char &ptr)
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t int ITT_FORMAT d no args no args unsigned int ITT_FORMAT u const __itt_domain __itt_id ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain __itt_id ITT_FORMAT p const __itt_domain __itt_id __itt_timestamp __itt_timestamp ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain ITT_FORMAT p const __itt_domain __itt_string_handle unsigned long long value
void pause()
Pause for a while.
Definition: tbb_machine.h:360
#define __TBB_ASSERT_EX(predicate, comment)
"Extended" version is useful to suppress warnings if a variable is only used with an assert
Definition: tbb_stddef.h:167
void __TBB_EXPORTED_FUNC runtime_warning(const char *format,...)
Report a runtime warning.
static bool PrintVersionFlag
Definition: tbb_misc.cpp:191
Class that implements exponential backoff.
Definition: tbb_machine.h:345
Exception for missing wait on structured_task_group.
Definition: tbb_exception.h:49
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:165
Exception for concurrent containers.
Definition: tbb_exception.h:28
void PrintVersion()
Prints TBB version information on stderr.
Definition: tbb_misc.cpp:193
The graph class.
Specialization for atomic<void*>, for sake of not allowing arithmetic or operator->.
Definition: atomic.h:499
#define __TBB_machine_cmpswp8
Definition: ibm_aix51.h:42
Exception for user-initiated abort.
Definition: tbb_exception.h:43
void __TBB_EXPORTED_FUNC throw_bad_last_alloc_exception_v4()
Obsolete.
Definition: tbb_misc.cpp:109
const char * what() const __TBB_override
Definition: tbb_misc.cpp:48
#define TBB_VERSION_STRINGS
Definition: tbb_version.h:105
int __TBB_EXPORTED_FUNC TBB_runtime_interface_version()
The function returns the interface version of the TBB shared library being used.
Definition: tbb_misc.cpp:254
bool __TBB_TryLockByte(__TBB_atomic_flag &flag)
Definition: tbb_machine.h:913
Exception for repeated scheduling of the same task_handle.
Definition: tbb_exception.h:55
void PrintRMLVersionInfo(void *arg, const char *server_info)
A callback routine to print RML version information on stderr.
Definition: tbb_misc.cpp:209
The last enumerator tracks the number of defined IDs. It must remain the last one.
Definition: tbb_exception.h:93
#define DO_THROW(exc, init_args)
Definition: tbb_misc.cpp:63
void __TBB_machine_store8_slow_perf_warning(volatile void *ptr)
void fix_broken_rethrow()
Definition: tbb_misc.cpp:184
void __TBB_EXPORTED_FUNC throw_exception_v4(exception_id)
Gathers all throw operators in one place.
Definition: tbb_misc.cpp:113
const char * what() const __TBB_override
Definition: tbb_misc.cpp:51
void handle_win_error(int error_code)
Throws std::runtime_error with what() returning error_code description prefixed with aux_info.
bool cpu_has_speculation()
check for transaction support.
Definition: tbb_misc.cpp:217
void PrintExtraVersionInfo(const char *category, const char *format,...)
Prints arbitrary extra TBB version information on stderr.
Definition: tbb_misc.cpp:198
static const char VersionString[]
Definition: tbb_misc.cpp:189
#define PRINT_ERROR_AND_ABORT(exc_name, msg)
Definition: tbb_misc.cpp:58
const char * what() const __TBB_override
Definition: tbb_misc.cpp:49
const char * what() const __TBB_override
Definition: tbb_misc.cpp:47
void __TBB_EXPORTED_FUNC handle_perror(int error_code, const char *aux_info)
Throws std::runtime_error with what() returning error_code description prefixed with aux_info.
Definition: tbb_misc.cpp:74
bool gcc_rethrow_exception_broken()
Definition: tbb_misc.cpp:185
const char * what() const __TBB_override
Definition: tbb_misc.cpp:50
void __TBB_machine_store8_slow(volatile void *ptr, int64_t value)
Handles misaligned 8-byte store.
#define TBB_INTERFACE_VERSION
Definition: tbb_stddef.h:25
Exception for PPL locks.
Definition: tbb_exception.h:37

Copyright © 2005-2019 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.