2007-11-26 17:13:05 +01:00
|
|
|
/* GIO - GLib Input, Output and Streaming Library
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006-2007 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General
|
|
|
|
* Public License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*
|
|
|
|
* Author: Alexander Larsson <alexl@redhat.com>
|
|
|
|
*/
|
|
|
|
|
2008-06-22 17:10:51 +02:00
|
|
|
#include "config.h"
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
|
|
#include "gasynchelper.h"
|
|
|
|
|
2007-11-28 13:39:07 +01:00
|
|
|
|
2011-09-06 06:12:12 +02:00
|
|
|
/*< private >
|
2007-11-27 15:00:13 +01:00
|
|
|
* SECTION:gasynchelper
|
|
|
|
* @short_description: Asynchronous Helper Functions
|
2008-02-21 19:20:17 +01:00
|
|
|
* @include: gio/gio.h
|
2007-11-28 07:43:33 +01:00
|
|
|
* @see_also: #GAsyncReady
|
2007-11-27 15:00:13 +01:00
|
|
|
*
|
|
|
|
* Provides helper functions for asynchronous operations.
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
/*************************************************************************
|
|
|
|
* fd source *
|
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
GSource source;
|
|
|
|
GPollFD pollfd;
|
|
|
|
} FDSource;
|
|
|
|
|
|
|
|
static gboolean
|
2007-11-29 08:17:59 +01:00
|
|
|
fd_source_prepare (GSource *source,
|
|
|
|
gint *timeout)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
|
|
|
*timeout = -1;
|
2010-11-06 20:49:55 +01:00
|
|
|
return FALSE;
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
|
|
|
|
2009-07-09 15:55:00 +02:00
|
|
|
static gboolean
|
2007-11-29 08:17:59 +01:00
|
|
|
fd_source_check (GSource *source)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
|
|
|
FDSource *fd_source = (FDSource *)source;
|
|
|
|
|
2010-11-06 20:49:55 +01:00
|
|
|
return fd_source->pollfd.revents != 0;
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
fd_source_dispatch (GSource *source,
|
|
|
|
GSourceFunc callback,
|
|
|
|
gpointer user_data)
|
|
|
|
|
|
|
|
{
|
|
|
|
GFDSourceFunc func = (GFDSourceFunc)callback;
|
|
|
|
FDSource *fd_source = (FDSource *)source;
|
|
|
|
|
2007-12-10 15:07:42 +01:00
|
|
|
g_warn_if_fail (func != NULL);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
2010-11-07 17:05:26 +01:00
|
|
|
return (*func) (fd_source->pollfd.fd, fd_source->pollfd.revents, user_data);
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
|
|
|
|
2009-07-09 15:55:00 +02:00
|
|
|
static void
|
2007-11-26 17:13:05 +01:00
|
|
|
fd_source_finalize (GSource *source)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-11-07 17:05:26 +01:00
|
|
|
static gboolean
|
|
|
|
fd_source_closure_callback (int fd,
|
|
|
|
GIOCondition condition,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GClosure *closure = data;
|
|
|
|
|
2011-09-30 18:08:15 +02:00
|
|
|
GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
|
|
|
|
GValue result_value = G_VALUE_INIT;
|
2010-11-07 17:05:26 +01:00
|
|
|
gboolean result;
|
|
|
|
|
|
|
|
g_value_init (&result_value, G_TYPE_BOOLEAN);
|
|
|
|
|
|
|
|
g_value_init (¶ms[0], G_TYPE_INT);
|
|
|
|
g_value_set_int (¶ms[0], fd);
|
|
|
|
|
|
|
|
g_value_init (¶ms[1], G_TYPE_IO_CONDITION);
|
|
|
|
g_value_set_flags (¶ms[1], condition);
|
|
|
|
|
|
|
|
g_closure_invoke (closure, &result_value, 2, params, NULL);
|
|
|
|
|
|
|
|
result = g_value_get_boolean (&result_value);
|
|
|
|
g_value_unset (&result_value);
|
|
|
|
g_value_unset (¶ms[0]);
|
|
|
|
g_value_unset (¶ms[1]);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
fd_source_closure_marshal (GClosure *closure,
|
|
|
|
GValue *return_value,
|
|
|
|
guint n_param_values,
|
|
|
|
const GValue *param_values,
|
|
|
|
gpointer invocation_hint,
|
|
|
|
gpointer marshal_data)
|
|
|
|
{
|
|
|
|
GFDSourceFunc callback;
|
|
|
|
GCClosure *cc = (GCClosure*) closure;
|
|
|
|
gboolean v_return;
|
|
|
|
|
|
|
|
g_return_if_fail (return_value != NULL);
|
|
|
|
g_return_if_fail (n_param_values == 0);
|
|
|
|
|
|
|
|
callback = (GFDSourceFunc) (marshal_data ? marshal_data : cc->callback);
|
|
|
|
|
|
|
|
v_return = callback (g_value_get_int (param_values),
|
|
|
|
g_value_get_flags (param_values + 1),
|
|
|
|
closure->data);
|
|
|
|
|
|
|
|
g_value_set_boolean (return_value, v_return);
|
|
|
|
}
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
static GSourceFuncs fd_source_funcs = {
|
|
|
|
fd_source_prepare,
|
|
|
|
fd_source_check,
|
|
|
|
fd_source_dispatch,
|
2010-11-07 17:05:26 +01:00
|
|
|
fd_source_finalize,
|
|
|
|
(GSourceFunc)fd_source_closure_callback,
|
|
|
|
(GSourceDummyMarshal)fd_source_closure_marshal,
|
2007-11-26 17:13:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
GSource *
|
2009-07-09 15:55:00 +02:00
|
|
|
_g_fd_source_new (int fd,
|
|
|
|
gushort events,
|
|
|
|
GCancellable *cancellable)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
|
|
|
GSource *source;
|
|
|
|
FDSource *fd_source;
|
|
|
|
|
|
|
|
source = g_source_new (&fd_source_funcs, sizeof (FDSource));
|
|
|
|
fd_source = (FDSource *)source;
|
|
|
|
|
|
|
|
fd_source->pollfd.fd = fd;
|
|
|
|
fd_source->pollfd.events = events;
|
|
|
|
g_source_add_poll (source, &fd_source->pollfd);
|
|
|
|
|
|
|
|
if (cancellable)
|
2010-11-06 20:49:55 +01:00
|
|
|
{
|
|
|
|
GSource *cancellable_source = g_cancellable_source_new (cancellable);
|
|
|
|
|
|
|
|
g_source_set_dummy_callback (cancellable_source);
|
|
|
|
g_source_add_child_source (source, cancellable_source);
|
|
|
|
g_source_unref (cancellable_source);
|
|
|
|
}
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
return source;
|
|
|
|
}
|