Simple tools for multi threading / objects in plain C Snapshot
tqueue.h
Go to the documentation of this file.
00001 /* Copyright (c) Michael Moser (2011) . 3-clause BSD License applies */
00002 
00003 #ifndef __TQUEUE__H_Y_Z_
00004 #define __TQUEUE__H_Y_Z_
00005 
00006 
00007 #include <cutils/dlist.h>
00008 #include <pthread.h>
00009 
00010 /**
00011  @defgroup TQUEUE
00012  
00013  @brief Implements a thread safe queue with maximum limit of requess
00014 
00015  @{
00016  */
00017 typedef struct tagTQUEUE {
00018   
00019   DLIST  dlist;
00020   size_t max_count;
00021   size_t waiting_empty;
00022 
00023   pthread_mutex_t mutex;
00024   pthread_cond_t  cond_empty;
00025   pthread_cond_t  cond_max;
00026 
00027 } TQUEUE;
00028 
00029 
00030 /**
00031  @brief create new queue 
00032  */
00033 int   TQUEUE_init(TQUEUE *queue, size_t max_count );
00034 
00035 /**
00036  @brief destroy queue
00037  */
00038 int   TQUEUE_free(TQUEUE *queue );
00039 
00040 /**
00041  @brief add new entry to queue, block if maximum queue limit has been reached 
00042 */
00043 int   TQUEUE_push_block_on_queue_full(TQUEUE *queue, void *entry);
00044 
00045 /**
00046  @brief add new entry to queue, fail if maximum queue limit has been reached 
00047 */
00048 int   TQUEUE_push_fail_on_queue_full(TQUEUE *queue, void *entry);
00049 
00050 /**
00051  @brief push out of order exit message (null message at top of queue)
00052 */
00053 int   TQUEUE_push_exit_message(TQUEUE *queue);
00054 
00055 /**
00056  @brief pop queue, block if empty
00057 */
00058 void *TQUEUE_pop(TQUEUE *queue);
00059 
00060 /**
00061  @brief pop queue, return error if empty. Does not block if queue is empty.
00062 */
00063 int   TQUEUE_pop_non_blocking(TQUEUE *queue, void **rret);
00064 
00065 /**
00066  @}
00067  */
00068 
00069 #endif
00070