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