Skip to content

Commit

Permalink
os_defines.h (_GLIBCXX_THREAD_ATEXIT_WIN32): Define.
Browse files Browse the repository at this point in the history
2014-08-19  Yaakov Selkowitz  <[email protected]>
	Kai Tietz  <[email protected]>

	* config/os/mingw32-w64/os_defines.h (_GLIBCXX_THREAD_ATEXIT_WIN32):
	Define.
	* config/os/newlib/os_defines.h (_GLIBCXX_THREAD_ATEXIT_WIN32):
	Ditto.
	* libsupc++/atexit_thread.cc [_GLIBCXX_THREAD_ATEXIT_WIN32]:
	#include <windows.h>.
	(struct elt): Add dll member.
	(run): Decrement dll refcount.
	(__cxxabiv1::__cxa_thread_atexit): Increment dll refcount.


Co-Authored-By: Kai Tietz <[email protected]>

From-SVN: r214163
  • Loading branch information
yselkowitz authored and Kai Tietz committed Aug 19, 2014
1 parent 25efdb9 commit 1ed3ba0
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
13 changes: 13 additions & 0 deletions libstdc++-v3/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
2014-08-19 Yaakov Selkowitz <[email protected]>
Kai Tietz <[email protected]>

* config/os/mingw32-w64/os_defines.h (_GLIBCXX_THREAD_ATEXIT_WIN32):
Define.
* config/os/newlib/os_defines.h (_GLIBCXX_THREAD_ATEXIT_WIN32):
Ditto.
* libsupc++/atexit_thread.cc [_GLIBCXX_THREAD_ATEXIT_WIN32]:
#include <windows.h>.
(struct elt): Add dll member.
(run): Decrement dll refcount.
(__cxxabiv1::__cxa_thread_atexit): Increment dll refcount.

2014-08-15 Jonathan Wakely <[email protected]>

PR libstdc++/62154
Expand Down
5 changes: 5 additions & 0 deletions libstdc++-v3/config/os/mingw32-w64/os_defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,9 @@
#define _GLIBCXX_LLP64 1
#endif

// Enable use of GetModuleHandleEx (requires Windows XP/2003) in
// __cxa_thread_atexit to prevent modules from being unloaded before
// their dtors are called
#define _GLIBCXX_THREAD_ATEXIT_WIN32 1

#endif
6 changes: 6 additions & 0 deletions libstdc++-v3/config/os/newlib/os_defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@

// See libstdc++/20806.
#define _GLIBCXX_HAVE_DOS_BASED_FILESYSTEM 1

// Enable use of GetModuleHandleEx (requires Windows XP/2003) in
// __cxa_thread_atexit to prevent modules from being unloaded before
// their dtors are called
#define _GLIBCXX_THREAD_ATEXIT_WIN32 1

#endif

#endif
20 changes: 20 additions & 0 deletions libstdc++-v3/libsupc++/atexit_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
#include <cstdlib>
#include <new>
#include "bits/gthr.h"
#ifdef _GLIBCXX_THREAD_ATEXIT_WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif

#if _GLIBCXX_HAVE___CXA_THREAD_ATEXIT_IMPL

Expand All @@ -47,6 +51,9 @@ namespace {
void (*destructor)(void *);
void *object;
elt *next;
#ifdef _GLIBCXX_THREAD_ATEXIT_WIN32
HMODULE dll;
#endif
};

// Keep a per-thread list of cleanups in gthread_key storage.
Expand All @@ -62,6 +69,11 @@ namespace {
{
elt *old_e = e;
e->destructor (e->object);
#ifdef _GLIBCXX_THREAD_ATEXIT_WIN32
/* Decrement DLL count */
if (e->dll)
FreeLibrary (e->dll);
#endif
e = e->next;
delete (old_e);
}
Expand Down Expand Up @@ -133,6 +145,14 @@ __cxxabiv1::__cxa_thread_atexit (void (*dtor)(void *), void *obj, void */*dso_ha
new_elt->destructor = dtor;
new_elt->object = obj;
new_elt->next = first;
#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

if (__gthread_active_p ())
__gthread_setspecific (key, new_elt);
Expand Down

0 comments on commit 1ed3ba0

Please sign in to comment.