2011-12-22 15:19:25 +00:00
|
|
|
/*
|
|
|
|
* \brief Queue with first-in first-out semantics
|
|
|
|
* \author Norman Feske
|
|
|
|
* \date 2008-08-15
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2019-01-29 18:29:03 +00:00
|
|
|
* Copyright (C) 2008-2019 Genode Labs GmbH
|
2011-12-22 15:19:25 +00:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
2017-02-20 12:23:52 +00:00
|
|
|
* under the terms of the GNU Affero General Public License version 3.
|
2011-12-22 15:19:25 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _INCLUDE__UTIL__FIFO_H_
|
|
|
|
#define _INCLUDE__UTIL__FIFO_H_
|
|
|
|
|
|
|
|
namespace Genode {
|
2015-03-04 20:12:14 +00:00
|
|
|
|
|
|
|
template<typename> class Fifo;
|
|
|
|
template <typename> class Fifo_element;
|
|
|
|
}
|
|
|
|
|
2011-12-22 15:19:25 +00:00
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
/**
|
2019-05-22 13:37:45 +00:00
|
|
|
* First-in first-out (FIFO) queue
|
2015-03-04 20:12:14 +00:00
|
|
|
*
|
|
|
|
* \param QT queue element type
|
|
|
|
*/
|
|
|
|
template <typename QT>
|
|
|
|
class Genode::Fifo
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
class Element
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
|
|
|
|
friend class Fifo;
|
|
|
|
|
2019-01-29 18:29:03 +00:00
|
|
|
QT *_next { nullptr };
|
|
|
|
bool _enqueued { false };
|
2015-03-04 20:12:14 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2019-01-29 18:29:03 +00:00
|
|
|
Element() { }
|
2015-03-04 20:12:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true is fifo element is enqueued in a fifo
|
|
|
|
*/
|
2019-11-05 17:48:41 +00:00
|
|
|
bool enqueued() const { return _enqueued; }
|
2016-05-11 16:21:47 +00:00
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
/**
|
|
|
|
* Return next element in queue
|
2019-01-29 18:29:03 +00:00
|
|
|
*
|
|
|
|
* \deprecated use 'Fifo::for_each' instead
|
2015-03-04 20:12:14 +00:00
|
|
|
*/
|
|
|
|
QT *next() const { return _next; }
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2019-01-29 18:29:03 +00:00
|
|
|
QT *_head { nullptr }; /* oldest element */
|
|
|
|
Element *_tail { nullptr }; /* newest element */
|
2015-03-04 20:12:14 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true if queue is empty
|
|
|
|
*/
|
2019-07-29 12:12:59 +00:00
|
|
|
bool empty() const { return _tail == nullptr; }
|
2015-03-04 20:12:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*/
|
2019-01-29 18:29:03 +00:00
|
|
|
Fifo() { }
|
2015-03-04 20:12:14 +00:00
|
|
|
|
|
|
|
/**
|
2019-05-22 13:37:45 +00:00
|
|
|
* Call 'func' of type 'void (QT&)' the head element
|
2015-03-04 20:12:14 +00:00
|
|
|
*/
|
2019-01-29 18:29:03 +00:00
|
|
|
template <typename FUNC>
|
2019-05-22 13:37:45 +00:00
|
|
|
void head(FUNC const &func) const { if (_head) func(*_head); }
|
2015-03-04 20:12:14 +00:00
|
|
|
|
|
|
|
/**
|
2019-05-22 13:37:45 +00:00
|
|
|
* Remove element explicitly from queue
|
2015-03-04 20:12:14 +00:00
|
|
|
*/
|
2019-01-29 18:29:03 +00:00
|
|
|
void remove(QT &qe)
|
2015-03-04 20:12:14 +00:00
|
|
|
{
|
|
|
|
if (empty()) return;
|
|
|
|
|
|
|
|
/* if specified element is the first of the queue */
|
2019-01-29 18:29:03 +00:00
|
|
|
if (&qe == _head) {
|
|
|
|
_head = qe.Fifo::Element::_next;
|
2019-05-22 13:37:45 +00:00
|
|
|
if (!_head)
|
|
|
|
_tail = nullptr;
|
|
|
|
|
|
|
|
} else {
|
2012-11-16 10:35:40 +00:00
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
/* search specified element in the queue */
|
|
|
|
Element *e = _head;
|
2019-01-29 18:29:03 +00:00
|
|
|
while (e->_next && (e->_next != &qe))
|
2015-03-04 20:12:14 +00:00
|
|
|
e = e->_next;
|
|
|
|
|
|
|
|
/* element is not member of the queue */
|
|
|
|
if (!e->_next) return;
|
|
|
|
|
|
|
|
/* e->_next is the element to remove, skip it in list */
|
2018-10-30 09:22:34 +00:00
|
|
|
e->Fifo::Element::_next = e->Fifo::Element::_next->Fifo::Element::_next;
|
2015-03-04 20:12:14 +00:00
|
|
|
if (!e->Element::_next) _tail = e;
|
2011-12-22 15:19:25 +00:00
|
|
|
}
|
|
|
|
|
2019-01-29 18:29:03 +00:00
|
|
|
qe.Fifo::Element::_next = nullptr;
|
|
|
|
qe.Fifo::Element::_enqueued = false;
|
2015-03-04 20:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attach element at the end of the queue
|
|
|
|
*/
|
2019-01-29 18:29:03 +00:00
|
|
|
void enqueue(QT &e)
|
2015-03-04 20:12:14 +00:00
|
|
|
{
|
2019-01-29 18:29:03 +00:00
|
|
|
e.Fifo::Element::_next = nullptr;
|
|
|
|
e.Fifo::Element::_enqueued = true;
|
2015-03-04 20:12:14 +00:00
|
|
|
|
|
|
|
if (empty()) {
|
2019-01-29 18:29:03 +00:00
|
|
|
_tail = _head = &e;
|
2015-03-04 20:12:14 +00:00
|
|
|
return;
|
2011-12-22 15:19:25 +00:00
|
|
|
}
|
2015-03-04 20:12:14 +00:00
|
|
|
|
2019-01-29 18:29:03 +00:00
|
|
|
_tail->Fifo::Element::_next = &e;
|
|
|
|
_tail = &e;
|
2015-03-04 20:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-05-22 13:37:45 +00:00
|
|
|
* Call 'func' of type 'void (QT&)' for each element in order
|
2015-03-04 20:12:14 +00:00
|
|
|
*/
|
2019-01-29 18:29:03 +00:00
|
|
|
template <typename FUNC>
|
|
|
|
void for_each(FUNC const &func) const
|
|
|
|
{
|
|
|
|
QT *elem = _head;
|
|
|
|
while (elem != nullptr) {
|
|
|
|
/* take the next pointer so 'func' cannot modify it */
|
|
|
|
QT *next = elem->Fifo::Element::_next;;
|
|
|
|
func(*elem);
|
|
|
|
elem = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-05-22 13:37:45 +00:00
|
|
|
* Remove head and call 'func' of type 'void (QT&)'
|
2019-01-29 18:29:03 +00:00
|
|
|
*/
|
|
|
|
template <typename FUNC>
|
|
|
|
void dequeue(FUNC const &func)
|
2015-03-04 20:12:14 +00:00
|
|
|
{
|
|
|
|
QT *result = _head;
|
|
|
|
|
|
|
|
/* check if queue has only one last element */
|
|
|
|
if (_head == _tail) {
|
2019-01-29 18:29:03 +00:00
|
|
|
_head = nullptr;
|
|
|
|
_tail = nullptr;
|
2015-03-04 20:12:14 +00:00
|
|
|
} else
|
|
|
|
_head = _head->Fifo::Element::_next;
|
|
|
|
|
|
|
|
/* mark fifo queue element as free */
|
|
|
|
if (result) {
|
2019-01-29 18:29:03 +00:00
|
|
|
result->Fifo::Element::_next = nullptr;
|
2016-05-11 16:21:47 +00:00
|
|
|
result->Fifo::Element::_enqueued = false;
|
2019-01-29 18:29:03 +00:00
|
|
|
|
|
|
|
/* pass to caller */
|
|
|
|
func(*result);
|
2013-10-08 08:40:33 +00:00
|
|
|
}
|
2019-01-29 18:29:03 +00:00
|
|
|
}
|
2015-03-04 20:12:14 +00:00
|
|
|
|
2019-01-29 18:29:03 +00:00
|
|
|
/**
|
2019-05-22 13:37:45 +00:00
|
|
|
* Remove all fifo elements
|
|
|
|
*
|
|
|
|
* This method removes all elements in order and calls the lambda
|
|
|
|
* 'func' of type 'void (QT&)' for each element. It is intended to be
|
|
|
|
* used prior the destruction of the FIFO.
|
2019-01-29 18:29:03 +00:00
|
|
|
*/
|
|
|
|
template <typename FUNC>
|
|
|
|
void dequeue_all(FUNC const &func)
|
|
|
|
{
|
|
|
|
while (_head != nullptr) dequeue(func);
|
2015-03-04 20:12:14 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper for using member variables as FIFO elements
|
|
|
|
*
|
|
|
|
* \param T type of compound object to be organized in a FIFO
|
|
|
|
*
|
2019-05-22 13:37:45 +00:00
|
|
|
* This helper allows the creation of FIFOs that use member variables to
|
2015-03-04 20:12:14 +00:00
|
|
|
* connect their elements. This way, the organized type does not need to
|
2019-05-22 13:37:45 +00:00
|
|
|
* publicly inherit 'Fifo<QT>::Element'. Furthermore, objects can easily
|
2015-03-04 20:12:14 +00:00
|
|
|
* be organized in multiple FIFOs by embedding multiple 'Fifo_element'
|
|
|
|
* member variables.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
class Genode::Fifo_element : public Fifo<Fifo_element<T> >::Element
|
|
|
|
{
|
2019-01-29 18:29:03 +00:00
|
|
|
T &_object;
|
2015-03-04 20:12:14 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2019-01-29 18:29:03 +00:00
|
|
|
Fifo_element(T &object) : _object(object) { }
|
2015-03-04 20:12:14 +00:00
|
|
|
|
2019-04-11 17:59:07 +00:00
|
|
|
T &object() { return _object; }
|
|
|
|
T const &object() const { return _object; }
|
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
};
|
2011-12-22 15:19:25 +00:00
|
|
|
|
|
|
|
#endif /* _INCLUDE__UTIL__FIFO_H_ */
|