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

@ -1,205 +1,206 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="Utilities" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="../../../deps/debug/libUtilities" prefix_auto="1" extension_auto="1" />
<Option working_dir="" />
<Option object_output="./.objs/debug/" />
<Option type="2" />
<Option compiler="gcc" />
<Option createDefFile="1" />
<Compiler>
<Add option="-W" />
<Add option="-g" />
<Add option="`wx-config --version=2.8 --static=no --unicode=yes --cflags`" />
<Add option="-DPCSX2_DEBUG" />
<Add option="-DPCSX2_DEVBUILD" />
</Compiler>
</Target>
<Target title="Devel">
<Option output="../../../deps/devel/libUtilities" prefix_auto="1" extension_auto="1" />
<Option working_dir="" />
<Option object_output="./.objs/devel/" />
<Option type="2" />
<Option compiler="gcc" />
<Option createDefFile="1" />
<Compiler>
<Add option="`wx-config --version=2.8 --static=no --unicode=yes --cflags`" />
<Add option="-fdefer-pop" />
<Add option="-fcprop-registers" />
<Add option="-fif-conversion" />
<Add option="-fif-conversion2" />
<Add option="-ftree-ccp" />
<Add option="-ftree-dce" />
<Add option="-ftree-dominator-opts" />
<Add option="-ftree-ter" />
<Add option="-ftree-lrs" />
<Add option="-ftree-sra" />
<Add option="-ftree-copyrename" />
<Add option="-ftree-fre" />
<Add option="-ftree-ch" />
<Add option="-funit-at-a-time" />
<Add option="-fmerge-constants" />
<Add option="-fthread-jumps" />
<Add option="-fcrossjumping" />
<Add option="-foptimize-sibling-calls" />
<Add option="-fcse-follow-jumps" />
<Add option="-fcse-skip-blocks" />
<Add option="-fgcse -fgcse-lm" />
<Add option="-frerun-cse-after-loop" />
<Add option="-fcaller-saves" />
<Add option="-fpeephole2" />
<Add option="-fsched-interblock -fsched-spec" />
<Add option="-fregmove" />
<Add option="-fstrict-overflow" />
<Add option="-fdelete-null-pointer-checks" />
<Add option="-freorder-blocks -freorder-functions" />
<Add option="-falign-functions -falign-jumps" />
<Add option="-falign-loops -falign-labels" />
<Add option="-ftree-vrp" />
<Add option="-ftree-pre" />
<Add option="-DPCSX2_DEVBUILD" />
<Add option="-DPCSX2_DEVEL" />
<Add directory="../../include" />
<Add directory="../../include/Utilities" />
</Compiler>
</Target>
<Target title="Release">
<Option output="../../../deps/release/libUtilities" prefix_auto="1" extension_auto="1" />
<Option working_dir="" />
<Option object_output="./.objs/release/" />
<Option type="2" />
<Option compiler="gcc" />
<Option createDefFile="1" />
<Compiler>
<Add option="`wx-config --version=2.8 --static=no --unicode=yes --cflags`" />
<Add option="-fdefer-pop" />
<Add option="-fcprop-registers" />
<Add option="-fif-conversion" />
<Add option="-fif-conversion2" />
<Add option="-ftree-ccp" />
<Add option="-ftree-dce" />
<Add option="-ftree-dominator-opts" />
<Add option="-ftree-ter" />
<Add option="-ftree-lrs" />
<Add option="-ftree-sra" />
<Add option="-ftree-copyrename" />
<Add option="-ftree-fre" />
<Add option="-ftree-ch" />
<Add option="-funit-at-a-time" />
<Add option="-fmerge-constants" />
<Add option="-fthread-jumps" />
<Add option="-fcrossjumping" />
<Add option="-foptimize-sibling-calls" />
<Add option="-fcse-follow-jumps" />
<Add option="-fcse-skip-blocks" />
<Add option="-fgcse -fgcse-lm" />
<Add option="-frerun-cse-after-loop" />
<Add option="-fcaller-saves" />
<Add option="-fpeephole2" />
<Add option="-fsched-interblock -fsched-spec" />
<Add option="-fregmove" />
<Add option="-fstrict-overflow" />
<Add option="-fdelete-null-pointer-checks" />
<Add option="-freorder-blocks -freorder-functions" />
<Add option="-falign-functions -falign-jumps" />
<Add option="-falign-loops -falign-labels" />
<Add option="-ftree-vrp" />
<Add option="-ftree-pre" />
<Add directory="../../include" />
<Add directory="../../include/Utilities" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-march=i486" />
<Add option="-Wno-format" />
<Add option="-Wno-unused-parameter" />
<Add option="-Wno-unused-value" />
<Add option="-Wunused-variable" />
<Add option="-fno-guess-branch-probability" />
<Add option="-fno-dse" />
<Add option="-fno-tree-dse" />
<Add option="-fno-strict-aliasing" />
<Add option="-pipe -msse -msse2" />
<Add option="-m32" />
<Add directory="../../include/Utilities" />
<Add directory="../../include" />
<Add directory="../../../3rdparty" />
</Compiler>
<Unit filename="../../../3rdparty/google/dense_hash_map" />
<Unit filename="../../../3rdparty/google/dense_hash_set" />
<Unit filename="../../../3rdparty/google/sparse_hash_map" />
<Unit filename="../../../3rdparty/google/sparse_hash_set" />
<Unit filename="../../../3rdparty/google/sparsehash/densehashtable.h" />
<Unit filename="../../../3rdparty/google/sparsehash/sparseconfig.h" />
<Unit filename="../../../3rdparty/google/sparsehash/sparsehashtable.h" />
<Unit filename="../../../3rdparty/google/sparsetable" />
<Unit filename="../../../3rdparty/google/type_traits.h" />
<Unit filename="../../include/Utilities/Assertions.h" />
<Unit filename="../../include/Utilities/CheckedStaticBox.h" />
<Unit filename="../../include/Utilities/Console.h" />
<Unit filename="../../include/Utilities/Dependencies.h" />
<Unit filename="../../include/Utilities/EventSource.h" />
<Unit filename="../../include/Utilities/EventSource.inl" />
<Unit filename="../../include/Utilities/Exceptions.h" />
<Unit filename="../../include/Utilities/FixedPointTypes.h" />
<Unit filename="../../include/Utilities/General.h" />
<Unit filename="../../include/Utilities/HashMap.h" />
<Unit filename="../../include/Utilities/MemcpyFast.h" />
<Unit filename="../../include/Utilities/Path.h" />
<Unit filename="../../include/Utilities/RedtapeWindows.h" />
<Unit filename="../../include/Utilities/SafeArray.h" />
<Unit filename="../../include/Utilities/ScopedPtr.h" />
<Unit filename="../../include/Utilities/StringHelpers.h" />
<Unit filename="../../include/Utilities/Threading.h" />
<Unit filename="../../include/Utilities/lnx_memzero.h" />
<Unit filename="../../include/Utilities/pxCheckBox.h" />
<Unit filename="../../include/Utilities/pxRadioPanel.h" />
<Unit filename="../../include/Utilities/pxStaticText.h" />
<Unit filename="../../include/Utilities/win_memzero.h" />
<Unit filename="../../include/Utilities/wxBaseTools.h" />
<Unit filename="../../include/Utilities/wxGuiTools.h" />
<Unit filename="../../include/Utilities/wxHelpers.h" />
<Unit filename="../../include/intrin_x86.h" />
<Unit filename="../../src/Utilities/AlignedMalloc.cpp" />
<Unit filename="../../src/Utilities/CheckedStaticBox.cpp" />
<Unit filename="../../src/Utilities/Console.cpp" />
<Unit filename="../../src/Utilities/EventSource.cpp" />
<Unit filename="../../src/Utilities/Exceptions.cpp" />
<Unit filename="../../src/Utilities/HashTools.cpp" />
<Unit filename="../../src/Utilities/Linux/LnxHostSys.cpp" />
<Unit filename="../../src/Utilities/Linux/LnxMisc.cpp" />
<Unit filename="../../src/Utilities/Linux/LnxThreads.cpp" />
<Unit filename="../../src/Utilities/Mutex.cpp" />
<Unit filename="../../src/Utilities/PathUtils.cpp" />
<Unit filename="../../src/Utilities/PrecompiledHeader.cpp" />
<Unit filename="../../src/Utilities/PrecompiledHeader.h" />
<Unit filename="../../src/Utilities/Semaphore.cpp" />
<Unit filename="../../src/Utilities/StringHelpers.cpp" />
<Unit filename="../../src/Utilities/ThreadTools.cpp" />
<Unit filename="../../src/Utilities/ThreadingInternal.h" />
<Unit filename="../../src/Utilities/pxCheckBox.cpp" />
<Unit filename="../../src/Utilities/pxRadioPanel.cpp" />
<Unit filename="../../src/Utilities/pxStaticText.cpp" />
<Unit filename="../../src/Utilities/vssprintf.cpp" />
<Unit filename="../../src/Utilities/wxGuiTools.cpp" />
<Unit filename="../../src/Utilities/wxHelpers.cpp" />
<Unit filename="../../src/Utilities/x86/MemcpyFast.S" />
<Extensions>
<envvars />
<code_completion>
<search_path add="/usr/include/wx-2.8" />
</code_completion>
<debugger />
</Extensions>
</Project>
</CodeBlocks_project_file>
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="Utilities" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="../../../deps/debug/libUtilities" prefix_auto="1" extension_auto="1" />
<Option working_dir="" />
<Option object_output="./.objs/debug/" />
<Option type="2" />
<Option compiler="gcc" />
<Option createDefFile="1" />
<Compiler>
<Add option="-W" />
<Add option="-g" />
<Add option="`wx-config --version=2.8 --static=no --unicode=yes --cflags`" />
<Add option="-DPCSX2_DEBUG" />
<Add option="-DPCSX2_DEVBUILD" />
</Compiler>
</Target>
<Target title="Devel">
<Option output="../../../deps/devel/libUtilities" prefix_auto="1" extension_auto="1" />
<Option working_dir="" />
<Option object_output="./.objs/devel/" />
<Option type="2" />
<Option compiler="gcc" />
<Option createDefFile="1" />
<Compiler>
<Add option="`wx-config --version=2.8 --static=no --unicode=yes --cflags`" />
<Add option="-fdefer-pop" />
<Add option="-fcprop-registers" />
<Add option="-fif-conversion" />
<Add option="-fif-conversion2" />
<Add option="-ftree-ccp" />
<Add option="-ftree-dce" />
<Add option="-ftree-dominator-opts" />
<Add option="-ftree-ter" />
<Add option="-ftree-lrs" />
<Add option="-ftree-sra" />
<Add option="-ftree-copyrename" />
<Add option="-ftree-fre" />
<Add option="-ftree-ch" />
<Add option="-funit-at-a-time" />
<Add option="-fmerge-constants" />
<Add option="-fthread-jumps" />
<Add option="-fcrossjumping" />
<Add option="-foptimize-sibling-calls" />
<Add option="-fcse-follow-jumps" />
<Add option="-fcse-skip-blocks" />
<Add option="-fgcse -fgcse-lm" />
<Add option="-frerun-cse-after-loop" />
<Add option="-fcaller-saves" />
<Add option="-fpeephole2" />
<Add option="-fsched-interblock -fsched-spec" />
<Add option="-fregmove" />
<Add option="-fstrict-overflow" />
<Add option="-fdelete-null-pointer-checks" />
<Add option="-freorder-blocks -freorder-functions" />
<Add option="-falign-functions -falign-jumps" />
<Add option="-falign-loops -falign-labels" />
<Add option="-ftree-vrp" />
<Add option="-ftree-pre" />
<Add option="-DPCSX2_DEVBUILD" />
<Add option="-DPCSX2_DEVEL" />
<Add directory="../../include" />
<Add directory="../../include/Utilities" />
</Compiler>
</Target>
<Target title="Release">
<Option output="../../../deps/release/libUtilities" prefix_auto="1" extension_auto="1" />
<Option working_dir="" />
<Option object_output="./.objs/release/" />
<Option type="2" />
<Option compiler="gcc" />
<Option createDefFile="1" />
<Compiler>
<Add option="`wx-config --version=2.8 --static=no --unicode=yes --cflags`" />
<Add option="-fdefer-pop" />
<Add option="-fcprop-registers" />
<Add option="-fif-conversion" />
<Add option="-fif-conversion2" />
<Add option="-ftree-ccp" />
<Add option="-ftree-dce" />
<Add option="-ftree-dominator-opts" />
<Add option="-ftree-ter" />
<Add option="-ftree-lrs" />
<Add option="-ftree-sra" />
<Add option="-ftree-copyrename" />
<Add option="-ftree-fre" />
<Add option="-ftree-ch" />
<Add option="-funit-at-a-time" />
<Add option="-fmerge-constants" />
<Add option="-fthread-jumps" />
<Add option="-fcrossjumping" />
<Add option="-foptimize-sibling-calls" />
<Add option="-fcse-follow-jumps" />
<Add option="-fcse-skip-blocks" />
<Add option="-fgcse -fgcse-lm" />
<Add option="-frerun-cse-after-loop" />
<Add option="-fcaller-saves" />
<Add option="-fpeephole2" />
<Add option="-fsched-interblock -fsched-spec" />
<Add option="-fregmove" />
<Add option="-fstrict-overflow" />
<Add option="-fdelete-null-pointer-checks" />
<Add option="-freorder-blocks -freorder-functions" />
<Add option="-falign-functions -falign-jumps" />
<Add option="-falign-loops -falign-labels" />
<Add option="-ftree-vrp" />
<Add option="-ftree-pre" />
<Add directory="../../include" />
<Add directory="../../include/Utilities" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-march=i486" />
<Add option="-Wno-format" />
<Add option="-Wno-unused-parameter" />
<Add option="-Wno-unused-value" />
<Add option="-Wunused-variable" />
<Add option="-fno-guess-branch-probability" />
<Add option="-fno-dse" />
<Add option="-fno-tree-dse" />
<Add option="-fno-strict-aliasing" />
<Add option="-pipe -msse -msse2" />
<Add option="-m32" />
<Add directory="../../include/Utilities" />
<Add directory="../../include" />
<Add directory="../../../3rdparty" />
</Compiler>
<Unit filename="../../../3rdparty/google/dense_hash_map" />
<Unit filename="../../../3rdparty/google/dense_hash_set" />
<Unit filename="../../../3rdparty/google/sparse_hash_map" />
<Unit filename="../../../3rdparty/google/sparse_hash_set" />
<Unit filename="../../../3rdparty/google/sparsehash/densehashtable.h" />
<Unit filename="../../../3rdparty/google/sparsehash/sparseconfig.h" />
<Unit filename="../../../3rdparty/google/sparsehash/sparsehashtable.h" />
<Unit filename="../../../3rdparty/google/sparsetable" />
<Unit filename="../../../3rdparty/google/type_traits.h" />
<Unit filename="../../include/Utilities/Assertions.h" />
<Unit filename="../../include/Utilities/CheckedStaticBox.h" />
<Unit filename="../../include/Utilities/Console.h" />
<Unit filename="../../include/Utilities/Dependencies.h" />
<Unit filename="../../include/Utilities/EventSource.h" />
<Unit filename="../../include/Utilities/EventSource.inl" />
<Unit filename="../../include/Utilities/Exceptions.h" />
<Unit filename="../../include/Utilities/FixedPointTypes.h" />
<Unit filename="../../include/Utilities/General.h" />
<Unit filename="../../include/Utilities/HashMap.h" />
<Unit filename="../../include/Utilities/MemcpyFast.h" />
<Unit filename="../../include/Utilities/Path.h" />
<Unit filename="../../include/Utilities/RedtapeWindows.h" />
<Unit filename="../../include/Utilities/SafeArray.h" />
<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" />
<Unit filename="../../include/Utilities/pxStaticText.h" />
<Unit filename="../../include/Utilities/win_memzero.h" />
<Unit filename="../../include/Utilities/wxBaseTools.h" />
<Unit filename="../../include/Utilities/wxGuiTools.h" />
<Unit filename="../../include/Utilities/wxHelpers.h" />
<Unit filename="../../include/intrin_x86.h" />
<Unit filename="../../src/Utilities/AlignedMalloc.cpp" />
<Unit filename="../../src/Utilities/CheckedStaticBox.cpp" />
<Unit filename="../../src/Utilities/Console.cpp" />
<Unit filename="../../src/Utilities/EventSource.cpp" />
<Unit filename="../../src/Utilities/Exceptions.cpp" />
<Unit filename="../../src/Utilities/HashTools.cpp" />
<Unit filename="../../src/Utilities/Linux/LnxHostSys.cpp" />
<Unit filename="../../src/Utilities/Linux/LnxMisc.cpp" />
<Unit filename="../../src/Utilities/Linux/LnxThreads.cpp" />
<Unit filename="../../src/Utilities/Mutex.cpp" />
<Unit filename="../../src/Utilities/PathUtils.cpp" />
<Unit filename="../../src/Utilities/PrecompiledHeader.cpp" />
<Unit filename="../../src/Utilities/PrecompiledHeader.h" />
<Unit filename="../../src/Utilities/Semaphore.cpp" />
<Unit filename="../../src/Utilities/StringHelpers.cpp" />
<Unit filename="../../src/Utilities/ThreadTools.cpp" />
<Unit filename="../../src/Utilities/ThreadingInternal.h" />
<Unit filename="../../src/Utilities/pxCheckBox.cpp" />
<Unit filename="../../src/Utilities/pxRadioPanel.cpp" />
<Unit filename="../../src/Utilities/pxStaticText.cpp" />
<Unit filename="../../src/Utilities/vssprintf.cpp" />
<Unit filename="../../src/Utilities/wxGuiTools.cpp" />
<Unit filename="../../src/Utilities/wxHelpers.cpp" />
<Unit filename="../../src/Utilities/x86/MemcpyFast.S" />
<Extensions>
<envvars />
<code_completion>
<search_path add="/usr/include/wx-2.8" />
</code_completion>
<debugger />
</Extensions>
</Project>
</CodeBlocks_project_file>

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;