1 gasyncqueue.h |
1 /* GLIB - Library of useful routines for C programming |
|
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald |
|
3 * Portions copyright (c) 2006 Nokia Corporation. All rights reserved. |
|
4 * |
|
5 * This library is free software; you can redistribute it and/or |
|
6 * modify it under the terms of the GNU Lesser General Public |
|
7 * License as published by the Free Software Foundation; either |
|
8 * version 2 of the License, or (at your option) any later version. |
|
9 * |
|
10 * This library is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 * Lesser General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU Lesser General Public |
|
16 * License along with this library; if not, write to the |
|
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
|
18 * Boston, MA 02111-1307, USA. |
|
19 */ |
|
20 |
|
21 /* |
|
22 * Modified by the GLib Team and others 1997-2000. See the AUTHORS |
|
23 * file for a list of people on the GLib Team. See the ChangeLog |
|
24 * files for a list of changes. These files are distributed with |
|
25 * GLib at ftp://ftp.gtk.org/pub/gtk/. |
|
26 */ |
|
27 |
|
28 #ifndef __G_ASYNCQUEUE_H__ |
|
29 #define __G_ASYNCQUEUE_H__ |
|
30 |
|
31 #include <_ansi.h> |
|
32 #include <glib/gthread.h> |
|
33 |
|
34 G_BEGIN_DECLS |
|
35 |
|
36 typedef struct _GAsyncQueue GAsyncQueue; |
|
37 |
|
38 /* Asyncronous Queues, can be used to communicate between threads */ |
|
39 |
|
40 /* Get a new GAsyncQueue with the ref_count 1 */ |
|
41 IMPORT_C GAsyncQueue* g_async_queue_new (void); |
|
42 |
|
43 /* Lock and unlock a GAsyncQueue. All functions lock the queue for |
|
44 * themselves, but in certain cirumstances you want to hold the lock longer, |
|
45 * thus you lock the queue, call the *_unlocked functions and unlock it again. |
|
46 */ |
|
47 IMPORT_C void g_async_queue_lock (GAsyncQueue *queue); |
|
48 IMPORT_C void g_async_queue_unlock (GAsyncQueue *queue); |
|
49 |
|
50 /* Ref and unref the GAsyncQueue. */ |
|
51 IMPORT_C GAsyncQueue* g_async_queue_ref (GAsyncQueue *queue); |
|
52 IMPORT_C void g_async_queue_unref (GAsyncQueue *queue); |
|
53 |
|
54 #ifndef G_DISABLE_DEPRECATED |
|
55 /* You don't have to hold the lock for calling *_ref and *_unref anymore. */ |
|
56 IMPORT_C void g_async_queue_ref_unlocked (GAsyncQueue *queue); |
|
57 IMPORT_C void g_async_queue_unref_and_unlock (GAsyncQueue *queue); |
|
58 #endif /* !G_DISABLE_DEPRECATED */ |
|
59 |
|
60 /* Push data into the async queue. Must not be NULL. */ |
|
61 IMPORT_C void g_async_queue_push (GAsyncQueue *queue, |
|
62 gpointer data); |
|
63 IMPORT_C void g_async_queue_push_unlocked (GAsyncQueue *queue, |
|
64 gpointer data); |
|
65 |
|
66 IMPORT_C void g_async_queue_push_sorted (GAsyncQueue *queue, |
|
67 gpointer data, |
|
68 GCompareDataFunc func, |
|
69 gpointer user_data); |
|
70 IMPORT_C void g_async_queue_push_sorted_unlocked (GAsyncQueue *queue, |
|
71 gpointer data, |
|
72 GCompareDataFunc func, |
|
73 gpointer user_data); |
|
74 |
|
75 /* Pop data from the async queue. When no data is there, the thread is blocked |
|
76 * until data arrives. |
|
77 */ |
|
78 IMPORT_C gpointer g_async_queue_pop (GAsyncQueue *queue); |
|
79 IMPORT_C gpointer g_async_queue_pop_unlocked (GAsyncQueue *queue); |
|
80 |
|
81 /* Try to pop data. NULL is returned in case of empty queue. */ |
|
82 IMPORT_C gpointer g_async_queue_try_pop (GAsyncQueue *queue); |
|
83 IMPORT_C gpointer g_async_queue_try_pop_unlocked (GAsyncQueue *queue); |
|
84 |
|
85 |
|
86 |
|
87 /* Wait for data until at maximum until end_time is reached. NULL is returned |
|
88 * in case of empty queue. |
|
89 */ |
|
90 IMPORT_C gpointer g_async_queue_timed_pop (GAsyncQueue *queue, |
|
91 GTimeVal *end_time); |
|
92 IMPORT_C gpointer g_async_queue_timed_pop_unlocked (GAsyncQueue *queue, |
|
93 GTimeVal *end_time); |
|
94 |
|
95 /* Return the length of the queue. Negative values mean that threads |
|
96 * are waiting, positve values mean that there are entries in the |
|
97 * queue. Actually this function returns the length of the queue minus |
|
98 * the number of waiting threads, g_async_queue_length == 0 could also |
|
99 * mean 'n' entries in the queue and 'n' thread waiting. Such can |
|
100 * happen due to locking of the queue or due to scheduling. |
|
101 */ |
|
102 IMPORT_C gint g_async_queue_length (GAsyncQueue *queue); |
|
103 IMPORT_C gint g_async_queue_length_unlocked (GAsyncQueue *queue); |
|
104 IMPORT_C void g_async_queue_sort (GAsyncQueue *queue, |
|
105 GCompareDataFunc func, |
|
106 gpointer user_data); |
|
107 IMPORT_C void g_async_queue_sort_unlocked (GAsyncQueue *queue, |
|
108 GCompareDataFunc func, |
|
109 gpointer user_data); |
|
110 |
|
111 /* Private API */ |
|
112 GMutex* _g_async_queue_get_mutex (GAsyncQueue *queue); |
|
113 |
|
114 G_END_DECLS |
|
115 |
|
116 #endif /* __G_ASYNCQUEUE_H__ */ |
|
117 |