1/* Copyright (c) 2012-2013, 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, 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
136void* mm_jpeg_queue_peek(mm_jpeg_queue_t* queue)
137{
138    mm_jpeg_q_node_t* node = NULL;
139    void* data = NULL;
140    struct cam_list *head = NULL;
141    struct cam_list *pos = NULL;
142
143    pthread_mutex_lock(&queue->lock);
144    head = &queue->head.list;
145    pos = head->next;
146    if (pos != head) {
147        node = member_of(pos, mm_jpeg_q_node_t, list);
148    }
149    pthread_mutex_unlock(&queue->lock);
150
151    if (NULL != node) {
152        data = node->data;
153    }
154    return data;
155}
156