From bc1fa51747c66325b3166765cc4772afd204f020 Mon Sep 17 00:00:00 2001 From: john30 Date: Sat, 7 Feb 2015 14:25:09 +0100 Subject: [PATCH] added WaitThread, removed unused detach() --- src/lib/utils/thread.cpp | 59 ++++++++++++++++++++++++++++------------ src/lib/utils/thread.h | 54 ++++++++++++++++++++++++++++-------- 2 files changed, 85 insertions(+), 28 deletions(-) diff --git a/src/lib/utils/thread.cpp b/src/lib/utils/thread.cpp index b2f6f4ab..fa584391 100644 --- a/src/lib/utils/thread.cpp +++ b/src/lib/utils/thread.cpp @@ -31,7 +31,7 @@ void* Thread::runThread(void* arg) Thread::~Thread() { - if (m_started == true && m_detached == false) + if (m_started == true) pthread_detach(m_threadid); if (m_started == true) @@ -66,7 +66,6 @@ bool Thread::join() result = pthread_join(m_threadid, NULL); if (result == 0) { - m_detached = false; m_started = false; } } @@ -74,23 +73,49 @@ bool Thread::join() return result == 0; } -bool Thread::detach() -{ - int result = -1; - - if (m_started == true && m_detached == false) { - result = pthread_detach(m_threadid); - - if (result == 0) - m_detached = true; - - } - - return result == 0; -} - void Thread::enter() { m_running = true; run(); m_running = false; } + + +WaitThread::WaitThread() + : Thread() +{ + pthread_mutex_init(&m_mutex, NULL); + pthread_cond_init(&m_cond, NULL); +} + +WaitThread::~WaitThread() +{ + pthread_mutex_destroy(&m_mutex); + pthread_cond_destroy(&m_cond); +} + +void WaitThread::stop() +{ + pthread_mutex_lock(&m_mutex); + pthread_cond_signal(&m_cond); + pthread_mutex_unlock(&m_mutex); + Thread::stop(); +} + +bool WaitThread::join() +{ + pthread_mutex_lock(&m_mutex); + pthread_cond_signal(&m_cond); + pthread_mutex_unlock(&m_mutex); + return Thread::join(); +} + +bool WaitThread::Wait(int seconds) +{ + struct timespec t; + clock_gettime(CLOCK_REALTIME, &t); + t.tv_sec += seconds; + pthread_mutex_lock(&m_mutex); + pthread_cond_timedwait(&m_cond, &m_mutex, &t); + pthread_mutex_unlock(&m_mutex); + return isRunning(); +} diff --git a/src/lib/utils/thread.h b/src/lib/utils/thread.h index d9f1b4d6..78078285 100644 --- a/src/lib/utils/thread.h +++ b/src/lib/utils/thread.h @@ -1,5 +1,6 @@ /* - * Copyright (C) Roland Jax 2012-2014 + * Copyright (C) Roland Jax 2012-2014 , + * John Baier 2014-2015 * * This file is part of ebusd. * @@ -34,7 +35,7 @@ public: /** * constructor. */ - Thread() : m_threadid(0), m_started(false), m_running(false), m_stopped(false), m_detached(false) {} + Thread() : m_threadid(0), m_started(false), m_running(false), m_stopped(false) {} /** * virtual destructor. @@ -72,17 +73,11 @@ public: */ virtual bool join(); - /** - * Detach the thread. - * @return whether the thread was detached. - */ - virtual bool detach(); - /** * Get the thread id. * @return the thread id. */ - pthread_t self() {return m_threadid; } + pthread_t self() { return m_threadid; } protected: @@ -110,8 +105,45 @@ private: /** Whether the thread was stopped by @a stop() or @a join(). */ bool m_stopped; - /** Whether the thread was detached */ - bool m_detached; +}; + + +/** + * A @a Thread that can be waited on. + */ +class WaitThread : public Thread +{ + +public: + /** + * Constructor. + */ + WaitThread(); + + /** + * Destructor. + */ + virtual ~WaitThread(); + + // @copydoc + virtual void stop(); + + // @copydoc + virtual bool join(); + + /** + * Wait for the specified amount of time. + * @param seconds the number of seconds to wait. + * @return true if this @a Thread is till running and not yet stopped. + */ + bool Wait(int seconds); + +private: + /** the mutex for waiting. */ + pthread_mutex_t m_mutex; + + /** the condition for waiting. */ + pthread_cond_t m_cond; };