From da4c199467151123c39a3d03fc665d1aed95b84f Mon Sep 17 00:00:00 2001 From: Bart van Strien Date: Thu, 9 Jun 2011 12:23:11 +0200 Subject: [PATCH] Hopefully the last fixes to the win32 thread backend --- src/modules/thread/win32/threads.cpp | 39 ++++++++++------------------ src/modules/thread/win32/threads.h | 7 +++-- 2 files changed, 16 insertions(+), 30 deletions(-) diff --git a/src/modules/thread/win32/threads.cpp b/src/modules/thread/win32/threads.cpp index 228aa671a..fea2ac40e 100644 --- a/src/modules/thread/win32/threads.cpp +++ b/src/modules/thread/win32/threads.cpp @@ -59,20 +59,17 @@ namespace thread ThreadBase::~ThreadBase() { - if (running) { + if (running) wait(); - } } bool ThreadBase::start() { - thread = CreateThread(NULL, 0, thread_runner, this, NULL); - if (thread == NULL) { + thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) thread_runner, this, 0, NULL); + if (thread == NULL) return false; - } else { - running = true; - return true; - } + else + return (running = true); } void ThreadBase::wait() @@ -93,15 +90,10 @@ namespace thread return (unsigned int)GetCurrentThreadId(); } - Semaphore::Semaphore() - : Semaphore(0) - { - } - Semaphore::Semaphore(unsigned int initial_value) : count(initial_value) { - semaphore = CreateSemaphore(NULL, initial_value, 65535, NULL); + semaphore = CreateSemaphore(NULL, initial_value, UINT_MAX, NULL); } Semaphore::~Semaphore() @@ -117,9 +109,8 @@ namespace thread void Semaphore::post() { InterlockedIncrement(&count); - if (ReleaseSemaphore(semaphore, 1, NULL) == FALSE) { + if (ReleaseSemaphore(semaphore, 1, NULL) == FALSE) InterlockedDecrement(&count); - } } bool Semaphore::wait(int timeout) @@ -132,15 +123,8 @@ namespace thread InterlockedDecrement(&count); return true; } - else if (result == WAIT_TIMEOUT) - { - return false; - } else - { - // error return false; - } } bool Semaphore::tryWait() @@ -187,9 +171,11 @@ namespace thread { int num = waiting - signals; signals = waiting; - for(int i = 0; i < num; i++) sem.post(); + for (int i = 0; i < num; i++) + sem.post(); mutex.unlock(); - for(int i = 0; i < num; i++) done.wait(); + for (int i = 0; i < num; i++) + done.wait(); } else { @@ -206,7 +192,6 @@ namespace thread cmutex->unlock(); bool ret = sem.wait(timeout); - mutex.lock(); if (signals > 0) { @@ -218,6 +203,8 @@ namespace thread waiting--; mutex.unlock(); cmutex->lock(); + + return true; } } // thread diff --git a/src/modules/thread/win32/threads.h b/src/modules/thread/win32/threads.h index c19349c59..19595b78d 100644 --- a/src/modules/thread/win32/threads.h +++ b/src/modules/thread/win32/threads.h @@ -22,7 +22,7 @@ #define LOVE_THREAD_WIN32_THREADS_H #include - +#include namespace love { @@ -50,7 +50,7 @@ namespace thread ThreadBase(ThreadBase& thread) {} bool running; - static int thread_runner(void* param); + static int WINAPI thread_runner(void* param); protected: @@ -76,8 +76,7 @@ namespace thread unsigned int count; public: - Semaphore(); - Semaphore(unsigned int initial_value); + Semaphore(unsigned int initial_value = 0); ~Semaphore(); unsigned int value();