Added. Added. Added. Added.
[platform/upstream/glib.git] / gio / gasynchelper.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 Red Hat, Inc.
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
16  * Public 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  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include <config.h>
24
25 #include "gasynchelper.h"
26
27 #include "gioalias.h"
28
29 /**
30  * SECTION:gasynchelper
31  * @short_description: Asynchronous Helper Functions
32  * @see_also: #GAsyncReady
33  * 
34  * Provides helper functions for asynchronous operations.
35  *
36  **/
37
38 static void
39 async_result_free (gpointer data)
40 {
41   GAsyncResultData *res = data;
42
43   if (res->error)
44     g_error_free (res->error);
45
46   g_object_unref (res->async_object);
47   
48   g_free (res);
49 }
50
51 void
52 _g_queue_async_result (GAsyncResultData *result,
53                        gpointer        async_object,
54                        GError         *error,
55                        gpointer        user_data,
56                        GSourceFunc     source_func)
57 {
58   GSource *source;
59
60   g_return_if_fail (G_IS_OBJECT (async_object));
61   
62   result->async_object = g_object_ref (async_object);
63   result->user_data = user_data;
64   result->error = error;
65
66   source = g_idle_source_new ();
67   g_source_set_priority (source, G_PRIORITY_DEFAULT);
68   g_source_set_callback (source, source_func, result, async_result_free);
69   g_source_attach (source, NULL);
70   g_source_unref (source);
71 }
72
73 /*************************************************************************
74  *             fd source                                                 *
75  ************************************************************************/
76
77 typedef struct 
78 {
79   GSource source;
80   GPollFD pollfd;
81   GCancellable *cancellable;
82   gulong cancelled_tag;
83 } FDSource;
84
85 static gboolean 
86 fd_source_prepare (GSource  *source,
87                    gint     *timeout)
88 {
89   FDSource *fd_source = (FDSource *)source;
90   *timeout = -1;
91   
92   return g_cancellable_is_cancelled (fd_source->cancellable);
93 }
94
95 static gboolean 
96 fd_source_check (GSource  *source)
97 {
98   FDSource *fd_source = (FDSource *)source;
99
100   return
101     g_cancellable_is_cancelled  (fd_source->cancellable) ||
102     fd_source->pollfd.revents != 0;
103 }
104
105 static gboolean
106 fd_source_dispatch (GSource     *source,
107                     GSourceFunc  callback,
108                     gpointer     user_data)
109
110 {
111   GFDSourceFunc func = (GFDSourceFunc)callback;
112   FDSource *fd_source = (FDSource *)source;
113
114   g_assert (func != NULL);
115
116   return (*func) (user_data, fd_source->pollfd.revents, fd_source->pollfd.fd);
117 }
118
119 static void 
120 fd_source_finalize (GSource *source)
121 {
122   FDSource *fd_source = (FDSource *)source;
123
124   if (fd_source->cancelled_tag)
125     g_signal_handler_disconnect (fd_source->cancellable,
126                                  fd_source->cancelled_tag);
127
128   if (fd_source->cancellable)
129     g_object_unref (fd_source->cancellable);
130 }
131
132 static GSourceFuncs fd_source_funcs = {
133   fd_source_prepare,
134   fd_source_check,
135   fd_source_dispatch,
136   fd_source_finalize
137 };
138
139 /* Might be called on another thread */
140 static void
141 fd_source_cancelled_cb (GCancellable *cancellable,
142                         gpointer data)
143 {
144   /* Wake up the mainloop in case we're waiting on async calls with FDSource */
145   g_main_context_wakeup (NULL);
146 }
147
148 GSource *
149 _g_fd_source_new (int fd,
150                   gushort events,
151                   GCancellable *cancellable)
152 {
153   GSource *source;
154   FDSource *fd_source;
155
156   source = g_source_new (&fd_source_funcs, sizeof (FDSource));
157   fd_source = (FDSource *)source;
158
159   if (cancellable)
160     fd_source->cancellable = g_object_ref (cancellable);
161   
162   fd_source->pollfd.fd = fd;
163   fd_source->pollfd.events = events;
164   g_source_add_poll (source, &fd_source->pollfd);
165
166   if (cancellable)
167     fd_source->cancelled_tag =
168       g_signal_connect_data (cancellable, "cancelled",
169                              (GCallback)fd_source_cancelled_cb,
170                              NULL, NULL,
171                              0);
172   
173   return source;
174 }