blob: 549d747ba91b90d09a97653978956a214208b703 [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
Jonathan Roelofs40e98422014-05-06 21:30:56 +000016#include "config.h"
Asiri Rathnayake97ba9fa2017-01-03 12:58:34 +000017#include <__threading_support>
Jonathan Roelofs40e98422014-05-06 21:30:56 +000018
Asiri Rathnayake7c98baa2016-09-21 09:09:32 +000019#if defined(_LIBCXXABI_HAS_NO_THREADS)
Jonathan Roelofs40e98422014-05-06 21:30:56 +000020
21namespace __cxxabiv1 {
22extern "C" {
23 static __cxa_eh_globals eh_globals;
24 __cxa_eh_globals *__cxa_get_globals() { return &eh_globals; }
25 __cxa_eh_globals *__cxa_get_globals_fast() { return &eh_globals; }
26 }
27}
28
29#elif defined(HAS_THREAD_LOCAL)
Marshall Clow1df50ca2011-07-20 14:53:53 +000030
31namespace __cxxabiv1 {
32
33namespace {
Howard Hinnant47cb85482012-01-30 16:07:00 +000034 __cxa_eh_globals * __globals () {
Marshall Clow1df50ca2011-07-20 14:53:53 +000035 static thread_local __cxa_eh_globals eh_globals;
36 return &eh_globals;
37 }
38 }
39
40extern "C" {
Howard Hinnant47cb85482012-01-30 16:07:00 +000041 __cxa_eh_globals * __cxa_get_globals () { return __globals (); }
42 __cxa_eh_globals * __cxa_get_globals_fast () { return __globals (); }
Marshall Clow1df50ca2011-07-20 14:53:53 +000043 }
44}
45
46#else
47
Nick Kledzik7cb49262011-08-02 01:34:26 +000048#include "abort_message.h"
Igor Kudrind9edde42016-10-07 08:48:28 +000049#include "fallback_malloc.h"
Marshall Clow1df50ca2011-07-20 14:53:53 +000050
Asiri Rathnayake6d3ea682016-10-13 15:05:19 +000051// In general, we treat all threading errors as fatal.
Nick Kledzik7cb49262011-08-02 01:34:26 +000052// We cannot call std::terminate() because that will in turn
53// call __cxa_get_globals() and cause infinite recursion.
Marshall Clow1df50ca2011-07-20 14:53:53 +000054
55namespace __cxxabiv1 {
56namespace {
Asiri Rathnayake97ba9fa2017-01-03 12:58:34 +000057 std::__libcpp_tls_key key_;
58 std::__libcpp_exec_once_flag flag_ = _LIBCPP_EXEC_ONCE_INITIALIZER;
Marshall Clow1df50ca2011-07-20 14:53:53 +000059
Howard Hinnant47cb85482012-01-30 16:07:00 +000060 void destruct_ (void *p) {
Igor Kudrind9edde42016-10-07 08:48:28 +000061 __free_with_fallback ( p );
Asiri Rathnayake97ba9fa2017-01-03 12:58:34 +000062 if ( 0 != std::__libcpp_tls_set ( key_, NULL ) )
Nick Kledzik7cb49262011-08-02 01:34:26 +000063 abort_message("cannot zero out thread value for __cxa_get_globals()");
Marshall Clow1df50ca2011-07-20 14:53:53 +000064 }
65
Howard Hinnant47cb85482012-01-30 16:07:00 +000066 void construct_ () {
Asiri Rathnayake97ba9fa2017-01-03 12:58:34 +000067 if ( 0 != std::__libcpp_tls_create ( &key_, destruct_ ) )
Asiri Rathnayake6d3ea682016-10-13 15:05:19 +000068 abort_message("cannot create thread specific key for __cxa_get_globals()");
Marshall Clow1df50ca2011-07-20 14:53:53 +000069 }
70}
71
72extern "C" {
Howard Hinnant47cb85482012-01-30 16:07:00 +000073 __cxa_eh_globals * __cxa_get_globals () {
Marshall Clow1df50ca2011-07-20 14:53:53 +000074 // 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*>
Igor Kudrind9edde42016-10-07 08:48:28 +000080 (__calloc_with_fallback (1, sizeof (__cxa_eh_globals)));
Nick Kledzik7cb49262011-08-02 01:34:26 +000081 if ( NULL == retVal )
82 abort_message("cannot allocate __cxa_eh_globals");
Asiri Rathnayake97ba9fa2017-01-03 12:58:34 +000083 if ( 0 != std::__libcpp_tls_set ( key_, retVal ) )
84 abort_message("std::__libcpp_tls_set failure in __cxa_get_globals()");
Nick Kledzik7cb49262011-08-02 01:34:26 +000085 }
Marshall Clow1df50ca2011-07-20 14:53:53 +000086 return retVal;
87 }
88
Howard Hinnantdb970642012-01-25 19:19:13 +000089 // Note that this implementation will reliably return NULL if not
Howard Hinnantbe2eced2013-02-15 15:48:49 +000090 // preceded by a call to __cxa_get_globals(). This is an extension
Howard Hinnantdb970642012-01-25 19:19:13 +000091 // to the Itanium ABI and is taken advantage of in several places in
92 // libc++abi.
Howard Hinnant47cb85482012-01-30 16:07:00 +000093 __cxa_eh_globals * __cxa_get_globals_fast () {
Howard Hinnantfad744d2012-01-23 23:55:58 +000094 // First time through, create the key.
Asiri Rathnayake97ba9fa2017-01-03 12:58:34 +000095 if (0 != std::__libcpp_execute_once(&flag_, construct_))
Asiri Rathnayake6d3ea682016-10-13 15:05:19 +000096 abort_message("execute once failure in __cxa_get_globals_fast()");
Howard Hinnant47cb85482012-01-30 16:07:00 +000097// static int init = construct_();
Asiri Rathnayake97ba9fa2017-01-03 12:58:34 +000098 return static_cast<__cxa_eh_globals*>(std::__libcpp_tls_get(key_));
Marshall Clow1df50ca2011-07-20 14:53:53 +000099 }
100
101}
102}
103#endif