2021-01-04 10:26:59 +01:00
|
|
|
// Copyright (C) 2012-2021 Free Software Foundation, Inc.
|
2012-10-08 16:45:24 +02:00
|
|
|
//
|
|
|
|
// This file is part of GCC.
|
|
|
|
//
|
|
|
|
// GCC is free software; you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation; either version 3, or (at your option)
|
|
|
|
// any later version.
|
|
|
|
|
|
|
|
// GCC is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// Under Section 7 of GPL version 3, you are granted additional
|
|
|
|
// permissions described in the GCC Runtime Library Exception, version
|
|
|
|
// 3.1, as published by the Free Software Foundation.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License and
|
|
|
|
// a copy of the GCC Runtime Library Exception along with this program;
|
|
|
|
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
|
|
|
|
// <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
#include <cxxabi.h>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <new>
|
|
|
|
#include "bits/gthr.h"
|
2014-08-19 17:25:12 +02:00
|
|
|
#ifdef _GLIBCXX_THREAD_ATEXIT_WIN32
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
2012-10-08 16:45:24 +02:00
|
|
|
|
2020-10-08 04:26:13 +02:00
|
|
|
// Simplify it a little for this file.
|
|
|
|
#ifndef _GLIBCXX_CDTOR_CALLABI
|
|
|
|
# define _GLIBCXX_CDTOR_CALLABI
|
|
|
|
#endif
|
|
|
|
|
2017-01-04 16:41:19 +01:00
|
|
|
#if _GLIBCXX_HAVE___CXA_THREAD_ATEXIT
|
|
|
|
|
|
|
|
// Libc provides __cxa_thread_atexit definition.
|
|
|
|
|
|
|
|
#elif _GLIBCXX_HAVE___CXA_THREAD_ATEXIT_IMPL
|
2013-02-26 05:39:04 +01:00
|
|
|
|
2020-10-08 04:26:13 +02:00
|
|
|
extern "C" int __cxa_thread_atexit_impl (void (_GLIBCXX_CDTOR_CALLABI *func) (void *),
|
2013-02-26 05:39:04 +01:00
|
|
|
void *arg, void *d);
|
|
|
|
extern "C" int
|
2020-10-08 04:26:13 +02:00
|
|
|
__cxxabiv1::__cxa_thread_atexit (void (_GLIBCXX_CDTOR_CALLABI *dtor)(void *),
|
2013-02-26 05:39:04 +01:00
|
|
|
void *obj, void *dso_handle)
|
|
|
|
_GLIBCXX_NOTHROW
|
|
|
|
{
|
|
|
|
return __cxa_thread_atexit_impl (dtor, obj, dso_handle);
|
|
|
|
}
|
|
|
|
|
2014-08-01 21:45:56 +02:00
|
|
|
#else /* _GLIBCXX_HAVE___CXA_THREAD_ATEXIT_IMPL */
|
2013-02-26 05:39:04 +01:00
|
|
|
|
2012-10-08 16:45:24 +02:00
|
|
|
namespace {
|
2012-10-15 09:32:13 +02:00
|
|
|
// One element in a singly-linked stack of cleanups.
|
|
|
|
struct elt
|
2012-10-08 16:45:24 +02:00
|
|
|
{
|
2020-10-08 04:26:13 +02:00
|
|
|
void (_GLIBCXX_CDTOR_CALLABI *destructor)(void *);
|
2012-10-15 09:32:13 +02:00
|
|
|
void *object;
|
|
|
|
elt *next;
|
2014-08-19 17:25:12 +02:00
|
|
|
#ifdef _GLIBCXX_THREAD_ATEXIT_WIN32
|
|
|
|
HMODULE dll;
|
|
|
|
#endif
|
2012-10-08 16:45:24 +02:00
|
|
|
};
|
|
|
|
|
2012-10-15 09:32:13 +02:00
|
|
|
// Keep a per-thread list of cleanups in gthread_key storage.
|
|
|
|
__gthread_key_t key;
|
|
|
|
// But also support non-threaded mode.
|
|
|
|
elt *single_thread;
|
2012-10-08 16:45:24 +02:00
|
|
|
|
2012-10-15 09:32:13 +02:00
|
|
|
// Run the specified stack of cleanups.
|
|
|
|
void run (void *p)
|
2012-10-08 16:45:24 +02:00
|
|
|
{
|
2012-10-15 09:32:13 +02:00
|
|
|
elt *e = static_cast<elt*>(p);
|
2013-07-22 21:43:27 +02:00
|
|
|
while (e)
|
|
|
|
{
|
|
|
|
elt *old_e = e;
|
|
|
|
e->destructor (e->object);
|
2014-08-19 17:25:12 +02:00
|
|
|
#ifdef _GLIBCXX_THREAD_ATEXIT_WIN32
|
|
|
|
/* Decrement DLL count */
|
|
|
|
if (e->dll)
|
|
|
|
FreeLibrary (e->dll);
|
|
|
|
#endif
|
2013-07-22 21:43:27 +02:00
|
|
|
e = e->next;
|
|
|
|
delete (old_e);
|
|
|
|
}
|
2012-10-08 16:45:24 +02:00
|
|
|
}
|
|
|
|
|
2012-10-15 09:32:13 +02:00
|
|
|
// Run the stack of cleanups for the current thread.
|
|
|
|
void run ()
|
2012-10-08 16:45:24 +02:00
|
|
|
{
|
2012-10-15 09:32:13 +02:00
|
|
|
void *e;
|
|
|
|
if (__gthread_active_p ())
|
2013-07-22 21:43:27 +02:00
|
|
|
{
|
|
|
|
e = __gthread_getspecific (key);
|
|
|
|
__gthread_setspecific (key, NULL);
|
|
|
|
}
|
2012-10-15 09:32:13 +02:00
|
|
|
else
|
2013-07-22 21:43:27 +02:00
|
|
|
{
|
|
|
|
e = single_thread;
|
|
|
|
single_thread = NULL;
|
|
|
|
}
|
2012-10-15 09:32:13 +02:00
|
|
|
run (e);
|
2012-10-08 16:45:24 +02:00
|
|
|
}
|
|
|
|
|
2012-10-15 09:32:13 +02:00
|
|
|
// Initialize the key for the cleanup stack. We use a static local for
|
|
|
|
// key init/delete rather than atexit so that delete is run on dlclose.
|
2012-10-08 16:45:24 +02:00
|
|
|
void key_init() {
|
2012-10-15 09:32:13 +02:00
|
|
|
struct key_s {
|
|
|
|
key_s() { __gthread_key_create (&key, run); }
|
|
|
|
~key_s() { __gthread_key_delete (key); }
|
|
|
|
};
|
|
|
|
static key_s ks;
|
2012-10-08 16:45:24 +02:00
|
|
|
// Also make sure the destructors are run by std::exit.
|
|
|
|
// FIXME TLS cleanups should run before static cleanups and atexit
|
|
|
|
// cleanups.
|
2012-10-15 09:32:13 +02:00
|
|
|
std::atexit (run);
|
2012-10-08 16:45:24 +02:00
|
|
|
}
|
2012-10-15 09:32:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int
|
2020-10-08 04:26:13 +02:00
|
|
|
__cxxabiv1::__cxa_thread_atexit (void (_GLIBCXX_CDTOR_CALLABI *dtor)(void *),
|
|
|
|
void *obj, void */*dso_handle*/)
|
2012-10-15 09:32:13 +02:00
|
|
|
_GLIBCXX_NOTHROW
|
|
|
|
{
|
|
|
|
// Do this initialization once.
|
|
|
|
if (__gthread_active_p ())
|
|
|
|
{
|
|
|
|
// When threads are active use __gthread_once.
|
|
|
|
static __gthread_once_t once = __GTHREAD_ONCE_INIT;
|
|
|
|
__gthread_once (&once, key_init);
|
|
|
|
}
|
|
|
|
else
|
2012-10-08 16:45:24 +02:00
|
|
|
{
|
2012-10-15 09:32:13 +02:00
|
|
|
// And when threads aren't active use a static local guard.
|
|
|
|
static bool queued;
|
|
|
|
if (!queued)
|
2012-10-08 16:45:24 +02:00
|
|
|
{
|
2012-10-15 09:32:13 +02:00
|
|
|
queued = true;
|
|
|
|
std::atexit (run);
|
2012-10-08 16:45:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-15 09:32:13 +02:00
|
|
|
elt *first;
|
|
|
|
if (__gthread_active_p ())
|
|
|
|
first = static_cast<elt*>(__gthread_getspecific (key));
|
|
|
|
else
|
|
|
|
first = single_thread;
|
2012-10-08 16:45:24 +02:00
|
|
|
|
2012-10-15 09:32:13 +02:00
|
|
|
elt *new_elt = new (std::nothrow) elt;
|
|
|
|
if (!new_elt)
|
|
|
|
return -1;
|
|
|
|
new_elt->destructor = dtor;
|
|
|
|
new_elt->object = obj;
|
|
|
|
new_elt->next = first;
|
2014-08-19 17:25:12 +02:00
|
|
|
#ifdef _GLIBCXX_THREAD_ATEXIT_WIN32
|
|
|
|
/* Store the DLL address for a later call to FreeLibrary in new_elt and
|
|
|
|
increment DLL load count. This blocks the unloading of the DLL
|
|
|
|
before the thread-local dtors have been called. This does NOT help
|
|
|
|
if FreeLibrary/dlclose is called in excess. */
|
|
|
|
GetModuleHandleExW (GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
|
|
|
|
(LPCWSTR) dtor, &new_elt->dll);
|
|
|
|
#endif
|
2012-10-15 09:32:13 +02:00
|
|
|
|
|
|
|
if (__gthread_active_p ())
|
|
|
|
__gthread_setspecific (key, new_elt);
|
|
|
|
else
|
|
|
|
single_thread = new_elt;
|
|
|
|
|
|
|
|
return 0;
|
2012-10-08 16:45:24 +02:00
|
|
|
}
|
2013-02-26 05:39:04 +01:00
|
|
|
|
2014-08-01 21:45:56 +02:00
|
|
|
#endif /* _GLIBCXX_HAVE___CXA_THREAD_ATEXIT_IMPL */
|