tbb_exception.h

00001 /*
00002     Copyright 2005-2010 Intel Corporation.  All Rights Reserved.
00003 
00004     The source code contained or described herein and all documents related
00005     to the source code ("Material") are owned by Intel Corporation or its
00006     suppliers or licensors.  Title to the Material remains with Intel
00007     Corporation or its suppliers and licensors.  The Material is protected
00008     by worldwide copyright laws and treaty provisions.  No part of the
00009     Material may be used, copied, reproduced, modified, published, uploaded,
00010     posted, transmitted, distributed, or disclosed in any way without
00011     Intel's prior express written permission.
00012 
00013     No license under any patent, copyright, trade secret or other
00014     intellectual property right is granted to or conferred upon you by
00015     disclosure or delivery of the Materials, either expressly, by
00016     implication, inducement, estoppel or otherwise.  Any license under such
00017     intellectual property rights must be express and approved by Intel in
00018     writing.
00019 */
00020 
00021 #ifndef __TBB_exception_H
00022 #define __TBB_exception_H
00023 
00024 #include "tbb_stddef.h"
00025 
00026 #if !TBB_USE_EXCEPTIONS && _MSC_VER
00027     // Suppress "C++ exception handler used, but unwind semantics are not enabled" warning in STL headers
00028     #pragma warning (push)
00029     #pragma warning (disable: 4530)
00030 #endif
00031 
00032 #include <stdexcept>
00033 #include <string> // required to construct std exception classes
00034 
00035 #if !TBB_USE_EXCEPTIONS && _MSC_VER
00036     #pragma warning (pop)
00037 #endif
00038 
00039 namespace tbb {
00040 
00042 class bad_last_alloc : public std::bad_alloc {
00043 public:
00044     /*override*/ const char* what() const throw();
00045 #if __TBB_DEFAULT_DTOR_THROW_SPEC_BROKEN
00046     /*override*/ ~bad_last_alloc() throw() {}
00047 #endif
00048 };
00049 
00051 class improper_lock : public std::exception {
00052 public:
00053     /*override*/ const char* what() const throw();
00054 };
00055 
00057 class missing_wait : public std::exception {
00058 public:
00059     /*override*/ const char* what() const throw();
00060 };
00061 
00063 class invalid_multiple_scheduling : public std::exception {
00064 public:
00065     /*override*/ const char* what() const throw();
00066 };
00067 
00068 namespace internal {
00070 void __TBB_EXPORTED_FUNC throw_bad_last_alloc_exception_v4();
00071 
00072 enum exception_id {
00073     eid_bad_alloc = 1,
00074     eid_bad_last_alloc,
00075     eid_nonpositive_step,
00076     eid_out_of_range,
00077     eid_segment_range_error,
00078     eid_index_range_error,
00079     eid_missing_wait,
00080     eid_invalid_multiple_scheduling,
00081     eid_improper_lock,
00082     eid_possible_deadlock,
00083     eid_operation_not_permitted,
00084     eid_condvar_wait_failed,
00085     eid_invalid_load_factor,
00086     eid_reserved, // free slot for backward compatibility, can be reused.
00087     eid_invalid_swap,
00088     eid_reservation_length_error,
00089     eid_invalid_key,
00091 
00093     eid_max
00094 };
00095 
00097 
00099 void __TBB_EXPORTED_FUNC throw_exception_v4 ( exception_id );
00100 
00102 inline void throw_exception ( exception_id eid ) { throw_exception_v4(eid); }
00103 
00104 } // namespace internal
00105 } // namespace tbb
00106 
00107 #if __TBB_TASK_GROUP_CONTEXT
00108 #include "tbb_allocator.h"
00109 #include <exception>
00110 #include <typeinfo>
00111 #include <new>
00112 
00113 namespace tbb {
00114 
00116 
00136 class tbb_exception : public std::exception
00137 {
00141     void* operator new ( size_t );
00142 
00143 public:
00145 
00146     virtual tbb_exception* move () throw() = 0;
00147     
00149 
00151     virtual void destroy () throw() = 0;
00152 
00154 
00158     virtual void throw_self () = 0;
00159 
00161     virtual const char* name() const throw() = 0;
00162 
00164     virtual const char* what() const throw() = 0;
00165 
00172     void operator delete ( void* p ) {
00173         internal::deallocate_via_handler_v3(p);
00174     }
00175 };
00176 
00178 
00182 class captured_exception : public tbb_exception
00183 {
00184 public:
00185     captured_exception ( const captured_exception& src )
00186         : tbb_exception(src), my_dynamic(false)
00187     {
00188         set(src.my_exception_name, src.my_exception_info);
00189     }
00190 
00191     captured_exception ( const char* name_, const char* info )
00192         : my_dynamic(false)
00193     {
00194         set(name_, info);
00195     }
00196 
00197     __TBB_EXPORTED_METHOD ~captured_exception () throw() {
00198         clear();
00199     }
00200 
00201     captured_exception& operator= ( const captured_exception& src ) {
00202         if ( this != &src ) {
00203             clear();
00204             set(src.my_exception_name, src.my_exception_info);
00205         }
00206         return *this;
00207     }
00208 
00209     /*override*/ 
00210     captured_exception* __TBB_EXPORTED_METHOD move () throw();
00211 
00212     /*override*/ 
00213     void __TBB_EXPORTED_METHOD destroy () throw();
00214 
00215     /*override*/ 
00216     void throw_self () { __TBB_THROW(*this); }
00217 
00218     /*override*/ 
00219     const char* __TBB_EXPORTED_METHOD name() const throw();
00220 
00221     /*override*/ 
00222     const char* __TBB_EXPORTED_METHOD what() const throw();
00223 
00224     void __TBB_EXPORTED_METHOD set ( const char* name, const char* info ) throw();
00225     void __TBB_EXPORTED_METHOD clear () throw();
00226 
00227 private:
00229     captured_exception() {}
00230 
00232     static captured_exception* allocate ( const char* name, const char* info );
00233 
00234     bool my_dynamic;
00235     const char* my_exception_name;
00236     const char* my_exception_info;
00237 };
00238 
00240 
00244 template<typename ExceptionData>
00245 class movable_exception : public tbb_exception
00246 {
00247     typedef movable_exception<ExceptionData> self_type;
00248 
00249 public:
00250     movable_exception ( const ExceptionData& data_ ) 
00251         : my_exception_data(data_)
00252         , my_dynamic(false)
00253         , my_exception_name(
00254 #if TBB_USE_EXCEPTIONS
00255         typeid(self_type).name()
00256 #else /* !TBB_USE_EXCEPTIONS */
00257         "movable_exception"
00258 #endif /* !TBB_USE_EXCEPTIONS */
00259         )
00260     {}
00261 
00262     movable_exception ( const movable_exception& src ) throw () 
00263         : tbb_exception(src)
00264         , my_exception_data(src.my_exception_data)
00265         , my_dynamic(false)
00266         , my_exception_name(src.my_exception_name)
00267     {}
00268 
00269     ~movable_exception () throw() {}
00270 
00271     const movable_exception& operator= ( const movable_exception& src ) {
00272         if ( this != &src ) {
00273             my_exception_data = src.my_exception_data;
00274             my_exception_name = src.my_exception_name;
00275         }
00276         return *this;
00277     }
00278 
00279     ExceptionData& data () throw() { return my_exception_data; }
00280 
00281     const ExceptionData& data () const throw() { return my_exception_data; }
00282 
00283     /*override*/ const char* name () const throw() { return my_exception_name; }
00284 
00285     /*override*/ const char* what () const throw() { return "tbb::movable_exception"; }
00286 
00287     /*override*/ 
00288     movable_exception* move () throw() {
00289         void* e = internal::allocate_via_handler_v3(sizeof(movable_exception));
00290         if ( e ) {
00291             ::new (e) movable_exception(*this);
00292             ((movable_exception*)e)->my_dynamic = true;
00293         }
00294         return (movable_exception*)e;
00295     }
00296     /*override*/ 
00297     void destroy () throw() {
00298         __TBB_ASSERT ( my_dynamic, "Method destroy can be called only on dynamically allocated movable_exceptions" );
00299         if ( my_dynamic ) {
00300             this->~movable_exception();
00301             internal::deallocate_via_handler_v3(this);
00302         }
00303     }
00304     /*override*/ 
00305     void throw_self () { __TBB_THROW( *this ); }
00306 
00307 protected:
00309     ExceptionData  my_exception_data;
00310 
00311 private:
00313     bool my_dynamic;
00314 
00316 
00317     const char* my_exception_name;
00318 };
00319 
00320 #if !TBB_USE_CAPTURED_EXCEPTION
00321 namespace internal {
00322 
00324 
00326 class tbb_exception_ptr {
00327     std::exception_ptr  my_ptr;
00328 
00329 public:
00330     static tbb_exception_ptr* allocate ();
00331     static tbb_exception_ptr* allocate ( const tbb_exception& tag );
00333     static tbb_exception_ptr* allocate ( captured_exception& src );
00334     
00336 
00337     void destroy () throw();
00338 
00340     void throw_self () { std::rethrow_exception(my_ptr); }
00341 
00342 private:
00343     tbb_exception_ptr ( const std::exception_ptr& src ) : my_ptr(src) {}
00344     tbb_exception_ptr ( const captured_exception& src ) : my_ptr(std::copy_exception(src)) {}
00345 }; // class tbb::internal::tbb_exception_ptr
00346 
00347 } // namespace internal
00348 #endif /* !TBB_USE_CAPTURED_EXCEPTION */
00349 
00350 } // namespace tbb
00351 
00352 #endif /* __TBB_TASK_GROUP_CONTEXT */
00353 
00354 #endif /* __TBB_exception_H */

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