Thread Local Storage Fixes:

* Implemented TlsVariable, a nifty alternative to __threadlocal, suitable for archaic operating systems that don't have native TLS support (namely Mac OS/X).
 * Added a forced reference to TLS in AppInit so that TLS is sure to be available to DLLs (otherwise windows doesn't init TLS by default).
 * Disabled TLS support in the x86emitter by default, since it's looking increasingly like we won't find a use for multithreading PS2 sub-components (can be re-enabled later if spontaneous brilliance at a later date proves me wrong).

git-svn-id: http://pcsx2.googlecode.com/svn/trunk@2396 96395faa-99c1-11dd-bbfe-3dabce05a288
This commit is contained in:
Jake.Stine 2009-12-24 22:22:34 +00:00
parent 3d9bb25505
commit 9473e69b7f
20 changed files with 464 additions and 263 deletions

View File

@ -161,6 +161,7 @@
<Unit filename="../../include/Utilities/ScopedPtr.h" />
<Unit filename="../../include/Utilities/StringHelpers.h" />
<Unit filename="../../include/Utilities/Threading.h" />
<Unit filename="../../include/Utilities/TlsVariable.h" />
<Unit filename="../../include/Utilities/lnx_memzero.h" />
<Unit filename="../../include/Utilities/pxCheckBox.h" />
<Unit filename="../../include/Utilities/pxRadioPanel.h" />

View File

@ -223,10 +223,6 @@
RelativePath="..\..\src\Utilities\x86\MemcpyFast.cpp"
>
</File>
<File
RelativePath="..\..\src\Utilities\Mutex.cpp"
>
</File>
<File
RelativePath="..\..\src\Utilities\PathUtils.cpp"
>
@ -271,22 +267,10 @@
RelativePath="..\..\src\Utilities\pxStaticText.cpp"
>
</File>
<File
RelativePath="..\..\src\Utilities\Semaphore.cpp"
>
</File>
<File
RelativePath="..\..\src\Utilities\StringHelpers.cpp"
>
</File>
<File
RelativePath="..\..\src\Utilities\ThreadingInternal.h"
>
</File>
<File
RelativePath="..\..\src\Utilities\ThreadTools.cpp"
>
</File>
<File
RelativePath="..\..\src\Utilities\vssprintf.cpp"
>
@ -431,6 +415,26 @@
>
</File>
</Filter>
<Filter
Name="Threading"
>
<File
RelativePath="..\..\src\Utilities\Mutex.cpp"
>
</File>
<File
RelativePath="..\..\src\Utilities\Semaphore.cpp"
>
</File>
<File
RelativePath="..\..\src\Utilities\ThreadingInternal.h"
>
</File>
<File
RelativePath="..\..\src\Utilities\ThreadTools.cpp"
>
</File>
</Filter>
</Filter>
<Filter
Name="Header Files"
@ -525,10 +529,6 @@
RelativePath="..\..\include\Utilities\StringHelpers.h"
>
</File>
<File
RelativePath="..\..\include\Utilities\Threading.h"
>
</File>
<File
RelativePath="..\..\include\Utilities\win_memzero.h"
>
@ -541,6 +541,18 @@
RelativePath="..\..\include\Utilities\wxGuiTools.h"
>
</File>
<Filter
Name="Threading"
>
<File
RelativePath="..\..\include\Utilities\Threading.h"
>
</File>
<File
RelativePath="..\..\include\Utilities\TlsVariable.inl"
>
</File>
</Filter>
</Filter>
</Files>
<Globals>

View File

@ -22,11 +22,27 @@
#include "Pcsx2Defs.h"
#include "ScopedPtr.h"
#undef Yield // release th burden of windows.h global namespace spam.
#undef Yield // release the burden of windows.h global namespace spam.
#define AffinityAssert_AllowFromMain() \
pxAssertMsg( wxThread::IsMain(), "Thread affinity violation: Call allowed from main thread only." )
// --------------------------------------------------------------------------------------
// PCSX2_THREAD_LOCAL - Defines platform/operating system support for Thread Local Storage
// --------------------------------------------------------------------------------------
// For complimentary support for TLS, include Utilities/TlsVariable.inl, and use the
// DeclareTls macro in the place of __threadlocal.
//
//#define PCSX2_THREAD_LOCAL 0 // uncomment this line to force-disable native TLS (useful for testing TlsVariabel on windows/linux)
#ifndef PCSX2_THREAD_LOCAL
# ifdef __WXMAC__
# define PCSX2_THREAD_LOCAL 0
# else
# define PCSX2_THREAD_LOCAL 1
# endif
#endif
class wxTimeSpan;
namespace Threading
@ -131,6 +147,7 @@ namespace Exception
#endif
}
namespace Threading
{
// --------------------------------------------------------------------------------------

View File

@ -0,0 +1,125 @@
/* PCSX2 - PS2 Emulator for PCs
* Copyright (C) 2002-2009 PCSX2 Dev Team
*
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* PCSX2 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.
*
* You should have received a copy of the GNU General Public License along with PCSX2.
* If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "Threading.h"
#if PCSX2_THREAD_LOCAL
# define DeclareTls(x) __threadlocal x
#else
# define DeclareTls(x) Threading::TlsVariable<x>
#endif
namespace Threading
{
// --------------------------------------------------------------------------------------
// TlsVariable - Thread local storage
// --------------------------------------------------------------------------------------
// Wrapper class for pthread_getspecific, which is pthreads language for "thread local
// storage." This class enables code to act as a drop-in replacement for compiler-native
// thread local storage (typically specified via __threadlocal). Mac OS/X (Darwin) does
// not have TLS, which is the main reason for this class existing.
//
// Performance considerations: While certainly convenient, performance of this class can
// be sub-optimal when the operator overloads are used, since each one will most likely
// result in repeated calls to pthread_getspecific. (if the function inlines then it
// should actually optimize well enough, but I doubt it does).
//
template< typename T >
class TlsVariable
{
DeclareNoncopyableObject(TlsVariable);
protected:
pthread_key_t m_thread_key;
T m_initval;
public:
TlsVariable();
TlsVariable( T initval );
virtual ~TlsVariable() throw();
T* GetPtr() const;
T& GetRef() const { return *GetPtr(); }
TlsVariable& operator=( const T& src )
{
GetRef() = src;
return *this;
}
bool operator==( const T& src ) const { return GetRef() == src; }
bool operator!=( const T& src ) const { return GetRef() != src; }
bool operator>( const T& src ) const { return GetRef() > src; }
bool operator<( const T& src ) const { return GetRef() < src; }
bool operator>=( const T& src ) const { return GetRef() >= src; }
bool operator<=( const T& src ) const { return GetRef() <= src; }
T operator+( const T& src ) const { return GetRef() + src; }
T operator-( const T& src ) const { return GetRef() - src; }
void operator+=( const T& src ) { GetRef() += src; }
void operator-=( const T& src ) { GetRef() -= src; }
operator T&() const { return GetRef(); }
protected:
void CreateKey();
};
};
template< typename T >
Threading::TlsVariable<T>::TlsVariable()
{
CreateKey();
}
template< typename T >
Threading::TlsVariable<T>::TlsVariable( T initval )
{
CreateKey();
m_initval = initval;
}
template< typename T >
Threading::TlsVariable<T>::~TlsVariable() throw()
{
if( m_thread_key != NULL )
pthread_key_delete( m_thread_key );
}
template< typename T >
T* Threading::TlsVariable<T>::GetPtr() const
{
T* result = (T*)pthread_getspecific( m_thread_key );
if( result == NULL )
{
pthread_setspecific( m_thread_key, result = (T*)_aligned_malloc( sizeof(T), 16 ) );
if( result == NULL )
throw Exception::OutOfMemory( "Out of memory allocating thread local storage variable." );
*result = m_initval;
}
return result;
}
template< typename T >
void Threading::TlsVariable<T>::CreateKey()
{
if( 0 != pthread_key_create(&m_thread_key, _aligned_free) )
{
pxFailRel( "Thread Local Storage Error: key creation failed." );
}
}

View File

@ -27,8 +27,31 @@ enum XMMSSEType
//XMMT_FPD = 3, // double
};
extern __threadlocal u8 *x86Ptr;
extern __threadlocal XMMSSEType g_xmmtypes[iREGCNT_XMM];
// --------------------------------------------------------------------------------------
// __tls_emit / x86EMIT_MULTITHREADED
// --------------------------------------------------------------------------------------
// Multithreaded support for the x86 emitter. (defaults to 0)
// To enable the multithreaded emitter, either set the below define to 1, or set the define
// as a project option. The multithreaded emitter relies on native compiler support for
// TLS -- Macs are crap out of luck there (for now).
#ifndef x86EMIT_MULTITHREADED
# define x86EMIT_MULTITHREADED 0
#endif
#ifndef __tls_emit
# if x86EMIT_MULTITHREADED && PCSX2_THREAD_LOCAL
# define __tls_emit __threadlocal
# else
// Using TlsVariable is sub-optimal and could result in huge executables, so we
// force-disable TLS entirely, and disallow running multithreaded recompilation
// components within PCSX2 manually.
# define __tls_emit
# endif
#endif
extern __tls_emit u8* x86Ptr;
extern __tls_emit XMMSSEType g_xmmtypes[iREGCNT_XMM];
namespace x86Emitter
{

View File

@ -15,9 +15,17 @@
#include "PrecompiledHeader.h"
#include "Threading.h"
#include "TlsVariable.inl"
using namespace Threading;
// thread-local console indentation setting.
static DeclareTls(int) conlog_Indent( 0 );
// thread-local console color storage.
static DeclareTls(ConsoleColors) conlog_Color( DefaultConsoleColor );
static wxString m_buffer; // used by ConsoleBuffer
static Mutex m_bufferlock; // used by ConsoleBuffer
@ -364,12 +372,6 @@ static void format_that_unicode_mess( SafeArray<wxChar>& buffer, const wxChar* f
// though it'd be kinda nice if we did.
}
// thread-local console indentation setting.
static __threadlocal int conlog_Indent = 0;
// thread-local console color storage.
static __threadlocal ConsoleColors conlog_Color = DefaultConsoleColor;
static wxString ascii_format_string(const char* fmt, va_list argptr)
{
if( ascii_buffer_is_deleted )

View File

@ -17,6 +17,7 @@
#include <wx/app.h>
#include "Threading.h"
#include "TlsVariable.inl"
wxString GetEnglish( const char* msg )
{
@ -41,7 +42,7 @@ wxString GetTranslation( const char* msg )
// Using a threadlocal assertion guard. Separate threads can assert at the same time.
// That's ok. What we don't want is the *same* thread recurse-asserting.
static __threadlocal int s_assert_guard = 0;
static DeclareTls(int) s_assert_guard( 0 );
pxDoAssertFnType* pxDoAssert = pxAssertImpl_LogIt;

View File

@ -37,8 +37,6 @@ const wxTimeSpan Threading::def_yieldgui_interval( 0, 0, 0, 100 );
// three second interval for deadlock protection on waitgui.
const wxTimeSpan Threading::def_deadlock_timeout( 0, 0, 3, 0 );
//static __threadlocal PersistentThread* tls_current_thread = NULL;
static pthread_key_t curthread_key = NULL;
static s32 total_key_count = 0;
static Mutex total_key_lock;

View File

@ -36,6 +36,13 @@ void CountLogicalCores( int LogicalCoresPerPhysicalCPU, int PhysicalCoresPerPhys
}
}
bool CanEmitShit()
{
// In Linux I'm pretty sure TLS always works, none of the funny business that Windows
// has involving DLLs. >_<
return true;
}
bool CanTestInstructionSets()
{
// Not implemented yet for linux. (see cpudetect_internal.h for details)

View File

@ -49,15 +49,29 @@ bool _test_instruction( void* pfnCall )
u128 regsave;
((void (__fastcall *)(void*))pfnCall)( &regsave );
}
__except(EXCEPTION_EXECUTE_HANDLER) {
return false;
__except(EXCEPTION_EXECUTE_HANDLER) { return false; }
return true;
}
bool CanEmitShit()
{
// Under Windows, pre 0.9.6 versions of PCSX2 may not initialize the TLS
// register (FS register), so plugins (DLLs) using our x86emitter in multithreaded
// mode will just crash/fail if it tries to do the instruction set tests.
#if x86EMIT_MULTITHREADED
static __threadlocal int tls_failcheck;
__try { tls_failcheck = 1; }
__except(EXCEPTION_EXECUTE_HANDLER) { return false; }
#endif
return true;
}
bool CanTestInstructionSets()
{
return true;
return CanEmitShit();
}
SingleCoreAffinity::SingleCoreAffinity()

View File

@ -96,6 +96,8 @@ void EstablishMXCSRmask()
MXCSR_Mask.bitmask = 0xFFFF; // SSE2 features added
}
if( !CanEmitShit() ) return;
// the fxsave buffer must be 16-byte aligned to avoid GPF. I just save it to an
// unused portion of recSSE, since it has plenty of room to spare.

View File

@ -51,5 +51,6 @@ public:
// This secondary test fixes such cases (although apparently a CMOS reset does as well).
//
extern bool CanEmitShit();
extern bool CanTestInstructionSets();
extern bool _test_instruction( void* pfnCall );

View File

@ -64,9 +64,8 @@
//
__threadlocal u8 *x86Ptr;
__threadlocal XMMSSEType g_xmmtypes[iREGCNT_XMM] = { XMMT_INT };
__tls_emit u8* x86Ptr;
__tls_emit XMMSSEType g_xmmtypes[iREGCNT_XMM] = { XMMT_INT };
namespace x86Emitter {

View File

@ -82,14 +82,6 @@ extern bool renderswitch;
std::list<uint> ringposStack;
#endif
static __threadlocal SysMtgsThread* tls_mtgsThread = NULL;
SysMtgsThread& SysMtgsThread::Get()
{
pxAssertMsg( tls_mtgsThread != NULL, L"This function must be called from the context of a running SysMtgsThread." );
return *tls_mtgsThread;
}
SysMtgsThread::SysMtgsThread() :
SysThreadBase()
#ifdef RINGBUF_DEBUG_STACK
@ -268,8 +260,6 @@ public:
void SysMtgsThread::ExecuteTaskInThread()
{
tls_mtgsThread = this;
#ifdef RINGBUF_DEBUG_STACK
PacketTagType prevCmd;
#endif
@ -513,7 +503,6 @@ void SysMtgsThread::OnResumeInThread( bool isSuspended )
void SysMtgsThread::OnCleanupInThread()
{
ClosePlugin();
tls_mtgsThread = NULL;
_parent::OnCleanupInThread();
}

View File

@ -53,8 +53,6 @@
// might as well add them here)
#include <stddef.h>
#include <malloc.h>
#include <assert.h>
#include <sys/stat.h>
#include <pthread.h>

View File

@ -23,6 +23,7 @@
#include "PageFaultSource.h"
#include "SysThreads.h"
#include "Utilities/TlsVariable.inl"
#ifdef __WXMSW__
# include <wx/msw/wrapwin.h>
@ -30,7 +31,7 @@
#include <xmmintrin.h>
static __threadlocal SysCoreThread* tls_coreThread = NULL;
static DeclareTls(SysCoreThread*) tls_coreThread( NULL );
// --------------------------------------------------------------------------------------
// SysCoreThread *External Thread* Implementations

View File

@ -15,6 +15,7 @@
#include "PrecompiledHeader.h"
#include "App.h"
#include "Utilities/TlsVariable.inl"
#include <wx/stackwalk.h>
@ -105,7 +106,7 @@ static wxString pxGetStackTrace( const FnChar_t* calledFrom )
#ifdef __WXDEBUG__
static __threadlocal int _reentrant_lock = 0;
static TlsVariable< int > _reentrant_lock( 0 );
// This override of wx's implementation provides thread safe assertion message reporting. If we aren't
// on the main gui thread then the assertion message box needs to be passed off to the main gui thread

View File

@ -518,6 +518,16 @@ Pcsx2App::Pcsx2App()
SetAppName( L"pcsx2" );
BuildCommandHash();
#ifdef __WXMSW__
// This variable assignment ensures that MSVC links in the TLS setup stubs even in
// full optimization builds. Without it, DLLs that use TLS won't work because the
// FS segment register won't have been initialized by the main exe, due to tls_insurance
// being optimized away >_< --air
static __threadlocal int tls_insurance = 0;
tls_insurance = 1;
#endif
}
Pcsx2App::~Pcsx2App()

View File

@ -22,8 +22,8 @@
#include "VU.h"
#include "R3000A.h"
__threadlocal u8 *j8Ptr[32];
__threadlocal u32 *j32Ptr[32];
__tls_emit u8 *j8Ptr[32];
__tls_emit u32 *j32Ptr[32];
u16 g_x86AllocCounter = 0;
u16 g_xmmAllocCounter = 0;

View File

@ -265,8 +265,8 @@ extern u32 g_cpuRegHasSignExt, g_cpuPrevRegHasSignExt;
extern _xmmregs xmmregs[iREGCNT_XMM], s_saveXMMregs[iREGCNT_XMM];
extern __threadlocal u8 *j8Ptr[32]; // depreciated item. use local u8* vars instead.
extern __threadlocal u32 *j32Ptr[32]; // depreciated item. use local u32* vars instead.
extern __tls_emit u8 *j8Ptr[32]; // depreciated item. use local u8* vars instead.
extern __tls_emit u32 *j32Ptr[32]; // depreciated item. use local u32* vars instead.
extern u16 g_x86AllocCounter;
extern u16 g_xmmAllocCounter;