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