blob: c641e0225f8b30e0104bfb2202f5e15fa7060549 [file] [log] [blame]
Marshall Clow61b898e2011-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 Roelofsc82e02d2014-02-12 04:49:09 +000010// http://mentorembedded.github.io/cxx-abi/abi-eh.html (section 2.2.2)
Marshall Clow61b898e2011-07-20 14:53:53 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "cxa_exception.hpp"
15
Asiri Rathnayake71ba2872017-01-03 12:58:34 +000016#include <__threading_support>
Jonathan Roelofsc285efa2014-05-06 21:30:56 +000017
Asiri Rathnayakeb62a4dd2016-09-21 09:09:32 +000018#if defined(_LIBCXXABI_HAS_NO_THREADS)
Jonathan Roelofsc285efa2014-05-06 21:30:56 +000019
20namespace __cxxabiv1 {
21extern "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 Clow61b898e2011-07-20 14:53:53 +000029
30namespace __cxxabiv1 {
31
32namespace {
Howard Hinnant7da9b962012-01-30 16:07:00 +000033 __cxa_eh_globals * __globals () {
Marshall Clow61b898e2011-07-20 14:53:53 +000034 static thread_local __cxa_eh_globals eh_globals;
35 return &eh_globals;
36 }
37 }
38
39extern "C" {
Howard Hinnant7da9b962012-01-30 16:07:00 +000040 __cxa_eh_globals * __cxa_get_globals () { return __globals (); }
41 __cxa_eh_globals * __cxa_get_globals_fast () { return __globals (); }
Marshall Clow61b898e2011-07-20 14:53:53 +000042 }
43}
44
45#else
46
Nick Kledzikf4429fe2011-08-02 01:34:26 +000047#include "abort_message.h"
Igor Kudrinace65722016-10-07 08:48:28 +000048#include "fallback_malloc.h"
Marshall Clow61b898e2011-07-20 14:53:53 +000049
Asiri Rathnayake51806732016-10-13 15:05:19 +000050// In general, we treat all threading errors as fatal.
Nick Kledzikf4429fe2011-08-02 01:34:26 +000051// We cannot call std::terminate() because that will in turn
52// call __cxa_get_globals() and cause infinite recursion.
Marshall Clow61b898e2011-07-20 14:53:53 +000053
54namespace __cxxabiv1 {
55namespace {
Asiri Rathnayake71ba2872017-01-03 12:58:34 +000056 std::__libcpp_tls_key key_;
57 std::__libcpp_exec_once_flag flag_ = _LIBCPP_EXEC_ONCE_INITIALIZER;
Marshall Clow61b898e2011-07-20 14:53:53 +000058
Shoaib Meenaieb5fa062017-08-08 00:54:33 +000059 void _LIBCPP_TLS_DESTRUCTOR_CC destruct_ (void *p) {
Igor Kudrinace65722016-10-07 08:48:28 +000060 __free_with_fallback ( p );
Asiri Rathnayake71ba2872017-01-03 12:58:34 +000061 if ( 0 != std::__libcpp_tls_set ( key_, NULL ) )
Nick Kledzikf4429fe2011-08-02 01:34:26 +000062 abort_message("cannot zero out thread value for __cxa_get_globals()");
Marshall Clow61b898e2011-07-20 14:53:53 +000063 }
64
Howard Hinnant7da9b962012-01-30 16:07:00 +000065 void construct_ () {
Asiri Rathnayake71ba2872017-01-03 12:58:34 +000066 if ( 0 != std::__libcpp_tls_create ( &key_, destruct_ ) )
Asiri Rathnayake51806732016-10-13 15:05:19 +000067 abort_message("cannot create thread specific key for __cxa_get_globals()");
Marshall Clow61b898e2011-07-20 14:53:53 +000068 }
69}
70
71extern "C" {
Howard Hinnant7da9b962012-01-30 16:07:00 +000072 __cxa_eh_globals * __cxa_get_globals () {
Marshall Clow61b898e2011-07-20 14:53:53 +000073 // Try to get the globals for this thread
74 __cxa_eh_globals* retVal = __cxa_get_globals_fast ();
75
76 // If this is the first time we've been asked for these globals, create them
77 if ( NULL == retVal ) {
78 retVal = static_cast<__cxa_eh_globals*>
Igor Kudrinace65722016-10-07 08:48:28 +000079 (__calloc_with_fallback (1, sizeof (__cxa_eh_globals)));
Nick Kledzikf4429fe2011-08-02 01:34:26 +000080 if ( NULL == retVal )
81 abort_message("cannot allocate __cxa_eh_globals");
Asiri Rathnayake71ba2872017-01-03 12:58:34 +000082 if ( 0 != std::__libcpp_tls_set ( key_, retVal ) )
83 abort_message("std::__libcpp_tls_set failure in __cxa_get_globals()");
Nick Kledzikf4429fe2011-08-02 01:34:26 +000084 }
Marshall Clow61b898e2011-07-20 14:53:53 +000085 return retVal;
86 }
87
Howard Hinnantf6d4cba2012-01-25 19:19:13 +000088 // Note that this implementation will reliably return NULL if not
Howard Hinnant6d00fef2013-02-15 15:48:49 +000089 // preceded by a call to __cxa_get_globals(). This is an extension
Howard Hinnantf6d4cba2012-01-25 19:19:13 +000090 // to the Itanium ABI and is taken advantage of in several places in
91 // libc++abi.
Howard Hinnant7da9b962012-01-30 16:07:00 +000092 __cxa_eh_globals * __cxa_get_globals_fast () {
Howard Hinnant928afcd2012-01-23 23:55:58 +000093 // First time through, create the key.
Asiri Rathnayake71ba2872017-01-03 12:58:34 +000094 if (0 != std::__libcpp_execute_once(&flag_, construct_))
Asiri Rathnayake51806732016-10-13 15:05:19 +000095 abort_message("execute once failure in __cxa_get_globals_fast()");
Howard Hinnant7da9b962012-01-30 16:07:00 +000096// static int init = construct_();
Asiri Rathnayake71ba2872017-01-03 12:58:34 +000097 return static_cast<__cxa_eh_globals*>(std::__libcpp_tls_get(key_));
Marshall Clow61b898e2011-07-20 14:53:53 +000098 }
99
100}
101}
102#endif