Marshall Clow | 61b898e | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 1 | //===--------------------- cxa_exception_storage.cpp ----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
| 7 | // |
| 8 | // |
| 9 | // This file implements the storage for the "Caught Exception Stack" |
Jonathan Roelofs | c82e02d | 2014-02-12 04:49:09 +0000 | [diff] [blame] | 10 | // http://mentorembedded.github.io/cxx-abi/abi-eh.html (section 2.2.2) |
Marshall Clow | 61b898e | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "cxa_exception.hpp" |
| 15 | |
Jonathan Roelofs | c285efa | 2014-05-06 21:30:56 +0000 | [diff] [blame] | 16 | #include "config.h" |
| 17 | |
Asiri Rathnayake | b62a4dd | 2016-09-21 09:09:32 +0000 | [diff] [blame^] | 18 | #if defined(_LIBCXXABI_HAS_NO_THREADS) |
Jonathan Roelofs | c285efa | 2014-05-06 21:30:56 +0000 | [diff] [blame] | 19 | |
| 20 | namespace __cxxabiv1 { |
| 21 | extern "C" { |
| 22 | static __cxa_eh_globals eh_globals; |
| 23 | __cxa_eh_globals *__cxa_get_globals() { return &eh_globals; } |
| 24 | __cxa_eh_globals *__cxa_get_globals_fast() { return &eh_globals; } |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | #elif defined(HAS_THREAD_LOCAL) |
Marshall Clow | 61b898e | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 29 | |
| 30 | namespace __cxxabiv1 { |
| 31 | |
| 32 | namespace { |
Howard Hinnant | 7da9b96 | 2012-01-30 16:07:00 +0000 | [diff] [blame] | 33 | __cxa_eh_globals * __globals () { |
Marshall Clow | 61b898e | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 34 | static thread_local __cxa_eh_globals eh_globals; |
| 35 | return &eh_globals; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | extern "C" { |
Howard Hinnant | 7da9b96 | 2012-01-30 16:07:00 +0000 | [diff] [blame] | 40 | __cxa_eh_globals * __cxa_get_globals () { return __globals (); } |
| 41 | __cxa_eh_globals * __cxa_get_globals_fast () { return __globals (); } |
Marshall Clow | 61b898e | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 42 | } |
| 43 | } |
| 44 | |
| 45 | #else |
| 46 | |
| 47 | #include <pthread.h> |
| 48 | #include <cstdlib> // for calloc, free |
Nick Kledzik | f4429fe | 2011-08-02 01:34:26 +0000 | [diff] [blame] | 49 | #include "abort_message.h" |
Marshall Clow | 61b898e | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 50 | |
Nick Kledzik | f4429fe | 2011-08-02 01:34:26 +0000 | [diff] [blame] | 51 | // In general, we treat all pthread errors as fatal. |
| 52 | // We cannot call std::terminate() because that will in turn |
| 53 | // call __cxa_get_globals() and cause infinite recursion. |
Marshall Clow | 61b898e | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 54 | |
| 55 | namespace __cxxabiv1 { |
| 56 | namespace { |
Marshall Clow | 61b898e | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 57 | pthread_key_t key_; |
Howard Hinnant | 7da9b96 | 2012-01-30 16:07:00 +0000 | [diff] [blame] | 58 | pthread_once_t flag_ = PTHREAD_ONCE_INIT; |
Marshall Clow | 61b898e | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 59 | |
Howard Hinnant | 7da9b96 | 2012-01-30 16:07:00 +0000 | [diff] [blame] | 60 | void destruct_ (void *p) { |
Marshall Clow | 61b898e | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 61 | std::free ( p ); |
Nick Kledzik | f4429fe | 2011-08-02 01:34:26 +0000 | [diff] [blame] | 62 | if ( 0 != ::pthread_setspecific ( key_, NULL ) ) |
| 63 | abort_message("cannot zero out thread value for __cxa_get_globals()"); |
Marshall Clow | 61b898e | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Howard Hinnant | 7da9b96 | 2012-01-30 16:07:00 +0000 | [diff] [blame] | 66 | void construct_ () { |
Nick Kledzik | f4429fe | 2011-08-02 01:34:26 +0000 | [diff] [blame] | 67 | if ( 0 != pthread_key_create ( &key_, destruct_ ) ) |
| 68 | abort_message("cannot create pthread key for __cxa_get_globals()"); |
Marshall Clow | 61b898e | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
| 72 | extern "C" { |
Howard Hinnant | 7da9b96 | 2012-01-30 16:07:00 +0000 | [diff] [blame] | 73 | __cxa_eh_globals * __cxa_get_globals () { |
Marshall Clow | 61b898e | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 74 | // Try to get the globals for this thread |
| 75 | __cxa_eh_globals* retVal = __cxa_get_globals_fast (); |
| 76 | |
| 77 | // If this is the first time we've been asked for these globals, create them |
| 78 | if ( NULL == retVal ) { |
| 79 | retVal = static_cast<__cxa_eh_globals*> |
| 80 | (std::calloc (1, sizeof (__cxa_eh_globals))); |
Nick Kledzik | f4429fe | 2011-08-02 01:34:26 +0000 | [diff] [blame] | 81 | if ( NULL == retVal ) |
| 82 | abort_message("cannot allocate __cxa_eh_globals"); |
| 83 | if ( 0 != pthread_setspecific ( key_, retVal ) ) |
| 84 | abort_message("pthread_setspecific failure in __cxa_get_globals()"); |
| 85 | } |
Marshall Clow | 61b898e | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 86 | return retVal; |
| 87 | } |
| 88 | |
Howard Hinnant | f6d4cba | 2012-01-25 19:19:13 +0000 | [diff] [blame] | 89 | // Note that this implementation will reliably return NULL if not |
Howard Hinnant | 6d00fef | 2013-02-15 15:48:49 +0000 | [diff] [blame] | 90 | // preceded by a call to __cxa_get_globals(). This is an extension |
Howard Hinnant | f6d4cba | 2012-01-25 19:19:13 +0000 | [diff] [blame] | 91 | // to the Itanium ABI and is taken advantage of in several places in |
| 92 | // libc++abi. |
Howard Hinnant | 7da9b96 | 2012-01-30 16:07:00 +0000 | [diff] [blame] | 93 | __cxa_eh_globals * __cxa_get_globals_fast () { |
Howard Hinnant | 928afcd | 2012-01-23 23:55:58 +0000 | [diff] [blame] | 94 | // First time through, create the key. |
Howard Hinnant | 7da9b96 | 2012-01-30 16:07:00 +0000 | [diff] [blame] | 95 | if (0 != pthread_once(&flag_, construct_)) |
| 96 | abort_message("pthread_once failure in __cxa_get_globals_fast()"); |
| 97 | // static int init = construct_(); |
Marshall Clow | 61b898e | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 98 | return static_cast<__cxa_eh_globals*>(::pthread_getspecific(key_)); |
| 99 | } |
| 100 | |
| 101 | } |
| 102 | } |
| 103 | #endif |