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
|
|
|
|
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;
|
|
|
|
GCancellable *cancellable;
|
|
|
|
gulong cancelled_tag;
|
|
|
|
} 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
|
|
|
{
|
|
|
|
FDSource *fd_source = (FDSource *)source;
|
|
|
|
*timeout = -1;
|
|
|
|
|
|
|
|
return g_cancellable_is_cancelled (fd_source->cancellable);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
return
|
|
|
|
g_cancellable_is_cancelled (fd_source->cancellable) ||
|
|
|
|
fd_source->pollfd.revents != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2009-07-09 15:55:00 +02:00
|
|
|
return (*func) (user_data, fd_source->pollfd.revents, fd_source->pollfd.fd);
|
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)
|
|
|
|
{
|
|
|
|
FDSource *fd_source = (FDSource *)source;
|
|
|
|
|
|
|
|
if (fd_source->cancelled_tag)
|
2009-04-20 13:14:32 +02:00
|
|
|
g_cancellable_disconnect (fd_source->cancellable,
|
|
|
|
fd_source->cancelled_tag);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
|
|
if (fd_source->cancellable)
|
|
|
|
g_object_unref (fd_source->cancellable);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GSourceFuncs fd_source_funcs = {
|
|
|
|
fd_source_prepare,
|
|
|
|
fd_source_check,
|
|
|
|
fd_source_dispatch,
|
|
|
|
fd_source_finalize
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Might be called on another thread */
|
|
|
|
static void
|
|
|
|
fd_source_cancelled_cb (GCancellable *cancellable,
|
2007-11-29 08:17:59 +01:00
|
|
|
gpointer data)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
|
|
|
/* Wake up the mainloop in case we're waiting on async calls with FDSource */
|
|
|
|
g_main_context_wakeup (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (cancellable)
|
|
|
|
fd_source->cancellable = g_object_ref (cancellable);
|
2009-07-09 15:55:00 +02:00
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
fd_source->pollfd.fd = fd;
|
|
|
|
fd_source->pollfd.events = events;
|
|
|
|
g_source_add_poll (source, &fd_source->pollfd);
|
|
|
|
|
|
|
|
if (cancellable)
|
|
|
|
fd_source->cancelled_tag =
|
2009-07-09 15:55:00 +02:00
|
|
|
g_cancellable_connect (cancellable,
|
2007-11-26 17:13:05 +01:00
|
|
|
(GCallback)fd_source_cancelled_cb,
|
2009-04-20 13:14:32 +02:00
|
|
|
NULL, NULL);
|
2009-07-09 15:55:00 +02:00
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
return source;
|
|
|
|
}
|