1/* 2Copyright (c) 2012, Code Aurora Forum. All rights reserved. 3 4Redistribution and use in source and binary forms, with or without 5modification, are permitted provided that the following conditions are 6met: 7 * Redistributions of source code must retain the above copyright 8 notice, this list of conditions and the following disclaimer. 9 * Redistributions in binary form must reproduce the above 10 copyright notice, this list of conditions and the following 11 disclaimer in the documentation and/or other materials provided 12 with the distribution. 13 * Neither the name of Code Aurora Forum, Inc. nor the names of its 14 contributors may be used to endorse or promote products derived 15 from this software without specific prior written permission. 16 17THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED 18WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT 20ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 21BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 24BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 25WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 26OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 27IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 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, void* 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 63void* mm_jpeg_queue_deq(mm_jpeg_queue_t* queue) 64{ 65 mm_jpeg_q_node_t* node = NULL; 66 void* data = NULL; 67 struct cam_list *head = NULL; 68 struct cam_list *pos = NULL; 69 70 pthread_mutex_lock(&queue->lock); 71 head = &queue->head.list; 72 pos = head->next; 73 if (pos != head) { 74 node = member_of(pos, mm_jpeg_q_node_t, list); 75 cam_list_del_node(&node->list); 76 queue->size--; 77 } 78 pthread_mutex_unlock(&queue->lock); 79 80 if (NULL != node) { 81 data = node->data; 82 free(node); 83 } 84 85 return data; 86} 87 88uint32_t mm_jpeg_queue_get_size(mm_jpeg_queue_t* queue) 89{ 90 uint32_t size = 0; 91 92 pthread_mutex_lock(&queue->lock); 93 size = queue->size; 94 pthread_mutex_unlock(&queue->lock); 95 96 return size; 97 98} 99 100int32_t mm_jpeg_queue_deinit(mm_jpeg_queue_t* queue) 101{ 102 mm_jpeg_queue_flush(queue); 103 pthread_mutex_destroy(&queue->lock); 104 return 0; 105} 106 107int32_t mm_jpeg_queue_flush(mm_jpeg_queue_t* queue) 108{ 109 mm_jpeg_q_node_t* node = NULL; 110 void* data = NULL; 111 struct cam_list *head = NULL; 112 struct cam_list *pos = NULL; 113 114 pthread_mutex_lock(&queue->lock); 115 head = &queue->head.list; 116 pos = head->next; 117 118 while(pos != head) { 119 node = member_of(pos, mm_jpeg_q_node_t, list); 120 cam_list_del_node(&node->list); 121 queue->size--; 122 123 /* for now we only assume there is no ptr inside data 124 * so we free data directly */ 125 if (NULL != node->data) { 126 free(node->data); 127 } 128 free(node); 129 pos = pos->next; 130 } 131 queue->size = 0; 132 pthread_mutex_unlock(&queue->lock); 133 return 0; 134} 135