1 /*--------------------------------------------------------------------------
2 Copyright (c) 2010-2012, 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
check_if_queue_empty(unsigned int queuetocheck,void * queuecontext)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
queue_get_cmd(void * queuecontext)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
queue_post_cmdq(void * queuecontext,struct video_msgq * pitem)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 /* Unlock the mutex to release the critical section */
121 pthread_mutex_unlock(&ptr_q->mutex);
122 return 0;
123 }
124 else
125 {
126 /* Store the command in the Message Queue & increment write offset */
127 memcpy ( &ptr_q->ptr_cmdq [ptr_q->write_comq],pitem, \
128 sizeof (struct video_msgq));
129 ptr_q->write_comq = (ptr_q->write_comq + 1) % ptr_q->commandq_size;
130 }
131
132 /* Unlock the mutex to release the critical section */
133 pthread_mutex_unlock(&ptr_q->mutex);
134
135 /* Post the semaphore */
136 sem_post(&ptr_q->sem_message);
137 return 1;
138 }
139
140
queue_post_dataq(void * queuecontext,struct video_msgq * pitem)141 int queue_post_dataq ( void *queuecontext,
142 struct video_msgq *pitem
143 )
144 {
145 struct video_queue_context *ptr_q = NULL;
146
147 if (pitem == NULL || queuecontext == NULL)
148 {
149 return -1;
150 }
151 ptr_q = (struct video_queue_context *)queuecontext;
152
153 /* Lock the mutex to protect the critical section */
154 pthread_mutex_lock(&ptr_q->mutex);
155
156 if ((ptr_q->write_dataq + 1) % ptr_q->dataq_size == ptr_q->read_dataq)
157 {
158 printf("\n QUEUE is FULL");
159 /* Unlock the mutex to release the critical section */
160 pthread_mutex_unlock(&ptr_q->mutex);
161 return 0;
162 }
163 else
164 {
165 /* Store the command in the Message Queue & increment write offset */
166 memcpy ( &ptr_q->ptr_dataq [ptr_q->write_dataq],pitem, \
167 sizeof (struct video_msgq));
168 ptr_q->write_dataq = (ptr_q->write_dataq + 1) % ptr_q->dataq_size;
169 }
170
171 /* Unlock the mutex to release the critical section */
172 pthread_mutex_unlock(&ptr_q->mutex);
173
174 /* Post the semaphore */
175 sem_post(&ptr_q->sem_message);
176 return 1;
177
178 }
179