mirror of https://github.com/OpenTTD/OpenTTD.git
(svn r15158) -Cleanup: remove some unused/unneeded cruft from the thread generalisation.
This commit is contained in:
parent
5f3772a42c
commit
bb77071749
|
@ -300,7 +300,7 @@ void GenerateWorld(GenerateWorldMode mode, uint size_x, uint size_y)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() == 0 ||
|
if (BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() == 0 ||
|
||||||
(_gw.thread = ThreadObject::New(&_GenerateWorld, NULL)) == NULL) {
|
!ThreadObject::New(&_GenerateWorld, NULL, &_gw.thread)) {
|
||||||
DEBUG(misc, 1, "Cannot create genworld thread, reverting to single-threaded mode");
|
DEBUG(misc, 1, "Cannot create genworld thread, reverting to single-threaded mode");
|
||||||
_gw.threaded = false;
|
_gw.threaded = false;
|
||||||
_GenerateWorld(NULL);
|
_GenerateWorld(NULL);
|
||||||
|
|
|
@ -1720,7 +1720,7 @@ SaveOrLoadResult SaveOrLoad(const char *filename, int mode, Subdirectory sb)
|
||||||
|
|
||||||
SaveFileStart();
|
SaveFileStart();
|
||||||
if (_network_server ||
|
if (_network_server ||
|
||||||
(_save_thread = ThreadObject::New(&SaveFileToDiskThread, NULL)) == NULL) {
|
!ThreadObject::New(&SaveFileToDiskThread, NULL, &_save_thread)) {
|
||||||
if (!_network_server) DEBUG(sl, 1, "Cannot create savegame thread, reverting to single-threaded mode...");
|
if (!_network_server) DEBUG(sl, 1, "Cannot create savegame thread, reverting to single-threaded mode...");
|
||||||
|
|
||||||
SaveOrLoadResult result = SaveFileToDisk(false);
|
SaveOrLoadResult result = SaveFileToDisk(false);
|
||||||
|
|
62
src/thread.h
62
src/thread.h
|
@ -19,18 +19,6 @@ public:
|
||||||
*/
|
*/
|
||||||
virtual ~ThreadObject() {};
|
virtual ~ThreadObject() {};
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if the thread is currently running.
|
|
||||||
* @return True if the thread is running.
|
|
||||||
*/
|
|
||||||
virtual bool IsRunning() = 0;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Waits for the thread to exit.
|
|
||||||
* @return True if the thread has exited.
|
|
||||||
*/
|
|
||||||
virtual bool WaitForStop() = 0;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Exit this thread.
|
* Exit this thread.
|
||||||
*/
|
*/
|
||||||
|
@ -41,61 +29,15 @@ public:
|
||||||
*/
|
*/
|
||||||
virtual void Join() = 0;
|
virtual void Join() = 0;
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if this thread is the current active thread.
|
|
||||||
* @return True if it is the current active thread.
|
|
||||||
*/
|
|
||||||
virtual bool IsCurrent() = 0;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the unique ID of this thread.
|
|
||||||
* @return A value unique to each thread.
|
|
||||||
*/
|
|
||||||
virtual uint GetId() = 0;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a thread; proc will be called as first function inside the thread,
|
* Create a thread; proc will be called as first function inside the thread,
|
||||||
* with optinal params.
|
* with optinal params.
|
||||||
* @param proc The procedure to call inside the thread.
|
* @param proc The procedure to call inside the thread.
|
||||||
* @param param The params to give with 'proc'.
|
* @param param The params to give with 'proc'.
|
||||||
|
* @param thread Place to store a pointer to the thread in. May be NULL.
|
||||||
* @return True if the thread was started correctly.
|
* @return True if the thread was started correctly.
|
||||||
*/
|
*/
|
||||||
static ThreadObject *New(OTTDThreadFunc proc, void *param);
|
static bool New(OTTDThreadFunc proc, void *param, ThreadObject **thread = NULL);
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert the current thread to a new ThreadObject.
|
|
||||||
* @return A new ThreadObject with the current thread attached to it.
|
|
||||||
*/
|
|
||||||
static ThreadObject *AttachCurrent();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Find the Id of the current running thread.
|
|
||||||
* @return The thread ID of the current active thread.
|
|
||||||
*/
|
|
||||||
static uint CurrentId();
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Cross-platform Thread Semaphore. Wait() waits for a Set() of someone else.
|
|
||||||
*/
|
|
||||||
class ThreadSemaphore {
|
|
||||||
public:
|
|
||||||
static ThreadSemaphore *New();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Virtual Destructor to avoid compiler warnings.
|
|
||||||
*/
|
|
||||||
virtual ~ThreadSemaphore() {};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Signal all threads that are in Wait() to continue.
|
|
||||||
*/
|
|
||||||
virtual void Set() = 0;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Wait until we are signaled by a call to Set().
|
|
||||||
*/
|
|
||||||
virtual void Wait() = 0;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* THREAD_H */
|
#endif /* THREAD_H */
|
||||||
|
|
|
@ -59,12 +59,14 @@ private:
|
||||||
APTR m_thr; ///< System thread identifier.
|
APTR m_thr; ///< System thread identifier.
|
||||||
struct MsgPort *m_replyport;
|
struct MsgPort *m_replyport;
|
||||||
struct OTTDThreadStartupMessage m_msg;
|
struct OTTDThreadStartupMessage m_msg;
|
||||||
|
bool self_destruct;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* Create a sub process and start it, calling proc(param).
|
* Create a sub process and start it, calling proc(param).
|
||||||
*/
|
*/
|
||||||
ThreadObject_MorphOS(OTTDThreadFunc proc, void *param) : m_thr(0)
|
ThreadObject_MorphOS(OTTDThreadFunc proc, void *param, self_destruct) :
|
||||||
|
m_thr(0), self_destruct(self_destruct)
|
||||||
{
|
{
|
||||||
struct Task *parent;
|
struct Task *parent;
|
||||||
|
|
||||||
|
@ -108,46 +110,16 @@ public:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a thread and attach current thread to it.
|
|
||||||
*/
|
|
||||||
ThreadObject_MorphOS() : m_thr(0)
|
|
||||||
{
|
|
||||||
m_thr = FindTask(NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* virtual */ ~ThreadObject_MorphOS()
|
/* virtual */ ~ThreadObject_MorphOS()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
/* virtual */ bool IsRunning()
|
|
||||||
{
|
|
||||||
return m_thr != 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* virtual */ bool WaitForStop()
|
|
||||||
{
|
|
||||||
/* You can't wait on yourself */
|
|
||||||
assert(!IsCurrent());
|
|
||||||
/* If the thread is not running, waiting is over */
|
|
||||||
if (!IsRunning()) return true;
|
|
||||||
|
|
||||||
WaitPort(m_replyport);
|
|
||||||
|
|
||||||
GetMsg(m_replyport);
|
|
||||||
DeleteMsgPort(m_replyport);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* virtual */ bool Exit()
|
/* virtual */ bool Exit()
|
||||||
{
|
{
|
||||||
struct OTTDThreadStartupMessage *msg;
|
struct OTTDThreadStartupMessage *msg;
|
||||||
|
|
||||||
/* You can only exit yourself */
|
/* You can only exit yourself */
|
||||||
assert(IsCurrent());
|
assert(IsCurrent());
|
||||||
/* If the thread is not running, we are already closed */
|
|
||||||
if (!IsRunning()) return false;
|
|
||||||
|
|
||||||
KPutStr("[Child] Aborting...\n");
|
KPutStr("[Child] Aborting...\n");
|
||||||
|
|
||||||
|
@ -180,11 +152,6 @@ public:
|
||||||
return FindTask(NULL) == m_thr;
|
return FindTask(NULL) == m_thr;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* virtual */ uint GetId()
|
|
||||||
{
|
|
||||||
return (uint)m_thr;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/**
|
/**
|
||||||
* On thread creation, this function is called, which calls the real startup
|
* On thread creation, this function is called, which calls the real startup
|
||||||
|
@ -212,56 +179,14 @@ private:
|
||||||
|
|
||||||
/* Quit the child, exec.library will reply the startup msg internally. */
|
/* Quit the child, exec.library will reply the startup msg internally. */
|
||||||
KPutStr("[Child] Done.\n");
|
KPutStr("[Child] Done.\n");
|
||||||
|
|
||||||
|
if (self_destruct) delete this;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/* static */ ThreadObject *ThreadObject::New(OTTDThreadFunc proc, void *param)
|
/* static */ bool ThreadObject::New(OTTDThreadFunc proc, void *param, ThreadObject **thread)
|
||||||
{
|
{
|
||||||
return new ThreadObject_MorphOS(proc, param);
|
ThreadObject *to = new ThreadObject_MorphOS(proc, param, thread == NULL);
|
||||||
}
|
if (thread != NULL) *thread = to;
|
||||||
|
return true;
|
||||||
/* static */ ThreadObject *ThreadObject::AttachCurrent()
|
|
||||||
{
|
|
||||||
return new ThreadObject_MorphOS();
|
|
||||||
}
|
|
||||||
|
|
||||||
/* static */ uint ThreadObject::CurrentId()
|
|
||||||
{
|
|
||||||
return (uint) FindTask(NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* MorphOS version of ThreadSemaphore.
|
|
||||||
*/
|
|
||||||
class ThreadSemaphore_MorphOS : public ThreadSemaphore {
|
|
||||||
private:
|
|
||||||
struct SignalSemaphore m_sem;
|
|
||||||
|
|
||||||
public:
|
|
||||||
ThreadSemaphore_MorphOS()
|
|
||||||
{
|
|
||||||
InitSemaphore(&m_sem);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* virtual */ ~ThreadSemaphore_MorphOS()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/* virtual */ void Set()
|
|
||||||
{
|
|
||||||
/* Check if semaphore count is really important there. */
|
|
||||||
ReleaseSemaphore(&m_sem);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* virtual */ void Wait()
|
|
||||||
{
|
|
||||||
ObtainSemaphore(&m_sem);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/* static */ ThreadSemaphore *ThreadSemaphore::New()
|
|
||||||
{
|
|
||||||
return new ThreadSemaphore_MorphOS();
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,22 +5,8 @@
|
||||||
#include "stdafx.h"
|
#include "stdafx.h"
|
||||||
#include "thread.h"
|
#include "thread.h"
|
||||||
|
|
||||||
/* static */ ThreadObject *ThreadObject::New(OTTDThreadFunc proc, void *param)
|
/* static */ bool ThreadObject::New(OTTDThreadFunc proc, void *param, ThreadObject **thread)
|
||||||
{
|
{
|
||||||
return NULL;
|
if (thread != NULL) *thread = NULL;
|
||||||
}
|
return false;
|
||||||
|
|
||||||
/* static */ ThreadObject *ThreadObject::AttachCurrent()
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* static */ uint ThreadObject::CurrentId()
|
|
||||||
{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* static */ ThreadSemaphore *ThreadSemaphore::New()
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -59,22 +59,8 @@ void OTTDExitThread()
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* static */ ThreadObject *ThreadObject::New(OTTDThreadFunc proc, void *param)
|
/* static */ ThreadObject *ThreadObject::New(OTTDThreadFunc proc, void *param, ThreadObject **thread)
|
||||||
{
|
{
|
||||||
return NULL;
|
if (thread != NULL) *thread = NULL;
|
||||||
}
|
return false;
|
||||||
|
|
||||||
/* static */ ThreadObject *ThreadObject::AttachCurrent()
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* static */ uint ThreadObject::CurrentId()
|
|
||||||
{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* static */ ThreadSemaphore *ThreadSemaphore::New()
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,81 +16,27 @@
|
||||||
*/
|
*/
|
||||||
class ThreadObject_pthread : public ThreadObject {
|
class ThreadObject_pthread : public ThreadObject {
|
||||||
private:
|
private:
|
||||||
pthread_t m_thr; ///< System thread identifier.
|
pthread_t thread; ///< System thread identifier.
|
||||||
OTTDThreadFunc m_proc; ///< External thread procedure.
|
OTTDThreadFunc proc; ///< External thread procedure.
|
||||||
void *m_param; ///< Parameter for the external thread procedure.
|
void *param; ///< Parameter for the external thread procedure.
|
||||||
bool m_attached; ///< True if the ThreadObject was attached to an existing thread.
|
bool self_destruct; ///< Free ourselves when done?
|
||||||
sem_t m_sem_start; ///< Here the new thread waits before it starts.
|
|
||||||
sem_t m_sem_stop; ///< Here the other thread can wait for this thread to end.
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* Create a pthread and start it, calling proc(param).
|
* Create a pthread and start it, calling proc(param).
|
||||||
*/
|
*/
|
||||||
ThreadObject_pthread(OTTDThreadFunc proc, void *param) :
|
ThreadObject_pthread(OTTDThreadFunc proc, void *param, bool self_destruct) :
|
||||||
m_thr(0),
|
thread(0),
|
||||||
m_proc(proc),
|
proc(proc),
|
||||||
m_param(param),
|
param(param),
|
||||||
m_attached(false)
|
self_destruct(self_destruct)
|
||||||
{
|
{
|
||||||
sem_init(&m_sem_start, 0, 0);
|
pthread_create(&this->thread, NULL, &stThreadProc, this);
|
||||||
sem_init(&m_sem_stop, 0, 0);
|
|
||||||
|
|
||||||
pthread_create(&m_thr, NULL, &stThreadProc, this);
|
|
||||||
sem_post(&m_sem_start);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a pthread and attach current thread to it.
|
|
||||||
*/
|
|
||||||
ThreadObject_pthread() :
|
|
||||||
m_thr(0),
|
|
||||||
m_proc(NULL),
|
|
||||||
m_param(0),
|
|
||||||
m_attached(true)
|
|
||||||
{
|
|
||||||
sem_init(&m_sem_start, 0, 0);
|
|
||||||
sem_init(&m_sem_stop, 0, 0);
|
|
||||||
|
|
||||||
m_thr = pthread_self();
|
|
||||||
}
|
|
||||||
|
|
||||||
/* virtual */ ~ThreadObject_pthread()
|
|
||||||
{
|
|
||||||
sem_destroy(&m_sem_stop);
|
|
||||||
sem_destroy(&m_sem_start);
|
|
||||||
};
|
|
||||||
|
|
||||||
/* virtual */ bool IsRunning()
|
|
||||||
{
|
|
||||||
int sval;
|
|
||||||
sem_getvalue(&m_sem_stop, &sval);
|
|
||||||
return sval == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* virtual */ bool WaitForStop()
|
|
||||||
{
|
|
||||||
/* You can't wait on yourself */
|
|
||||||
assert(!IsCurrent());
|
|
||||||
/* If the thread is not running, waiting is over */
|
|
||||||
if (!IsRunning()) return true;
|
|
||||||
|
|
||||||
int ret = sem_wait(&m_sem_stop);
|
|
||||||
if (ret == 0) {
|
|
||||||
/* We have passed semaphore so increment it again */
|
|
||||||
sem_post(&m_sem_stop);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* virtual */ bool Exit()
|
/* virtual */ bool Exit()
|
||||||
{
|
{
|
||||||
/* You can only exit yourself */
|
assert(pthread_self() == this->thread);
|
||||||
assert(IsCurrent());
|
|
||||||
/* If the thread is not running, we are already closed */
|
|
||||||
if (!IsRunning()) return false;
|
|
||||||
|
|
||||||
/* For now we terminate by throwing an error, gives much cleaner cleanup */
|
/* For now we terminate by throwing an error, gives much cleaner cleanup */
|
||||||
throw OTTDThreadExitSignal();
|
throw OTTDThreadExitSignal();
|
||||||
}
|
}
|
||||||
|
@ -98,22 +44,10 @@ public:
|
||||||
/* virtual */ void Join()
|
/* virtual */ void Join()
|
||||||
{
|
{
|
||||||
/* You cannot join yourself */
|
/* You cannot join yourself */
|
||||||
assert(!IsCurrent());
|
assert(pthread_self() != this->thread);
|
||||||
|
pthread_join(this->thread, NULL);
|
||||||
pthread_join(m_thr, NULL);
|
this->thread = 0;
|
||||||
m_thr = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* virtual */ bool IsCurrent()
|
|
||||||
{
|
|
||||||
return pthread_self() == m_thr;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* virtual */ uint GetId()
|
|
||||||
{
|
|
||||||
return (uint)m_thr;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/**
|
/**
|
||||||
* On thread creation, this function is called, which calls the real startup
|
* On thread creation, this function is called, which calls the real startup
|
||||||
|
@ -131,69 +65,21 @@ private:
|
||||||
*/
|
*/
|
||||||
void ThreadProc()
|
void ThreadProc()
|
||||||
{
|
{
|
||||||
/* The new thread stops here so the calling thread can complete pthread_create() call */
|
|
||||||
sem_wait(&m_sem_start);
|
|
||||||
|
|
||||||
/* Call the proc of the creator to continue this thread */
|
/* Call the proc of the creator to continue this thread */
|
||||||
try {
|
try {
|
||||||
m_proc(m_param);
|
this->proc(this->param);
|
||||||
} catch (OTTDThreadExitSignal e) {
|
} catch (OTTDThreadExitSignal e) {
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
NOT_REACHED();
|
NOT_REACHED();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Notify threads waiting for our completion */
|
if (self_destruct) delete this;
|
||||||
sem_post(&m_sem_stop);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/* static */ ThreadObject *ThreadObject::New(OTTDThreadFunc proc, void *param)
|
/* static */ bool ThreadObject::New(OTTDThreadFunc proc, void *param, ThreadObject **thread)
|
||||||
{
|
{
|
||||||
return new ThreadObject_pthread(proc, param);
|
ThreadObject *to = new ThreadObject_pthread(proc, param, thread == NULL);
|
||||||
}
|
if (thread != NULL) *thread = to;
|
||||||
|
return true;
|
||||||
/* static */ ThreadObject *ThreadObject::AttachCurrent()
|
|
||||||
{
|
|
||||||
return new ThreadObject_pthread();
|
|
||||||
}
|
|
||||||
|
|
||||||
/* static */ uint ThreadObject::CurrentId()
|
|
||||||
{
|
|
||||||
return (uint)pthread_self();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* POSIX pthread version of ThreadSemaphore.
|
|
||||||
*/
|
|
||||||
class ThreadSemaphore_pthread : public ThreadSemaphore {
|
|
||||||
private:
|
|
||||||
sem_t m_sem;
|
|
||||||
|
|
||||||
public:
|
|
||||||
ThreadSemaphore_pthread()
|
|
||||||
{
|
|
||||||
sem_init(&m_sem, 0, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* virtual */ ~ThreadSemaphore_pthread()
|
|
||||||
{
|
|
||||||
sem_destroy(&m_sem);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* virtual */ void Set()
|
|
||||||
{
|
|
||||||
int val = 0;
|
|
||||||
if (sem_getvalue(&m_sem, &val) == 0 && val == 0) sem_post(&m_sem);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* virtual */ void Wait()
|
|
||||||
{
|
|
||||||
sem_wait(&m_sem);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/* static */ ThreadSemaphore *ThreadSemaphore::New()
|
|
||||||
{
|
|
||||||
return new ThreadSemaphore_pthread();
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,77 +15,39 @@
|
||||||
*/
|
*/
|
||||||
class ThreadObject_Win32 : public ThreadObject {
|
class ThreadObject_Win32 : public ThreadObject {
|
||||||
private:
|
private:
|
||||||
uint m_id_thr;
|
HANDLE thread; ///< System thread identifier.
|
||||||
HANDLE m_h_thr;
|
uint id; ///< Thread identifier.
|
||||||
OTTDThreadFunc m_proc;
|
OTTDThreadFunc proc; ///< External thread procedure.
|
||||||
void *m_param;
|
void *param; ///< Parameter for the external thread procedure.
|
||||||
bool m_attached;
|
bool self_destruct; ///< Free ourselves when done?
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* Create a win32 thread and start it, calling proc(param).
|
* Create a win32 thread and start it, calling proc(param).
|
||||||
*/
|
*/
|
||||||
ThreadObject_Win32(OTTDThreadFunc proc, void *param) :
|
ThreadObject_Win32(OTTDThreadFunc proc, void *param, bool self_destruct) :
|
||||||
m_id_thr(0),
|
thread(NULL),
|
||||||
m_h_thr(NULL),
|
id(0),
|
||||||
m_proc(proc),
|
proc(proc),
|
||||||
m_param(param),
|
param(param),
|
||||||
m_attached(false)
|
self_destruct(self_destruct)
|
||||||
{
|
{
|
||||||
m_h_thr = (HANDLE)_beginthreadex(NULL, 0, &stThreadProc, this, CREATE_SUSPENDED, &m_id_thr);
|
this->thread = (HANDLE)_beginthreadex(NULL, 0, &stThreadProc, this, CREATE_SUSPENDED, &this->id);
|
||||||
if (m_h_thr == NULL) return;
|
if (this->thread == NULL) return;
|
||||||
ResumeThread(m_h_thr);
|
ResumeThread(this->thread);
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a win32 thread and attach current thread to it.
|
|
||||||
*/
|
|
||||||
ThreadObject_Win32() :
|
|
||||||
m_id_thr(0),
|
|
||||||
m_h_thr(NULL),
|
|
||||||
m_proc(NULL),
|
|
||||||
m_param(NULL),
|
|
||||||
m_attached(false)
|
|
||||||
{
|
|
||||||
BOOL ret = DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), GetCurrentProcess(), &m_h_thr, 0, FALSE, DUPLICATE_SAME_ACCESS);
|
|
||||||
if (!ret) return;
|
|
||||||
m_id_thr = GetCurrentThreadId();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* virtual */ ~ThreadObject_Win32()
|
/* virtual */ ~ThreadObject_Win32()
|
||||||
{
|
{
|
||||||
if (m_h_thr != NULL) {
|
if (this->thread != NULL) {
|
||||||
CloseHandle(m_h_thr);
|
CloseHandle(this->thread);
|
||||||
m_h_thr = NULL;
|
this->thread = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* virtual */ bool IsRunning()
|
|
||||||
{
|
|
||||||
if (m_h_thr == NULL) return false;
|
|
||||||
DWORD exit_code = 0;
|
|
||||||
if (!GetExitCodeThread(m_h_thr, &exit_code)) return false;
|
|
||||||
return (exit_code == STILL_ACTIVE);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* virtual */ bool WaitForStop()
|
|
||||||
{
|
|
||||||
/* You can't wait on yourself */
|
|
||||||
assert(!IsCurrent());
|
|
||||||
/* If the thread is not running, waiting is over */
|
|
||||||
if (!IsRunning()) return true;
|
|
||||||
|
|
||||||
DWORD res = WaitForSingleObject(m_h_thr, INFINITE);
|
|
||||||
return res == WAIT_OBJECT_0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* virtual */ bool Exit()
|
/* virtual */ bool Exit()
|
||||||
{
|
{
|
||||||
/* You can only exit yourself */
|
assert(GetCurrentThreadId() == this->id);
|
||||||
assert(IsCurrent());
|
|
||||||
/* If the thread is not running, we are already closed */
|
|
||||||
if (!IsRunning()) return false;
|
|
||||||
|
|
||||||
/* For now we terminate by throwing an error, gives much cleaner cleanup */
|
/* For now we terminate by throwing an error, gives much cleaner cleanup */
|
||||||
throw OTTDThreadExitSignal();
|
throw OTTDThreadExitSignal();
|
||||||
}
|
}
|
||||||
|
@ -93,20 +55,8 @@ public:
|
||||||
/* virtual */ void Join()
|
/* virtual */ void Join()
|
||||||
{
|
{
|
||||||
/* You cannot join yourself */
|
/* You cannot join yourself */
|
||||||
assert(!IsCurrent());
|
assert(GetCurrentThreadId() != this->id);
|
||||||
|
WaitForSingleObject(this->thread, INFINITE);
|
||||||
WaitForSingleObject(m_h_thr, INFINITE);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* virtual */ bool IsCurrent()
|
|
||||||
{
|
|
||||||
DWORD id_cur = GetCurrentThreadId();
|
|
||||||
return id_cur == m_id_thr;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* virtual */ uint GetId()
|
|
||||||
{
|
|
||||||
return m_id_thr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -127,60 +77,19 @@ private:
|
||||||
void ThreadProc()
|
void ThreadProc()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
m_proc(m_param);
|
this->proc(this->param);
|
||||||
} catch (OTTDThreadExitSignal) {
|
} catch (OTTDThreadExitSignal) {
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
NOT_REACHED();
|
NOT_REACHED();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (self_destruct) delete this;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/* static */ ThreadObject *ThreadObject::New(OTTDThreadFunc proc, void *param)
|
/* static */ bool ThreadObject::New(OTTDThreadFunc proc, void *param, ThreadObject **thread)
|
||||||
{
|
{
|
||||||
return new ThreadObject_Win32(proc, param);
|
ThreadObject *to = new ThreadObject_Win32(proc, param, thread == NULL);
|
||||||
}
|
if (thread != NULL) *thread = to;
|
||||||
|
return true;
|
||||||
/* static */ ThreadObject* ThreadObject::AttachCurrent()
|
|
||||||
{
|
|
||||||
return new ThreadObject_Win32();
|
|
||||||
}
|
|
||||||
|
|
||||||
/* static */ uint ThreadObject::CurrentId()
|
|
||||||
{
|
|
||||||
return GetCurrentThreadId();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Win32 thread version of ThreadSemaphore.
|
|
||||||
*/
|
|
||||||
class ThreadSemaphore_Win32 : public ThreadSemaphore {
|
|
||||||
private:
|
|
||||||
HANDLE m_handle;
|
|
||||||
|
|
||||||
public:
|
|
||||||
ThreadSemaphore_Win32()
|
|
||||||
{
|
|
||||||
m_handle = ::CreateEvent(NULL, FALSE, FALSE, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* virtual */ ~ThreadSemaphore_Win32()
|
|
||||||
{
|
|
||||||
::CloseHandle(m_handle);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* virtual */ void Set()
|
|
||||||
{
|
|
||||||
::SetEvent(m_handle);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* virtual */ void Wait()
|
|
||||||
{
|
|
||||||
::WaitForSingleObject(m_handle, INFINITE);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/* static */ ThreadSemaphore *ThreadSemaphore::New()
|
|
||||||
{
|
|
||||||
return new ThreadSemaphore_Win32();
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue