simplifed Thread and fixed some cleanup

This commit is contained in:
john30
2014-11-29 19:01:15 +01:00
parent 7783af84c9
commit b3b19d199e
5 changed files with 88 additions and 83 deletions
Regular → Executable
+8 -19
View File
@@ -37,10 +37,8 @@ extern Appl& A;
int Connection::m_ids = 0;
void* Connection::run()
void Connection::run()
{
m_running = true;
int ret;
struct timespec tdiff;
@@ -142,15 +140,12 @@ void* Connection::run()
}
delete m_socket;
m_running = false;
L.log(net, trace, "[%05d] connection closed", getID());
return NULL;
}
Network::Network(const bool local, WQueue<NetMessage*>* netQueue)
: m_netQueue(netQueue), m_listening(false), m_running(false)
: m_netQueue(netQueue), m_listening(false)
{
if (local == true)
m_tcpServer = new TCPServer(A.getOptVal<int>("port"), "127.0.0.1");
@@ -172,18 +167,16 @@ Network::~Network()
delete connection;
}
if (m_running == true)
stop();
stop();
join();
delete m_tcpServer;
}
void* Network::run()
void Network::run()
{
if (m_listening == false)
return NULL;
m_running = true;
return;
int ret;
struct timespec tdiff;
@@ -239,8 +232,7 @@ void* Network::run()
#ifdef HAVE_PPOLL
// new data from notify
if (fds[0].revents & POLLIN) {
m_running = false;
break;
return;
}
// new data from socket
@@ -249,8 +241,7 @@ void* Network::run()
#ifdef HAVE_PSELECT
// new data from notify
if (FD_ISSET(m_notify.notifyFD(), &readfds)) {
m_running = false;
break;
return;
}
// new data from socket
@@ -272,8 +263,6 @@ void* Network::run()
}
}
return NULL;
}
void Network::cleanConnections()
Regular → Executable
+4 -18
View File
@@ -132,25 +132,18 @@ public:
* @param netQueue the remote queue for network messages.
*/
Connection(TCPSocket* socket, WQueue<NetMessage*>* netQueue)
: m_socket(socket), m_netQueue(netQueue), m_running(false)
: m_socket(socket), m_netQueue(netQueue)
{ m_id = ++m_ids; }
/**
* @brief endless loop for connection instance.
* @return void pointer.
*/
void* run();
virtual void run();
/**
* @brief close active connection.
*/
void stop() const { m_notify.notify(); }
/**
* @brief status of connection instance.
* @return true if connection is running.
*/
bool isRunning() const { return m_running; }
virtual void stop() { m_notify.notify(); Thread::stop(); }
/**
* @brief return own connection id.
@@ -168,9 +161,6 @@ private:
/** notification object for shutdown procedure */
Notify m_notify;
/** true if this instance is running */
bool m_running;
/** id of current connection*/
int m_id;
@@ -200,9 +190,8 @@ public:
/**
* @brief endless loop for network instance.
* @return void pointer.
*/
void* run();
virtual void run();
/**
* @brief shutdown network subsystem.
@@ -225,9 +214,6 @@ private:
/** true if this instance is listening */
bool m_listening;
/** true if this instance is running */
bool m_running;
/**
* @brief clean inactive connections from container.
*/
Regular → Executable
+4 -9
View File
@@ -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
View File
@@ -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
View File
@@ -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;
};