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.16 2008/05/30 13:51:28 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 /*! \brief The data buffer, sized at the time the structure is created. */ 00067 uint8_t data[]; 00068 } queue_state_t; 00069 00070 #define QUEUE_STATE_T_SIZE(len) (sizeof(queue_state_t) + len + 1) 00071 00072 #if defined(__cplusplus) 00073 extern "C" 00074 { 00075 #endif 00076 00077 /*! Check if a queue is empty. 00078 \brief Check if a queue is empty. 00079 \param s The queue context. 00080 \return TRUE if empty, else FALSE. */ 00081 int queue_empty(queue_state_t *s); 00082 00083 /*! Check the available free space in a queue's buffer. 00084 \brief Check available free space. 00085 \param s The queue context. 00086 \return The number of bytes of free space. */ 00087 int queue_free_space(queue_state_t *s); 00088 00089 /*! Check the contents of a queue. 00090 \brief Check the contents of a queue. 00091 \param s The queue context. 00092 \return The number of bytes in the queue. */ 00093 int queue_contents(queue_state_t *s); 00094 00095 /*! Flush the contents of a queue. 00096 \brief Flush the contents of a queue. 00097 \param s The queue context. */ 00098 void queue_flush(queue_state_t *s); 00099 00100 /*! Copy bytes from a queue. This is similar to queue_read, but 00101 the data remains in the queue. 00102 \brief Copy bytes from a queue. 00103 \param s The queue context. 00104 \param buf The buffer into which the bytes will be read. 00105 \param len The length of the buffer. 00106 \return the number of bytes returned. */ 00107 int queue_view(queue_state_t *s, uint8_t *buf, int len); 00108 00109 /*! Read bytes from a queue. 00110 \brief Read bytes from a queue. 00111 \param s The queue context. 00112 \param buf The buffer into which the bytes will be read. 00113 \param len The length of the buffer. 00114 \return the number of bytes returned. */ 00115 int queue_read(queue_state_t *s, uint8_t *buf, int len); 00116 00117 /*! Read a byte from a queue. 00118 \brief Read a byte from a queue. 00119 \param s The queue context. 00120 \return the byte, or -1 if the queue is empty. */ 00121 int queue_read_byte(queue_state_t *s); 00122 00123 /*! Write bytes to a queue. 00124 \brief Write bytes to a queue. 00125 \param s The queue context. 00126 \param buf The buffer containing the bytes to be written. 00127 \param len The length of the buffer. 00128 \return the number of bytes actually written. */ 00129 int queue_write(queue_state_t *s, const uint8_t *buf, int len); 00130 00131 /*! Write a byte to a queue. 00132 \brief Write a byte to a queue. 00133 \param s The queue context. 00134 \param byte The byte to be written. 00135 \return the number of bytes actually written. */ 00136 int queue_write_byte(queue_state_t *s, uint8_t byte); 00137 00138 /*! Test the length of the message at the head of a queue. 00139 \brief Test message length. 00140 \param s The queue context. 00141 \return The length of the next message, in byte. If there are 00142 no messages in the queue, -1 is returned. */ 00143 int queue_state_test_msg(queue_state_t *s); 00144 00145 /*! Read a message from a queue. If the message is longer than the buffer 00146 provided, only the first len bytes of the message will be returned. The 00147 remainder of the message will be discarded. 00148 \brief Read a message from a queue. 00149 \param s The queue context. 00150 \param buf The buffer into which the message will be read. 00151 \param len The length of the buffer. 00152 \return The number of bytes returned. If there are 00153 no messages in the queue, -1 is returned. */ 00154 int queue_read_msg(queue_state_t *s, uint8_t *buf, int len); 00155 00156 /*! Write a message to a queue. 00157 \brief Write a message to a queue. 00158 \param s The queue context. 00159 \param buf The buffer from which the message will be written. 00160 \param len The length of the message. 00161 \return The number of bytes actually written. */ 00162 int queue_write_msg(queue_state_t *s, const uint8_t *buf, int len); 00163 00164 /*! Initialise a queue. 00165 \brief Initialise a queue. 00166 \param s The queue context. If is imperative that the context this 00167 points to is immediately followed by a buffer of the required 00168 size + 1 octet. 00169 \param len The length of the queue's buffer. 00170 \param flags Flags controlling the operation of the queue. 00171 Valid flags are QUEUE_READ_ATOMIC and QUEUE_WRITE_ATOMIC. 00172 \return A pointer to the context if OK, else NULL. */ 00173 queue_state_t *queue_init(queue_state_t *s, int len, int flags); 00174 00175 /*! Delete a queue. 00176 \brief Delete a queue. 00177 \param s The queue context. 00178 \return 0 if deleted OK, else -1. */ 00179 int queue_free(queue_state_t *s); 00180 00181 #if defined(__cplusplus) 00182 } 00183 #endif 00184 00185 #endif 00186 /*- End of file ------------------------------------------------------------*/