gasynchelper.c 4.78 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* 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>
 */

23
#include "config.h"
24 25 26

#include "gasynchelper.h"

27 28
#include "gioalias.h"

29 30 31
/**
 * SECTION:gasynchelper
 * @short_description: Asynchronous Helper Functions
Matthias Clasen's avatar
Matthias Clasen committed
32
 * @include: gio/gio.h
33
 * @see_also: #GAsyncReady
34 35 36 37 38
 * 
 * Provides helper functions for asynchronous operations.
 *
 **/

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
static void
async_result_free (gpointer data)
{
  GAsyncResultData *res = data;

  if (res->error)
    g_error_free (res->error);

  g_object_unref (res->async_object);
  
  g_free (res);
}

void
_g_queue_async_result (GAsyncResultData *result,
Matthias Clasen's avatar
Matthias Clasen committed
54 55 56 57
		       gpointer          async_object,
		       GError           *error,
		       gpointer          user_data,
		       GSourceFunc       source_func)
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
{
  GSource *source;

  g_return_if_fail (G_IS_OBJECT (async_object));
  
  result->async_object = g_object_ref (async_object);
  result->user_data = user_data;
  result->error = error;

  source = g_idle_source_new ();
  g_source_set_priority (source, G_PRIORITY_DEFAULT);
  g_source_set_callback (source, source_func, result, async_result_free);
  g_source_attach (source, NULL);
  g_source_unref (source);
}

/*************************************************************************
 *             fd source                                                 *
 ************************************************************************/

typedef struct 
{
  GSource source;
  GPollFD pollfd;
  GCancellable *cancellable;
  gulong cancelled_tag;
84
  GObject *object;
85 86 87
} FDSource;

static gboolean 
Matthias Clasen's avatar
Matthias Clasen committed
88 89
fd_source_prepare (GSource *source,
		   gint    *timeout)
90 91 92 93 94 95 96
{
  FDSource *fd_source = (FDSource *)source;
  *timeout = -1;
  
  return g_cancellable_is_cancelled (fd_source->cancellable);
}

97
static gboolean
Matthias Clasen's avatar
Matthias Clasen committed
98
fd_source_check (GSource *source)
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
{
  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;
114
  GFDSourceObjectFunc func2 = (GFDSourceObjectFunc)callback;
115 116
  FDSource *fd_source = (FDSource *)source;

117
  g_warn_if_fail (func != NULL);
118

119 120 121 122
  if (fd_source->object)
    return (*func2) (fd_source->object, fd_source->pollfd.revents, user_data);
  else
    return (*func) (user_data, fd_source->pollfd.revents, fd_source->pollfd.fd);
123 124
}

125
static void
126 127 128 129 130
fd_source_finalize (GSource *source)
{
  FDSource *fd_source = (FDSource *)source;

  if (fd_source->cancelled_tag)
131 132
    g_cancellable_disconnect (fd_source->cancellable,
			      fd_source->cancelled_tag);
133 134 135

  if (fd_source->cancellable)
    g_object_unref (fd_source->cancellable);
136 137 138

  if (fd_source->object)
    g_object_unref (fd_source->object);
139 140 141 142 143 144 145 146 147 148 149 150
}

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,
Matthias Clasen's avatar
Matthias Clasen committed
151
			gpointer      data)
152 153 154 155 156 157
{
  /* Wake up the mainloop in case we're waiting on async calls with FDSource */
  g_main_context_wakeup (NULL);
}

GSource *
158 159 160 161
_g_fd_source_new_with_object (GObject      *object,
			      int           fd,
			      gushort       events,
			      GCancellable *cancellable)
162 163 164 165 166 167 168 169 170
{
  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);
171 172 173 174

  if (object)
    fd_source->object = g_object_ref (object);

175 176 177 178 179 180
  fd_source->pollfd.fd = fd;
  fd_source->pollfd.events = events;
  g_source_add_poll (source, &fd_source->pollfd);

  if (cancellable)
    fd_source->cancelled_tag =
181
      g_cancellable_connect (cancellable,
182
			     (GCallback)fd_source_cancelled_cb,
183
			     NULL, NULL);
184

185 186
  return source;
}
187 188 189 190 191 192 193 194

GSource *
_g_fd_source_new (int           fd,
		  gushort       events,
		  GCancellable *cancellable)
{
  return _g_fd_source_new_with_object (NULL, fd, events, cancellable);
}