queue.h

Go to the documentation of this file.
00001 /*
00002  * SpanDSP - a series of DSP components for telephony
00003  *
00004  * queue.h - simple in process message queuing
00005  *
00006  * Written by Steve Underwood <steveu@coppice.org>
00007  *
00008  * Copyright (C) 2004 Steve Underwood
00009  *
00010  * All rights reserved.
00011  *
00012  * This program is free software; you can redistribute it and/or modify
00013  * it under the terms of the GNU Lesser General Public License version 2.1,
00014  * as published by the Free Software Foundation.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU Lesser General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU Lesser General Public
00022  * License along with this program; if not, write to the Free Software
00023  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00024  *
00025  * $Id: queue.h,v 1.17 2008/09/09 16:25:51 steveu Exp $
00026  */
00027 
00028 /*! \file */
00029 
00030 /*! \page queue_page Queuing
00031 \section queue_page_sec_1 What does it do?
00032 This module provides lock free queuing for either octet streams or messages.
00033 Specifically, lock free means one thread can write and another can read without
00034 locking the queue. It does NOT means a free-for-all is possible, with many
00035 threads writing or many threads reading. Those things would require locking,
00036 to avoid conflicts between the multiple threads acting on one end of the queue.
00037 
00038 \section queue_page_sec_2 How does it work?
00039 ???.
00040 */
00041 
00042 #if !defined(_SPANDSP_QUEUE_H_)
00043 #define _SPANDSP_QUEUE_H_
00044 
00045 /*! Flag bit to indicate queue reads are atomic operations. This must be set
00046     if the queue is to be used with the message oriented functions. */
00047 #define QUEUE_READ_ATOMIC   0x0001
00048 /*! Flag bit to indicate queue writes are atomic operations. This must be set
00049     if the queue is to be used with the message oriented functions. */
00050 #define QUEUE_WRITE_ATOMIC  0x0002
00051 
00052 /*!
00053     Queue descriptor. This defines the working state for a single instance of
00054     a byte stream or message oriented queue.
00055 */
00056 typedef struct
00057 {
00058     /*! \brief Flags indicating the mode of the queue. */
00059     int flags;
00060     /*! \brief The length of the data buffer. */
00061     int len;
00062     /*! \brief The buffer input pointer. */
00063     volatile int iptr;
00064     /*! \brief The buffer output pointer. */
00065     volatile int optr;
00066 #if defined(FULLY_DEFINE_QUEUE_STATE_T)
00067     /*! \brief The data buffer, sized at the time the structure is created. */
00068     uint8_t data[];
00069 #endif
00070 } queue_state_t;
00071 
00072 #define QUEUE_STATE_T_SIZE(len) (sizeof(queue_state_t) + len + 1)
00073 
00074 #if defined(__cplusplus)
00075 extern "C"
00076 {
00077 #endif
00078 
00079 /*! Check if a queue is empty.
00080     \brief Check if a queue is empty.
00081     \param s The queue context.
00082     \return TRUE if empty, else FALSE. */
00083 int queue_empty(queue_state_t *s);
00084 
00085 /*! Check the available free space in a queue's buffer.
00086     \brief Check available free space.
00087     \param s The queue context.
00088     \return The number of bytes of free space. */
00089 int queue_free_space(queue_state_t *s);
00090 
00091 /*! Check the contents of a queue.
00092     \brief Check the contents of a queue.
00093     \param s The queue context.
00094     \return The number of bytes in the queue. */
00095 int queue_contents(queue_state_t *s);
00096 
00097 /*! Flush the contents of a queue.
00098     \brief Flush the contents of a queue.
00099     \param s The queue context. */
00100 void queue_flush(queue_state_t *s);
00101 
00102 /*! Copy bytes from a queue. This is similar to queue_read, but
00103     the data remains in the queue.
00104     \brief Copy bytes from a queue.
00105     \param s The queue context.
00106     \param buf The buffer into which the bytes will be read.
00107     \param len The length of the buffer.
00108     \return the number of bytes returned. */
00109 int queue_view(queue_state_t *s, uint8_t *buf, int len);
00110 
00111 /*! Read bytes from a queue.
00112     \brief Read bytes from a queue.
00113     \param s The queue context.
00114     \param buf The buffer into which the bytes will be read.
00115     \param len The length of the buffer.
00116     \return the number of bytes returned. */
00117 int queue_read(queue_state_t *s, uint8_t *buf, int len);
00118 
00119 /*! Read a byte from a queue.
00120     \brief Read a byte from a queue.
00121     \param s The queue context.
00122     \return the byte, or -1 if the queue is empty. */
00123 int queue_read_byte(queue_state_t *s);
00124 
00125 /*! Write bytes to a queue.
00126     \brief Write bytes to a queue.
00127     \param s The queue context.
00128     \param buf The buffer containing the bytes to be written.
00129     \param len The length of the buffer.
00130     \return the number of bytes actually written. */
00131 int queue_write(queue_state_t *s, const uint8_t *buf, int len);
00132 
00133 /*! Write a byte to a queue.
00134     \brief Write a byte to a queue.
00135     \param s The queue context.
00136     \param byte The byte to be written.
00137     \return the number of bytes actually written. */
00138 int queue_write_byte(queue_state_t *s, uint8_t byte);
00139 
00140 /*! Test the length of the message at the head of a queue.
00141     \brief Test message length.
00142     \param s The queue context.
00143     \return The length of the next message, in byte. If there are
00144             no messages in the queue, -1 is returned. */
00145 int queue_state_test_msg(queue_state_t *s);
00146 
00147 /*! Read a message from a queue. If the message is longer than the buffer
00148     provided, only the first len bytes of the message will be returned. The
00149     remainder of the message will be discarded.
00150     \brief Read a message from a queue.
00151     \param s The queue context.
00152     \param buf The buffer into which the message will be read.
00153     \param len The length of the buffer.
00154     \return The number of bytes returned. If there are
00155             no messages in the queue, -1 is returned. */
00156 int queue_read_msg(queue_state_t *s, uint8_t *buf, int len);
00157 
00158 /*! Write a message to a queue.
00159     \brief Write a message to a queue.
00160     \param s The queue context.
00161     \param buf The buffer from which the message will be written.
00162     \param len The length of the message.
00163     \return The number of bytes actually written. */
00164 int queue_write_msg(queue_state_t *s, const uint8_t *buf, int len);
00165 
00166 /*! Initialise a queue.
00167     \brief Initialise a queue.
00168     \param s The queue context. If is imperative that the context this
00169            points to is immediately followed by a buffer of the required
00170            size + 1 octet.
00171     \param len The length of the queue's buffer.
00172     \param flags Flags controlling the operation of the queue.
00173            Valid flags are QUEUE_READ_ATOMIC and QUEUE_WRITE_ATOMIC.
00174     \return A pointer to the context if OK, else NULL. */
00175 queue_state_t *queue_init(queue_state_t *s, int len, int flags);
00176 
00177 /*! Delete a queue.
00178     \brief Delete a queue.
00179     \param s The queue context.
00180     \return 0 if deleted OK, else -1. */
00181 int queue_free(queue_state_t *s);
00182 
00183 #if defined(__cplusplus)
00184 }
00185 #endif
00186 
00187 #endif
00188 /*- End of file ------------------------------------------------------------*/

Generated on Tue Oct 7 20:25:47 2008 for spandsp by  doxygen 1.5.6