Replaced Common::Thread with a partial implementation of std::thread. (rvalue references are used if available, <thread> is used if possible) Eliminates the need to use dynamic memory allocation for threads, so it's impossible to forget to delete a thread or set a pointer to NULL. Enables use of type-safe thread functions, no need to cast to and from void*. I've made sure the code compiles in vs08 and tested the functionality of "StdThread.h" on Linux so I'm hoping everything will work :p. In the future "StdThread.h" can be removed (maybe when OS X ships with gcc 4.4 and vs2015 is released :p).

git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@6933 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
Jordan Woyak
2011-01-27 20:47:58 +00:00
parent 0371f15c23
commit 2c05c49a04
29 changed files with 484 additions and 396 deletions

View File

@ -845,6 +845,10 @@
RelativePath=".\Src\stdafx.h"
>
</File>
<File
RelativePath=".\Src\StdThread.h"
>
</File>
<File
RelativePath=".\Src\StringUtil.cpp"
>

View File

@ -322,6 +322,7 @@
<ClInclude Include="Src\SDCardUtil.h" />
<ClInclude Include="Src\Setup.h" />
<ClInclude Include="Src\stdafx.h" />
<ClInclude Include="Src\StdThread.h" />
<ClInclude Include="Src\StringUtil.h" />
<ClInclude Include="Src\svnrev.h" />
<ClInclude Include="Src\svnrev_template.h" />

View File

@ -0,0 +1,298 @@
#ifndef STD_THREAD_H_
#define STD_THREAD_H_
#define GCC_VER(x,y,z) ((x) * 10000 + (y) * 100 + (z))
#define GCC_VERSION GCC_VER(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__)
#if GCC_VERSION >= GCC_VER(4,4,0) && __GXX_EXPERIMENTAL_CXX0X__
// GCC 4.4 provides <thread>
#include <thread>
#else
// partial std::thread implementation for win32/pthread
#include <algorithm>
#if (_MSC_VER >= 1600) || (GCC_VERSION >= GCC_VER(4,3,0) && __GXX_EXPERIMENTAL_CXX0X__)
#define USE_RVALUE_REFERENCES
#endif
#if defined(_WIN32)
// WIN32
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#if defined(_MSC_VER) && defined(_MT)
// When linking with LIBCMT (the multithreaded C library), Microsoft recommends
// using _beginthreadex instead of CreateThread.
#define USE_BEGINTHREADEX
#include <process.h>
#endif
#ifdef USE_BEGINTHREADEX
#define THREAD_ID unsigned
#define THREAD_RETURN unsigned __stdcall
#else
#define THREAD_ID DWORD
#define THREAD_RETURN DWORD WINAPI
#endif
#define THREAD_HANDLE HANDLE
#else
// PTHREAD
#include <unistd.h>
#ifndef _POSIX_THREADS
#error unsupported platform (no pthreads?)
#endif
#include <pthread.h>
#define THREAD_ID pthread_t
#define THREAD_HANDLE pthread_t
#define THREAD_RETURN void*
#endif
namespace std
{
class thread
{
public:
typedef THREAD_HANDLE native_handle_type;
class id
{
friend class thread;
public:
id() : m_thread(0) {}
id(THREAD_ID _id) : m_thread(_id) {}
bool operator==(const id& rhs) const
{
return m_thread == rhs.m_thread;
}
bool operator!=(const id& rhs) const
{
return !(*this == rhs);
}
bool operator<(const id& rhs) const
{
return m_thread < rhs.m_thread;
}
private:
THREAD_ID m_thread;
};
// no variadic template support in msvc
//template <typename C, typename... A>
//thread(C&& func, A&&... args);
template <typename C>
thread(C func)
{
StartThread(new Func<C>(func));
}
template <typename C, typename A>
thread(C func, A arg)
{
StartThread(new FuncArg<C, A>(func, arg));
}
thread() /*= default;*/ {}
#ifdef USE_RVALUE_REFERENCES
thread(const thread&) /*= delete*/;
thread(thread&& other)
{
#else
thread(const thread& t)
{
// ugly const_cast to get around lack of rvalue references
thread& other = const_cast<thread&>(t);
#endif
swap(other);
}
#ifdef USE_RVALUE_REFERENCES
thread& operator=(const thread&) /*= delete*/;
thread& operator=(thread&& other)
{
#else
thread& operator=(const thread& t)
{
// ugly const_cast to get around lack of rvalue references
thread& other = const_cast<thread&>(t);
#endif
if (joinable())
detach();
swap(other);
return *this;
}
~thread()
{
if (joinable())
detach();
}
bool joinable() const
{
return m_id != id();
}
id get_id() const
{
return m_id;
}
native_handle_type native_handle()
{
#ifdef _WIN32
return m_handle;
#else
return m_id.m_thread;
#endif
}
void join()
{
#ifdef _WIN32
WaitForSingleObject(m_handle, INFINITE);
detach();
#else
pthread_join(m_id.m_thread, NULL);
m_id = id();
#endif
}
void detach()
{
#ifdef _WIN32
CloseHandle(m_handle);
#else
pthread_detach(m_id.m_thread);
#endif
m_id = id();
}
void swap(thread& other)
{
std::swap(m_id, other.m_id);
#ifdef _WIN32
std::swap(m_handle, other.m_handle);
#endif
}
static unsigned hardware_concurrency()
{
#ifdef _WIN32
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
return static_cast<unsigned>(sysinfo.dwNumberOfProcessors);
#else
return 0;
#endif
}
private:
id m_id;
#ifdef _WIN32
native_handle_type m_handle;
#endif
template <typename F>
void StartThread(F* param)
{
#ifdef USE_BEGINTHREADEX
m_handle = (HANDLE)_beginthreadex(NULL, 0, &RunAndDelete<F>, param, 0, &m_id.m_thread);
#elif defined(_WIN32)
m_handle = CreateThread(NULL, 0, &RunAndDelete<F>, param, 0, &m_id.m_thread);
#else
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setstacksize(&attr, 1024 * 1024);
if (pthread_create(&m_id.m_thread, &attr, &RunAndDelete<F>, param))
m_id = id();
#endif
}
template <typename C>
class Func
{
public:
Func(C _func) : func(_func) {}
void Run() { func(); }
private:
C const func;
};
template <typename C, typename A>
class FuncArg
{
public:
FuncArg(C _func, A _arg) : func(_func), arg(_arg) {}
void Run() { func(arg); }
private:
C const func;
A arg;
};
template <typename F>
static THREAD_RETURN RunAndDelete(void* param)
{
static_cast<F*>(param)->Run();
delete static_cast<F*>(param);
return 0;
}
};
namespace this_thread
{
inline void yield()
{
#ifdef _WIN32
Sleep(1);
#else
usleep(1000 * 1);
#endif
}
inline thread::id get_id()
{
#ifdef _WIN32
return GetCurrentThreadId();
#else
return pthread_self();
#endif
}
} // namespace this_thread
} // namespace std
#undef USE_RVALUE_REFERENCES
#undef USE_BEGINTHREADEX
#undef THREAD_ID
#undef THREAD_RETURN
#undef THREAD_HANDLE
#endif
#endif

View File

@ -25,18 +25,28 @@
namespace Common
{
int Thread::CurrentId()
{
int CurrentThreadId()
{
#ifdef _WIN32
return GetCurrentThreadId();
return GetCurrentThreadId();
#else
return 0;
return 0;
#endif
}
}
#ifdef _WIN32
void SetThreadAffinity(std::thread::native_handle_type thread, u32 mask)
{
SetThreadAffinityMask(thread, mask);
}
void SetCurrentThreadAffinity(u32 mask)
{
SetThreadAffinityMask(GetCurrentThread(), mask);
}
CriticalSection::CriticalSection(int spincount)
{
if (spincount)
@ -70,54 +80,6 @@ namespace Common
LeaveCriticalSection(&section);
}
Thread::Thread(ThreadFunc function, void* arg)
: m_hThread(NULL), m_threadId(0)
{
#ifdef USE_BEGINTHREADEX
m_hThread = (HANDLE)_beginthreadex(NULL, 0, function, arg, 0, &m_threadId);
#else
m_hThread = CreateThread(NULL, 0, function, arg, 0, &m_threadId);
#endif
}
Thread::~Thread()
{
WaitForDeath();
}
DWORD Thread::WaitForDeath(const int iWait)
{
if (m_hThread)
{
DWORD Wait = WaitForSingleObject(m_hThread, iWait);
CloseHandle(m_hThread);
m_hThread = NULL;
return Wait;
}
return NULL;
}
void Thread::SetAffinity(int mask)
{
SetThreadAffinityMask(m_hThread, mask);
}
void Thread::SetPriority(int priority)
{
SetThreadPriority(m_hThread, priority);
}
void Thread::SetCurrentThreadAffinity(int mask)
{
SetThreadAffinityMask(GetCurrentThread(), mask);
}
bool Thread::IsCurrentThread()
{
return GetCurrentThreadId() == m_threadId;
}
EventEx::EventEx()
{
InterlockedExchange(&m_Lock, 1);
@ -300,7 +262,32 @@ namespace Common
}
#else // !WIN32, so must be POSIX threads
void LinuxSetThreadAffinity(pthread_t thread, u32 mask)
{
// This is non-standard
#ifdef __linux__
cpu_set_t cpu_set;
CPU_ZERO(&cpu_set);
for (int i = 0; i != sizeof(mask) * 8; ++i)
if ((mask >> i) & 1)
CPU_SET(i, &cpu_set);
pthread_setaffinity_np(thread, sizeof(cpu_set), &cpu_set);
#endif
}
void SetThreadAffinity(std::thread::native_handle_type thread, u32 mask)
{
LinuxSetThreadAffinity(thread, mask);
}
void SetCurrentThreadAffinity(u32 mask)
{
LinuxSetThreadAffinity(pthread_self(), mask);
}
static pthread_key_t threadname_key;
static pthread_once_t threadname_key_once = PTHREAD_ONCE_INIT;
@ -344,82 +331,6 @@ namespace Common
pthread_mutex_unlock(&mutex);
#endif
}
Thread::Thread(ThreadFunc function, void* arg)
: thread_id(0)
{
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setstacksize(&attr, 1024 * 1024);
int ret = pthread_create(&thread_id, &attr, function, arg);
if (ret) ERROR_LOG(COMMON, "%s: pthread_create(%p, %p, %p, %p) failed: %s\n",
__FUNCTION__, &thread_id, &attr, function, arg, strerror(ret));
INFO_LOG(COMMON, "created new thread %lu (func=%p, arg=%p)\n",
(unsigned long)thread_id, function, arg);
}
Thread::~Thread()
{
WaitForDeath();
}
void Thread::WaitForDeath()
{
if (thread_id)
{
void* exit_status;
int ret = pthread_join(thread_id, &exit_status);
if (ret) ERROR_LOG(COMMON,
"error joining thread %lu: %s\n",
(unsigned long)thread_id, strerror(ret));
if (exit_status)
ERROR_LOG(COMMON,
"thread %lu exited with status %d\n",
(unsigned long)thread_id, *(int *)exit_status);
thread_id = 0;
}
}
void Thread::SetAffinity(int mask)
{
// This is non-standard
#ifdef __linux__
cpu_set_t cpu_set;
CPU_ZERO(&cpu_set);
for (unsigned int i = 0; i < sizeof(mask) * 8; i++)
{
if ((mask >> i) & 1){CPU_SET(i, &cpu_set);}
}
pthread_setaffinity_np(thread_id, sizeof(cpu_set), &cpu_set);
#endif
}
void Thread::SetCurrentThreadAffinity(int mask)
{
#ifdef __linux__
cpu_set_t cpu_set;
CPU_ZERO(&cpu_set);
for (size_t i = 0; i < sizeof(mask) * 8; i++)
{
if ((mask >> i) & 1){CPU_SET(i, &cpu_set);}
}
pthread_setaffinity_np(pthread_self(), sizeof(cpu_set), &cpu_set);
#endif
}
bool Thread::IsCurrentThread()
{
return pthread_equal(pthread_self(), thread_id) != 0;
}
void SleepCurrentThread(int ms)
{

View File

@ -18,37 +18,7 @@
#ifndef _THREAD_H_
#define _THREAD_H_
#ifdef _WIN32
#if defined(_MSC_VER) && defined(_MT)
// When linking with LIBCMT (the multithreaded C library), Microsoft recommends
// using _beginthreadex instead of CreateThread.
#define USE_BEGINTHREADEX
#endif
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#ifdef USE_BEGINTHREADEX
#define THREAD_RETURN unsigned __stdcall
#else
#define THREAD_RETURN DWORD WINAPI
#endif
#else
#include <xmmintrin.h>
#define THREAD_RETURN void*
#include <unistd.h>
#ifdef _POSIX_THREADS
#include <pthread.h>
#elif GEKKO
#include <ogc/lwp_threads.h>
#else
#error unsupported platform (no pthreads?)
#endif
#endif
#include "StdThread.h"
// Don't include common.h here as it will break LogManager
#include "CommonTypes.h"
@ -61,6 +31,8 @@
#define INFINITE 0xffffffff
#endif
#include <xmmintrin.h>
//for gettimeofday and struct time(spec|val)
#include <time.h>
#include <sys/time.h>
@ -69,6 +41,11 @@
namespace Common
{
int CurrentThreadId();
void SetThreadAffinity(std::thread::native_handle_type thread, u32 mask);
void SetCurrentThreadAffinity(u32 mask);
class CriticalSection
{
@ -88,57 +65,6 @@ namespace Common
void Leave();
};
#ifdef _WIN32
#ifdef USE_BEGINTHREADEX
typedef unsigned (__stdcall *ThreadFunc)(void* arg);
#else
typedef DWORD (WINAPI *ThreadFunc)(void* arg);
#endif
#else
typedef void* (*ThreadFunc)(void* arg);
#endif
class Thread
{
public:
Thread(ThreadFunc entry, void* arg);
~Thread();
void SetAffinity(int mask);
static void SetCurrentThreadAffinity(int mask);
static int CurrentId();
bool IsCurrentThread();
#ifdef _WIN32
void SetPriority(int priority);
DWORD WaitForDeath(const int iWait = INFINITE);
#else
void WaitForDeath();
#endif
private:
#ifdef _WIN32
HANDLE m_hThread;
#ifdef USE_BEGINTHREADEX
unsigned m_threadId;
#else
DWORD m_threadId;
#endif
#else
#ifdef _POSIX_THREADS
pthread_t thread_id;
#endif
#endif
};
#ifdef _WIN32
// Event(WaitForSingleObject) is too expensive
// as it always enters Ring0 regardless of the state of lock