Home | History | Annotate | Download | only in src
      1 /*--------------------------------------------------------------------------
      2 Copyright (c) 2010, Code Aurora Forum. All rights reserved.
      3 
      4 Redistribution and use in source and binary forms, with or without
      5 modification, are permitted provided that the following conditions are met:
      6     * Redistributions of source code must retain the above copyright
      7       notice, this list of conditions and the following disclaimer.
      8     * Redistributions in binary form must reproduce the above copyright
      9       notice, this list of conditions and the following disclaimer in the
     10       documentation and/or other materials provided with the distribution.
     11     * Neither the name of Code Aurora nor
     12       the names of its contributors may be used to endorse or promote
     13       products derived from this software without specific prior written
     14       permission.
     15 
     16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18 IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
     19 NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     20 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     21 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     22 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     23 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     24 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     25 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     26 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27 --------------------------------------------------------------------------*/
     28 #include "message_queue.h"
     29 
     30 int check_if_queue_empty ( unsigned int queuetocheck, void* queuecontext )
     31 {
     32     struct video_queue_context *ptr_q = NULL;
     33     /*
     34      * queuetocheck - 0 command queue
     35      * queuetocheck - 1 data queue
     36      */
     37     if ( queuecontext == NULL || (queuetocheck > 1 ) )
     38     {
     39         return 1;
     40     }
     41     ptr_q = (struct video_queue_context *)queuecontext;
     42 
     43     if (queuetocheck == 0)
     44     {
     45       if (ptr_q->read_comq == ptr_q->write_comq)
     46       {
     47           return 1;
     48       }
     49     }
     50     else if (queuetocheck == 1)
     51     {
     52         if (ptr_q->write_dataq == ptr_q->read_dataq)
     53         {
     54             return 1;
     55         }
     56     }
     57 
     58     return 0;
     59 }
     60 
     61 
     62 
     63 struct video_msgq * queue_get_cmd (void* queuecontext )
     64 {
     65     struct video_queue_context *ptr_q = NULL;
     66     struct video_msgq *pitem = NULL;
     67 
     68     if( NULL == queuecontext )
     69     {
     70         printf("\n queue_get_cmd: Invalid Input parameter\n");
     71         return NULL;
     72     }
     73 
     74     ptr_q = (struct video_queue_context *)queuecontext;
     75 
     76     /* Wait on the semaphore till it is released */
     77     sem_wait(&ptr_q->sem_message);
     78 
     79     /* Lock the mutex to protect the critical section */
     80     pthread_mutex_lock(&ptr_q->mutex);
     81 
     82     if (ptr_q->read_comq != ptr_q->write_comq)
     83     {
     84         pitem = &ptr_q->ptr_cmdq [ptr_q->read_comq];
     85         ptr_q->read_comq = (ptr_q->read_comq + 1) % \
     86                             ptr_q->commandq_size;
     87     }
     88     else if (ptr_q->write_dataq != ptr_q->read_dataq)
     89     {
     90         pitem = &ptr_q->ptr_dataq [ptr_q->read_dataq];
     91         ptr_q->read_dataq = (ptr_q->read_dataq + 1) % \
     92                             ptr_q->dataq_size;
     93     }
     94 
     95     /* Unlock the mutex to release the critical section */
     96     pthread_mutex_unlock(&ptr_q->mutex);
     97 
     98     return pitem;
     99 }
    100 
    101 
    102 int queue_post_cmdq ( void* queuecontext,
    103                      struct video_msgq *pitem
    104                      )
    105 {
    106     struct video_queue_context *ptr_q = NULL;
    107 
    108     if (pitem == NULL || queuecontext == NULL)
    109     {
    110         return -1;
    111     }
    112     ptr_q = (struct video_queue_context *)queuecontext;
    113 
    114     /* Lock the mutex to protect the critical section */
    115     pthread_mutex_lock(&ptr_q->mutex);
    116 
    117     if ((ptr_q->write_comq + 1) % ptr_q->commandq_size == ptr_q->read_comq)
    118     {
    119         printf("\n QUEUE is FULL");
    120         return 0;
    121     }
    122     else
    123     {
    124         /* Store the command in the Message Queue & increment write offset */
    125         memcpy ( &ptr_q->ptr_cmdq [ptr_q->write_comq],pitem, \
    126                  sizeof (struct video_msgq));
    127         ptr_q->write_comq = (ptr_q->write_comq + 1) % ptr_q->commandq_size;
    128     }
    129 
    130     /* Unlock the mutex to release the critical section */
    131     pthread_mutex_unlock(&ptr_q->mutex);
    132 
    133     /* Post the semaphore */
    134     sem_post(&ptr_q->sem_message);
    135     return 1;
    136 }
    137 
    138 
    139 int queue_post_dataq ( void *queuecontext,
    140                        struct video_msgq *pitem
    141                       )
    142 {
    143     struct video_queue_context *ptr_q = NULL;
    144 
    145     if (pitem == NULL || queuecontext == NULL)
    146     {
    147         return -1;
    148     }
    149     ptr_q = (struct video_queue_context *)queuecontext;
    150 
    151     /* Lock the mutex to protect the critical section */
    152     pthread_mutex_lock(&ptr_q->mutex);
    153 
    154     if ((ptr_q->write_dataq + 1) % ptr_q->dataq_size == ptr_q->read_dataq)
    155     {
    156         printf("\n QUEUE is FULL");
    157         return 0;
    158     }
    159     else
    160     {
    161         /* Store the command in the Message Queue & increment write offset */
    162         memcpy ( &ptr_q->ptr_dataq [ptr_q->write_dataq],pitem, \
    163                  sizeof (struct video_msgq));
    164         ptr_q->write_dataq = (ptr_q->write_dataq + 1) % ptr_q->dataq_size;
    165     }
    166 
    167     /* Unlock the mutex to release the critical section */
    168     pthread_mutex_unlock(&ptr_q->mutex);
    169 
    170     /* Post the semaphore */
    171     sem_post(&ptr_q->sem_message);
    172     return 1;
    173 
    174 }
    175