* config/rs6000/rs6000.c (rs6000_delegitimize_address): Check that
[official-gcc.git] / libstdc++-v3 / libsupc++ / atexit_thread.cc
blob95bdcf09dec079554b5dca6a5ff6a8d6c594b86d
1 // Copyright (C) 2012 Free Software Foundation, Inc.
2 //
3 // This file is part of GCC.
4 //
5 // GCC is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3, or (at your option)
8 // any later version.
10 // GCC is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // Under Section 7 of GPL version 3, you are granted additional
16 // permissions described in the GCC Runtime Library Exception, version
17 // 3.1, as published by the Free Software Foundation.
19 // You should have received a copy of the GNU General Public License and
20 // a copy of the GCC Runtime Library Exception along with this program;
21 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
22 // <http://www.gnu.org/licenses/>.
24 #include <cxxabi.h>
25 #include <cstdlib>
26 #include <new>
27 #include "bits/gthr.h"
29 namespace {
30 // One element in a singly-linked stack of cleanups.
31 struct elt
33 void (*destructor)(void *);
34 void *object;
35 elt *next;
38 // Keep a per-thread list of cleanups in gthread_key storage.
39 __gthread_key_t key;
40 // But also support non-threaded mode.
41 elt *single_thread;
43 // Run the specified stack of cleanups.
44 void run (void *p)
46 elt *e = static_cast<elt*>(p);
47 for (; e; e = e->next)
48 e->destructor (e->object);
51 // Run the stack of cleanups for the current thread.
52 void run ()
54 void *e;
55 if (__gthread_active_p ())
56 e = __gthread_getspecific (key);
57 else
58 e = single_thread;
59 run (e);
62 // Initialize the key for the cleanup stack. We use a static local for
63 // key init/delete rather than atexit so that delete is run on dlclose.
64 void key_init() {
65 struct key_s {
66 key_s() { __gthread_key_create (&key, run); }
67 ~key_s() { __gthread_key_delete (key); }
69 static key_s ks;
70 // Also make sure the destructors are run by std::exit.
71 // FIXME TLS cleanups should run before static cleanups and atexit
72 // cleanups.
73 std::atexit (run);
77 extern "C" int
78 __cxxabiv1::__cxa_thread_atexit (void (*dtor)(void *), void *obj, void */*dso_handle*/)
79 _GLIBCXX_NOTHROW
81 // Do this initialization once.
82 if (__gthread_active_p ())
84 // When threads are active use __gthread_once.
85 static __gthread_once_t once = __GTHREAD_ONCE_INIT;
86 __gthread_once (&once, key_init);
88 else
90 // And when threads aren't active use a static local guard.
91 static bool queued;
92 if (!queued)
94 queued = true;
95 std::atexit (run);
99 elt *first;
100 if (__gthread_active_p ())
101 first = static_cast<elt*>(__gthread_getspecific (key));
102 else
103 first = single_thread;
105 elt *new_elt = new (std::nothrow) elt;
106 if (!new_elt)
107 return -1;
108 new_elt->destructor = dtor;
109 new_elt->object = obj;
110 new_elt->next = first;
112 if (__gthread_active_p ())
113 __gthread_setspecific (key, new_elt);
114 else
115 single_thread = new_elt;
117 return 0;