diff options
-rw-r--r-- | src/genworld.cpp | 2 | ||||
-rw-r--r-- | src/saveload/saveload.cpp | 2 | ||||
-rw-r--r-- | src/thread.h | 62 | ||||
-rw-r--r-- | src/thread_morphos.cpp | 91 | ||||
-rw-r--r-- | src/thread_none.cpp | 20 | ||||
-rw-r--r-- | src/thread_os2.cpp | 20 | ||||
-rw-r--r-- | src/thread_pthread.cpp | 154 | ||||
-rw-r--r-- | src/thread_win32.cpp | 143 |
8 files changed, 64 insertions, 430 deletions
diff --git a/src/genworld.cpp b/src/genworld.cpp index 2ff8f3674..1580dc410 100644 --- a/src/genworld.cpp +++ b/src/genworld.cpp @@ -300,7 +300,7 @@ void GenerateWorld(GenerateWorldMode mode, uint size_x, uint size_y) } 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"); _gw.threaded = false; _GenerateWorld(NULL); diff --git a/src/saveload/saveload.cpp b/src/saveload/saveload.cpp index 58808f84f..b7dc4e9a9 100644 --- a/src/saveload/saveload.cpp +++ b/src/saveload/saveload.cpp @@ -1720,7 +1720,7 @@ SaveOrLoadResult SaveOrLoad(const char *filename, int mode, Subdirectory sb) SaveFileStart(); 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..."); SaveOrLoadResult result = SaveFileToDisk(false); diff --git a/src/thread.h b/src/thread.h index 8d0a8ee84..71041e9d7 100644 --- a/src/thread.h +++ b/src/thread.h @@ -20,18 +20,6 @@ public: 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. */ virtual bool Exit() = 0; @@ -42,60 +30,14 @@ public: 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, * with optinal params. * @param proc The procedure to call inside the thread. * @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. */ - static ThreadObject *New(OTTDThreadFunc proc, void *param); - - /** - * 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; + static bool New(OTTDThreadFunc proc, void *param, ThreadObject **thread = NULL); }; #endif /* THREAD_H */ diff --git a/src/thread_morphos.cpp b/src/thread_morphos.cpp index fcf459d04..f549d2eea 100644 --- a/src/thread_morphos.cpp +++ b/src/thread_morphos.cpp @@ -59,12 +59,14 @@ private: APTR m_thr; ///< System thread identifier. struct MsgPort *m_replyport; struct OTTDThreadStartupMessage m_msg; + bool self_destruct; public: /** * 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; @@ -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 */ 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() { struct OTTDThreadStartupMessage *msg; /* You can only exit yourself */ assert(IsCurrent()); - /* If the thread is not running, we are already closed */ - if (!IsRunning()) return false; KPutStr("[Child] Aborting...\n"); @@ -180,11 +152,6 @@ public: return FindTask(NULL) == m_thr; } - /* virtual */ uint GetId() - { - return (uint)m_thr; - } - private: /** * 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. */ KPutStr("[Child] Done.\n"); - } -}; - -/* static */ ThreadObject *ThreadObject::New(OTTDThreadFunc proc, void *param) -{ - return new ThreadObject_MorphOS(proc, param); -} - -/* 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); + if (self_destruct) delete this; } }; -/* static */ ThreadSemaphore *ThreadSemaphore::New() +/* static */ bool ThreadObject::New(OTTDThreadFunc proc, void *param, ThreadObject **thread) { - return new ThreadSemaphore_MorphOS(); + ThreadObject *to = new ThreadObject_MorphOS(proc, param, thread == NULL); + if (thread != NULL) *thread = to; + return true; } diff --git a/src/thread_none.cpp b/src/thread_none.cpp index a5599dff1..34f8ca1ff 100644 --- a/src/thread_none.cpp +++ b/src/thread_none.cpp @@ -5,22 +5,8 @@ #include "stdafx.h" #include "thread.h" -/* static */ ThreadObject *ThreadObject::New(OTTDThreadFunc proc, void *param) +/* static */ bool ThreadObject::New(OTTDThreadFunc proc, void *param, ThreadObject **thread) { - return NULL; -} - -/* static */ ThreadObject *ThreadObject::AttachCurrent() -{ - return NULL; -} - -/* static */ uint ThreadObject::CurrentId() -{ - return -1; -} - -/* static */ ThreadSemaphore *ThreadSemaphore::New() -{ - return NULL; + if (thread != NULL) *thread = NULL; + return false; } diff --git a/src/thread_os2.cpp b/src/thread_os2.cpp index 29b33557f..9d05f7e02 100644 --- a/src/thread_os2.cpp +++ b/src/thread_os2.cpp @@ -59,22 +59,8 @@ void OTTDExitThread() #endif -/* static */ ThreadObject *ThreadObject::New(OTTDThreadFunc proc, void *param) +/* static */ ThreadObject *ThreadObject::New(OTTDThreadFunc proc, void *param, ThreadObject **thread) { - return NULL; -} - -/* static */ ThreadObject *ThreadObject::AttachCurrent() -{ - return NULL; -} - -/* static */ uint ThreadObject::CurrentId() -{ - return -1; -} - -/* static */ ThreadSemaphore *ThreadSemaphore::New() -{ - return NULL; + if (thread != NULL) *thread = NULL; + return false; } diff --git a/src/thread_pthread.cpp b/src/thread_pthread.cpp index 2fbca79b2..87d524784 100644 --- a/src/thread_pthread.cpp +++ b/src/thread_pthread.cpp @@ -16,81 +16,27 @@ */ class ThreadObject_pthread : public ThreadObject { private: - pthread_t m_thr; ///< System thread identifier. - OTTDThreadFunc m_proc; ///< External thread procedure. - void *m_param; ///< Parameter for the external thread procedure. - bool m_attached; ///< True if the ThreadObject was attached to an existing thread. - 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. + 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? public: /** * Create a pthread and start it, calling proc(param). */ - ThreadObject_pthread(OTTDThreadFunc proc, void *param) : - m_thr(0), - m_proc(proc), - m_param(param), - m_attached(false) + ThreadObject_pthread(OTTDThreadFunc proc, void *param, bool self_destruct) : + thread(0), + proc(proc), + param(param), + self_destruct(self_destruct) { - sem_init(&m_sem_start, 0, 0); - 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; + pthread_create(&this->thread, NULL, &stThreadProc, this); } /* virtual */ bool Exit() { - /* You can only exit yourself */ - assert(IsCurrent()); - /* If the thread is not running, we are already closed */ - if (!IsRunning()) return false; - + assert(pthread_self() == this->thread); /* For now we terminate by throwing an error, gives much cleaner cleanup */ throw OTTDThreadExitSignal(); } @@ -98,22 +44,10 @@ public: /* virtual */ void Join() { /* You cannot join yourself */ - assert(!IsCurrent()); - - pthread_join(m_thr, NULL); - m_thr = 0; - } - - /* virtual */ bool IsCurrent() - { - return pthread_self() == m_thr; - } - - /* virtual */ uint GetId() - { - return (uint)m_thr; + assert(pthread_self() != this->thread); + pthread_join(this->thread, NULL); + this->thread = 0; } - private: /** * On thread creation, this function is called, which calls the real startup @@ -131,69 +65,21 @@ private: */ 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 */ try { - m_proc(m_param); + this->proc(this->param); } catch (OTTDThreadExitSignal e) { } catch (...) { NOT_REACHED(); } - /* Notify threads waiting for our completion */ - sem_post(&m_sem_stop); - } -}; - -/* static */ ThreadObject *ThreadObject::New(OTTDThreadFunc proc, void *param) -{ - return new ThreadObject_pthread(proc, param); -} - -/* 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); + if (self_destruct) delete this; } }; -/* static */ ThreadSemaphore *ThreadSemaphore::New() +/* static */ bool ThreadObject::New(OTTDThreadFunc proc, void *param, ThreadObject **thread) { - return new ThreadSemaphore_pthread(); + ThreadObject *to = new ThreadObject_pthread(proc, param, thread == NULL); + if (thread != NULL) *thread = to; + return true; } diff --git a/src/thread_win32.cpp b/src/thread_win32.cpp index 83bb5a76a..0cb2825af 100644 --- a/src/thread_win32.cpp +++ b/src/thread_win32.cpp @@ -15,77 +15,39 @@ */ class ThreadObject_Win32 : public ThreadObject { private: - uint m_id_thr; - HANDLE m_h_thr; - OTTDThreadFunc m_proc; - void *m_param; - bool m_attached; + HANDLE thread; ///< System thread identifier. + uint id; ///< Thread identifier. + OTTDThreadFunc proc; ///< External thread procedure. + void *param; ///< Parameter for the external thread procedure. + bool self_destruct; ///< Free ourselves when done? public: /** * Create a win32 thread and start it, calling proc(param). */ - ThreadObject_Win32(OTTDThreadFunc proc, void *param) : - m_id_thr(0), - m_h_thr(NULL), - m_proc(proc), - m_param(param), - m_attached(false) + ThreadObject_Win32(OTTDThreadFunc proc, void *param, bool self_destruct) : + thread(NULL), + id(0), + proc(proc), + param(param), + self_destruct(self_destruct) { - m_h_thr = (HANDLE)_beginthreadex(NULL, 0, &stThreadProc, this, CREATE_SUSPENDED, &m_id_thr); - if (m_h_thr == NULL) return; - ResumeThread(m_h_thr); - } - - /** - * 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(); + this->thread = (HANDLE)_beginthreadex(NULL, 0, &stThreadProc, this, CREATE_SUSPENDED, &this->id); + if (this->thread == NULL) return; + ResumeThread(this->thread); } /* virtual */ ~ThreadObject_Win32() { - if (m_h_thr != NULL) { - CloseHandle(m_h_thr); - m_h_thr = NULL; + if (this->thread != NULL) { + CloseHandle(this->thread); + 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() { - /* You can only exit yourself */ - assert(IsCurrent()); - /* If the thread is not running, we are already closed */ - if (!IsRunning()) return false; - + assert(GetCurrentThreadId() == this->id); /* For now we terminate by throwing an error, gives much cleaner cleanup */ throw OTTDThreadExitSignal(); } @@ -93,20 +55,8 @@ public: /* virtual */ void Join() { /* You cannot join yourself */ - assert(!IsCurrent()); - - 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; + assert(GetCurrentThreadId() != this->id); + WaitForSingleObject(this->thread, INFINITE); } private: @@ -127,60 +77,19 @@ private: void ThreadProc() { try { - m_proc(m_param); + this->proc(this->param); } catch (OTTDThreadExitSignal) { } catch (...) { NOT_REACHED(); } - } -}; -/* static */ ThreadObject *ThreadObject::New(OTTDThreadFunc proc, void *param) -{ - return new ThreadObject_Win32(proc, param); -} - -/* 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); + if (self_destruct) delete this; } }; -/* static */ ThreadSemaphore *ThreadSemaphore::New() +/* static */ bool ThreadObject::New(OTTDThreadFunc proc, void *param, ThreadObject **thread) { - return new ThreadSemaphore_Win32(); + ThreadObject *to = new ThreadObject_Win32(proc, param, thread == NULL); + if (thread != NULL) *thread = to; + return true; } |