tbb_exception.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. Copyright (c) 2005-2020 Intel Corporation
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #ifndef __TBB_exception_H
  14. #define __TBB_exception_H
  15. #define __TBB_tbb_exception_H_include_area
  16. #include "internal/_warning_suppress_enable_notice.h"
  17. #include "tbb_stddef.h"
  18. #include <exception>
  19. #include <new> // required for bad_alloc definition, operators new
  20. #include <string> // required to construct std exception classes
  21. namespace tbb {
  22. //! Exception for concurrent containers
  23. class bad_last_alloc : public std::bad_alloc {
  24. public:
  25. const char* what() const throw() __TBB_override;
  26. #if __TBB_DEFAULT_DTOR_THROW_SPEC_BROKEN
  27. ~bad_last_alloc() throw() __TBB_override {}
  28. #endif
  29. };
  30. //! Exception for PPL locks
  31. class __TBB_DEPRECATED improper_lock : public std::exception {
  32. public:
  33. const char* what() const throw() __TBB_override;
  34. };
  35. //! Exception for user-initiated abort
  36. class user_abort : public std::exception {
  37. public:
  38. const char* what() const throw() __TBB_override;
  39. };
  40. //! Exception for missing wait on structured_task_group
  41. class missing_wait : public std::exception {
  42. public:
  43. const char* what() const throw() __TBB_override;
  44. };
  45. //! Exception for repeated scheduling of the same task_handle
  46. class invalid_multiple_scheduling : public std::exception {
  47. public:
  48. const char* what() const throw() __TBB_override;
  49. };
  50. namespace internal {
  51. //! Obsolete
  52. void __TBB_EXPORTED_FUNC throw_bad_last_alloc_exception_v4();
  53. enum exception_id {
  54. eid_bad_alloc = 1,
  55. eid_bad_last_alloc,
  56. eid_nonpositive_step,
  57. eid_out_of_range,
  58. eid_segment_range_error,
  59. eid_index_range_error,
  60. eid_missing_wait,
  61. eid_invalid_multiple_scheduling,
  62. eid_improper_lock,
  63. eid_possible_deadlock,
  64. eid_operation_not_permitted,
  65. eid_condvar_wait_failed,
  66. eid_invalid_load_factor,
  67. eid_reserved, // free slot for backward compatibility, can be reused.
  68. eid_invalid_swap,
  69. eid_reservation_length_error,
  70. eid_invalid_key,
  71. eid_user_abort,
  72. eid_reserved1,
  73. #if __TBB_SUPPORTS_WORKERS_WAITING_IN_TERMINATE
  74. // This id is used only from inside the library and only for support of CPF functionality.
  75. // So, if we drop the functionality, eid_reserved1 can be safely renamed and reused.
  76. eid_blocking_thread_join_impossible = eid_reserved1,
  77. #endif
  78. eid_bad_tagged_msg_cast,
  79. //! The last enumerator tracks the number of defined IDs. It must remain the last one.
  80. /** When adding new IDs, place them immediately _before_ this comment (that is
  81. _after_ all the existing IDs. NEVER insert new IDs between the existing ones. **/
  82. eid_max
  83. };
  84. //! Gathers all throw operators in one place.
  85. /** Its purpose is to minimize code bloat that can be caused by throw operators
  86. scattered in multiple places, especially in templates. **/
  87. void __TBB_EXPORTED_FUNC throw_exception_v4 ( exception_id );
  88. //! Versionless convenience wrapper for throw_exception_v4()
  89. inline void throw_exception ( exception_id eid ) { throw_exception_v4(eid); }
  90. } // namespace internal
  91. } // namespace tbb
  92. #if __TBB_TASK_GROUP_CONTEXT
  93. #include "tbb_allocator.h"
  94. #include <typeinfo> //for typeid
  95. namespace tbb {
  96. //! Interface to be implemented by all exceptions TBB recognizes and propagates across the threads.
  97. /** If an unhandled exception of the type derived from tbb::tbb_exception is intercepted
  98. by the TBB scheduler in one of the worker threads, it is delivered to and re-thrown in
  99. the root thread. The root thread is the thread that has started the outermost algorithm
  100. or root task sharing the same task_group_context with the guilty algorithm/task (the one
  101. that threw the exception first).
  102. Note: when documentation mentions workers with respect to exception handling,
  103. masters are implied as well, because they are completely equivalent in this context.
  104. Consequently a root thread can be master or worker thread.
  105. NOTE: In case of nested algorithms or complex task hierarchies when the nested
  106. levels share (explicitly or by means of implicit inheritance) the task group
  107. context of the outermost level, the exception may be (re-)thrown multiple times
  108. (ultimately - in each worker on each nesting level) before reaching the root
  109. thread at the outermost level. IMPORTANT: if you intercept an exception derived
  110. from this class on a nested level, you must re-throw it in the catch block by means
  111. of the "throw;" operator.
  112. TBB provides two implementations of this interface: tbb::captured_exception and
  113. template class tbb::movable_exception. See their declarations for more info. **/
  114. class __TBB_DEPRECATED tbb_exception : public std::exception
  115. {
  116. /** No operator new is provided because the TBB usage model assumes dynamic
  117. creation of the TBB exception objects only by means of applying move()
  118. operation on an exception thrown out of TBB scheduler. **/
  119. void* operator new ( size_t );
  120. public:
  121. #if __clang__
  122. // At -O3 or even -O2 optimization level, Clang may fully throw away an empty destructor
  123. // of tbb_exception from destructors of derived classes. As a result, it does not create
  124. // vtable for tbb_exception, which is a required part of TBB binary interface.
  125. // Making the destructor non-empty (with just a semicolon) prevents that optimization.
  126. ~tbb_exception() throw() { /* keep the semicolon! */ ; }
  127. #endif
  128. //! Creates and returns pointer to the deep copy of this exception object.
  129. /** Move semantics is allowed. **/
  130. virtual tbb_exception* move() throw() = 0;
  131. //! Destroys objects created by the move() method.
  132. /** Frees memory and calls destructor for this exception object.
  133. Can and must be used only on objects created by the move method. **/
  134. virtual void destroy() throw() = 0;
  135. //! Throws this exception object.
  136. /** Make sure that if you have several levels of derivation from this interface
  137. you implement or override this method on the most derived level. The implementation
  138. is as simple as "throw *this;". Failure to do this will result in exception
  139. of a base class type being thrown. **/
  140. virtual void throw_self() = 0;
  141. //! Returns RTTI name of the originally intercepted exception
  142. virtual const char* name() const throw() = 0;
  143. //! Returns the result of originally intercepted exception's what() method.
  144. virtual const char* what() const throw() __TBB_override = 0;
  145. /** Operator delete is provided only to allow using existing smart pointers
  146. with TBB exception objects obtained as the result of applying move()
  147. operation on an exception thrown out of TBB scheduler.
  148. When overriding method move() make sure to override operator delete as well
  149. if memory is allocated not by TBB's scalable allocator. **/
  150. void operator delete ( void* p ) {
  151. internal::deallocate_via_handler_v3(p);
  152. }
  153. };
  154. //! This class is used by TBB to propagate information about unhandled exceptions into the root thread.
  155. /** Exception of this type is thrown by TBB in the root thread (thread that started a parallel
  156. algorithm ) if an unhandled exception was intercepted during the algorithm execution in one
  157. of the workers.
  158. \sa tbb::tbb_exception **/
  159. class __TBB_DEPRECATED_IN_VERBOSE_MODE captured_exception : public tbb_exception
  160. {
  161. public:
  162. captured_exception( const captured_exception& src )
  163. : tbb_exception(src), my_dynamic(false)
  164. {
  165. set(src.my_exception_name, src.my_exception_info);
  166. }
  167. captured_exception( const char* name_, const char* info )
  168. : my_dynamic(false)
  169. {
  170. set(name_, info);
  171. }
  172. __TBB_EXPORTED_METHOD ~captured_exception() throw();
  173. captured_exception& operator= ( const captured_exception& src ) {
  174. if ( this != &src ) {
  175. clear();
  176. set(src.my_exception_name, src.my_exception_info);
  177. }
  178. return *this;
  179. }
  180. captured_exception* __TBB_EXPORTED_METHOD move() throw() __TBB_override;
  181. void __TBB_EXPORTED_METHOD destroy() throw() __TBB_override;
  182. void throw_self() __TBB_override { __TBB_THROW(*this); }
  183. const char* __TBB_EXPORTED_METHOD name() const throw() __TBB_override;
  184. const char* __TBB_EXPORTED_METHOD what() const throw() __TBB_override;
  185. void __TBB_EXPORTED_METHOD set( const char* name, const char* info ) throw();
  186. void __TBB_EXPORTED_METHOD clear() throw();
  187. private:
  188. //! Used only by method move().
  189. captured_exception() : my_dynamic(), my_exception_name(), my_exception_info() {}
  190. //! Functionally equivalent to {captured_exception e(name,info); return e.move();}
  191. static captured_exception* allocate( const char* name, const char* info );
  192. bool my_dynamic;
  193. const char* my_exception_name;
  194. const char* my_exception_info;
  195. };
  196. //! Template that can be used to implement exception that transfers arbitrary ExceptionData to the root thread
  197. /** Code using TBB can instantiate this template with an arbitrary ExceptionData type
  198. and throw this exception object. Such exceptions are intercepted by the TBB scheduler
  199. and delivered to the root thread ().
  200. \sa tbb::tbb_exception **/
  201. template<typename ExceptionData>
  202. class __TBB_DEPRECATED movable_exception : public tbb_exception
  203. {
  204. typedef movable_exception<ExceptionData> self_type;
  205. public:
  206. movable_exception( const ExceptionData& data_ )
  207. : my_exception_data(data_)
  208. , my_dynamic(false)
  209. , my_exception_name(
  210. #if TBB_USE_EXCEPTIONS
  211. typeid(self_type).name()
  212. #else /* !TBB_USE_EXCEPTIONS */
  213. "movable_exception"
  214. #endif /* !TBB_USE_EXCEPTIONS */
  215. )
  216. {}
  217. movable_exception( const movable_exception& src ) throw ()
  218. : tbb_exception(src)
  219. , my_exception_data(src.my_exception_data)
  220. , my_dynamic(false)
  221. , my_exception_name(src.my_exception_name)
  222. {}
  223. ~movable_exception() throw() {}
  224. const movable_exception& operator= ( const movable_exception& src ) {
  225. if ( this != &src ) {
  226. my_exception_data = src.my_exception_data;
  227. my_exception_name = src.my_exception_name;
  228. }
  229. return *this;
  230. }
  231. ExceptionData& data() throw() { return my_exception_data; }
  232. const ExceptionData& data() const throw() { return my_exception_data; }
  233. const char* name() const throw() __TBB_override { return my_exception_name; }
  234. const char* what() const throw() __TBB_override { return "tbb::movable_exception"; }
  235. movable_exception* move() throw() __TBB_override {
  236. void* e = internal::allocate_via_handler_v3(sizeof(movable_exception));
  237. if ( e ) {
  238. ::new (e) movable_exception(*this);
  239. ((movable_exception*)e)->my_dynamic = true;
  240. }
  241. return (movable_exception*)e;
  242. }
  243. void destroy() throw() __TBB_override {
  244. __TBB_ASSERT ( my_dynamic, "Method destroy can be called only on dynamically allocated movable_exceptions" );
  245. if ( my_dynamic ) {
  246. this->~movable_exception();
  247. internal::deallocate_via_handler_v3(this);
  248. }
  249. }
  250. void throw_self() __TBB_override { __TBB_THROW( *this ); }
  251. protected:
  252. //! User data
  253. ExceptionData my_exception_data;
  254. private:
  255. //! Flag specifying whether this object has been dynamically allocated (by the move method)
  256. bool my_dynamic;
  257. //! RTTI name of this class
  258. /** We rely on the fact that RTTI names are static string constants. **/
  259. const char* my_exception_name;
  260. };
  261. #if !TBB_USE_CAPTURED_EXCEPTION
  262. namespace internal {
  263. //! Exception container that preserves the exact copy of the original exception
  264. /** This class can be used only when the appropriate runtime support (mandated
  265. by C++11) is present **/
  266. class tbb_exception_ptr {
  267. std::exception_ptr my_ptr;
  268. public:
  269. static tbb_exception_ptr* allocate();
  270. static tbb_exception_ptr* allocate( const tbb_exception& tag );
  271. //! This overload uses move semantics (i.e. it empties src)
  272. static tbb_exception_ptr* allocate( captured_exception& src );
  273. //! Destroys this objects
  274. /** Note that objects of this type can be created only by the allocate() method. **/
  275. void destroy() throw();
  276. //! Throws the contained exception .
  277. void throw_self() { std::rethrow_exception(my_ptr); }
  278. private:
  279. tbb_exception_ptr( const std::exception_ptr& src ) : my_ptr(src) {}
  280. tbb_exception_ptr( const captured_exception& src ) :
  281. #if __TBB_MAKE_EXCEPTION_PTR_PRESENT
  282. my_ptr(std::make_exception_ptr(src)) // the final function name in C++11
  283. #else
  284. my_ptr(std::copy_exception(src)) // early C++0x drafts name
  285. #endif
  286. {}
  287. }; // class tbb::internal::tbb_exception_ptr
  288. } // namespace internal
  289. #endif /* !TBB_USE_CAPTURED_EXCEPTION */
  290. } // namespace tbb
  291. #endif /* __TBB_TASK_GROUP_CONTEXT */
  292. #include "internal/_warning_suppress_disable_notice.h"
  293. #undef __TBB_tbb_exception_H_include_area
  294. #endif /* __TBB_exception_H */