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