2008-04-14 19:54:33 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
/** @file thread_pthread.cpp POSIX pthread implementation of Threads. */
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "thread.h"
|
|
|
|
#include <pthread.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* POSIX pthread version for ThreadObject.
|
|
|
|
*/
|
|
|
|
class ThreadObject_pthread : public ThreadObject {
|
|
|
|
private:
|
2009-01-20 03:12:46 +00:00
|
|
|
pthread_t thread; ///< System thread identifier.
|
|
|
|
OTTDThreadFunc proc; ///< External thread procedure.
|
|
|
|
void *param; ///< Parameter for the external thread procedure.
|
|
|
|
bool self_destruct; ///< Free ourselves when done?
|
2008-04-14 19:54:33 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Create a pthread and start it, calling proc(param).
|
|
|
|
*/
|
2009-01-20 03:12:46 +00:00
|
|
|
ThreadObject_pthread(OTTDThreadFunc proc, void *param, bool self_destruct) :
|
|
|
|
thread(0),
|
|
|
|
proc(proc),
|
|
|
|
param(param),
|
|
|
|
self_destruct(self_destruct)
|
2008-04-14 19:54:33 +00:00
|
|
|
{
|
2009-01-20 03:12:46 +00:00
|
|
|
pthread_create(&this->thread, NULL, &stThreadProc, this);
|
2008-04-14 19:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual */ bool Exit()
|
|
|
|
{
|
2009-01-20 03:12:46 +00:00
|
|
|
assert(pthread_self() == this->thread);
|
2008-04-14 19:54:33 +00:00
|
|
|
/* For now we terminate by throwing an error, gives much cleaner cleanup */
|
2009-01-12 14:31:49 +00:00
|
|
|
throw OTTDThreadExitSignal();
|
2008-04-14 19:54:33 +00:00
|
|
|
}
|
|
|
|
|
2008-06-08 10:51:36 +00:00
|
|
|
/* virtual */ void Join()
|
2008-04-14 19:54:33 +00:00
|
|
|
{
|
|
|
|
/* You cannot join yourself */
|
2009-01-20 03:12:46 +00:00
|
|
|
assert(pthread_self() != this->thread);
|
|
|
|
pthread_join(this->thread, NULL);
|
|
|
|
this->thread = 0;
|
2008-04-14 19:54:33 +00:00
|
|
|
}
|
|
|
|
private:
|
|
|
|
/**
|
|
|
|
* On thread creation, this function is called, which calls the real startup
|
|
|
|
* function. This to get back into the correct instance again.
|
|
|
|
*/
|
|
|
|
static void *stThreadProc(void *thr)
|
|
|
|
{
|
2008-06-08 10:51:36 +00:00
|
|
|
((ThreadObject_pthread *)thr)->ThreadProc();
|
|
|
|
pthread_exit(NULL);
|
2008-04-14 19:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A new thread is created, and this function is called. Call the custom
|
|
|
|
* function of the creator of the thread.
|
|
|
|
*/
|
2008-06-08 10:51:36 +00:00
|
|
|
void ThreadProc()
|
2008-04-14 19:54:33 +00:00
|
|
|
{
|
|
|
|
/* Call the proc of the creator to continue this thread */
|
|
|
|
try {
|
2009-01-20 03:12:46 +00:00
|
|
|
this->proc(this->param);
|
2009-01-12 14:31:49 +00:00
|
|
|
} catch (OTTDThreadExitSignal e) {
|
2008-04-14 19:54:33 +00:00
|
|
|
} catch (...) {
|
2009-01-12 14:31:49 +00:00
|
|
|
NOT_REACHED();
|
2008-04-14 19:54:33 +00:00
|
|
|
}
|
|
|
|
|
2009-01-20 03:12:46 +00:00
|
|
|
if (self_destruct) delete this;
|
2008-04-14 19:54:33 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-01-20 03:12:46 +00:00
|
|
|
/* static */ bool ThreadObject::New(OTTDThreadFunc proc, void *param, ThreadObject **thread)
|
2008-04-14 19:54:33 +00:00
|
|
|
{
|
2009-01-20 03:12:46 +00:00
|
|
|
ThreadObject *to = new ThreadObject_pthread(proc, param, thread == NULL);
|
|
|
|
if (thread != NULL) *thread = to;
|
|
|
|
return true;
|
2008-04-14 19:54:33 +00:00
|
|
|
}
|
2009-01-20 03:44:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* POSIX pthread version of ThreadMutex.
|
|
|
|
*/
|
|
|
|
class ThreadMutex_pthread : public ThreadMutex {
|
|
|
|
private:
|
|
|
|
pthread_mutex_t mutex;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ThreadMutex_pthread()
|
|
|
|
{
|
|
|
|
pthread_mutex_init(&this->mutex, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual */ ~ThreadMutex_pthread()
|
|
|
|
{
|
|
|
|
pthread_mutex_destroy(&this->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual */ void BeginCritical()
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&this->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual */ void EndCritical()
|
|
|
|
{
|
|
|
|
pthread_mutex_unlock(&this->mutex);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* static */ ThreadMutex *ThreadMutex::New()
|
|
|
|
{
|
|
|
|
return new ThreadMutex_pthread();
|
|
|
|
}
|