PageRenderTime 13ms CodeModel.GetById 6ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/src/q.h

http://bdremote-ng.googlecode.com/
C++ Header | 115 lines | 31 code | 20 blank | 64 comment | 0 complexity | 50d09900abb1f4efaa59e496808b0db2 MD5 | raw file
  1/*
  2 *  bdremoteng - helper daemon for Sony(R) BD Remote Control
  3 *  Based on bdremoted, written by Anton Starikov <antst@mail.ru>.
  4 *
  5 *  Copyright (C) 2009  Michael Wojciechowski <wojci@wojci.dk>
  6 *
  7 *
  8 *  This program is free software; you can redistribute it and/or modify
  9 *  it under the terms of the GNU General Public License as published by
 10 *  the Free Software Foundation; either version 2 of the License, or
 11 *  (at your option) any later version.
 12 *
 13 *  This program is distributed in the hope that it will be useful,
 14 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16 *  GNU General Public License for more details.
 17 *
 18 *  You should have received a copy of the GNU General Public License
 19 *  along with this program; if not, write to the Free Software
 20 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 21 *
 22 */
 23
 24/* The following was based on some pthread examples written by Andrae
 25 * Muys.
 26 */
 27
 28/** \ingroup Gen */
 29/*@{*/
 30
 31/*! \file q.h
 32  \brief Thread safe queue.
 33
 34  Thread safe queue used for communication between bluetooth thread
 35  and LIRC thread.
 36
 37*/
 38
 39#ifndef BD_Q_H
 40#define BD_Q_H
 41
 42#include <globaldefs.h>
 43#include <pthread.h>
 44
 45/** Max queue size. */
 46#define QUEUESIZE 254
 47
 48/** OK */
 49#define Q_OK   0
 50
 51/** Error. */
 52#define Q_ERR -1
 53
 54/** Data stored in this queue. */
 55typedef struct
 56{
 57  /** Data. */
 58  char* buffer;
 59  /** Data size. */
 60  int   size;
 61} queueData;
 62
 63/** Queue. */
 64typedef struct
 65{
 66  /** Array of pointers to the data stored. */
 67  queueData* buf[QUEUESIZE];
 68  /** Head of the queue. */
 69  long             head;
 70  /** Tail of the queue. */
 71  long             tail;
 72  /** Indicates if the queue is full. */
 73  int              full;
 74  /** Indicates if the queue is empty. */
 75  int              empty;
 76  /** Mutex. */
 77  pthread_mutex_t* mut;
 78  /** Condition. */
 79  pthread_cond_t*  notFull;
 80  /** Condition. */
 81  pthread_cond_t*  notEmpty;
 82} queue;
 83
 84/** Allocate a struct used for keeping data in this queue. */
 85queueData* queueDataInit(const char* _s, int _size);
 86
 87/** Deallocate the struct used for keeping data in this queue. */
 88void queueDataDeInit(queueData* _qd);
 89
 90/** Init queue. */
 91int queueInit (queue* _q);
 92
 93/** Deinit queue. */
 94void queueDeinit (queue* _q);
 95
 96/** Add an element to the queue. Blocks if the queue is full.
 97 * Notice that the pointer belongs to the function.
 98 */
 99void queueAdd (queue* _q, queueData* _in);
100
101/** Remove an element from the queue. Blocks.
102 * Notice that the pointer should be disposed of by the caller.
103 */
104void queueRemBlock (queue* _q, queueData** _out);
105
106/** Remove an element from the queue. Returns Q_ERR if the queue is empty.
107 * Blocks for most _n ms.
108 * Notice that the pointer should be disposed of by the caller.
109 */
110int queueRemNonBlock (queue* _q, int _n, queueData** _out);
111
112#endif /* BD_Q_H */
113
114/*@}*/
115