1/*-------------------------------------------------------------------------- 2Copyright (c) 2010-2011, 2013, The Linux Foundation. All rights reserved. 3 4Redistribution and use in source and binary forms, with or without 5modification, 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 The Linux Foundation 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 16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 20CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 21EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 22PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 23OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 25OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 26ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27--------------------------------------------------------------------------*/ 28#include "queue.h" 29 30int check_if_queue_empty ( unsigned int queuetocheck, void* queuecontext ) 31{ 32 struct video_queue_context *ptr_q = NULL; 33 34 /* 35 * queuetocheck - 0 command queue 36 * queuetocheck - 1 data queue 37 */ 38 if ( queuecontext == NULL || (queuetocheck > 1 ) ) { 39 return 1; 40 } 41 42 ptr_q = (struct video_queue_context *)queuecontext; 43 44 if (queuetocheck == 0) { 45 if (ptr_q->read_comq == ptr_q->write_comq) { 46 return 1; 47 } 48 } else if (queuetocheck == 1) { 49 if (ptr_q->write_dataq == ptr_q->read_dataq) { 50 return 1; 51 } 52 } 53 54 return 0; 55} 56 57 58 59struct video_msgq * queue_get_cmd (void* queuecontext ) { 60 struct video_queue_context *ptr_q = NULL; 61 struct video_msgq *pitem = NULL; 62 63 if ( NULL == queuecontext ) { 64 printf("\n queue_get_cmd: Invalid Input parameter\n"); 65 return NULL; 66 } 67 68 ptr_q = (struct video_queue_context *)queuecontext; 69 70 /* Wait on the semaphore till it is released */ 71 sem_wait(&ptr_q->sem_message); 72 73 /* Lock the mutex to protect the critical section */ 74 pthread_mutex_lock(&ptr_q->mutex); 75 76 if (ptr_q->read_comq != ptr_q->write_comq) { 77 pitem = &ptr_q->ptr_cmdq [ptr_q->read_comq]; 78 ptr_q->read_comq = (ptr_q->read_comq + 1) % \ 79 ptr_q->commandq_size; 80 } else if (ptr_q->write_dataq != ptr_q->read_dataq) { 81 pitem = &ptr_q->ptr_dataq [ptr_q->read_dataq]; 82 ptr_q->read_dataq = (ptr_q->read_dataq + 1) % \ 83 ptr_q->dataq_size; 84 } 85 86 /* Unlock the mutex to release the critical section */ 87 pthread_mutex_unlock(&ptr_q->mutex); 88 89 return pitem; 90} 91 92 93int queue_post_cmdq ( void* queuecontext, 94 struct video_msgq *pitem 95 ) 96{ 97 struct video_queue_context *ptr_q = NULL; 98 99 if (pitem == NULL || queuecontext == NULL) { 100 return -1; 101 } 102 103 ptr_q = (struct video_queue_context *)queuecontext; 104 105 /* Lock the mutex to protect the critical section */ 106 pthread_mutex_lock(&ptr_q->mutex); 107 108 if ((ptr_q->write_comq + 1) % ptr_q->commandq_size == ptr_q->read_comq) { 109 printf("\n QUEUE is FULL"); 110 return 0; 111 } else { 112 /* Store the command in the Message Queue & increment write offset */ 113 memcpy ( &ptr_q->ptr_cmdq [ptr_q->write_comq],pitem, \ 114 sizeof (struct video_msgq)); 115 ptr_q->write_comq = (ptr_q->write_comq + 1) % ptr_q->commandq_size; 116 } 117 118 /* Unlock the mutex to release the critical section */ 119 pthread_mutex_unlock(&ptr_q->mutex); 120 121 /* Post the semaphore */ 122 sem_post(&ptr_q->sem_message); 123 return 1; 124} 125 126 127int queue_post_dataq ( void *queuecontext, 128 struct video_msgq *pitem 129 ) 130{ 131 struct video_queue_context *ptr_q = NULL; 132 133 if (pitem == NULL || queuecontext == NULL) { 134 return -1; 135 } 136 137 ptr_q = (struct video_queue_context *)queuecontext; 138 139 /* Lock the mutex to protect the critical section */ 140 pthread_mutex_lock(&ptr_q->mutex); 141 142 if ((ptr_q->write_dataq + 1) % ptr_q->dataq_size == ptr_q->read_dataq) { 143 printf("\n QUEUE is FULL"); 144 return 0; 145 } else { 146 /* Store the command in the Message Queue & increment write offset */ 147 memcpy ( &ptr_q->ptr_dataq [ptr_q->write_dataq],pitem, \ 148 sizeof (struct video_msgq)); 149 ptr_q->write_dataq = (ptr_q->write_dataq + 1) % ptr_q->dataq_size; 150 } 151 152 /* Unlock the mutex to release the critical section */ 153 pthread_mutex_unlock(&ptr_q->mutex); 154 155 /* Post the semaphore */ 156 sem_post(&ptr_q->sem_message); 157 return 1; 158 159} 160