Merge pull request #18 from john30/master; first version supporting get, set, and cyc again.

This commit is contained in:
Roland Jax
2014-12-07 12:00:03 +01:00
parent 4bd18de1e5
commit 3b3a5f2e07
49 changed files with 2536 additions and 4884 deletions
Regular → Executable
View File
+3
View File
@@ -160,6 +160,9 @@ void Appl::setOptVal(const char* option, const string value, DataType datatype)
case dt_bool:
m_optvals[option] = true;
break;
case dt_hex:
m_optvals[option] = strtol(value.c_str(), NULL, 16);
break;
case dt_int:
m_optvals[option] = strtol(value.c_str(), NULL, 10);
break;
+2 -1
View File
@@ -33,7 +33,8 @@ using namespace std;
enum DataType {
dt_none, /*!< default for __text_only__ */
dt_bool, /*!< boolean */
dt_int, /*!< integer */
dt_hex, /*!< hex integer */
dt_int, /*!< dec integer */
dt_long, /*!< long */
dt_float, /*!< float */
dt_string /*!< string */
+10 -12
View File
@@ -100,7 +100,7 @@ void LogSink::addMessage(const LogMessage& message)
m_logQueue.add((tmp));
}
void* LogSink::run()
void LogSink::run()
{
while (1) {
LogMessage* message = m_logQueue.remove();
@@ -111,13 +111,12 @@ void* LogSink::run()
write(*message);
delete message;
}
return NULL;
return;
}
write(*message);
delete message;
}
return NULL;
}
@@ -187,7 +186,7 @@ Logger& Logger::operator-=(const LogSink* sink)
void Logger::log(const int area, const int level, const string& data, ...)
{
if (m_running == true) {
if (isRunning() == true) {
char* tmp;
va_list ap;
va_start(ap, data);
@@ -203,11 +202,11 @@ void Logger::log(const int area, const int level, const string& data, ...)
}
void* Logger::run()
void Logger::run()
{
m_running = true;
bool running = true;
while (m_running == true) {
do {
LogMessage* message = m_logQueue.remove();
sinkCI_t iter = m_sinks.begin();
@@ -215,28 +214,27 @@ void* Logger::run()
for (; iter != m_sinks.end(); ++iter) {
if (*iter != 0) {
if (((*iter)->getAreas() & message->getArea()
if ((((*iter)->getAreas() & message->getArea()) != 0
&& (*iter)->getLevel() >= message->getLevel())
&& message->isRunning() == true) {
(*iter)->addMessage(*message);
}
else if (message->isRunning() == false) {
(*iter)->addMessage(*message);
m_running = false;
running = false;
}
}
}
delete message;
}
return NULL;
} while (running == true);
}
void Logger::stop()
{
m_logQueue.add(new LogMessage(LogMessage(bas, error, "", false)));
usleep(100000);
Thread::stop();
}
+3 -8
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,14 +293,13 @@ 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:
/**
@@ -334,9 +332,6 @@ private:
/** queue for logging messages */
WQueue<LogMessage*> m_logQueue;
/** true if this instance is running */
bool m_running;
};
#endif // LIBUTILS_LOGGER_H_
+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;
}
+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;
};
+44 -14
View File
@@ -67,17 +67,25 @@ public:
/**
* @brief remove the first item from queue.
* @return the item.
* @param wait true to wait for an item to be added to the queue, false to return NULL if no item is available.
* @return the item, or NULL if no item is available and wait was false.
*/
T remove()
T remove(bool wait=true)
{
pthread_mutex_lock(&m_mutex);
while (m_queue.size() == 0)
pthread_cond_wait(&m_cond, &m_mutex);
T item = m_queue.front();
m_queue.pop_front();
T item;
if (wait == true) {
while (m_queue.size() == 0)
pthread_cond_wait(&m_cond, &m_mutex);
item = m_queue.front();
m_queue.pop_front();
}
else if (m_queue.size() > 0) {
item = m_queue.front();
m_queue.pop_front();
} else
item = NULL;
pthread_mutex_unlock(&m_mutex);
@@ -85,17 +93,39 @@ public:
}
/**
* @brief return the first item from queue without remove.
* @return the item.
* @brief Remove the specified item from queue.
* @param item the item to remove.
* @return whether the item was removed.
*/
T next()
bool remove(T item)
{
pthread_mutex_lock(&m_mutex);
int oldSize = m_queue.size();
if (oldSize > 0)
m_queue.remove(item);
int newSize = m_queue.size();
pthread_mutex_unlock(&m_mutex);
return newSize != oldSize;
}
/**
* @brief return the first item from queue without remove.
* @return the item, or NULL if no item is available and wait was false.
*/
T next(bool wait=true)
{
pthread_mutex_lock(&m_mutex);
while (m_queue.size() == 0)
pthread_cond_wait(&m_cond, &m_mutex);
T item = m_queue.front();
T item;
if (wait == true) {
while (m_queue.size() == 0)
pthread_cond_wait(&m_cond, &m_mutex);
item = m_queue.front();
}
else if (m_queue.size() > 0)
item = m_queue.front();
else
item = NULL;
pthread_mutex_unlock(&m_mutex);