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