2010-08-05 12:21:19 +00:00
|
|
|
/* PCSX2 - PS2 Emulator for PCs
|
|
|
|
* Copyright (C) 2002-2010 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
|
2016-11-12 15:28:37 +00:00
|
|
|
#define DeclareTls(x) __threadlocal x
|
2010-08-05 12:21:19 +00:00
|
|
|
#else
|
2016-11-12 15:28:37 +00:00
|
|
|
#define DeclareTls(x) Threading::TlsVariable<x>
|
2010-08-05 12:21:19 +00:00
|
|
|
#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).
|
|
|
|
//
|
2016-11-12 15:28:37 +00:00
|
|
|
template <typename T>
|
2010-08-05 12:21:19 +00:00
|
|
|
class BaseTlsVariable
|
|
|
|
{
|
2016-11-12 15:28:37 +00:00
|
|
|
DeclareNoncopyableObject(BaseTlsVariable<T>);
|
2010-08-05 12:21:19 +00:00
|
|
|
|
|
|
|
protected:
|
2016-11-12 15:28:37 +00:00
|
|
|
pthread_key_t m_thread_key;
|
|
|
|
bool m_IsDisposed;
|
2010-08-05 12:21:19 +00:00
|
|
|
|
|
|
|
public:
|
2016-11-12 15:28:37 +00:00
|
|
|
BaseTlsVariable();
|
2010-08-05 12:21:19 +00:00
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
virtual ~BaseTlsVariable() throw()
|
|
|
|
{
|
|
|
|
Dispose();
|
|
|
|
}
|
2010-08-05 12:21:19 +00:00
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
T *GetPtr() const;
|
|
|
|
T &GetRef() const { return *GetPtr(); }
|
2010-08-05 12:21:19 +00:00
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
operator T &() const { return GetRef(); }
|
|
|
|
T *operator->() const { return GetPtr(); }
|
2010-08-05 12:21:19 +00:00
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
void Dispose()
|
|
|
|
{
|
|
|
|
if (!m_IsDisposed) {
|
|
|
|
m_IsDisposed = true;
|
|
|
|
KillKey();
|
|
|
|
}
|
|
|
|
}
|
2010-08-05 12:21:19 +00:00
|
|
|
|
|
|
|
protected:
|
2016-11-12 15:28:37 +00:00
|
|
|
void CreateKey();
|
|
|
|
void KillKey();
|
|
|
|
|
|
|
|
virtual void CreateInstance(T *result) const
|
|
|
|
{
|
|
|
|
new (result) T();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _aligned_delete_and_free(void *ptr)
|
|
|
|
{
|
|
|
|
if (!ptr)
|
|
|
|
return;
|
|
|
|
((T *)ptr)->~T();
|
|
|
|
_aligned_free(ptr);
|
|
|
|
}
|
2010-08-05 12:21:19 +00:00
|
|
|
};
|
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
template <typename T>
|
2010-08-05 12:21:19 +00:00
|
|
|
class TlsVariable : public BaseTlsVariable<T>
|
|
|
|
{
|
2016-11-12 15:28:37 +00:00
|
|
|
DeclareNoncopyableObject(TlsVariable<T>);
|
2010-08-05 12:21:19 +00:00
|
|
|
|
|
|
|
protected:
|
2016-11-12 15:28:37 +00:00
|
|
|
T m_initval;
|
2010-08-05 12:21:19 +00:00
|
|
|
|
|
|
|
public:
|
2016-11-12 15:28:37 +00:00
|
|
|
TlsVariable() {}
|
|
|
|
TlsVariable(const T &initval)
|
|
|
|
: m_initval(initval)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is needed; The C++ standard likes making life suck for programmers.
|
|
|
|
using BaseTlsVariable<T>::GetRef;
|
|
|
|
|
|
|
|
virtual ~TlsVariable() throw()
|
|
|
|
{
|
|
|
|
// disable the parent cleanup. This leaks memory blocks, but its necessary because
|
|
|
|
// TLS is expected to be persistent until the very end of execution on the main thread.
|
|
|
|
// Killing the pthread_key at all will lead to the console logger death, etc.
|
|
|
|
|
|
|
|
// DON'T REMOVE this->, the "official"[ly stupid] C++ standard requires it because of its
|
|
|
|
// insistence that variables be looked up during initial template parsing and not during
|
|
|
|
// instantiation.
|
|
|
|
this->m_IsDisposed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
TlsVariable<T> &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; }
|
|
|
|
|
2010-08-05 12:21:19 +00:00
|
|
|
protected:
|
2016-11-12 15:28:37 +00:00
|
|
|
virtual void CreateInstance(T *result) const
|
|
|
|
{
|
|
|
|
new (result) T(m_initval);
|
|
|
|
}
|
2010-08-05 12:21:19 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
template <typename T>
|
2010-08-05 12:21:19 +00:00
|
|
|
Threading::BaseTlsVariable<T>::BaseTlsVariable()
|
|
|
|
{
|
2016-11-12 15:28:37 +00:00
|
|
|
m_IsDisposed = false;
|
|
|
|
CreateKey();
|
2010-08-05 12:21:19 +00:00
|
|
|
}
|
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
template <typename T>
|
2010-08-05 12:21:19 +00:00
|
|
|
void Threading::BaseTlsVariable<T>::KillKey()
|
|
|
|
{
|
2016-11-12 15:28:37 +00:00
|
|
|
if (!m_thread_key)
|
|
|
|
return;
|
2010-08-05 12:21:19 +00:00
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
// Delete the handle for the current thread (which should always be the main/UI thread!)
|
|
|
|
// This is needed because pthreads does *not* clean up the dangling objects when you delete
|
|
|
|
// the key. The TLS for the process main thread will only be deleted when the process
|
|
|
|
// ends; which is too damn late (it shows up int he leaked memory blocks).
|
2010-08-05 12:21:19 +00:00
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
BaseTlsVariable<T>::_aligned_delete_and_free(pthread_getspecific(m_thread_key));
|
2010-08-05 12:21:19 +00:00
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
pthread_key_delete(m_thread_key);
|
|
|
|
m_thread_key = 0;
|
2010-08-05 12:21:19 +00:00
|
|
|
}
|
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
template <typename T>
|
|
|
|
T *Threading::BaseTlsVariable<T>::GetPtr() const
|
2010-08-05 12:21:19 +00:00
|
|
|
{
|
2016-11-12 15:28:37 +00:00
|
|
|
T *result = (T *)pthread_getspecific(m_thread_key);
|
|
|
|
if (result == NULL) {
|
|
|
|
pthread_setspecific(m_thread_key, result = (T *)_aligned_malloc(sizeof(T), 16));
|
|
|
|
CreateInstance(result);
|
|
|
|
if (result == NULL)
|
|
|
|
throw Exception::OutOfMemory(L"thread local storage variable instance");
|
|
|
|
}
|
|
|
|
return result;
|
2010-08-05 12:21:19 +00:00
|
|
|
}
|
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
template <typename T>
|
2010-08-05 12:21:19 +00:00
|
|
|
void Threading::BaseTlsVariable<T>::CreateKey()
|
|
|
|
{
|
2016-11-12 15:28:37 +00:00
|
|
|
if (0 != pthread_key_create(&m_thread_key, BaseTlsVariable<T>::_aligned_delete_and_free)) {
|
|
|
|
pxFailRel("Thread Local Storage Error: key creation failed. This will most likely lead to a rapid application crash.");
|
|
|
|
}
|
2010-08-05 12:21:19 +00:00
|
|
|
}
|