properly support POSIX threads kthx
git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@1838 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
parent
96f9e7a82e
commit
969496d93b
|
@ -15,19 +15,10 @@
|
||||||
// Official SVN repository and contact information can be found at
|
// Official SVN repository and contact information can be found at
|
||||||
// http://code.google.com/p/dolphin-emu/
|
// http://code.google.com/p/dolphin-emu/
|
||||||
|
|
||||||
#include "Common.h"
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
#include <windows.h>
|
|
||||||
#elif __GNUC__
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <pthread.h>
|
|
||||||
#else
|
|
||||||
#error unsupported platform
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "Thread.h"
|
#include "Thread.h"
|
||||||
|
|
||||||
|
#define THREAD_DEBUG 1
|
||||||
|
|
||||||
namespace Common
|
namespace Common
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
|
@ -205,10 +196,13 @@ LONG SyncInterlockedExchange(LONG *Dest, LONG Val)
|
||||||
return InterlockedExchange(Dest, Val);
|
return InterlockedExchange(Dest, Val);
|
||||||
}
|
}
|
||||||
|
|
||||||
#elif __GNUC__
|
#else // !WIN32, so must be POSIX threads
|
||||||
|
|
||||||
|
pthread_key_t threadname_key;
|
||||||
|
|
||||||
CriticalSection::CriticalSection(int spincount_unused)
|
CriticalSection::CriticalSection(int spincount_unused)
|
||||||
{
|
{
|
||||||
pthread_mutex_init(&mutex, 0);
|
pthread_mutex_init(&mutex, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -220,7 +214,9 @@ CriticalSection::~CriticalSection()
|
||||||
|
|
||||||
void CriticalSection::Enter()
|
void CriticalSection::Enter()
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(&mutex);
|
int ret = pthread_mutex_lock(&mutex);
|
||||||
|
if (ret) fprintf(stderr, "%s: pthread_mutex_lock(%p) failed: %s\n",
|
||||||
|
__FUNCTION__, &mutex, strerror(ret));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -232,7 +228,9 @@ bool CriticalSection::TryEnter()
|
||||||
|
|
||||||
void CriticalSection::Leave()
|
void CriticalSection::Leave()
|
||||||
{
|
{
|
||||||
pthread_mutex_unlock(&mutex);
|
int ret = pthread_mutex_unlock(&mutex);
|
||||||
|
if (ret) fprintf(stderr, "%s: pthread_mutex_unlock(%p) failed: %s\n",
|
||||||
|
__FUNCTION__, &mutex, strerror(ret));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -242,7 +240,13 @@ Thread::Thread(ThreadFunc function, void* arg)
|
||||||
pthread_attr_t attr;
|
pthread_attr_t attr;
|
||||||
pthread_attr_init(&attr);
|
pthread_attr_init(&attr);
|
||||||
pthread_attr_setstacksize(&attr, 1024 * 1024);
|
pthread_attr_setstacksize(&attr, 1024 * 1024);
|
||||||
pthread_create(&thread_id, &attr, function, arg);
|
int ret = pthread_create(&thread_id, &attr, function, arg);
|
||||||
|
if (ret) fprintf(stderr, "%s: pthread_create(%p, %p, %p, %p) failed: %s\n",
|
||||||
|
__FUNCTION__, &thread_id, &attr, function, arg, strerror(ret));
|
||||||
|
|
||||||
|
#ifdef THREAD_DEBUG
|
||||||
|
fprintf(stderr, "created new thread %d (func=%p, arg=%p)\n", thread_id, function, arg);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -257,9 +261,10 @@ void Thread::WaitForDeath()
|
||||||
if (thread_id)
|
if (thread_id)
|
||||||
{
|
{
|
||||||
void* exit_status;
|
void* exit_status;
|
||||||
pthread_join(thread_id, &exit_status);
|
int ret = pthread_join(thread_id, &exit_status);
|
||||||
if (exit_status)
|
if (ret) fprintf(stderr, "error joining thread %d: %s\n", thread_id, strerror(ret));
|
||||||
fprintf(stderr, "error %d joining thread\n", *(int *)exit_status);
|
if (exit_status)
|
||||||
|
fprintf(stderr, "thread %d exited with status %d\n", thread_id, *(int *)exit_status);
|
||||||
thread_id = 0;
|
thread_id = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -297,6 +302,15 @@ void Thread::SetCurrentThreadAffinity(int mask)
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void InitThreading() {
|
||||||
|
static int thread_init_done = 0;
|
||||||
|
if (thread_init_done) return;
|
||||||
|
thread_init_done++;
|
||||||
|
|
||||||
|
if (pthread_key_create(&threadname_key, NULL/*free*/) != 0)
|
||||||
|
perror("Unable to create thread name key: ");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
void SleepCurrentThread(int ms)
|
void SleepCurrentThread(int ms)
|
||||||
{
|
{
|
||||||
|
@ -306,7 +320,10 @@ void SleepCurrentThread(int ms)
|
||||||
|
|
||||||
void SetCurrentThreadName(const TCHAR* szThreadName)
|
void SetCurrentThreadName(const TCHAR* szThreadName)
|
||||||
{
|
{
|
||||||
// noop
|
pthread_setspecific(threadname_key, strdup(szThreadName));
|
||||||
|
#ifdef THREAD_DEBUG
|
||||||
|
fprintf(stderr, "%s(%s)\n", __FUNCTION__, szThreadName);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -18,28 +18,27 @@
|
||||||
#ifndef _THREAD_H
|
#ifndef _THREAD_H
|
||||||
#define _THREAD_H
|
#define _THREAD_H
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
#include <windows.h>
|
|
||||||
#else
|
|
||||||
#include <pthread.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#define THREAD_RETURN DWORD WINAPI
|
#define THREAD_RETURN DWORD WINAPI
|
||||||
#else
|
#else
|
||||||
#define THREAD_RETURN void*
|
#define THREAD_RETURN void*
|
||||||
|
#include <unistd.h>
|
||||||
|
#ifdef _POSIX_THREADS
|
||||||
|
#include <pthread.h>
|
||||||
|
#else
|
||||||
|
#error unsupported platform (no pthreads?)
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "Common.h"
|
#include "Common.h"
|
||||||
|
|
||||||
|
|
||||||
namespace Common
|
namespace Common
|
||||||
{
|
{
|
||||||
class CriticalSection
|
class CriticalSection
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
CRITICAL_SECTION section;
|
CRITICAL_SECTION section;
|
||||||
#elif __GNUC__
|
#else
|
||||||
pthread_mutex_t mutex;
|
pthread_mutex_t mutex;
|
||||||
#endif
|
#endif
|
||||||
public:
|
public:
|
||||||
|
@ -53,7 +52,7 @@ public:
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
typedef DWORD (WINAPI * ThreadFunc)(void* arg);
|
typedef DWORD (WINAPI * ThreadFunc)(void* arg);
|
||||||
#elif __GNUC__
|
#else
|
||||||
typedef void* (*ThreadFunc)(void* arg);
|
typedef void* (*ThreadFunc)(void* arg);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -73,7 +72,7 @@ private:
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
HANDLE m_hThread;
|
HANDLE m_hThread;
|
||||||
DWORD m_threadId;
|
DWORD m_threadId;
|
||||||
#elif __GNUC__
|
#else
|
||||||
pthread_t thread_id;
|
pthread_t thread_id;
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
@ -96,13 +95,14 @@ class Event
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
HANDLE m_hEvent;
|
HANDLE m_hEvent;
|
||||||
#elif __GNUC__
|
#else
|
||||||
bool is_set_;
|
bool is_set_;
|
||||||
pthread_cond_t event_;
|
pthread_cond_t event_;
|
||||||
pthread_mutex_t mutex_;
|
pthread_mutex_t mutex_;
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void InitThreading(void);
|
||||||
void SleepCurrentThread(int ms);
|
void SleepCurrentThread(int ms);
|
||||||
|
|
||||||
void SetCurrentThreadName(const char *name);
|
void SetCurrentThreadName(const char *name);
|
||||||
|
|
Loading…
Reference in New Issue