New File implementing an asynchronous queue to be used for asynchronous
[platform/upstream/glib.git] / glib / gasyncqueue.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * GAsyncQueue: asyncronous queue implementation, based on Gqueue.
5  * Copyright (C) 2000 Sebastian Wilhelmi; University of Karlsruhe
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 /*
24  * MT safe
25  */
26
27 #include "glib.h"
28
29 struct _GAsyncQueue
30 {
31   GMutex *mutex;
32   GCond *cond;
33   GQueue *queue;
34   guint waiting_threads;
35   guint ref_count;
36 };
37
38 GAsyncQueue*
39 g_async_queue_new ()
40 {
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;
47   return retval;
48 }
49
50 void 
51 g_async_queue_ref (GAsyncQueue *queue)
52 {
53   g_return_if_fail (queue);
54   g_return_if_fail (queue->ref_count > 0);
55   
56   g_mutex_lock (queue->mutex);
57   queue->ref_count++;
58   g_mutex_unlock (queue->mutex);
59 }
60
61 void 
62 g_async_queue_ref_unlocked (GAsyncQueue *queue)
63 {
64   g_return_if_fail (queue);
65   g_return_if_fail (queue->ref_count > 0);
66   
67   queue->ref_count++;
68 }
69
70 void 
71 g_async_queue_unref_and_unlock (GAsyncQueue *queue)
72 {
73   gboolean stop;
74
75   g_return_if_fail (queue);
76   g_return_if_fail (queue->ref_count > 0);
77
78   queue->ref_count--;
79   stop = (queue->ref_count == 0);
80   g_mutex_unlock (queue->mutex);
81   
82   if (stop)
83     {
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);
88       g_free (queue);
89     }
90 }
91
92 void 
93 g_async_queue_unref (GAsyncQueue *queue)
94 {
95   gboolean stop;
96
97   g_return_if_fail (queue);
98   g_return_if_fail (queue->ref_count > 0);
99
100   g_mutex_lock (queue->mutex);
101   g_async_queue_unref_and_unlock (queue);
102 }
103
104 void
105 g_async_queue_lock (GAsyncQueue *queue)
106 {
107   g_return_if_fail (queue);
108   g_return_if_fail (queue->ref_count > 0);
109
110   g_mutex_lock (queue->mutex);
111 }
112
113 void 
114 g_async_queue_unlock (GAsyncQueue *queue)
115 {
116   g_return_if_fail (queue);
117   g_return_if_fail (queue->ref_count > 0);
118
119   g_mutex_unlock (queue->mutex);
120 }
121
122 void
123 g_async_queue_push (GAsyncQueue* queue, gpointer data)
124 {
125   g_return_if_fail (queue);
126   g_return_if_fail (queue->ref_count > 0);
127   g_return_if_fail (data);
128
129   g_mutex_lock (queue->mutex);
130   g_async_queue_push_unlocked (queue, data);
131   g_mutex_unlock (queue->mutex);
132 }
133
134 void
135 g_async_queue_push_unlocked (GAsyncQueue* queue, gpointer data)
136 {
137   g_return_if_fail (queue);
138   g_return_if_fail (queue->ref_count > 0);
139   g_return_if_fail (data);
140
141   g_queue_push_head (queue->queue, data);
142   g_cond_signal (queue->cond);
143 }
144
145 static gpointer
146 g_async_queue_pop_intern_unlocked (GAsyncQueue* queue, gboolean try, 
147                                    GTimeVal *end_time)
148 {
149   gpointer retval;
150
151   if (!g_queue_peek_tail (queue->queue))
152     {
153       if (try)
154         return NULL;
155       if (!end_time)
156         {
157           queue->waiting_threads++;
158           while (!g_queue_peek_tail (queue->queue))
159             g_cond_wait(queue->cond, queue->mutex);
160           queue->waiting_threads--;
161         }
162       else
163         {
164           queue->waiting_threads++;
165           while (!g_queue_peek_tail (queue->queue))
166             if (!g_cond_timed_wait (queue->cond, queue->mutex, end_time))
167               break;
168           queue->waiting_threads--;
169           if (!g_queue_peek_tail (queue->queue))
170             return NULL;
171         }
172     }
173
174   retval = g_queue_pop_tail (queue->queue);
175
176   g_assert (retval);
177
178   return retval;
179 }
180
181 gpointer
182 g_async_queue_pop (GAsyncQueue* queue)
183 {
184   gpointer retval;
185
186   g_return_val_if_fail (queue, NULL);
187   g_return_val_if_fail (queue->ref_count > 0, NULL);
188
189   g_mutex_lock (queue->mutex);
190   retval = g_async_queue_pop_intern_unlocked (queue, FALSE, NULL);
191   g_mutex_unlock (queue->mutex);
192
193   return retval;
194 }
195
196 gpointer
197 g_async_queue_pop_unlocked (GAsyncQueue* queue)
198 {
199   g_return_val_if_fail (queue, NULL);
200   g_return_val_if_fail (queue->ref_count > 0, NULL);
201
202   return g_async_queue_pop_intern_unlocked (queue, FALSE, NULL);
203 }
204
205 gpointer
206 g_async_queue_try_pop (GAsyncQueue* queue)
207 {
208   gpointer retval;
209
210   g_return_val_if_fail (queue, NULL);
211   g_return_val_if_fail (queue->ref_count > 0, NULL);
212
213   g_mutex_lock (queue->mutex);
214   retval = g_async_queue_pop_intern_unlocked (queue, TRUE, NULL);
215   g_mutex_unlock (queue->mutex);
216
217   return retval;
218 }
219
220 gpointer
221 g_async_queue_try_pop_unlocked (GAsyncQueue* queue)
222 {
223   g_return_val_if_fail (queue, NULL);
224   g_return_val_if_fail (queue->ref_count > 0, NULL);
225
226   return g_async_queue_pop_intern_unlocked (queue, TRUE, NULL);
227 }
228
229 gpointer
230 g_async_queue_timed_pop (GAsyncQueue* queue, GTimeVal *end_time)
231 {
232   gpointer retval;
233
234   g_return_val_if_fail (queue, NULL);
235   g_return_val_if_fail (queue->ref_count > 0, NULL);
236
237   g_mutex_lock (queue->mutex);
238   retval = g_async_queue_pop_intern_unlocked (queue, FALSE, end_time);
239   g_mutex_unlock (queue->mutex);
240
241   return retval;  
242 }
243
244 gpointer
245 g_async_queue_timed_pop_unlocked (GAsyncQueue* queue, GTimeVal *end_time)
246 {
247   g_return_val_if_fail (queue, NULL);
248   g_return_val_if_fail (queue->ref_count > 0, NULL);
249
250   return g_async_queue_pop_intern_unlocked (queue, FALSE, end_time);
251 }
252
253 gint
254 g_async_queue_length_unlocked (GAsyncQueue* queue)
255 {
256   g_return_val_if_fail (queue, 0);
257   g_return_val_if_fail (queue->ref_count > 0, 0);
258
259   return queue->queue->length - queue->waiting_threads;
260 }
261
262 gint
263 g_async_queue_length(GAsyncQueue* queue)
264 {
265   glong retval;
266
267   g_return_val_if_fail (queue, 0);
268   g_return_val_if_fail (queue->ref_count > 0, 0);
269
270   g_mutex_lock (queue->mutex);
271   retval = queue->queue->length - queue->waiting_threads;
272   g_mutex_unlock (queue->mutex);
273
274   return retval;
275 }
276