message_queue.c revision 8767726b6d6b2194cbb3704d7d82f0c7ef34eeb3
1/*--------------------------------------------------------------------------
2Copyright (c) 2010-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 "message_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("queue_get_cmd: Invalid Input parameter");
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("QUEUE is FULL");
110        /* Unlock the mutex to release the critical section */
111        pthread_mutex_unlock(&ptr_q->mutex);
112        return 0;
113    } else {
114        /* Store the command in the Message Queue & increment write offset */
115        memcpy ( &ptr_q->ptr_cmdq [ptr_q->write_comq],pitem, \
116                sizeof (struct video_msgq));
117        ptr_q->write_comq = (ptr_q->write_comq + 1) % ptr_q->commandq_size;
118    }
119
120    /* Unlock the mutex to release the critical section */
121    pthread_mutex_unlock(&ptr_q->mutex);
122
123    /* Post the semaphore */
124    sem_post(&ptr_q->sem_message);
125    return 1;
126}
127
128
129int queue_post_dataq ( void *queuecontext,
130        struct video_msgq *pitem
131        )
132{
133    struct video_queue_context *ptr_q = NULL;
134
135    if (pitem == NULL || queuecontext == NULL) {
136        return -1;
137    }
138
139    ptr_q = (struct video_queue_context *)queuecontext;
140
141    /* Lock the mutex to protect the critical section */
142    pthread_mutex_lock(&ptr_q->mutex);
143
144    if ((ptr_q->write_dataq + 1) % ptr_q->dataq_size == ptr_q->read_dataq) {
145        printf("QUEUE is FULL");
146        /* Unlock the mutex to release the critical section */
147        pthread_mutex_unlock(&ptr_q->mutex);
148        return 0;
149    } else {
150        /* Store the command in the Message Queue & increment write offset */
151        memcpy ( &ptr_q->ptr_dataq [ptr_q->write_dataq],pitem, \
152                sizeof (struct video_msgq));
153        ptr_q->write_dataq = (ptr_q->write_dataq + 1) % ptr_q->dataq_size;
154    }
155
156    /* Unlock the mutex to release the critical section */
157    pthread_mutex_unlock(&ptr_q->mutex);
158
159    /* Post the semaphore */
160    sem_post(&ptr_q->sem_message);
161    return 1;
162
163}
164