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: #GAsyncResult
34 * Provides helper functions for asynchronous operations.
38 /*************************************************************************
40 ************************************************************************/
49 fd_source_prepare (GSource *source,
57 fd_source_check (GSource *source)
59 FDSource *fd_source = (FDSource *)source;
61 return fd_source->pollfd.revents != 0;
65 fd_source_dispatch (GSource *source,
70 GFDSourceFunc func = (GFDSourceFunc)callback;
71 FDSource *fd_source = (FDSource *)source;
73 g_warn_if_fail (func != NULL);
75 return (*func) (fd_source->pollfd.fd, fd_source->pollfd.revents, user_data);
79 fd_source_finalize (GSource *source)
84 fd_source_closure_callback (int fd,
85 GIOCondition condition,
88 GClosure *closure = data;
90 GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
91 GValue result_value = G_VALUE_INIT;
94 g_value_init (&result_value, G_TYPE_BOOLEAN);
96 g_value_init (¶ms[0], G_TYPE_INT);
97 g_value_set_int (¶ms[0], fd);
99 g_value_init (¶ms[1], G_TYPE_IO_CONDITION);
100 g_value_set_flags (¶ms[1], condition);
102 g_closure_invoke (closure, &result_value, 2, params, NULL);
104 result = g_value_get_boolean (&result_value);
105 g_value_unset (&result_value);
106 g_value_unset (¶ms[0]);
107 g_value_unset (¶ms[1]);
112 static GSourceFuncs fd_source_funcs = {
117 (GSourceFunc)fd_source_closure_callback,
121 _g_fd_source_new (int fd,
123 GCancellable *cancellable)
128 source = g_source_new (&fd_source_funcs, sizeof (FDSource));
129 fd_source = (FDSource *)source;
131 fd_source->pollfd.fd = fd;
132 fd_source->pollfd.events = events;
133 g_source_add_poll (source, &fd_source->pollfd);
137 GSource *cancellable_source = g_cancellable_source_new (cancellable);
139 g_source_set_dummy_callback (cancellable_source);
140 g_source_add_child_source (source, cancellable_source);
141 g_source_unref (cancellable_source);
149 _g_win32_overlap_wait_result (HANDLE hfile,
152 GCancellable *cancellable)
155 gboolean result = FALSE;
158 pollfd[0].fd = (gint)overlap->hEvent;
159 pollfd[0].events = G_IO_IN;
162 if (g_cancellable_make_pollfd (cancellable, &pollfd[1]))
166 npoll = g_poll (pollfd, num, -1);
168 /* error out, should never happen */
171 if (g_cancellable_is_cancelled (cancellable))
173 /* CancelIO only cancels pending operations issued by the
174 * current thread and since we're doing only sync operations,
175 * this is safe.... */
176 /* CancelIoEx is only Vista+. Since we have only one overlap
177 * operaton on this thread, we can just use: */
178 result = CancelIo (hfile);
179 g_warn_if_fail (result);
182 result = GetOverlappedResult (overlap->hEvent, overlap, transferred, FALSE);
183 if (result == FALSE &&
184 GetLastError () == ERROR_IO_INCOMPLETE &&
185 !g_cancellable_is_cancelled (cancellable))
190 g_cancellable_release_fd (cancellable);