1/* Copyright (c) 2012-2014, The Linux Foundation. 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 <pthread.h> 31#include "mm_jpeg_dbg.h" 32#include "mm_jpeg.h" 33 34int32_t mm_jpeg_queue_init(mm_jpeg_queue_t* queue) 35{ 36 pthread_mutex_init(&queue->lock, NULL); 37 cam_list_init(&queue->head.list); 38 queue->size = 0; 39 return 0; 40} 41 42int32_t mm_jpeg_queue_enq(mm_jpeg_queue_t* queue, mm_jpeg_q_data_t data) 43{ 44 mm_jpeg_q_node_t* node = 45 (mm_jpeg_q_node_t *)malloc(sizeof(mm_jpeg_q_node_t)); 46 if (NULL == node) { 47 CDBG_ERROR("%s: No memory for mm_jpeg_q_node_t", __func__); 48 return -1; 49 } 50 51 memset(node, 0, sizeof(mm_jpeg_q_node_t)); 52 node->data = data; 53 54 pthread_mutex_lock(&queue->lock); 55 cam_list_add_tail_node(&node->list, &queue->head.list); 56 queue->size++; 57 pthread_mutex_unlock(&queue->lock); 58 59 return 0; 60 61} 62 63int32_t mm_jpeg_queue_enq_head(mm_jpeg_queue_t* queue, mm_jpeg_q_data_t data) 64{ 65 struct cam_list *head = NULL; 66 struct cam_list *pos = NULL; 67 mm_jpeg_q_node_t* node = 68 (mm_jpeg_q_node_t *)malloc(sizeof(mm_jpeg_q_node_t)); 69 if (NULL == node) { 70 CDBG_ERROR("%s: No memory for mm_jpeg_q_node_t", __func__); 71 return -1; 72 } 73 74 memset(node, 0, sizeof(mm_jpeg_q_node_t)); 75 node->data = data; 76 77 head = &queue->head.list; 78 pos = head->next; 79 80 pthread_mutex_lock(&queue->lock); 81 cam_list_insert_before_node(&node->list, pos); 82 queue->size++; 83 pthread_mutex_unlock(&queue->lock); 84 85 return 0; 86} 87 88mm_jpeg_q_data_t mm_jpeg_queue_deq(mm_jpeg_queue_t* queue) 89{ 90 mm_jpeg_q_data_t data; 91 mm_jpeg_q_node_t* node = NULL; 92 struct cam_list *head = NULL; 93 struct cam_list *pos = NULL; 94 95 memset(&data, 0, sizeof(data)); 96 97 pthread_mutex_lock(&queue->lock); 98 head = &queue->head.list; 99 pos = head->next; 100 if (pos != head) { 101 node = member_of(pos, mm_jpeg_q_node_t, list); 102 cam_list_del_node(&node->list); 103 queue->size--; 104 } 105 pthread_mutex_unlock(&queue->lock); 106 107 if (NULL != node) { 108 data = node->data; 109 free(node); 110 } 111 112 return data; 113} 114 115uint32_t mm_jpeg_queue_get_size(mm_jpeg_queue_t* queue) 116{ 117 uint32_t size = 0; 118 119 pthread_mutex_lock(&queue->lock); 120 size = queue->size; 121 pthread_mutex_unlock(&queue->lock); 122 123 return size; 124 125} 126 127int32_t mm_jpeg_queue_deinit(mm_jpeg_queue_t* queue) 128{ 129 mm_jpeg_queue_flush(queue); 130 pthread_mutex_destroy(&queue->lock); 131 return 0; 132} 133 134int32_t mm_jpeg_queue_flush(mm_jpeg_queue_t* queue) 135{ 136 mm_jpeg_q_node_t* node = NULL; 137 struct cam_list *head = NULL; 138 struct cam_list *pos = NULL; 139 140 pthread_mutex_lock(&queue->lock); 141 head = &queue->head.list; 142 pos = head->next; 143 144 while(pos != head) { 145 node = member_of(pos, mm_jpeg_q_node_t, list); 146 cam_list_del_node(&node->list); 147 queue->size--; 148 149 /* for now we only assume there is no ptr inside data 150 * so we free data directly */ 151 if (NULL != node->data.p) { 152 free(node->data.p); 153 } 154 free(node); 155 pos = pos->next; 156 } 157 queue->size = 0; 158 pthread_mutex_unlock(&queue->lock); 159 return 0; 160} 161 162mm_jpeg_q_data_t mm_jpeg_queue_peek(mm_jpeg_queue_t* queue) 163{ 164 mm_jpeg_q_data_t data; 165 mm_jpeg_q_node_t* node = NULL; 166 struct cam_list *head = NULL; 167 struct cam_list *pos = NULL; 168 169 memset(&data, 0, sizeof(data)); 170 171 pthread_mutex_lock(&queue->lock); 172 head = &queue->head.list; 173 pos = head->next; 174 if (pos != head) { 175 node = member_of(pos, mm_jpeg_q_node_t, list); 176 } 177 pthread_mutex_unlock(&queue->lock); 178 179 if (NULL != node) { 180 data = node->data; 181 } 182 return data; 183} 184