forked from rellla/libvdpau-sunxi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.c
186 lines (152 loc) · 3.07 KB
/
queue.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* Copyright (c) 2015 Andreas Baierl <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "queue.h"
/* initialize queue
*
*/
QUEUE *q_queue_init(void)
{
QUEUE *queue = (QUEUE *) calloc(1, sizeof(QUEUE));
queue->head = queue->tail = NULL;
queue->length = 0;
pthread_mutex_init(&queue->mutex, NULL);
return queue;
}
/* allocate node data
*
*/
NODE *allocate_node(void *data)
{
NODE *node = (NODE *)calloc(1, sizeof(NODE));
node->next = node->prev = NULL;
node->data = data;
return (node);
}
/* push node to the tail position
*
*/
qStatus q_push_tail(QUEUE *queue, void *data)
{
if (!queue || !data)
return Q_ERROR;
NODE *node = allocate_node(data);
if (!node)
return Q_ERROR;
q_lock(queue);
if (queue->head == NULL)
queue->head = node;
else
queue->tail->next = node;
node->prev = queue->tail;
queue->tail = node;
queue->length++;
q_unlock(queue);
return Q_SUCCESS;
}
/* drop node at head position
*
*/
qStatus q_pop_head(QUEUE *queue, void **data)
{
if (!queue)
return Q_ERROR;
q_lock(queue);
if (!queue->head)
{
q_unlock(queue);
return Q_ERROR;
}
*data = queue->head->data;
NODE *tmp = queue->head;
queue->head = queue->head->next;
if (queue->head == NULL)
queue->tail = NULL;
else
queue->head->prev = NULL;
queue->length--;
q_node_free(tmp, 0);
q_unlock(queue);
return Q_SUCCESS;
}
/* check, if queue is empty
*
*/
qStatus q_isEmpty(QUEUE *queue)
{
if (queue->length == 0)
return Q_EMPTY;
return Q_SUCCESS;
}
/* return the length of the queue
*
*/
int q_length(QUEUE *queue)
{
return queue->length;
}
/* free queue and all nodes
*
*/
qStatus q_queue_free(QUEUE *queue)
{
if (!queue)
return Q_ERROR;
q_lock(queue);
NODE *tmp = queue->head;
while(tmp)
{
NODE *next = tmp->next;
q_node_free(tmp, 1);
tmp = next;
}
q_unlock(queue);
pthread_mutex_destroy(&queue->mutex);
free(queue);
return Q_SUCCESS;
}
/* free node and allocated data
*
*/
qStatus q_node_free(NODE *node, int data_free)
{
if (!node)
return Q_ERROR;
if (data_free && node->data)
free(node->data);
free(node);
return Q_SUCCESS;
}
/* lock the queue
*
*/
qStatus q_lock(QUEUE *queue)
{
if(pthread_mutex_lock(&queue->mutex))
return Q_LOCKERROR;
return Q_SUCCESS;
}
/* unlock the queue
*
*/
qStatus q_unlock(QUEUE *queue)
{
if(pthread_mutex_unlock(&queue->mutex))
return Q_LOCKERROR;
return Q_SUCCESS;
}