felipec / gst-dsp

GStreamer elements for TI's OMAP DSP

cymacs (author)
Thu Aug 13 08:21:14 -0700 2009
felipec (committer)
Sat Sep 19 04:01:59 -0700 2009
gst-dsp / async_queue.c
100644 149 lines (119 sloc) 2.859 kb
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
/*
* Copyright (C) 2008-2009 Nokia Corporation.
*
* Author: Felipe Contreras <felipe.contreras@nokia.com>
*
* 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
* version 2.1 of the License.
*
* 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 <glib.h>
 
#include "async_queue.h"
 
AsyncQueue *
async_queue_new(void)
{
AsyncQueue *queue;
 
queue = g_slice_new0(AsyncQueue);
 
queue->condition = g_cond_new();
queue->mutex = g_mutex_new();
queue->enabled = TRUE;
 
return queue;
}
 
void
async_queue_free(AsyncQueue *queue)
{
g_cond_free(queue->condition);
g_mutex_free(queue->mutex);
 
g_list_free(queue->head);
g_slice_free(AsyncQueue, queue);
}
 
void
async_queue_push(AsyncQueue *queue,
gpointer data)
{
g_mutex_lock(queue->mutex);
 
queue->head = g_list_prepend(queue->head, data);
if (!queue->tail)
queue->tail = queue->head;
queue->length++;
 
g_cond_signal(queue->condition);
 
g_mutex_unlock(queue->mutex);
}
 
gpointer
async_queue_pop(AsyncQueue *queue)
{
gpointer data = NULL;
 
g_mutex_lock(queue->mutex);
 
if (!queue->enabled)
goto leave;
 
if (!queue->tail)
g_cond_wait(queue->condition, queue->mutex);
 
if (queue->tail) {
GList *node = queue->tail;
data = node->data;
 
queue->tail = node->prev;
if (queue->tail)
queue->tail->next = NULL;
else
queue->head = NULL;
queue->length--;
g_list_free_1(node);
}
 
leave:
g_mutex_unlock(queue->mutex);
 
return data;
}
 
gpointer
async_queue_pop_forced(AsyncQueue *queue)
{
gpointer data = NULL;
 
g_mutex_lock(queue->mutex);
 
if (queue->tail) {
GList *node = queue->tail;
data = node->data;
 
queue->tail = node->prev;
if (queue->tail)
queue->tail->next = NULL;
else
queue->head = NULL;
queue->length--;
g_list_free_1(node);
}
 
g_mutex_unlock(queue->mutex);
 
return data;
}
 
void
async_queue_disable(AsyncQueue *queue)
{
g_mutex_lock(queue->mutex);
queue->enabled = FALSE;
g_cond_broadcast(queue->condition);
g_mutex_unlock(queue->mutex);
}
 
void
async_queue_enable(AsyncQueue *queue)
{
g_mutex_lock(queue->mutex);
queue->enabled = TRUE;
g_mutex_unlock(queue->mutex);
}
 
void
async_queue_flush(AsyncQueue *queue)
{
g_mutex_lock(queue->mutex);
g_list_free(queue->head);
queue->head = queue->tail = NULL;
queue->length = 0;
g_mutex_unlock(queue->mutex);
}