1 /* Copyright (c) 2012, The Linux Foundataion. All rights reserved.
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * 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
9 *       copyright notice, this list of conditions and the following
10 *       disclaimer in the documentation and/or other materials provided
11 *       with the distribution.
12 *     * Neither the name of The Linux Foundation nor the names of its
13 *       contributors may be used to endorse or promote products derived
14 *       from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29 
30 #include "mm_qcamera_dbg.h"
31 #include "mm_qcamera_app.h"
32 
mm_camera_queue_init(mm_camera_queue_t * queue,release_data_fn data_rel_fn,void * user_data)33 int mm_camera_queue_init(mm_camera_queue_t *queue,
34                          release_data_fn data_rel_fn,
35                          void *user_data)
36 {
37     if ( NULL == queue ) {
38         return -1;
39     }
40 
41     pthread_mutex_init(&queue->m_lock, NULL);
42     cam_list_init(&queue->m_head.list);
43     queue->m_size = 0;
44     queue->m_dataFn = data_rel_fn;
45     queue->m_userData = user_data;
46 
47     return MM_CAMERA_OK;
48 }
49 
mm_qcamera_queue_release(mm_camera_queue_t * queue)50 int mm_qcamera_queue_release(mm_camera_queue_t *queue)
51 {
52     if ( NULL == queue ) {
53         return -1;
54     }
55 
56     mm_qcamera_queue_flush(queue);
57     pthread_mutex_destroy(&queue->m_lock);
58 
59     return MM_CAMERA_OK;
60 }
61 
mm_qcamera_queue_isempty(mm_camera_queue_t * queue)62 int mm_qcamera_queue_isempty(mm_camera_queue_t *queue)
63 {
64     if ( NULL == queue ) {
65         return 0;
66     }
67 
68     int flag = 1;
69     pthread_mutex_lock(&queue->m_lock);
70     if (queue->m_size > 0) {
71         flag = 0;
72     }
73     pthread_mutex_unlock(&queue->m_lock);
74 
75     return flag;
76 }
77 
mm_qcamera_queue_enqueue(mm_camera_queue_t * queue,void * data)78 int mm_qcamera_queue_enqueue(mm_camera_queue_t *queue, void *data)
79 {
80     if ( NULL == queue ) {
81         return -1;
82     }
83 
84     camera_q_node *node =
85         (camera_q_node *)malloc(sizeof(camera_q_node));
86     if (NULL == node) {
87         CDBG_ERROR("%s: No memory for camera_q_node", __func__);
88         return 0;
89     }
90 
91     memset(node, 0, sizeof(camera_q_node));
92     node->data = data;
93 
94     pthread_mutex_lock(&queue->m_lock);
95     cam_list_add_tail_node(&node->list, &queue->m_head.list);
96     queue->m_size++;
97     pthread_mutex_unlock(&queue->m_lock);
98 
99     return 1;
100 }
101 
mm_qcamera_queue_dequeue(mm_camera_queue_t * queue,int bFromHead)102 void* mm_qcamera_queue_dequeue(mm_camera_queue_t *queue, int bFromHead)
103 {
104     if ( NULL == queue ) {
105         return NULL;
106     }
107 
108     camera_q_node* node = NULL;
109     void* data = NULL;
110     struct cam_list *head = NULL;
111     struct cam_list *pos = NULL;
112 
113     pthread_mutex_lock(&queue->m_lock);
114     head = &queue->m_head.list;
115     if (bFromHead) {
116         pos = head->next;
117     } else {
118         pos = head->prev;
119     }
120     if (pos != head) {
121         node = member_of(pos, camera_q_node, list);
122         cam_list_del_node(&node->list);
123         queue->m_size--;
124     }
125     pthread_mutex_unlock(&queue->m_lock);
126 
127     if (NULL != node) {
128         data = node->data;
129         free(node);
130     }
131 
132     return data;
133 }
134 
mm_qcamera_queue_flush(mm_camera_queue_t * queue)135 void mm_qcamera_queue_flush(mm_camera_queue_t *queue)
136 {
137     camera_q_node* node = NULL;
138     struct cam_list *head = NULL;
139     struct cam_list *pos = NULL;
140 
141     if ( NULL == queue ) {
142         return;
143     }
144 
145     pthread_mutex_lock(&queue->m_lock);
146     head = &queue->m_head.list;
147     pos = head->next;
148 
149     while(pos != head) {
150         node = member_of(pos, camera_q_node, list);
151         pos = pos->next;
152         cam_list_del_node(&node->list);
153         queue->m_size--;
154 
155         if (NULL != node->data) {
156             if (queue->m_dataFn) {
157                 queue->m_dataFn(node->data, queue->m_userData);
158             }
159             free(node->data);
160         }
161         free(node);
162 
163     }
164     queue->m_size = 0;
165     pthread_mutex_unlock(&queue->m_lock);
166 }
167 
168