simplifed Thread and fixed some cleanup
This commit is contained in:
Regular → Executable
+8
-19
@@ -37,10 +37,8 @@ extern Appl& A;
|
|||||||
|
|
||||||
int Connection::m_ids = 0;
|
int Connection::m_ids = 0;
|
||||||
|
|
||||||
void* Connection::run()
|
void Connection::run()
|
||||||
{
|
{
|
||||||
m_running = true;
|
|
||||||
|
|
||||||
int ret;
|
int ret;
|
||||||
struct timespec tdiff;
|
struct timespec tdiff;
|
||||||
|
|
||||||
@@ -142,15 +140,12 @@ void* Connection::run()
|
|||||||
}
|
}
|
||||||
|
|
||||||
delete m_socket;
|
delete m_socket;
|
||||||
m_running = false;
|
|
||||||
L.log(net, trace, "[%05d] connection closed", getID());
|
L.log(net, trace, "[%05d] connection closed", getID());
|
||||||
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Network::Network(const bool local, WQueue<NetMessage*>* netQueue)
|
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)
|
if (local == true)
|
||||||
m_tcpServer = new TCPServer(A.getOptVal<int>("port"), "127.0.0.1");
|
m_tcpServer = new TCPServer(A.getOptVal<int>("port"), "127.0.0.1");
|
||||||
@@ -172,18 +167,16 @@ Network::~Network()
|
|||||||
delete connection;
|
delete connection;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_running == true)
|
stop();
|
||||||
stop();
|
join();
|
||||||
|
|
||||||
delete m_tcpServer;
|
delete m_tcpServer;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* Network::run()
|
void Network::run()
|
||||||
{
|
{
|
||||||
if (m_listening == false)
|
if (m_listening == false)
|
||||||
return NULL;
|
return;
|
||||||
|
|
||||||
m_running = true;
|
|
||||||
|
|
||||||
int ret;
|
int ret;
|
||||||
struct timespec tdiff;
|
struct timespec tdiff;
|
||||||
@@ -239,8 +232,7 @@ void* Network::run()
|
|||||||
#ifdef HAVE_PPOLL
|
#ifdef HAVE_PPOLL
|
||||||
// new data from notify
|
// new data from notify
|
||||||
if (fds[0].revents & POLLIN) {
|
if (fds[0].revents & POLLIN) {
|
||||||
m_running = false;
|
return;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// new data from socket
|
// new data from socket
|
||||||
@@ -249,8 +241,7 @@ void* Network::run()
|
|||||||
#ifdef HAVE_PSELECT
|
#ifdef HAVE_PSELECT
|
||||||
// new data from notify
|
// new data from notify
|
||||||
if (FD_ISSET(m_notify.notifyFD(), &readfds)) {
|
if (FD_ISSET(m_notify.notifyFD(), &readfds)) {
|
||||||
m_running = false;
|
return;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// new data from socket
|
// new data from socket
|
||||||
@@ -272,8 +263,6 @@ void* Network::run()
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Network::cleanConnections()
|
void Network::cleanConnections()
|
||||||
|
|||||||
Regular → Executable
+4
-18
@@ -132,25 +132,18 @@ public:
|
|||||||
* @param netQueue the remote queue for network messages.
|
* @param netQueue the remote queue for network messages.
|
||||||
*/
|
*/
|
||||||
Connection(TCPSocket* socket, WQueue<NetMessage*>* netQueue)
|
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; }
|
{ m_id = ++m_ids; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief endless loop for connection instance.
|
* @brief endless loop for connection instance.
|
||||||
* @return void pointer.
|
|
||||||
*/
|
*/
|
||||||
void* run();
|
virtual void run();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief close active connection.
|
* @brief close active connection.
|
||||||
*/
|
*/
|
||||||
void stop() const { m_notify.notify(); }
|
virtual void stop() { m_notify.notify(); Thread::stop(); }
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief status of connection instance.
|
|
||||||
* @return true if connection is running.
|
|
||||||
*/
|
|
||||||
bool isRunning() const { return m_running; }
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief return own connection id.
|
* @brief return own connection id.
|
||||||
@@ -168,9 +161,6 @@ private:
|
|||||||
/** notification object for shutdown procedure */
|
/** notification object for shutdown procedure */
|
||||||
Notify m_notify;
|
Notify m_notify;
|
||||||
|
|
||||||
/** true if this instance is running */
|
|
||||||
bool m_running;
|
|
||||||
|
|
||||||
/** id of current connection*/
|
/** id of current connection*/
|
||||||
int m_id;
|
int m_id;
|
||||||
|
|
||||||
@@ -200,9 +190,8 @@ public:
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief endless loop for network instance.
|
* @brief endless loop for network instance.
|
||||||
* @return void pointer.
|
|
||||||
*/
|
*/
|
||||||
void* run();
|
virtual void run();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief shutdown network subsystem.
|
* @brief shutdown network subsystem.
|
||||||
@@ -225,9 +214,6 @@ private:
|
|||||||
/** true if this instance is listening */
|
/** true if this instance is listening */
|
||||||
bool m_listening;
|
bool m_listening;
|
||||||
|
|
||||||
/** true if this instance is running */
|
|
||||||
bool m_running;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief clean inactive connections from container.
|
* @brief clean inactive connections from container.
|
||||||
*/
|
*/
|
||||||
|
|||||||
Regular → Executable
+4
-9
@@ -143,9 +143,8 @@ public:
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief endless loop for logging sink instance.
|
* @brief endless loop for logging sink instance.
|
||||||
* @return void pointer.
|
|
||||||
*/
|
*/
|
||||||
void* run();
|
void run();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief get the logging areas.
|
* @brief get the logging areas.
|
||||||
@@ -294,18 +293,17 @@ public:
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief endless loop for logger instance.
|
* @brief endless loop for logger instance.
|
||||||
* @return void pointer.
|
|
||||||
*/
|
*/
|
||||||
void* run();
|
virtual void run();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief shutdown logger subsystem.
|
* @brief shutdown logger subsystem.
|
||||||
*/
|
*/
|
||||||
void stop();
|
virtual void stop();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/** private constructor - singleton pattern */
|
/** private constructor - singleton pattern */
|
||||||
Logger() : m_running(false) {}
|
Logger() {}
|
||||||
Logger(const Logger&);
|
Logger(const Logger&);
|
||||||
Logger& operator=(const Logger&);
|
Logger& operator=(const Logger&);
|
||||||
|
|
||||||
@@ -319,9 +317,6 @@ private:
|
|||||||
/** queue for logging messages */
|
/** queue for logging messages */
|
||||||
WQueue<LogMessage*> m_logQueue;
|
WQueue<LogMessage*> m_logQueue;
|
||||||
|
|
||||||
/** true if this instance is running */
|
|
||||||
bool m_running;
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // LIBUTILS_LOGGER_H_
|
#endif // LIBUTILS_LOGGER_H_
|
||||||
|
|||||||
Regular → Executable
+25
-19
@@ -23,25 +23,22 @@
|
|||||||
|
|
||||||
#include "thread.h"
|
#include "thread.h"
|
||||||
|
|
||||||
/**
|
void* Thread::runThread(void* arg)
|
||||||
* @brief static function which will be called on thread startup.
|
|
||||||
* @return void pointer.
|
|
||||||
*/
|
|
||||||
static void* runThread(void* arg)
|
|
||||||
{
|
{
|
||||||
return ((Thread*)arg)->run();
|
((Thread*)arg)->enter();
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
Thread::~Thread()
|
Thread::~Thread()
|
||||||
{
|
{
|
||||||
if (m_running == true && m_detached == false)
|
if (m_started == true && m_detached == false)
|
||||||
pthread_detach(m_threadid);
|
pthread_detach(m_threadid);
|
||||||
|
|
||||||
if (m_running == true)
|
if (m_started == true)
|
||||||
pthread_cancel(m_threadid);
|
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);
|
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);
|
pthread_setname_np(m_threadid, name);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
m_running = true;
|
m_started = true;
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Thread::join()
|
bool Thread::join()
|
||||||
{
|
{
|
||||||
int result = -1;
|
int result = -1;
|
||||||
|
|
||||||
if (m_running == true) {
|
if (m_started == true) {
|
||||||
|
m_stopped = true;
|
||||||
result = pthread_join(m_threadid, NULL);
|
result = pthread_join(m_threadid, NULL);
|
||||||
|
|
||||||
if (result == 0)
|
if (result == 0) {
|
||||||
m_detached = false;
|
m_detached = false;
|
||||||
|
m_started = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Thread::detach()
|
bool Thread::detach()
|
||||||
{
|
{
|
||||||
int result = -1;
|
int result = -1;
|
||||||
|
|
||||||
if (m_running == true && m_detached == false) {
|
if (m_started == true && m_detached == false) {
|
||||||
result = pthread_detach(m_threadid);
|
result = pthread_detach(m_threadid);
|
||||||
|
|
||||||
if (result == 0)
|
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.
|
* @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.
|
* @brief virtual destructor.
|
||||||
@@ -40,44 +40,73 @@ public:
|
|||||||
virtual ~Thread();
|
virtual ~Thread();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief create the thread and set name for process list.
|
* @brief Thread entry helper for pthread_create.
|
||||||
* @param name the thread name which show in process list.
|
* @param arg pointer to the @a Thread.
|
||||||
* @return value of thread creating.
|
* @return NULL.
|
||||||
*/
|
*/
|
||||||
int start(const char* name);
|
static void* runThread(void* arg);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief join the thread.
|
* @brief Return whether this @a Thread is still running and not yet stopped.
|
||||||
* @return value of thread joining.
|
* @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.
|
* @brief Create the native thread and set its name.
|
||||||
* @return value of thread detaching.
|
* @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.
|
* @brief Notify the thread that it shall stop.
|
||||||
* @return own thread id.
|
*/
|
||||||
|
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; }
|
pthread_t self() {return m_threadid; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief virtul function which must be implemented in derived class.
|
* @brief Thread entry method to be overridden by derived class.
|
||||||
* @return void pointer.
|
|
||||||
*/
|
*/
|
||||||
virtual void* run() = 0;
|
virtual void run() = 0;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Enter the Thread loop by calling run().
|
||||||
|
*/
|
||||||
|
void enter();
|
||||||
|
|
||||||
/** own thread id */
|
/** own thread id */
|
||||||
pthread_t m_threadid;
|
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;
|
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;
|
bool m_detached;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user