2 * Copyright (C) 2008-2009 Nokia Corporation.
4 * Author: Felipe Contreras <felipe.contreras@nokia.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation
9 * version 2.1 of the License.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "async_queue.h"
27 async_queue_new (void)
31 queue = g_slice_new0 (AsyncQueue);
33 queue->condition = g_cond_new ();
34 queue->mutex = g_mutex_new ();
35 queue->enabled = TRUE;
41 async_queue_free (AsyncQueue * queue)
43 g_cond_free (queue->condition);
44 g_mutex_free (queue->mutex);
46 g_list_free (queue->head);
47 g_slice_free (AsyncQueue, queue);
51 async_queue_push (AsyncQueue * queue, gpointer data)
53 g_mutex_lock (queue->mutex);
55 queue->head = g_list_prepend (queue->head, data);
57 queue->tail = queue->head;
60 g_cond_signal (queue->condition);
62 g_mutex_unlock (queue->mutex);
66 async_queue_pop (AsyncQueue * queue)
70 g_mutex_lock (queue->mutex);
72 if (!queue->enabled) {
73 /* g_warning ("not enabled!"); */
78 g_cond_wait (queue->condition, queue->mutex);
82 GList *node = queue->tail;
85 queue->tail = node->prev;
87 queue->tail->next = NULL;
95 g_mutex_unlock (queue->mutex);
101 async_queue_pop_forced (AsyncQueue * queue)
103 gpointer data = NULL;
105 g_mutex_lock (queue->mutex);
108 GList *node = queue->tail;
111 queue->tail = node->prev;
113 queue->tail->next = NULL;
117 g_list_free_1 (node);
120 g_mutex_unlock (queue->mutex);
126 async_queue_disable (AsyncQueue * queue)
128 g_mutex_lock (queue->mutex);
129 queue->enabled = FALSE;
130 g_cond_broadcast (queue->condition);
131 g_mutex_unlock (queue->mutex);
135 async_queue_enable (AsyncQueue * queue)
137 g_mutex_lock (queue->mutex);
138 queue->enabled = TRUE;
139 g_mutex_unlock (queue->mutex);
143 async_queue_flush (AsyncQueue * queue)
145 g_mutex_lock (queue->mutex);
146 g_list_free (queue->head);
147 queue->head = queue->tail = NULL;
149 g_mutex_unlock (queue->mutex);