Home | History | Annotate | Download | only in src
      1 /*
      2 Copyright (c) 2012, The Linux Foundation. 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
      6 met:
      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 The Linux Foundation 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 
     17 THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
     18 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
     20 ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
     21 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
     24 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     25 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     26 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
     27 IF 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 
     34 int32_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 
     42 int32_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 
     63 
     64 void* mm_jpeg_queue_peek(mm_jpeg_queue_t* queue)
     65 {
     66     mm_jpeg_q_node_t* node = NULL;
     67     void* data = NULL;
     68     struct cam_list *head = NULL;
     69     struct cam_list *pos = NULL;
     70 
     71     pthread_mutex_lock(&queue->lock);
     72     head = &queue->head.list;
     73     pos = head->next;
     74     if (pos != head) {
     75         node = member_of(pos, mm_jpeg_q_node_t, list);
     76     }
     77     pthread_mutex_unlock(&queue->lock);
     78 
     79     if (NULL != node) {
     80         data = node->data;
     81     }
     82     return data;
     83 }
     84 
     85 void* mm_jpeg_queue_deq(mm_jpeg_queue_t* queue)
     86 {
     87     mm_jpeg_q_node_t* node = NULL;
     88     void* data = NULL;
     89     struct cam_list *head = NULL;
     90     struct cam_list *pos = NULL;
     91 
     92     pthread_mutex_lock(&queue->lock);
     93     head = &queue->head.list;
     94     pos = head->next;
     95     if (pos != head) {
     96         node = member_of(pos, mm_jpeg_q_node_t, list);
     97         cam_list_del_node(&node->list);
     98         queue->size--;
     99     }
    100     pthread_mutex_unlock(&queue->lock);
    101 
    102     if (NULL != node) {
    103         data = node->data;
    104         free(node);
    105     }
    106 
    107     return data;
    108 }
    109 
    110 uint32_t mm_jpeg_queue_get_size(mm_jpeg_queue_t* queue)
    111 {
    112     uint32_t size = 0;
    113 
    114     pthread_mutex_lock(&queue->lock);
    115     size = queue->size;
    116     pthread_mutex_unlock(&queue->lock);
    117 
    118     return size;
    119 
    120 }
    121 
    122 int32_t mm_jpeg_queue_deinit(mm_jpeg_queue_t* queue)
    123 {
    124     mm_jpeg_queue_flush(queue);
    125     pthread_mutex_destroy(&queue->lock);
    126     return 0;
    127 }
    128 
    129 int32_t mm_jpeg_queue_flush(mm_jpeg_queue_t* queue)
    130 {
    131     mm_jpeg_q_node_t* node = NULL;
    132     void* data = NULL;
    133     struct cam_list *head = NULL;
    134     struct cam_list *pos = NULL;
    135 
    136     pthread_mutex_lock(&queue->lock);
    137     head = &queue->head.list;
    138     pos = head->next;
    139 
    140     while(pos != head) {
    141         node = member_of(pos, mm_jpeg_q_node_t, list);
    142         cam_list_del_node(&node->list);
    143         queue->size--;
    144 
    145         /* for now we only assume there is no ptr inside data
    146          * so we free data directly */
    147         if (NULL != node->data) {
    148             free(node->data);
    149         }
    150         free(node);
    151         pos = pos->next;
    152     }
    153     queue->size = 0;
    154     pthread_mutex_unlock(&queue->lock);
    155     return 0;
    156 }
    157