gio/ docs/reference/gio Merged gio-standalone into glib.
[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 static void
28 async_result_free (gpointer data)
29 {
30   GAsyncResultData *res = data;
31
32   if (res->error)
33     g_error_free (res->error);
34
35   g_object_unref (res->async_object);
36   
37   g_free (res);
38 }
39
40 void
41 _g_queue_async_result (GAsyncResultData *result,
42                        gpointer        async_object,
43                        GError         *error,
44                        gpointer        user_data,
45                        GSourceFunc     source_func)
46 {
47   GSource *source;
48
49   g_return_if_fail (G_IS_OBJECT (async_object));
50   
51   result->async_object = g_object_ref (async_object);
52   result->user_data = user_data;
53   result->error = error;
54
55   source = g_idle_source_new ();
56   g_source_set_priority (source, G_PRIORITY_DEFAULT);
57   g_source_set_callback (source, source_func, result, async_result_free);
58   g_source_attach (source, NULL);
59   g_source_unref (source);
60 }
61
62 /*************************************************************************
63  *             fd source                                                 *
64  ************************************************************************/
65
66 typedef struct 
67 {
68   GSource source;
69   GPollFD pollfd;
70   GCancellable *cancellable;
71   gulong cancelled_tag;
72 } FDSource;
73
74 static gboolean 
75 fd_source_prepare (GSource  *source,
76                    gint     *timeout)
77 {
78   FDSource *fd_source = (FDSource *)source;
79   *timeout = -1;
80   
81   return g_cancellable_is_cancelled (fd_source->cancellable);
82 }
83
84 static gboolean 
85 fd_source_check (GSource  *source)
86 {
87   FDSource *fd_source = (FDSource *)source;
88
89   return
90     g_cancellable_is_cancelled  (fd_source->cancellable) ||
91     fd_source->pollfd.revents != 0;
92 }
93
94 static gboolean
95 fd_source_dispatch (GSource     *source,
96                     GSourceFunc  callback,
97                     gpointer     user_data)
98
99 {
100   GFDSourceFunc func = (GFDSourceFunc)callback;
101   FDSource *fd_source = (FDSource *)source;
102
103   g_assert (func != NULL);
104
105   return (*func) (user_data, fd_source->pollfd.revents, fd_source->pollfd.fd);
106 }
107
108 static void 
109 fd_source_finalize (GSource *source)
110 {
111   FDSource *fd_source = (FDSource *)source;
112
113   if (fd_source->cancelled_tag)
114     g_signal_handler_disconnect (fd_source->cancellable,
115                                  fd_source->cancelled_tag);
116
117   if (fd_source->cancellable)
118     g_object_unref (fd_source->cancellable);
119 }
120
121 static GSourceFuncs fd_source_funcs = {
122   fd_source_prepare,
123   fd_source_check,
124   fd_source_dispatch,
125   fd_source_finalize
126 };
127
128 /* Might be called on another thread */
129 static void
130 fd_source_cancelled_cb (GCancellable *cancellable,
131                         gpointer data)
132 {
133   /* Wake up the mainloop in case we're waiting on async calls with FDSource */
134   g_main_context_wakeup (NULL);
135 }
136
137 GSource *
138 _g_fd_source_new (int fd,
139                   gushort events,
140                   GCancellable *cancellable)
141 {
142   GSource *source;
143   FDSource *fd_source;
144
145   source = g_source_new (&fd_source_funcs, sizeof (FDSource));
146   fd_source = (FDSource *)source;
147
148   if (cancellable)
149     fd_source->cancellable = g_object_ref (cancellable);
150   
151   fd_source->pollfd.fd = fd;
152   fd_source->pollfd.events = events;
153   g_source_add_poll (source, &fd_source->pollfd);
154
155   if (cancellable)
156     fd_source->cancelled_tag =
157       g_signal_connect_data (cancellable, "cancelled",
158                              (GCallback)fd_source_cancelled_cb,
159                              NULL, NULL,
160                              0);
161   
162   return source;
163 }