blob: c3ee8565da4cfa9b9cc5703b2c5600c304164157 [file] [log] [blame]
Marshall Clow1df50ca2011-07-20 14:53:53 +00001//===--------------------- 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 Roelofsc5f7e6f2014-02-12 04:49:09 +000010// http://mentorembedded.github.io/cxx-abi/abi-eh.html (section 2.2.2)
Marshall Clow1df50ca2011-07-20 14:53:53 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "cxa_exception.hpp"
15
16#ifdef HAS_THREAD_LOCAL
17
18namespace __cxxabiv1 {
19
20namespace {
Howard Hinnant47cb85482012-01-30 16:07:00 +000021 __cxa_eh_globals * __globals () {
Marshall Clow1df50ca2011-07-20 14:53:53 +000022 static thread_local __cxa_eh_globals eh_globals;
23 return &eh_globals;
24 }
25 }
26
27extern "C" {
Howard Hinnant47cb85482012-01-30 16:07:00 +000028 __cxa_eh_globals * __cxa_get_globals () { return __globals (); }
29 __cxa_eh_globals * __cxa_get_globals_fast () { return __globals (); }
Marshall Clow1df50ca2011-07-20 14:53:53 +000030 }
31}
32
33#else
34
35#include <pthread.h>
36#include <cstdlib> // for calloc, free
Nick Kledzik7cb49262011-08-02 01:34:26 +000037#include "abort_message.h"
Marshall Clow1df50ca2011-07-20 14:53:53 +000038
Nick Kledzik7cb49262011-08-02 01:34:26 +000039// In general, we treat all pthread errors as fatal.
40// We cannot call std::terminate() because that will in turn
41// call __cxa_get_globals() and cause infinite recursion.
Marshall Clow1df50ca2011-07-20 14:53:53 +000042
43namespace __cxxabiv1 {
44namespace {
Marshall Clow1df50ca2011-07-20 14:53:53 +000045 pthread_key_t key_;
Howard Hinnant47cb85482012-01-30 16:07:00 +000046 pthread_once_t flag_ = PTHREAD_ONCE_INIT;
Marshall Clow1df50ca2011-07-20 14:53:53 +000047
Howard Hinnant47cb85482012-01-30 16:07:00 +000048 void destruct_ (void *p) {
Marshall Clow1df50ca2011-07-20 14:53:53 +000049 std::free ( p );
Nick Kledzik7cb49262011-08-02 01:34:26 +000050 if ( 0 != ::pthread_setspecific ( key_, NULL ) )
51 abort_message("cannot zero out thread value for __cxa_get_globals()");
Marshall Clow1df50ca2011-07-20 14:53:53 +000052 }
53
Howard Hinnant47cb85482012-01-30 16:07:00 +000054 void construct_ () {
Nick Kledzik7cb49262011-08-02 01:34:26 +000055 if ( 0 != pthread_key_create ( &key_, destruct_ ) )
56 abort_message("cannot create pthread key for __cxa_get_globals()");
Marshall Clow1df50ca2011-07-20 14:53:53 +000057 }
58}
59
60extern "C" {
Howard Hinnant47cb85482012-01-30 16:07:00 +000061 __cxa_eh_globals * __cxa_get_globals () {
Marshall Clow1df50ca2011-07-20 14:53:53 +000062 // Try to get the globals for this thread
63 __cxa_eh_globals* retVal = __cxa_get_globals_fast ();
64
65 // If this is the first time we've been asked for these globals, create them
66 if ( NULL == retVal ) {
67 retVal = static_cast<__cxa_eh_globals*>
68 (std::calloc (1, sizeof (__cxa_eh_globals)));
Nick Kledzik7cb49262011-08-02 01:34:26 +000069 if ( NULL == retVal )
70 abort_message("cannot allocate __cxa_eh_globals");
71 if ( 0 != pthread_setspecific ( key_, retVal ) )
72 abort_message("pthread_setspecific failure in __cxa_get_globals()");
73 }
Marshall Clow1df50ca2011-07-20 14:53:53 +000074 return retVal;
75 }
76
Howard Hinnantdb970642012-01-25 19:19:13 +000077 // Note that this implementation will reliably return NULL if not
Howard Hinnantbe2eced2013-02-15 15:48:49 +000078 // preceded by a call to __cxa_get_globals(). This is an extension
Howard Hinnantdb970642012-01-25 19:19:13 +000079 // to the Itanium ABI and is taken advantage of in several places in
80 // libc++abi.
Howard Hinnant47cb85482012-01-30 16:07:00 +000081 __cxa_eh_globals * __cxa_get_globals_fast () {
Howard Hinnantfad744d2012-01-23 23:55:58 +000082 // First time through, create the key.
Howard Hinnant47cb85482012-01-30 16:07:00 +000083 if (0 != pthread_once(&flag_, construct_))
84 abort_message("pthread_once failure in __cxa_get_globals_fast()");
85// static int init = construct_();
Marshall Clow1df50ca2011-07-20 14:53:53 +000086 return static_cast<__cxa_eh_globals*>(::pthread_getspecific(key_));
87 }
88
89}
90}
91#endif