Skip to content
Permalink
Browse files

3rdparty/pthreads4w: Fixup process exit logic. (#3056)

This PR modifies a third party module - I realize normally those should be fixed upstream, but I really doubt pthreads4w would a) consider it a valid bug and b) fix it. To make all my changes easily visible, I wrap them all in #if PCSX2_FIX.

This PR fixes a process exit routine in pthreads. This third party module exploits CRT initialization order to inject their initializer/deinitializer earlier than the others by putting their functions in .CRT$XCU and .CRT$XPU pseudo regions. The problem comes when a module gets build with dynamic CRT (/MD or /MDd), like most of PCSX2 plugins, it doesn't actually use .CRT$XPx regions as terminators, and instead lets dynamic CRT handle them.

This PR corrects this issue by registering the terminator via atexit, so it works with both static and dynamic CRT. This resolves an issue where SPU2-X plugin (and potentially more) leaks TLS handles when unloaded.
  • Loading branch information...
CookiePLMonster authored and lightningterror committed Aug 13, 2019
1 parent ed6ac00 commit af6f04020225eaf3239bc9b4542f4afd84560540
Showing with 14 additions and 0 deletions.
  1. +14 −0 3rdparty/pthreads4w/autostatic.c
@@ -36,22 +36,34 @@

#if defined(PTW32_STATIC_LIB)

#define PCSX2_FIX 1 // Comment out to restore code to pristine state

#if defined(__MINGW64__) || defined(__MINGW32__) || defined(_MSC_VER)

#include "pthread.h"
#include "implement.h"

#if !PCSX2_FIX
static void on_process_init(void)
{
pthread_win32_process_attach_np ();
}
#endif

static void on_process_exit(void)
{
pthread_win32_thread_detach_np ();
pthread_win32_process_detach_np ();
}

#if PCSX2_FIX
static void on_process_init(void)
{
pthread_win32_process_attach_np();
atexit(on_process_exit);
}
#endif

#if defined(__MINGW64__) || defined(__MINGW32__)
# define attribute_section(a) __attribute__((section(a)))
#elif defined(_MSC_VER)
@@ -62,7 +74,9 @@ attribute_section(".ctors") void *gcc_ctor = on_process_init;
attribute_section(".dtors") void *gcc_dtor = on_process_exit;

attribute_section(".CRT$XCU") void *msc_ctor = on_process_init;
#if !PCSX2_FIX
attribute_section(".CRT$XPU") void *msc_dtor = on_process_exit;
#endif

#endif /* defined(__MINGW64__) || defined(__MINGW32__) || defined(_MSC_VER) */

0 comments on commit af6f040

Please sign in to comment.
You can’t perform that action at this time.