diff --git a/src/lib/utils/Makefile.am b/src/lib/utils/Makefile.am index d10f81d6..3f9640ac 100644 --- a/src/lib/utils/Makefile.am +++ b/src/lib/utils/Makefile.am @@ -4,18 +4,20 @@ AM_CXXFLAGS = -fpic \ noinst_LIBRARIES = libutils.a -libutils_a_SOURCES = wqueue.h \ - notify.h \ - appl.cpp \ +libutils_a_SOURCES = appl.cpp \ appl.h \ daemon.cpp \ daemon.h \ logger.cpp \ logger.h \ + notify.cpp \ + notify.h \ + tcpsocket.cpp \ + tcpsocket.h \ thread.cpp \ thread.h \ - tcpsocket.cpp \ - tcpsocket.h + wqueue.h + distclean-local: -rm -f Makefile.in diff --git a/src/lib/utils/notify.cpp b/src/lib/utils/notify.cpp new file mode 100644 index 00000000..d9416162 --- /dev/null +++ b/src/lib/utils/notify.cpp @@ -0,0 +1,42 @@ +/* + * Copyright (C) Roland Jax 2012-2014 + * + * This file is part of ebusd. + * + * ebusd is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * ebusd is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with ebusd. If not, see http://www.gnu.org/licenses/. + */ + +#include "notify.h" + +Notify::Notify() +{ + int pipefd[2]; + int ret = pipe(pipefd); + + if (ret == 0) { + m_recvfd = pipefd[0]; + m_sendfd = pipefd[1]; + + fcntl(m_sendfd, F_SETFL, O_NONBLOCK); + } + +} + +Notify::~Notify() +{ + close(m_sendfd); + close(m_recvfd); +} + + diff --git a/src/lib/utils/notify.h b/src/lib/utils/notify.h index accc6339..05b1491b 100644 --- a/src/lib/utils/notify.h +++ b/src/lib/utils/notify.h @@ -23,30 +23,39 @@ #include #include +/** + * @brief class to notify other thread per pipe. + */ class Notify { public: - Notify() - { - int pipefd[2]; - int ret = pipe(pipefd); + /** + * @brief constructs a new instance and do notifying. + */ + Notify(); - if (ret == 0) { - m_recvfd = pipefd[0]; - m_sendfd = pipefd[1]; + /** + * @brief destructor. + */ + ~Notify(); - fcntl(m_sendfd, F_SETFL, O_NONBLOCK); - } + /** + * @brief file descriptor to watch for notify event. + * @return the notification value + */ + int notifyFD() { return m_recvfd; } - } - virtual ~Notify() { close(m_sendfd); close(m_recvfd); } - - int notifyFD() const { return m_recvfd; } + /** + * @brief write notify event to file descriptor. + * @return result of writing notification + */ int notify() const { return write(m_sendfd,"1",1); } private: + /** file descriptor to watch */ int m_recvfd; + /** file descriptor to notify */ int m_sendfd; }; diff --git a/src/lib/utils/wqueue.h b/src/lib/utils/wqueue.h index 99c4fc48..cf696370 100644 --- a/src/lib/utils/wqueue.h +++ b/src/lib/utils/wqueue.h @@ -23,22 +23,36 @@ #include #include -template class WQueue +/** + * @brief queue class template for all kinds data types with exclusiv lock. + */ +template +class WQueue { public: + /** + * @brief constructs a new instance. + */ WQueue() { pthread_mutex_init(&m_mutex, NULL); pthread_cond_init(&m_cond, NULL); } + /** + * @brief destructor. + */ ~WQueue() { pthread_mutex_destroy(&m_mutex); pthread_cond_destroy(&m_cond); } + /** + * @brief add a new item to the end of queue. + * @param item + */ void add(T item) { pthread_mutex_lock(&m_mutex); @@ -49,6 +63,10 @@ public: pthread_mutex_unlock(&m_mutex); } + /** + * @brief remove the first item from queue. + * @return the item + */ T remove() { pthread_mutex_lock(&m_mutex); @@ -64,6 +82,10 @@ public: return item; } + /** + * @brief return the first item from queue without remove. + * @return the item + */ T next() { pthread_mutex_lock(&m_mutex); @@ -78,6 +100,10 @@ public: return item; } + /** + * @brief the number of entries inside queue. + * @return the size + */ int size() { pthread_mutex_lock(&m_mutex); @@ -90,8 +116,11 @@ public: } private: + /** the queue itself */ std::list m_queue; + /** mutex variable for exclusive lock */ pthread_mutex_t m_mutex; + /** condition variable for exclusive lock */ pthread_cond_t m_cond; };