diff options
author | rubidium <rubidium@openttd.org> | 2009-01-20 03:12:46 +0000 |
---|---|---|
committer | rubidium <rubidium@openttd.org> | 2009-01-20 03:12:46 +0000 |
commit | bb770717494ec94d54edb4c1dd1f5a7a0c830ead (patch) | |
tree | f26af7908c8ee5a9d45520a8f2d6aff9f6bfdec6 /src/thread_pthread.cpp | |
parent | 5f3772a42c322a99d2ea57b6c97b76fceccbb781 (diff) | |
download | openttd-bb770717494ec94d54edb4c1dd1f5a7a0c830ead.tar.xz |
(svn r15158) -Cleanup: remove some unused/unneeded cruft from the thread generalisation.
Diffstat (limited to 'src/thread_pthread.cpp')
-rw-r--r-- | src/thread_pthread.cpp | 154 |
1 files changed, 20 insertions, 134 deletions
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; } |