Some fixes for the new threading backends

This commit is contained in:
Bart van Strien
2011-06-09 10:56:13 +02:00
parent f9ec21619a
commit 709263b032
9 changed files with 242 additions and 237 deletions
+2 -2
View File
@@ -35,10 +35,10 @@ namespace thread
Thread::ThreadThread::ThreadThread(ThreadData* comm) Thread::ThreadThread::ThreadThread(ThreadData* comm)
: comm(comm) : comm(comm)
{ {
} }
void Thread::ThreadThread::main() { void Thread::ThreadThread::main()
{
lua_State * L = lua_open(); lua_State * L = lua_open();
luaL_openlibs(L); luaL_openlibs(L);
#ifdef LOVE_BUILD_STANDALONE #ifdef LOVE_BUILD_STANDALONE
+64 -85
View File
@@ -24,83 +24,83 @@ namespace love
{ {
namespace thread namespace thread
{ {
Mutex::Mutex()
Mutex::Mutex() { {
pthread_create_mutex(&mutex, NULL); pthread_create_mutex(&mutex, NULL);
} }
Mutex::~Mutex() { Mutex::~Mutex()
{
pthread_mutex_destroy(&mutex); pthread_mutex_destroy(&mutex);
} }
void Mutex::lock() { void Mutex::lock()
{
pthread_mutex_lock(&mutex); pthread_mutex_lock(&mutex);
} }
void Mutex::unlock() { void Mutex::unlock()
{
pthread_mutex_unlock(&mutex); pthread_mutex_unlock(&mutex);
} }
void* ThreadBase::thread_runner(void* param)
{
void* ThreadBase::thread_runner(void* param) {
ThreadBase* thread = (ThreadBase*)param; ThreadBase* thread = (ThreadBase*)param;
thread->main(); thread->main();
return NULL; return NULL;
} }
ThreadBase::ThreadBase()
: running(false)
ThreadBase::ThreadBase() : running(false) { {
pthread_t thread; pthread_t thread;
} }
ThreadBase::~ThreadBase()
{
ThreadBase::~ThreadBase() { if (running)
if (running) { {
wait(); wait();
} }
} }
bool ThreadBase::start()
{
bool ThreadBase::start() { if (pthread_create(&thread, NULL, thread_runner, this))
if (pthread_create(&thread, NULL, thread_runner, this)) {
return false; return false;
} else { return (running = true);
running = true;
return true;
}
} }
void ThreadBase::wait()
void ThreadBase::wait() { {
pthread_join(thread, NULL); pthread_join(thread, NULL);
running = false; running = false;
} }
void ThreadBase::kill()
void ThreadBase::kill() { {
// FIXME: I'm not sure about that one.
pthread_kill(thread, 9); pthread_kill(thread, 9);
running = false; running = false;
} }
unsigned int ThreadBase::threadId() { unsigned int ThreadBase::threadId()
{
return (unsigned int)((size_t)pthread_self()); return (unsigned int)((size_t)pthread_self());
} }
Semaphore::Semaphore(unsigned int initial_value) { Semaphore::Semaphore(unsigned int initial_value)
{
sem_init(&sem, 0, initial_value); sem_init(&sem, 0, initial_value);
} }
Semaphore::~Semaphore() { Semaphore::~Semaphore()
{
sem_destroy(&sem); sem_destroy(&sem);
} }
unsigned int Semaphore::value() { unsigned int Semaphore::value()
{
int val = 0; int val = 0;
if (sem_getvalue(&sem, &val)) { if (sem_getvalue(&sem, &val)) {
return 0; return 0;
@@ -109,71 +109,57 @@ namespace thread
} }
} }
void Semaphore::post() { void Semaphore::post()
{
sem_post(&sem); sem_post(&sem);
} }
bool Semaphore::wait(int timeout) { bool Semaphore::wait(int timeout)
if (timeout < 0) { {
if (sem_wait(&sem)) { if (timeout < 0)
return false; return !sem_wait(&sem);
} else { else if (timeout == 0)
return true; return !sem_trywait(&sem);
} else
} else if (timeout == 0) { {
if (sem_trywait(&sem)) {
return false;
} else {
return true;
}
} else {
struct timespec ts; struct timespec ts;
ts.tv_sec = timeout/1000; ts.tv_sec = timeout/1000;
ts.tv_nsec = (timeout % 1000) * 1000000; ts.tv_nsec = (timeout % 1000) * 1000000;
if (sem_timedwait(&sem, &ts)) { return !sem_timedwait(&sem, &ts);
return false; // either timeout or error...
} else {
return true;
}
} }
} }
bool Semaphore::tryWait()
bool Semaphore::tryWait() { {
if (sem_trywait(&sem)) { return !sem_trywait(&sem);
return false;
} else {
return true;
}
} }
Conditional::Conditional()
{
Conditional::Conditional() {
pthread_cond_init(&cond, NULL); pthread_cond_init(&cond, NULL);
} }
Conditional::~Conditional() { Conditional::~Conditional()
{
pthread_cond_destroy(&cond); pthread_cond_destroy(&cond);
} }
void Conditional::signal() { void Conditional::signal()
{
pthread_cond_signal(&cond); pthread_cond_signal(&cond);
} }
void Conditional::broadcast() { void Conditional::broadcast()
{
pthread_cond_broadcast(&cond); pthread_cond_broadcast(&cond);
} }
bool Conditional::wait(Mutex* mutex, int timeout) { bool Conditional::wait(Mutex* mutex, int timeout)
if (timeout < 0) { {
if (pthread_cond_wait(cond, mutex->mutex)) { if (timeout < 0)
return false; return !pthread_cond_wait(cond, mutex->mutex);
} else { else
return true; {
}
} else {
struct timespec ts; struct timespec ts;
int ret; int ret;
@@ -181,16 +167,9 @@ namespace thread
ts.tv_nsec = (timeout % 1000) * 1000000; ts.tv_nsec = (timeout % 1000) * 1000000;
ret = pthread_cond_timedwait(&cond, mutex->mutex, &ts); ret = pthread_cond_timedwait(&cond, mutex->mutex, &ts);
if (ret == ETIMEDOUT) { return (ret == 0);
return false;
} else if (ret == 0) {
return true;
} else {
// something bad happend!
return false;
}
} }
} }
} // namespace thread } // thread
} // namespace love } // love
+13 -10
View File
@@ -18,8 +18,8 @@
* 3. This notice may not be removed or altered from any source distribution. * 3. This notice may not be removed or altered from any source distribution.
**/ **/
#ifndef LOVE_PLATFORM_POSIX_THREADS_H_ #ifndef LOVE_THREAD_POSIX_THREADS_H
#define LOVE_PLATFORM_POSIX_THREADS_H_ #define LOVE_THREAD_POSIX_THREADS_H
#include <pthread.h> #include <pthread.h>
#include <semaphore.h> #include <semaphore.h>
@@ -28,8 +28,8 @@ namespace love
{ {
namespace thread namespace thread
{ {
class Mutex
class Mutex { {
private: private:
pthread_mutex_t mutex; pthread_mutex_t mutex;
Mutex(const Mutex& mutex) {} Mutex(const Mutex& mutex) {}
@@ -45,7 +45,8 @@ namespace thread
}; };
class ThreadBase { class ThreadBase
{
private: private:
pthread_t thread; pthread_t thread;
ThreadBase(ThreadBase& thread) {} ThreadBase(ThreadBase& thread) {}
@@ -67,7 +68,8 @@ namespace thread
static unsigned int threadId(); static unsigned int threadId();
}; };
class Semaphore { class Semaphore
{
private: private:
Semaphore(const Semaphore& sem) {} Semaphore(const Semaphore& sem) {}
sem_t semaphore; sem_t semaphore;
@@ -83,7 +85,8 @@ namespace thread
}; };
// Should conditional inherit from mutex? // Should conditional inherit from mutex?
class Conditional { class Conditional
{
private: private:
pthread_cond_t cond; pthread_cond_t cond;
@@ -96,8 +99,8 @@ namespace thread
bool wait(Mutex* mutex, int timeout=-1); bool wait(Mutex* mutex, int timeout=-1);
}; };
} // namespace thread } // thread
} // namespace love } // love
#endif // !LOVE_PLATFORM_POSIX_THREADS_H_ #endif // LOVE_THREAD_POSIX_THREADS_H
+61 -65
View File
@@ -24,141 +24,137 @@ namespace love
{ {
namespace thread namespace thread
{ {
Mutex::Mutex()
Mutex::Mutex() { {
mutex = SDL_CreateMutex(); mutex = SDL_CreateMutex();
} }
Mutex::~Mutex() { Mutex::~Mutex()
{
SDL_DestroyMutex(mutex); SDL_DestroyMutex(mutex);
} }
void Mutex::lock() { void Mutex::lock()
{
SDL_mutexP(mutex); SDL_mutexP(mutex);
} }
void Mutex::unlock() { void Mutex::unlock()
{
SDL_mutexV(mutex); SDL_mutexV(mutex);
} }
int ThreadBase::thread_runner(void* param)
{
int ThreadBase::thread_runner(void* param) {
ThreadBase* thread = (ThreadBase*)param; ThreadBase* thread = (ThreadBase*)param;
thread->main(); thread->main();
return 0; return 0;
} }
ThreadBase::ThreadBase() : running(false) { ThreadBase::ThreadBase()
SDL_Thread* thread; : running(false)
{
} }
ThreadBase::~ThreadBase() { ThreadBase::~ThreadBase()
if (running) { {
if (running)
{
wait(); wait();
} }
} }
bool ThreadBase::start() { bool ThreadBase::start()
{
thread = SDL_CreateThread(thread_runner, this); thread = SDL_CreateThread(thread_runner, this);
if (thread == NULL) { if (thread == NULL)
return false; return false;
} else { else
running = true; return (running = true);
return true;
}
} }
void ThreadBase::wait() { void ThreadBase::wait()
{
SDL_WaitThread(thread, NULL); SDL_WaitThread(thread, NULL);
running = false; running = false;
} }
void ThreadBase::kill() { void ThreadBase::kill()
{
SDL_KillThread(thread); SDL_KillThread(thread);
running = false; running = false;
} }
unsigned int ThreadBase::threadId() { unsigned int ThreadBase::threadId()
{
return (unsigned int)SDL_ThreadID(); return (unsigned int)SDL_ThreadID();
} }
Semaphore::Semaphore(unsigned int initial_value) { Semaphore::Semaphore(unsigned int initial_value)
{
semaphore = SDL_CreateSemaphore(initial_value); semaphore = SDL_CreateSemaphore(initial_value);
} }
Semaphore::~Semaphore() { Semaphore::~Semaphore()
{
SDL_DestroySemaphore(semaphore); SDL_DestroySemaphore(semaphore);
} }
unsigned int Semaphore::value() { unsigned int Semaphore::value()
{
return SDL_SemValue(semaphore); return SDL_SemValue(semaphore);
} }
void Semaphore::post() { void Semaphore::post()
{
SDL_SemPost(semaphore); SDL_SemPost(semaphore);
} }
bool Semaphore::wait(int timeout) { bool Semaphore::wait(int timeout)
if (timeout < 0) { {
if (timeout < 0)
return SDL_SemWait(semaphore) ? false : true; return SDL_SemWait(semaphore) ? false : true;
} else if (timeout == 0) { else if (timeout == 0)
return SDL_SemTryWait(semaphore) ? false : true; return SDL_SemTryWait(semaphore) ? false : true;
} else { else
{
int ret = SDL_SemWaitTimeout(semaphore, timeout); int ret = SDL_SemWaitTimeout(semaphore, timeout);
if (ret == SDL_MUTEX_TIMEDOUT) { return (ret == 0);
return false;
} else if (ret == 0) {
return true;
} else {
// some nasty error
return false;
}
} }
} }
bool Semaphore::tryWait() { bool Semaphore::tryWait()
{
return SDL_SemTryWait(semaphore) ? false : true; return SDL_SemTryWait(semaphore) ? false : true;
} }
Conditional::Conditional()
{
Conditional::Conditional() {
cond = SDL_CreateCond(); cond = SDL_CreateCond();
} }
Conditional::~Conditional() { Conditional::~Conditional()
{
SDL_DestroyCond(cond); SDL_DestroyCond(cond);
} }
void Conditional::signal() { void Conditional::signal()
{
SDL_CondSignal(cond); SDL_CondSignal(cond);
} }
void Conditional::broadcast() { void Conditional::broadcast()
{
SDL_CondBroadcast(cond); SDL_CondBroadcast(cond);
} }
bool Conditional::wait(Mutex* mutex, int timeout) { bool Conditional::wait(Mutex* mutex, int timeout)
if (timeout < 0) { {
if (SDL_CondWait(cond, mutex->mutex)) { if (timeout < 0)
// error return !SDL_CondWait(cond, mutex->mutex);
return false; else
} else { return (SDL_CondWaitTimeout(cond, mutex->mutex, timeout) == 0);
return true;
}
} else {
int ret = SDL_CondWaitTimeout(cond, mutex->mutex, timeout);
if (ret == SDL_MUTEX_TIMEDOUT) {
return false;
} else if (ret == 0) {
return true;
} else {
// some bad error
return false;
}
}
} }
} // namespace thread } // thread
} // namespace love } // love
+5 -5
View File
@@ -18,8 +18,8 @@
* 3. This notice may not be removed or altered from any source distribution. * 3. This notice may not be removed or altered from any source distribution.
**/ **/
#ifndef LOVE_PLATFORM_SDL_THREADS_H_ #ifndef LOVE_THREAD_SDL_THREADS_H
#define LOVE_PLATFORM_SDL_THREADS_H_ #define LOVE_THREAD_SDL_THREADS_H
#include "SDL.h" #include "SDL.h"
@@ -97,8 +97,8 @@ namespace thread
bool wait(Mutex* mutex, int timeout=-1); bool wait(Mutex* mutex, int timeout=-1);
}; };
} // namespace thread } // thread
} // namespace love } // love
#endif // !LOVE_PLATFORM_SDL_THREADS_H_ #endif // LOVE_THREAD_SDL_THREADS_H
+2 -3
View File
@@ -32,8 +32,8 @@ namespace love
{ {
namespace thread namespace thread
{ {
const char* threadAPI()
const char* threadAPI() { {
#if LOVE_THREADS == LOVE_THREADS_POSIX #if LOVE_THREADS == LOVE_THREADS_POSIX
return "posix"; return "posix";
#elif LOVE_THREADS == LOVE_THREADS_WIN32 #elif LOVE_THREADS == LOVE_THREADS_WIN32
@@ -44,6 +44,5 @@ namespace thread
return "unknown"; // ?!?!?! return "unknown"; // ?!?!?!
#endif #endif
} }
} // namespace thread } // namespace thread
} // namespace love } // namespace love
+7 -6
View File
@@ -18,8 +18,8 @@
* 3. This notice may not be removed or altered from any source distribution. * 3. This notice may not be removed or altered from any source distribution.
**/ **/
#ifndef LOVE_THREADS_H_ #ifndef LOVE_THREAD_THREADS_H
#define LOVE_THREADS_H_ #define LOVE_THREAD_THREADS_H
#define LOVE_THREADS_SDL 0 #define LOVE_THREADS_SDL 0
#define LOVE_THREADS_WIN32 1 #define LOVE_THREADS_WIN32 1
@@ -56,7 +56,8 @@ namespace thread
const char* threadAPI(); const char* threadAPI();
class Lock { class Lock
{
private: private:
Mutex* mutex; Mutex* mutex;
@@ -76,7 +77,7 @@ namespace thread
} }
}; };
} // namespace thread } // thread
} // namespace love } // love
#endif /* LOVE_THREADS_H_ */ #endif /* LOVE_THREAD_THREADS_H */
+74 -52
View File
@@ -25,53 +25,48 @@ namespace love
namespace thread namespace thread
{ {
Mutex::Mutex() { Mutex::Mutex()
{
InitializeCriticalSection(&mutex); InitializeCriticalSection(&mutex);
} }
Mutex::~Mutex()
{
Mutex::~Mutex() {
DeleteCriticalSection(&mutex); DeleteCriticalSection(&mutex);
} }
void Mutex::lock()
{
void Mutex::lock() {
EnterCriticalSection(&mutex); EnterCriticalSection(&mutex);
} }
void Mutex::unlock()
{
void Mutex::unlock() {
LeaveCriticalSection(&mutex); LeaveCriticalSection(&mutex);
} }
int ThreadBase::thread_runner(void* param)
{
int ThreadBase::thread_runner(void* param) {
ThreadBase* thread = (ThreadBase*)param; ThreadBase* thread = (ThreadBase*)param;
thread->main(); thread->main();
return 0; return 0;
} }
ThreadBase::ThreadBase() : running(false) { ThreadBase::ThreadBase()
HANDLE thread; : running(false)
{
} }
ThreadBase::~ThreadBase()
{
ThreadBase::~ThreadBase() {
if (running) { if (running) {
wait(); wait();
} }
} }
bool ThreadBase::start()
{
bool ThreadBase::start() { thread = CreateThread(NULL, 0, thread_runner, this, NULL);
thread = CreateThread(NULL, 0, run_thread, rd, NULL);
if (thread == NULL) { if (thread == NULL) {
return false; return false;
} else { } else {
@@ -80,60 +75,76 @@ namespace thread
} }
} }
void ThreadBase::wait() { void ThreadBase::wait()
{
WaitForSingleObject(thread, INFINITE); WaitForSingleObject(thread, INFINITE);
CloseHandle(thread); CloseHandle(thread);
running = false; running = false;
} }
void ThreadBase::kill() { void ThreadBase::kill()
{
TerminateThread(thread, FALSE); TerminateThread(thread, FALSE);
running = false; running = false;
} }
unsigned int ThreadBase::threadId() { unsigned int ThreadBase::threadId()
{
return (unsigned int)GetCurrentThreadId(); return (unsigned int)GetCurrentThreadId();
} }
Semaphore::Semaphore()
: Semaphore(0)
{
}
Semaphore::Semaphore(unsigned int initial_value) Semaphore::Semaphore(unsigned int initial_value)
: count(initial_value) { : count(initial_value)
{
semaphore = CreateSemaphore(NULL, initial_value, 65535, NULL); semaphore = CreateSemaphore(NULL, initial_value, 65535, NULL);
} }
Semaphore::~Semaphore() { Semaphore::~Semaphore()
{
CloseHandle(semaphore); CloseHandle(semaphore);
} }
unsigned int Semaphore::value() { unsigned int Semaphore::value()
{
return count; return count;
} }
void Semaphore::post() { void Semaphore::post()
{
InterlockedIncrement(&count); InterlockedIncrement(&count);
if (ReleaseSemaphore(semaphore, 1, NULL) == FALSE) { if (ReleaseSemaphore(semaphore, 1, NULL) == FALSE) {
InterlockedDecrement(&count); InterlockedDecrement(&count);
} }
} }
bool Semaphore::wait(int timeout) { bool Semaphore::wait(int timeout)
{
int result; int result;
result = WaitForSingleObject(semaphore, timeout < 0 ? INFINITE : timeout); result = WaitForSingleObject(semaphore, timeout < 0 ? INFINITE : timeout);
if (result == WAIT_OBJECT_0) { if (result == WAIT_OBJECT_0)
{
InterlockedDecrement(&count); InterlockedDecrement(&count);
return true; return true;
} else if (result == WAIT_TIMEOUT) { }
else if (result == WAIT_TIMEOUT)
{
return false; return false;
} else { }
else
{
// error // error
return false; return false;
} }
} }
bool Semaphore::tryWait() { bool Semaphore::tryWait()
{
return wait(0); return wait(0);
} }
@@ -145,40 +156,49 @@ namespace thread
Conditional::Conditional() Conditional::Conditional()
: waiting(0), signals(0) { : waiting(0), signals(0)
{
} }
Conditional::~Conditional() { Conditional::~Conditional()
{
} }
void Conditional::signal() { void Conditional::signal()
{
mutex.lock(); mutex.lock();
if (waiting > signals) { if (waiting > signals)
{
signals++; signals++;
sem.post(); sem.post();
mutex.unlock(); mutex.unlock();
done.wait(); done.wait();
} else { }
else
{
mutex.unlock(); mutex.unlock();
} }
} }
void Conditional::broadcast() { void Conditional::broadcast()
{
mutex.lock(); mutex.lock();
if (waiting > signals) { if (waiting > signals)
{
int num = waiting - signals; int num = waiting - signals;
signals = waiting; signals = waiting;
for(int i = 0; i < num; i++) sem.post(); for(int i = 0; i < num; i++) sem.post();
mutex.unlock(); mutex.unlock();
for(int i = 0; i < num; i++) done.wait(); for(int i = 0; i < num; i++) done.wait();
} else { }
else
{
mutex.unlock(); mutex.unlock();
} }
} }
bool Conditional::wait(Mutex* cmutex, int timeout) { bool Conditional::wait(Mutex* cmutex, int timeout)
{
mutex.lock(); mutex.lock();
waiting++; waiting++;
mutex.unlock(); mutex.unlock();
@@ -188,8 +208,10 @@ namespace thread
mutex.lock(); mutex.lock();
if (signals > 0) { if (signals > 0)
if (!ret) sem.wait(); {
if (!ret)
sem.wait();
done.post(); done.post();
signals--; signals--;
} }
@@ -198,5 +220,5 @@ namespace thread
cmutex->lock(); cmutex->lock();
} }
} // namespace thread } // thread
} // namespace love } // love
+14 -9
View File
@@ -18,8 +18,8 @@
* 3. This notice may not be removed or altered from any source distribution. * 3. This notice may not be removed or altered from any source distribution.
**/ **/
#ifndef LOVE_PLATFROM_WIN32_THREADS_H_ #ifndef LOVE_THREAD_WIN32_THREADS_H
#define LOVE_PLATFROM_WIN32_THREADS_H_ #define LOVE_THREAD_WIN32_THREADS_H
#include <windows.h> #include <windows.h>
@@ -29,7 +29,8 @@ namespace love
namespace thread namespace thread
{ {
class Mutex { class Mutex
{
private: private:
CRITICAL_SECTION mutex; CRITICAL_SECTION mutex;
Mutex(const Mutex& mutex) {} Mutex(const Mutex& mutex) {}
@@ -42,7 +43,8 @@ namespace thread
void unlock(); void unlock();
}; };
class ThreadBase { class ThreadBase
{
private: private:
HANDLE thread; HANDLE thread;
ThreadBase(ThreadBase& thread) {} ThreadBase(ThreadBase& thread) {}
@@ -66,13 +68,15 @@ namespace thread
}; };
class Semaphore { class Semaphore
{
private: private:
Semaphore(const Semaphore& sem) {} Semaphore(const Semaphore& sem) {}
HANDLE semaphore; HANDLE semaphore;
unsigned int count; unsigned int count;
public: public:
Semaphore();
Semaphore(unsigned int initial_value); Semaphore(unsigned int initial_value);
~Semaphore(); ~Semaphore();
@@ -84,7 +88,8 @@ namespace thread
// Should conditional inherit from mutex? // Should conditional inherit from mutex?
class Conditional { class Conditional
{
private: private:
Mutex mutex; Mutex mutex;
int waiting; int waiting;
@@ -100,7 +105,7 @@ namespace thread
void broadcast(); void broadcast();
bool wait(Mutex* cmutex, int timeout=-1); bool wait(Mutex* cmutex, int timeout=-1);
}; };
} // namespace thread } // thread
} // namespace love } // love
#endif /* LOVE_PLATFROM_WIN32_THREADS_H_ */ #endif /* LOVE_THREAD_WIN32_THREADS_H */