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"
29 * SECTION:gasynchelper
30 * @short_description: Asynchronous Helper Functions
32 * @see_also: #GAsyncReady
34 * Provides helper functions for asynchronous operations.
38 /*************************************************************************
40 ************************************************************************/
46 GCancellable *cancellable;
51 fd_source_prepare (GSource *source,
54 FDSource *fd_source = (FDSource *)source;
57 return g_cancellable_is_cancelled (fd_source->cancellable);
61 fd_source_check (GSource *source)
63 FDSource *fd_source = (FDSource *)source;
66 g_cancellable_is_cancelled (fd_source->cancellable) ||
67 fd_source->pollfd.revents != 0;
71 fd_source_dispatch (GSource *source,
76 GFDSourceFunc func = (GFDSourceFunc)callback;
77 FDSource *fd_source = (FDSource *)source;
79 g_warn_if_fail (func != NULL);
81 return (*func) (user_data, fd_source->pollfd.revents, fd_source->pollfd.fd);
85 fd_source_finalize (GSource *source)
87 FDSource *fd_source = (FDSource *)source;
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.
93 if (fd_source->cancelled_tag)
94 g_signal_handler_disconnect (fd_source->cancellable,
95 fd_source->cancelled_tag);
97 if (fd_source->cancellable)
98 g_object_unref (fd_source->cancellable);
101 static GSourceFuncs fd_source_funcs = {
108 /* Might be called on another thread */
110 fd_source_cancelled_cb (GCancellable *cancellable,
113 /* Wake up the mainloop in case we're waiting on async calls with FDSource */
114 g_main_context_wakeup (NULL);
118 _g_fd_source_new (int fd,
120 GCancellable *cancellable)
125 source = g_source_new (&fd_source_funcs, sizeof (FDSource));
126 fd_source = (FDSource *)source;
129 fd_source->cancellable = g_object_ref (cancellable);
131 fd_source->pollfd.fd = fd;
132 fd_source->pollfd.events = events;
133 g_source_add_poll (source, &fd_source->pollfd);
136 fd_source->cancelled_tag =
137 g_cancellable_connect (cancellable,
138 (GCallback)fd_source_cancelled_cb,