diff options
Diffstat (limited to 'src/thread_morphos.cpp')
-rw-r--r-- | src/thread_morphos.cpp | 91 |
1 files changed, 8 insertions, 83 deletions
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; } |