1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
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.
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.
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.
20 * Author: Alexander Larsson <alexl@redhat.com>
25 #include "gasynchelper.h"
30 * SECTION:gasynchelper
31 * @short_description: Asynchronous Helper Functions
33 * @see_also: #GAsyncReady
35 * Provides helper functions for asynchronous operations.
40 async_result_free (gpointer data
)
42 GAsyncResultData
*res
= data
;
45 g_error_free (res
->error
);
47 g_object_unref (res
->async_object
);
53 _g_queue_async_result (GAsyncResultData
*result
,
54 gpointer async_object
,
57 GSourceFunc source_func
)
61 g_return_if_fail (G_IS_OBJECT (async_object
));
63 result
->async_object
= g_object_ref (async_object
);
64 result
->user_data
= user_data
;
65 result
->error
= error
;
67 source
= g_idle_source_new ();
68 g_source_set_priority (source
, G_PRIORITY_DEFAULT
);
69 g_source_set_callback (source
, source_func
, result
, async_result_free
);
70 g_source_attach (source
, NULL
);
71 g_source_unref (source
);
74 /*************************************************************************
76 ************************************************************************/
82 GCancellable
*cancellable
;
87 fd_source_prepare (GSource
*source
,
90 FDSource
*fd_source
= (FDSource
*)source
;
93 return g_cancellable_is_cancelled (fd_source
->cancellable
);
97 fd_source_check (GSource
*source
)
99 FDSource
*fd_source
= (FDSource
*)source
;
102 g_cancellable_is_cancelled (fd_source
->cancellable
) ||
103 fd_source
->pollfd
.revents
!= 0;
107 fd_source_dispatch (GSource
*source
,
108 GSourceFunc callback
,
112 GFDSourceFunc func
= (GFDSourceFunc
)callback
;
113 FDSource
*fd_source
= (FDSource
*)source
;
115 g_warn_if_fail (func
!= NULL
);
117 return (*func
) (user_data
, fd_source
->pollfd
.revents
, fd_source
->pollfd
.fd
);
121 fd_source_finalize (GSource
*source
)
123 FDSource
*fd_source
= (FDSource
*)source
;
125 if (fd_source
->cancelled_tag
)
126 g_signal_handler_disconnect (fd_source
->cancellable
,
127 fd_source
->cancelled_tag
);
129 if (fd_source
->cancellable
)
130 g_object_unref (fd_source
->cancellable
);
133 static GSourceFuncs fd_source_funcs
= {
140 /* Might be called on another thread */
142 fd_source_cancelled_cb (GCancellable
*cancellable
,
145 /* Wake up the mainloop in case we're waiting on async calls with FDSource */
146 g_main_context_wakeup (NULL
);
150 _g_fd_source_new (int fd
,
152 GCancellable
*cancellable
)
157 source
= g_source_new (&fd_source_funcs
, sizeof (FDSource
));
158 fd_source
= (FDSource
*)source
;
161 fd_source
->cancellable
= g_object_ref (cancellable
);
163 fd_source
->pollfd
.fd
= fd
;
164 fd_source
->pollfd
.events
= events
;
165 g_source_add_poll (source
, &fd_source
->pollfd
);
168 fd_source
->cancelled_tag
=
169 g_signal_connect_data (cancellable
, "cancelled",
170 (GCallback
)fd_source_cancelled_cb
,