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.

386 lines
14 KiB

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