Initial commit
[platform/upstream/glib2.0.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  * @include: gio/gio.h
33  * @see_also: #GAsyncReady
34  * 
35  * Provides helper functions for asynchronous operations.
36  *
37  **/
38
39 /*************************************************************************
40  *             fd source                                                 *
41  ************************************************************************/
42
43 typedef struct 
44 {
45   GSource source;
46   GPollFD pollfd;
47   GCancellable *cancellable;
48   gulong cancelled_tag;
49   GObject *object;
50 } FDSource;
51
52 static gboolean 
53 fd_source_prepare (GSource *source,
54                    gint    *timeout)
55 {
56   FDSource *fd_source = (FDSource *)source;
57   *timeout = -1;
58   
59   return g_cancellable_is_cancelled (fd_source->cancellable);
60 }
61
62 static gboolean
63 fd_source_check (GSource *source)
64 {
65   FDSource *fd_source = (FDSource *)source;
66
67   return
68     g_cancellable_is_cancelled  (fd_source->cancellable) ||
69     fd_source->pollfd.revents != 0;
70 }
71
72 static gboolean
73 fd_source_dispatch (GSource     *source,
74                     GSourceFunc  callback,
75                     gpointer     user_data)
76
77 {
78   GFDSourceFunc func = (GFDSourceFunc)callback;
79   GFDSourceObjectFunc func2 = (GFDSourceObjectFunc)callback;
80   FDSource *fd_source = (FDSource *)source;
81
82   g_warn_if_fail (func != NULL);
83
84   if (fd_source->object)
85     return (*func2) (fd_source->object, fd_source->pollfd.revents, user_data);
86   else
87     return (*func) (user_data, fd_source->pollfd.revents, fd_source->pollfd.fd);
88 }
89
90 static void
91 fd_source_finalize (GSource *source)
92 {
93   FDSource *fd_source = (FDSource *)source;
94
95   if (fd_source->cancelled_tag)
96     g_cancellable_disconnect (fd_source->cancellable,
97                               fd_source->cancelled_tag);
98
99   if (fd_source->cancellable)
100     g_object_unref (fd_source->cancellable);
101
102   if (fd_source->object)
103     g_object_unref (fd_source->object);
104 }
105
106 static GSourceFuncs fd_source_funcs = {
107   fd_source_prepare,
108   fd_source_check,
109   fd_source_dispatch,
110   fd_source_finalize
111 };
112
113 /* Might be called on another thread */
114 static void
115 fd_source_cancelled_cb (GCancellable *cancellable,
116                         gpointer      data)
117 {
118   /* Wake up the mainloop in case we're waiting on async calls with FDSource */
119   g_main_context_wakeup (NULL);
120 }
121
122 GSource *
123 _g_fd_source_new_with_object (GObject      *object,
124                               int           fd,
125                               gushort       events,
126                               GCancellable *cancellable)
127 {
128   GSource *source;
129   FDSource *fd_source;
130
131   source = g_source_new (&fd_source_funcs, sizeof (FDSource));
132   fd_source = (FDSource *)source;
133
134   if (cancellable)
135     fd_source->cancellable = g_object_ref (cancellable);
136
137   if (object)
138     fd_source->object = g_object_ref (object);
139
140   fd_source->pollfd.fd = fd;
141   fd_source->pollfd.events = events;
142   g_source_add_poll (source, &fd_source->pollfd);
143
144   if (cancellable)
145     fd_source->cancelled_tag =
146       g_cancellable_connect (cancellable,
147                              (GCallback)fd_source_cancelled_cb,
148                              NULL, NULL);
149
150   return source;
151 }
152
153 GSource *
154 _g_fd_source_new (int           fd,
155                   gushort       events,
156                   GCancellable *cancellable)
157 {
158   return _g_fd_source_new_with_object (NULL, fd, events, cancellable);
159 }