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