[5641] | 1 | /// ----------------------------------------*- mode: C++; -*--
|
---|
| 2 | /// @file fqueue.h
|
---|
| 3 | /// a wrapper class for fastqueue C implementation
|
---|
| 4 | /// ----------------------------------------------------------
|
---|
| 5 | /// $Id: fqueue.h 2549 2007-04-02 22:17:37Z bless $
|
---|
| 6 | /// $HeadURL: https://svn.ipv6.tm.uka.de/nsis/protlib/trunk/include/fqueue.h $
|
---|
| 7 | // ===========================================================
|
---|
| 8 | //
|
---|
| 9 | // Copyright (C) 2005-2007, all rights reserved by
|
---|
| 10 | // - Institute of Telematics, Universitaet Karlsruhe (TH)
|
---|
| 11 | //
|
---|
| 12 | // More information and contact:
|
---|
| 13 | // https://projekte.tm.uka.de/trac/NSIS
|
---|
| 14 | //
|
---|
| 15 | // This program is free software; you can redistribute it and/or modify
|
---|
| 16 | // it under the terms of the GNU General Public License as published by
|
---|
| 17 | // the Free Software Foundation; version 2 of the License
|
---|
| 18 | //
|
---|
| 19 | // This program is distributed in the hope that it will be useful,
|
---|
| 20 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 21 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 22 | // GNU General Public License for more details.
|
---|
| 23 | //
|
---|
| 24 | // You should have received a copy of the GNU General Public License along
|
---|
| 25 | // with this program; if not, write to the Free Software Foundation, Inc.,
|
---|
| 26 | // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
---|
| 27 | //
|
---|
| 28 | // ===========================================================
|
---|
| 29 |
|
---|
| 30 | /** @ingroup fastqueue
|
---|
| 31 | *
|
---|
| 32 | */
|
---|
| 33 |
|
---|
| 34 | #ifndef __FQUEUE_H__
|
---|
| 35 | #define __FQUEUE_H__
|
---|
| 36 |
|
---|
| 37 | #include <string>
|
---|
| 38 |
|
---|
| 39 | #include "protlib_types.h"
|
---|
| 40 | #include "messages.h"
|
---|
| 41 |
|
---|
| 42 | extern "C" {
|
---|
| 43 | #include"fastqueue.h"
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | namespace protlib {
|
---|
| 47 |
|
---|
| 48 | /** @addtogroup fastqueue Fast Queue
|
---|
| 49 | * @{
|
---|
| 50 | */
|
---|
| 51 |
|
---|
| 52 | // declared in messages.h
|
---|
| 53 | class message;
|
---|
| 54 |
|
---|
| 55 |
|
---|
| 56 | /**
|
---|
| 57 | * A fast message queue.
|
---|
| 58 | *
|
---|
| 59 | * This is a fast and thread-safe message queue with expedited data
|
---|
| 60 | * support. It is an object oriented wrapper around fastqueue.c.
|
---|
| 61 | * The queue grows dynamically and has no built-in entry limit.
|
---|
| 62 | */
|
---|
| 63 | class FastQueue {
|
---|
| 64 | public:
|
---|
| 65 | /// FastQueue error
|
---|
| 66 | class FQError{};
|
---|
| 67 | /// constructor
|
---|
| 68 | FastQueue(const char *qname = 0, bool exp = false);
|
---|
| 69 | /// destructor
|
---|
| 70 | ~FastQueue();
|
---|
| 71 | /// enqueue message
|
---|
| 72 | bool enqueue(message *element, bool exp = false);
|
---|
| 73 | /// dequeue message
|
---|
| 74 | message *dequeue(bool blocking = true);
|
---|
| 75 | /// dequeue message, timed wait
|
---|
| 76 | message *dequeue_timedwait(const struct timespec &tspec);
|
---|
| 77 | /// dequeue message, timed wait
|
---|
| 78 | message *dequeue_timedwait(const long int msec);
|
---|
| 79 | /// is queue empty
|
---|
| 80 | bool is_empty() const;
|
---|
| 81 | /// get number of enqueued messages
|
---|
| 82 | unsigned long size() const;
|
---|
| 83 | /// is expedited data support enabled
|
---|
| 84 | bool is_expedited_enabled() const;
|
---|
| 85 | /// enable/disable expedited data
|
---|
| 86 | bool enable_expedited(bool exp);
|
---|
| 87 | /// shutdown queue, do not accept messages
|
---|
| 88 | void shutdown();
|
---|
| 89 | /// delete stored messages
|
---|
| 90 | unsigned long cleanup();
|
---|
| 91 | /// Return the name of the queue.
|
---|
| 92 | const char* get_name() const { return queue_name.c_str(); }
|
---|
| 93 | private:
|
---|
| 94 | /// C fastqueue
|
---|
| 95 | queue_t *queue;
|
---|
| 96 | /// name of the queue, also stored in the queue_t
|
---|
| 97 | string queue_name;
|
---|
| 98 | /// accept or reject messages
|
---|
| 99 | bool shutdownflag;
|
---|
| 100 | };
|
---|
| 101 |
|
---|
| 102 |
|
---|
| 103 | /**
|
---|
| 104 | * Remove the first message from the queue.
|
---|
| 105 | *
|
---|
| 106 | * Messages are removed in the same order they were added to the queue (FIFO).
|
---|
| 107 | * If the expedited messages feature is enabled, there's an exception to
|
---|
| 108 | * this rule: Expedited messages are always removed before all other messages.
|
---|
| 109 | * The FIFO condition still holds among expedited messages, however.
|
---|
| 110 | *
|
---|
| 111 | * If blocking is set, wait infinitely for a message. If set to false,
|
---|
| 112 | * return immediately if the queue is empty. In this case, NULL is returned.
|
---|
| 113 | *
|
---|
| 114 | * @param blocking if true, block until a message arrives
|
---|
| 115 | *
|
---|
| 116 | * @return the message, or NULL
|
---|
| 117 | */
|
---|
| 118 | inline
|
---|
| 119 | message *
|
---|
| 120 | FastQueue::dequeue(bool blocking)
|
---|
| 121 | {
|
---|
| 122 | return static_cast<message*>(blocking ?
|
---|
| 123 | dequeue_element_wait(queue) :
|
---|
| 124 | dequeue_element_nonblocking(queue));
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 |
|
---|
| 128 | /**
|
---|
| 129 | * Wait for a message for a given time.
|
---|
| 130 | *
|
---|
| 131 | * If no message arrives in the given time period, NULL is returned.
|
---|
| 132 | *
|
---|
| 133 | * @param tspec the time to wait
|
---|
| 134 | *
|
---|
| 135 | * @return the message, or NULL
|
---|
| 136 | */
|
---|
| 137 | inline
|
---|
| 138 | message *
|
---|
| 139 | FastQueue::dequeue_timedwait(const struct timespec& tspec)
|
---|
| 140 | {
|
---|
| 141 | return (message*)dequeue_element_timedwait(queue, &tspec);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | //@}
|
---|
| 145 |
|
---|
| 146 | } // end namespace protlib
|
---|
| 147 |
|
---|
| 148 | #endif
|
---|