1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * GAsyncQueue: asyncronous queue implementation, based on Gqueue.
5 * Copyright (C) 2000 Sebastian Wilhelmi; University of Karlsruhe
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
34 guint waiting_threads;
41 GAsyncQueue* retval = g_new (GAsyncQueue, 1);
42 retval->mutex = g_mutex_new ();
43 retval->cond = g_cond_new ();
44 retval->queue = g_queue_new ();
45 retval->waiting_threads = 0;
46 retval->ref_count = 1;
51 g_async_queue_ref (GAsyncQueue *queue)
53 g_return_if_fail (queue);
54 g_return_if_fail (queue->ref_count > 0);
56 g_mutex_lock (queue->mutex);
58 g_mutex_unlock (queue->mutex);
62 g_async_queue_ref_unlocked (GAsyncQueue *queue)
64 g_return_if_fail (queue);
65 g_return_if_fail (queue->ref_count > 0);
71 g_async_queue_unref_and_unlock (GAsyncQueue *queue)
75 g_return_if_fail (queue);
76 g_return_if_fail (queue->ref_count > 0);
79 stop = (queue->ref_count == 0);
80 g_mutex_unlock (queue->mutex);
84 g_return_if_fail (queue->waiting_threads == 0);
85 g_mutex_free (queue->mutex);
86 g_cond_free (queue->cond);
87 g_queue_free (queue->queue);
93 g_async_queue_unref (GAsyncQueue *queue)
97 g_return_if_fail (queue);
98 g_return_if_fail (queue->ref_count > 0);
100 g_mutex_lock (queue->mutex);
101 g_async_queue_unref_and_unlock (queue);
105 g_async_queue_lock (GAsyncQueue *queue)
107 g_return_if_fail (queue);
108 g_return_if_fail (queue->ref_count > 0);
110 g_mutex_lock (queue->mutex);
114 g_async_queue_unlock (GAsyncQueue *queue)
116 g_return_if_fail (queue);
117 g_return_if_fail (queue->ref_count > 0);
119 g_mutex_unlock (queue->mutex);
123 g_async_queue_push (GAsyncQueue* queue, gpointer data)
125 g_return_if_fail (queue);
126 g_return_if_fail (queue->ref_count > 0);
127 g_return_if_fail (data);
129 g_mutex_lock (queue->mutex);
130 g_async_queue_push_unlocked (queue, data);
131 g_mutex_unlock (queue->mutex);
135 g_async_queue_push_unlocked (GAsyncQueue* queue, gpointer data)
137 g_return_if_fail (queue);
138 g_return_if_fail (queue->ref_count > 0);
139 g_return_if_fail (data);
141 g_queue_push_head (queue->queue, data);
142 g_cond_signal (queue->cond);
146 g_async_queue_pop_intern_unlocked (GAsyncQueue* queue, gboolean try,
151 if (!g_queue_peek_tail (queue->queue))
157 queue->waiting_threads++;
158 while (!g_queue_peek_tail (queue->queue))
159 g_cond_wait(queue->cond, queue->mutex);
160 queue->waiting_threads--;
164 queue->waiting_threads++;
165 while (!g_queue_peek_tail (queue->queue))
166 if (!g_cond_timed_wait (queue->cond, queue->mutex, end_time))
168 queue->waiting_threads--;
169 if (!g_queue_peek_tail (queue->queue))
174 retval = g_queue_pop_tail (queue->queue);
182 g_async_queue_pop (GAsyncQueue* queue)
186 g_return_val_if_fail (queue, NULL);
187 g_return_val_if_fail (queue->ref_count > 0, NULL);
189 g_mutex_lock (queue->mutex);
190 retval = g_async_queue_pop_intern_unlocked (queue, FALSE, NULL);
191 g_mutex_unlock (queue->mutex);
197 g_async_queue_pop_unlocked (GAsyncQueue* queue)
199 g_return_val_if_fail (queue, NULL);
200 g_return_val_if_fail (queue->ref_count > 0, NULL);
202 return g_async_queue_pop_intern_unlocked (queue, FALSE, NULL);
206 g_async_queue_try_pop (GAsyncQueue* queue)
210 g_return_val_if_fail (queue, NULL);
211 g_return_val_if_fail (queue->ref_count > 0, NULL);
213 g_mutex_lock (queue->mutex);
214 retval = g_async_queue_pop_intern_unlocked (queue, TRUE, NULL);
215 g_mutex_unlock (queue->mutex);
221 g_async_queue_try_pop_unlocked (GAsyncQueue* queue)
223 g_return_val_if_fail (queue, NULL);
224 g_return_val_if_fail (queue->ref_count > 0, NULL);
226 return g_async_queue_pop_intern_unlocked (queue, TRUE, NULL);
230 g_async_queue_timed_pop (GAsyncQueue* queue, GTimeVal *end_time)
234 g_return_val_if_fail (queue, NULL);
235 g_return_val_if_fail (queue->ref_count > 0, NULL);
237 g_mutex_lock (queue->mutex);
238 retval = g_async_queue_pop_intern_unlocked (queue, FALSE, end_time);
239 g_mutex_unlock (queue->mutex);
245 g_async_queue_timed_pop_unlocked (GAsyncQueue* queue, GTimeVal *end_time)
247 g_return_val_if_fail (queue, NULL);
248 g_return_val_if_fail (queue->ref_count > 0, NULL);
250 return g_async_queue_pop_intern_unlocked (queue, FALSE, end_time);
254 g_async_queue_length_unlocked (GAsyncQueue* queue)
256 g_return_val_if_fail (queue, 0);
257 g_return_val_if_fail (queue->ref_count > 0, 0);
259 return queue->queue->length - queue->waiting_threads;
263 g_async_queue_length(GAsyncQueue* queue)
267 g_return_val_if_fail (queue, 0);
268 g_return_val_if_fail (queue->ref_count > 0, 0);
270 g_mutex_lock (queue->mutex);
271 retval = queue->queue->length - queue->waiting_threads;
272 g_mutex_unlock (queue->mutex);