From 5e9094a19bde7157fb0397e65a137dd93a4ff32b Mon Sep 17 00:00:00 2001 From: john30 Date: Sat, 31 Oct 2015 16:22:13 +0100 Subject: [PATCH] reworked Queue, added TODO, corrected documentation, prepared execution of regular tasks in MainLoop --- src/lib/utils/wqueue.h | 202 ----------------------------------------- 1 file changed, 202 deletions(-) delete mode 100644 src/lib/utils/wqueue.h diff --git a/src/lib/utils/wqueue.h b/src/lib/utils/wqueue.h deleted file mode 100644 index 03315399..00000000 --- a/src/lib/utils/wqueue.h +++ /dev/null @@ -1,202 +0,0 @@ -/* - * Copyright (C) Roland Jax 2012-2014 , - * John Baier 2014-2015 - * - * 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/. - */ - -#ifndef LIBUTILS_WQUEUE_H_ -#define LIBUTILS_WQUEUE_H_ - -#include -#include - -/** \file wqueue.h */ - -using namespace std; - -/** - * @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); - } - -private: - /** - * @brief Hidden copy constructor. - * @param src the object to copy from. - */ - WQueue(const WQueue& src); - -public: - - /** - * @brief add a new item to the end of queue. - * @param item to add. - */ - void add(T item) - { - pthread_mutex_lock(&m_mutex); - - m_queue.push_back(item); - - pthread_cond_broadcast(&m_cond); - pthread_mutex_unlock(&m_mutex); - } - - /** - * @brief remove the first item from queue. - * @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(bool wait=true) - { - pthread_mutex_lock(&m_mutex); - - T item; - if (wait) { - 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); - - return item; - } - - /** - * @brief Remove the specified item from the queue. - * @param item the item to remove. - * @return whether the item was removed. - */ - bool remove(T item) - { - pthread_mutex_lock(&m_mutex); - - size_t oldSize = m_queue.size(); - if (oldSize > 0) - m_queue.remove(item); - size_t newSize = m_queue.size(); - - pthread_mutex_unlock(&m_mutex); - return newSize != oldSize; - } - - /** - * @brief Wait for the specified item to appear in the queue and remove it from the queue. - * @param item the item to wait for and remove. - * @return whether the item was removed. - */ - bool waitRemove(T item) - { - pthread_mutex_lock(&m_mutex); - - do { - size_t oldSize = m_queue.size(); - if (oldSize > 0) { - m_queue.remove(item); - if (m_queue.size() != oldSize) - break; - } - - pthread_cond_wait(&m_cond, &m_mutex); - } while (true); - - pthread_mutex_unlock(&m_mutex); - return true; - } - - /** - * @brief return the first item from the queue without remove. - * @param wait whether to wait for an entry if the queue is empty. - * @return the item, or NULL if no item is available and wait was false. - */ - T next(bool wait=true) - { - pthread_mutex_lock(&m_mutex); - - T item; - if (wait) { - 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); - - return item; - } - - /** - * @brief the number of entries inside queue. - * @return the size. - */ - size_t size() - { - pthread_mutex_lock(&m_mutex); - - size_t size = m_queue.size(); - - pthread_mutex_unlock(&m_mutex); - - return size; - } - -private: - /** the queue itself */ - list m_queue; - - /** mutex variable for exclusive lock */ - pthread_mutex_t m_mutex; - - /** condition variable for exclusive lock */ - pthread_cond_t m_cond; - -}; - -#endif // LIBUTILS_WQUEUE_H_