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 ************************************************************************/
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] = { { 0, }, { 0, } };
91 GValue result_value = { 0, };
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]);
113 fd_source_closure_marshal (GClosure *closure,
114 GValue *return_value,
115 guint n_param_values,
116 const GValue *param_values,
117 gpointer invocation_hint,
118 gpointer marshal_data)
120 GFDSourceFunc callback;
121 GCClosure *cc = (GCClosure*) closure;
124 g_return_if_fail (return_value != NULL);
125 g_return_if_fail (n_param_values == 0);
127 callback = (GFDSourceFunc) (marshal_data ? marshal_data : cc->callback);
129 v_return = callback (g_value_get_int (param_values),
130 g_value_get_flags (param_values + 1),
133 g_value_set_boolean (return_value, v_return);
136 static GSourceFuncs fd_source_funcs = {
141 (GSourceFunc)fd_source_closure_callback,
142 (GSourceDummyMarshal)fd_source_closure_marshal,
146 _g_fd_source_new (int fd,
148 GCancellable *cancellable)
153 source = g_source_new (&fd_source_funcs, sizeof (FDSource));
154 fd_source = (FDSource *)source;
156 fd_source->pollfd.fd = fd;
157 fd_source->pollfd.events = events;
158 g_source_add_poll (source, &fd_source->pollfd);
162 GSource *cancellable_source = g_cancellable_source_new (cancellable);
164 g_source_set_dummy_callback (cancellable_source);
165 g_source_add_child_source (source, cancellable_source);
166 g_source_unref (cancellable_source);