Implement closure-related methods for gio GSource types
[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
28 /**
29  * SECTION:gasynchelper
30  * @short_description: Asynchronous Helper Functions
31  * @include: gio/gio.h
32  * @see_also: #GAsyncReady
33  * 
34  * Provides helper functions for asynchronous operations.
35  *
36  **/
37
38 /*************************************************************************
39  *             fd source                                                 *
40  ************************************************************************/
41
42 typedef struct 
43 {
44   GSource source;
45   GPollFD pollfd;
46   GCancellable *cancellable;
47   gulong cancelled_tag;
48 } FDSource;
49
50 static gboolean 
51 fd_source_prepare (GSource *source,
52                    gint    *timeout)
53 {
54   FDSource *fd_source = (FDSource *)source;
55   *timeout = -1;
56   
57   return g_cancellable_is_cancelled (fd_source->cancellable);
58 }
59
60 static gboolean 
61 fd_source_check (GSource *source)
62 {
63   FDSource *fd_source = (FDSource *)source;
64
65   return
66     g_cancellable_is_cancelled  (fd_source->cancellable) ||
67     fd_source->pollfd.revents != 0;
68 }
69
70 static gboolean
71 fd_source_dispatch (GSource     *source,
72                     GSourceFunc  callback,
73                     gpointer     user_data)
74
75 {
76   GFDSourceFunc func = (GFDSourceFunc)callback;
77   FDSource *fd_source = (FDSource *)source;
78
79   g_warn_if_fail (func != NULL);
80
81   return (*func) (fd_source->pollfd.fd, fd_source->pollfd.revents, user_data);
82 }
83
84 static void 
85 fd_source_finalize (GSource *source)
86 {
87   FDSource *fd_source = (FDSource *)source;
88
89   /* we don't use g_cancellable_disconnect() here, since we are holding
90    * the main context lock here, and the ::disconnect signal handler
91    * will try to grab that, and deadlock looms.
92    */
93   if (fd_source->cancelled_tag)
94     g_signal_handler_disconnect (fd_source->cancellable,
95                                  fd_source->cancelled_tag);
96
97   if (fd_source->cancellable)
98     g_object_unref (fd_source->cancellable);
99 }
100
101 static gboolean
102 fd_source_closure_callback (int           fd,
103                             GIOCondition  condition,
104                             gpointer      data)
105 {
106   GClosure *closure = data;
107
108   GValue params[2] = { { 0, }, { 0, } };
109   GValue result_value = { 0, };
110   gboolean result;
111
112   g_value_init (&result_value, G_TYPE_BOOLEAN);
113
114   g_value_init (&params[0], G_TYPE_INT);
115   g_value_set_int (&params[0], fd);
116
117   g_value_init (&params[1], G_TYPE_IO_CONDITION);
118   g_value_set_flags (&params[1], condition);
119
120   g_closure_invoke (closure, &result_value, 2, params, NULL);
121
122   result = g_value_get_boolean (&result_value);
123   g_value_unset (&result_value);
124   g_value_unset (&params[0]);
125   g_value_unset (&params[1]);
126
127   return result;
128 }
129
130 static void
131 fd_source_closure_marshal (GClosure     *closure,
132                            GValue       *return_value,
133                            guint         n_param_values,
134                            const GValue *param_values,
135                            gpointer      invocation_hint,
136                            gpointer      marshal_data)
137 {
138   GFDSourceFunc callback;
139   GCClosure *cc = (GCClosure*) closure;
140   gboolean v_return;
141
142   g_return_if_fail (return_value != NULL);
143   g_return_if_fail (n_param_values == 0);
144
145   callback = (GFDSourceFunc) (marshal_data ? marshal_data : cc->callback);
146
147   v_return = callback (g_value_get_int (param_values),
148                        g_value_get_flags (param_values + 1),
149                        closure->data);
150
151   g_value_set_boolean (return_value, v_return);
152 }
153
154 static GSourceFuncs fd_source_funcs = {
155   fd_source_prepare,
156   fd_source_check,
157   fd_source_dispatch,
158   fd_source_finalize,
159   (GSourceFunc)fd_source_closure_callback,
160   (GSourceDummyMarshal)fd_source_closure_marshal,
161 };
162
163 /* Might be called on another thread */
164 static void
165 fd_source_cancelled_cb (GCancellable *cancellable,
166                         gpointer      data)
167 {
168   /* Wake up the mainloop in case we're waiting on async calls with FDSource */
169   g_main_context_wakeup (NULL);
170 }
171
172 GSource *
173 _g_fd_source_new (int           fd,
174                   gushort       events,
175                   GCancellable *cancellable)
176 {
177   GSource *source;
178   FDSource *fd_source;
179
180   source = g_source_new (&fd_source_funcs, sizeof (FDSource));
181   fd_source = (FDSource *)source;
182
183   if (cancellable)
184     fd_source->cancellable = g_object_ref (cancellable);
185   
186   fd_source->pollfd.fd = fd;
187   fd_source->pollfd.events = events;
188   g_source_add_poll (source, &fd_source->pollfd);
189
190   if (cancellable)
191     fd_source->cancelled_tag =
192       g_cancellable_connect (cancellable, 
193                              (GCallback)fd_source_cancelled_cb,
194                              NULL, NULL);
195   
196   return source;
197 }