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 General Public License version 2, as 00014 * 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 General Public License for more details. 00020 * 00021 * You should have received a copy of the GNU General Public License 00022 * 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.9 2007/04/30 13:13:36 steveu Exp $ 00026 */ 00027 00028 /*! \file */ 00029 00030 /*! \page queue_page Queuing 00031 \section queue_page_sec_1 What does it do? 00032 ???. 00033 00034 \section queue_page_sec_2 How does it work? 00035 ???. 00036 */ 00037 00038 #if !defined(_SPANDSP_QUEUE_H_) 00039 #define _SPANDSP_QUEUE_H_ 00040 00041 #define QUEUE_READ_ATOMIC 0x0001 00042 #define QUEUE_WRITE_ATOMIC 0x0002 00043 00044 typedef struct 00045 { 00046 int flags; 00047 int len; 00048 volatile int iptr; 00049 volatile int optr; 00050 uint8_t data[]; 00051 } queue_state_t; 00052 00053 #if defined(__cplusplus) 00054 extern "C" 00055 { 00056 #endif 00057 00058 /*! Check if a queue is empty. 00059 \brief Check if a queue is empty. 00060 \param s The queue context. 00061 \return TRUE if empty, else FALSE. */ 00062 int queue_empty(queue_state_t *s); 00063 00064 /*! Check the available free space in a queue's buffer. 00065 \brief Check available free space. 00066 \param s The queue context. 00067 \return The number of bytes of free space. */ 00068 int queue_free_space(queue_state_t *s); 00069 00070 /*! Check the contents of a queue. 00071 \brief Check the contents of a queue. 00072 \param s The queue context. 00073 \return The number of bytes in the queue. */ 00074 int queue_contents(queue_state_t *s); 00075 00076 /*! Flush the contents of a queue. 00077 \brief Flush the contents of a queue. 00078 \param s The queue context. */ 00079 void queue_flush(queue_state_t *s); 00080 00081 /*! Copy bytes from a queue. This is similar to queue_read, but 00082 the data remains in the queue. 00083 \brief Copy bytes from a queue. 00084 \param s The queue context. 00085 \param buf The buffer into which the bytes will be read. 00086 \param len The length of the buffer. 00087 \return the number of bytes returned. */ 00088 int queue_view(queue_state_t *s, uint8_t *buf, int len); 00089 00090 /*! Read bytes from a queue. 00091 \brief Read bytes from a queue. 00092 \param s The queue context. 00093 \param buf The buffer into which the bytes will be read. 00094 \param len The length of the buffer. 00095 \return the number of bytes returned. */ 00096 int queue_read(queue_state_t *s, uint8_t *buf, int len); 00097 00098 /*! Write bytes to a queue. 00099 \brief Write bytes to a queue. 00100 \param s The queue context. 00101 \param buf The buffer containing the bytes to be written. 00102 \param len The length of the buffer. 00103 \return the number of bytes actually written. */ 00104 int queue_write(queue_state_t *s, const uint8_t *buf, int len); 00105 00106 /*! Test the length of the message at the head of a queue. 00107 \brief Test message length. 00108 \param s The queue context. 00109 \return The length of the next message, in byte. If there are 00110 no messages in the queue, -1 is returned. */ 00111 int queue_state_test_msg(queue_state_t *s); 00112 00113 /*! Read a message from a queue. If the message is longer than the buffer 00114 provided, only the first len bytes of the message will be returned. The 00115 remainder of the message will be discarded. 00116 \brief Read a message from a queue. 00117 \param s The queue context. 00118 \param buf The buffer into which the message will be read. 00119 \param len The length of the buffer. 00120 \return The number of bytes returned. If there are 00121 no messages in the queue, -1 is returned. */ 00122 int queue_read_msg(queue_state_t *s, uint8_t *buf, int len); 00123 00124 /*! Write a message to a queue. 00125 \brief Write a message to a queue. 00126 \param s The queue context. 00127 \param buf The buffer from which the message will be written. 00128 \param len The length of the message. 00129 \return The number of bytes actually written. */ 00130 int queue_write_msg(queue_state_t *s, const uint8_t *buf, int len); 00131 00132 /*! Create a queue. 00133 \brief Create a queue. 00134 \param len The length of the queue's buffer. 00135 \param flags Flags controlling the operation of the queue. 00136 Valid flags are QUEUE_READ_ATOMIC and QUEUE_WRITE_ATOMIC. 00137 \return A pointer to the context if created OK, else NULL. */ 00138 queue_state_t *queue_create(int len, int flags); 00139 00140 /*! Delete a queue. 00141 \brief Delete a queue. 00142 \param s The queue context. 00143 \return 0 if deleted OK, else -1. */ 00144 int queue_delete(queue_state_t *s); 00145 00146 #if defined(__cplusplus) 00147 } 00148 #endif 00149 00150 #endif 00151 /*- End of file ------------------------------------------------------------*/