simplifed Thread and fixed some cleanup
This commit is contained in:
Regular → Executable
+4
-9
@@ -143,9 +143,8 @@ public:
|
||||
|
||||
/**
|
||||
* @brief endless loop for logging sink instance.
|
||||
* @return void pointer.
|
||||
*/
|
||||
void* run();
|
||||
void run();
|
||||
|
||||
/**
|
||||
* @brief get the logging areas.
|
||||
@@ -294,18 +293,17 @@ public:
|
||||
|
||||
/**
|
||||
* @brief endless loop for logger instance.
|
||||
* @return void pointer.
|
||||
*/
|
||||
void* run();
|
||||
virtual void run();
|
||||
|
||||
/**
|
||||
* @brief shutdown logger subsystem.
|
||||
*/
|
||||
void stop();
|
||||
virtual void stop();
|
||||
|
||||
private:
|
||||
/** private constructor - singleton pattern */
|
||||
Logger() : m_running(false) {}
|
||||
Logger() {}
|
||||
Logger(const Logger&);
|
||||
Logger& operator=(const Logger&);
|
||||
|
||||
@@ -319,9 +317,6 @@ private:
|
||||
/** queue for logging messages */
|
||||
WQueue<LogMessage*> m_logQueue;
|
||||
|
||||
/** true if this instance is running */
|
||||
bool m_running;
|
||||
|
||||
};
|
||||
|
||||
#endif // LIBUTILS_LOGGER_H_
|
||||
|
||||
Regular → Executable
+25
-19
@@ -23,25 +23,22 @@
|
||||
|
||||
#include "thread.h"
|
||||
|
||||
/**
|
||||
* @brief static function which will be called on thread startup.
|
||||
* @return void pointer.
|
||||
*/
|
||||
static void* runThread(void* arg)
|
||||
void* Thread::runThread(void* arg)
|
||||
{
|
||||
return ((Thread*)arg)->run();
|
||||
((Thread*)arg)->enter();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Thread::~Thread()
|
||||
{
|
||||
if (m_running == true && m_detached == false)
|
||||
if (m_started == true && m_detached == false)
|
||||
pthread_detach(m_threadid);
|
||||
|
||||
if (m_running == true)
|
||||
if (m_started == true)
|
||||
pthread_cancel(m_threadid);
|
||||
}
|
||||
|
||||
int Thread::start(const char* name)
|
||||
bool Thread::start(const char* name)
|
||||
{
|
||||
|
||||
int result = pthread_create(&m_threadid, NULL, runThread, this);
|
||||
@@ -52,32 +49,36 @@ int Thread::start(const char* name)
|
||||
pthread_setname_np(m_threadid, name);
|
||||
#endif
|
||||
|
||||
m_running = true;
|
||||
m_started = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return result;
|
||||
return false;
|
||||
}
|
||||
|
||||
int Thread::join()
|
||||
bool Thread::join()
|
||||
{
|
||||
int result = -1;
|
||||
|
||||
if (m_running == true) {
|
||||
if (m_started == true) {
|
||||
m_stopped = true;
|
||||
result = pthread_join(m_threadid, NULL);
|
||||
|
||||
if (result == 0)
|
||||
if (result == 0) {
|
||||
m_detached = false;
|
||||
|
||||
m_started = false;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
return result == 0;
|
||||
}
|
||||
|
||||
int Thread::detach()
|
||||
bool Thread::detach()
|
||||
{
|
||||
int result = -1;
|
||||
|
||||
if (m_running == true && m_detached == false) {
|
||||
if (m_started == true && m_detached == false) {
|
||||
result = pthread_detach(m_threadid);
|
||||
|
||||
if (result == 0)
|
||||
@@ -85,6 +86,11 @@ int Thread::detach()
|
||||
|
||||
}
|
||||
|
||||
return result;
|
||||
return result == 0;
|
||||
}
|
||||
|
||||
void Thread::enter() {
|
||||
m_running = true;
|
||||
run();
|
||||
m_running = false;
|
||||
}
|
||||
|
||||
Regular → Executable
+47
-18
@@ -32,7 +32,7 @@ public:
|
||||
/**
|
||||
* @brief constructor.
|
||||
*/
|
||||
Thread() : m_threadid(0), m_running(false), m_detached(false) {}
|
||||
Thread() : m_threadid(0), m_started(false), m_running(false), m_stopped(false), m_detached(false) {}
|
||||
|
||||
/**
|
||||
* @brief virtual destructor.
|
||||
@@ -40,44 +40,73 @@ public:
|
||||
virtual ~Thread();
|
||||
|
||||
/**
|
||||
* @brief create the thread and set name for process list.
|
||||
* @param name the thread name which show in process list.
|
||||
* @return value of thread creating.
|
||||
* @brief Thread entry helper for pthread_create.
|
||||
* @param arg pointer to the @a Thread.
|
||||
* @return NULL.
|
||||
*/
|
||||
int start(const char* name);
|
||||
static void* runThread(void* arg);
|
||||
|
||||
/**
|
||||
* @brief join the thread.
|
||||
* @return value of thread joining.
|
||||
* @brief Return whether this @a Thread is still running and not yet stopped.
|
||||
* @return true if this @a Thread is till running and not yet stopped.
|
||||
*/
|
||||
int join();
|
||||
virtual bool isRunning() { return m_running == true && m_stopped == false; }
|
||||
|
||||
/**
|
||||
* @brief detach the thread.
|
||||
* @return value of thread detaching.
|
||||
* @brief Create the native thread and set its name.
|
||||
* @param name the thread name to show in the process list.
|
||||
* @return whether the thread was started.
|
||||
*/
|
||||
int detach();
|
||||
virtual bool start(const char* name);
|
||||
|
||||
/**
|
||||
* @brief return the thread id.
|
||||
* @return own thread id.
|
||||
* @brief Notify the thread that it shall stop.
|
||||
*/
|
||||
virtual void stop() { m_stopped = true; }
|
||||
|
||||
/**
|
||||
* @brief Join the thread.
|
||||
* @return whether the thread was joined.
|
||||
*/
|
||||
virtual bool join();
|
||||
|
||||
/**
|
||||
* @brief Detach the thread.
|
||||
* @return whether the thread was detached.
|
||||
*/
|
||||
virtual bool detach();
|
||||
|
||||
/**
|
||||
* @brief Get the thread id.
|
||||
* @return the thread id.
|
||||
*/
|
||||
pthread_t self() {return m_threadid; }
|
||||
|
||||
/**
|
||||
* @brief virtul function which must be implemented in derived class.
|
||||
* @return void pointer.
|
||||
* @brief Thread entry method to be overridden by derived class.
|
||||
*/
|
||||
virtual void* run() = 0;
|
||||
virtual void run() = 0;
|
||||
|
||||
private:
|
||||
|
||||
/**
|
||||
* @brief Enter the Thread loop by calling run().
|
||||
*/
|
||||
void enter();
|
||||
|
||||
/** own thread id */
|
||||
pthread_t m_threadid;
|
||||
|
||||
/** true if thread is running */
|
||||
/** Whether the thread was started. */
|
||||
bool m_started;
|
||||
|
||||
/** Whether the thread is still running (i.e. in @a run() ). */
|
||||
bool m_running;
|
||||
|
||||
/** true if thread is detached */
|
||||
/** Whether the thread was stopped by @a stop() or @a join(). */
|
||||
bool m_stopped;
|
||||
|
||||
/** Whether the thread was detached */
|
||||
bool m_detached;
|
||||
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user