Blame gio/gcancellable.c

Packit ae235b
/* GIO - GLib Input, Output and Streaming Library
Packit ae235b
 * 
Packit ae235b
 * Copyright (C) 2006-2007 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Author: Alexander Larsson <alexl@redhat.com>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
#include "glib.h"
Packit ae235b
#include <gioerror.h>
Packit ae235b
#include "glib-private.h"
Packit ae235b
#include "gcancellable.h"
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gcancellable
Packit ae235b
 * @short_description: Thread-safe Operation Cancellation Stack
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 *
Packit ae235b
 * GCancellable is a thread-safe operation cancellation stack used 
Packit ae235b
 * throughout GIO to allow for cancellation of synchronous and
Packit ae235b
 * asynchronous operations.
Packit ae235b
 */
Packit ae235b
Packit ae235b
enum {
Packit ae235b
  CANCELLED,
Packit ae235b
  LAST_SIGNAL
Packit ae235b
};
Packit ae235b
Packit ae235b
struct _GCancellablePrivate
Packit ae235b
{
Packit ae235b
  guint cancelled : 1;
Packit ae235b
  guint cancelled_running : 1;
Packit ae235b
  guint cancelled_running_waiting : 1;
Packit ae235b
Packit ae235b
  guint fd_refcount;
Packit ae235b
  GWakeup *wakeup;
Packit ae235b
};
Packit ae235b
Packit ae235b
static guint signals[LAST_SIGNAL] = { 0 };
Packit ae235b
Packit ae235b
G_DEFINE_TYPE_WITH_PRIVATE (GCancellable, g_cancellable, G_TYPE_OBJECT)
Packit ae235b
Packit ae235b
static GPrivate current_cancellable;
Packit ae235b
static GMutex cancellable_mutex;
Packit ae235b
static GCond cancellable_cond;
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_cancellable_finalize (GObject *object)
Packit ae235b
{
Packit ae235b
  GCancellable *cancellable = G_CANCELLABLE (object);
Packit ae235b
Packit ae235b
  if (cancellable->priv->wakeup)
Packit ae235b
    GLIB_PRIVATE_CALL (g_wakeup_free) (cancellable->priv->wakeup);
Packit ae235b
Packit ae235b
  G_OBJECT_CLASS (g_cancellable_parent_class)->finalize (object);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_cancellable_class_init (GCancellableClass *klass)
Packit ae235b
{
Packit ae235b
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
Packit ae235b
Packit ae235b
  gobject_class->finalize = g_cancellable_finalize;
Packit ae235b
Packit ae235b
  /**
Packit ae235b
   * GCancellable::cancelled:
Packit ae235b
   * @cancellable: a #GCancellable.
Packit ae235b
   * 
Packit ae235b
   * Emitted when the operation has been cancelled.
Packit ae235b
   * 
Packit ae235b
   * Can be used by implementations of cancellable operations. If the
Packit ae235b
   * operation is cancelled from another thread, the signal will be
Packit ae235b
   * emitted in the thread that cancelled the operation, not the
Packit ae235b
   * thread that is running the operation.
Packit ae235b
   *
Packit ae235b
   * Note that disconnecting from this signal (or any signal) in a
Packit ae235b
   * multi-threaded program is prone to race conditions. For instance
Packit ae235b
   * it is possible that a signal handler may be invoked even after
Packit ae235b
   * a call to g_signal_handler_disconnect() for that handler has
Packit ae235b
   * already returned.
Packit ae235b
   * 
Packit ae235b
   * There is also a problem when cancellation happens right before
Packit ae235b
   * connecting to the signal. If this happens the signal will
Packit ae235b
   * unexpectedly not be emitted, and checking before connecting to
Packit ae235b
   * the signal leaves a race condition where this is still happening.
Packit ae235b
   *
Packit ae235b
   * In order to make it safe and easy to connect handlers there
Packit ae235b
   * are two helper functions: g_cancellable_connect() and
Packit ae235b
   * g_cancellable_disconnect() which protect against problems
Packit ae235b
   * like this.
Packit ae235b
   *
Packit ae235b
   * An example of how to us this:
Packit ae235b
   * |[
Packit ae235b
   *     // Make sure we don't do unnecessary work if already cancelled
Packit ae235b
   *     if (g_cancellable_set_error_if_cancelled (cancellable, error))
Packit ae235b
   *       return;
Packit ae235b
   *
Packit ae235b
   *     // Set up all the data needed to be able to handle cancellation
Packit ae235b
   *     // of the operation
Packit ae235b
   *     my_data = my_data_new (...);
Packit ae235b
   *
Packit ae235b
   *     id = 0;
Packit ae235b
   *     if (cancellable)
Packit ae235b
   *       id = g_cancellable_connect (cancellable,
Packit ae235b
   *     			      G_CALLBACK (cancelled_handler)
Packit ae235b
   *     			      data, NULL);
Packit ae235b
   *
Packit ae235b
   *     // cancellable operation here...
Packit ae235b
   *
Packit ae235b
   *     g_cancellable_disconnect (cancellable, id);
Packit ae235b
   *
Packit ae235b
   *     // cancelled_handler is never called after this, it is now safe
Packit ae235b
   *     // to free the data
Packit ae235b
   *     my_data_free (my_data);  
Packit ae235b
   * ]|
Packit ae235b
   *
Packit ae235b
   * Note that the cancelled signal is emitted in the thread that
Packit ae235b
   * the user cancelled from, which may be the main thread. So, the
Packit ae235b
   * cancellable signal should not do something that can block.
Packit ae235b
   */
Packit ae235b
  signals[CANCELLED] =
Packit ae235b
    g_signal_new (I_("cancelled"),
Packit ae235b
		  G_TYPE_FROM_CLASS (gobject_class),
Packit ae235b
		  G_SIGNAL_RUN_LAST,
Packit ae235b
		  G_STRUCT_OFFSET (GCancellableClass, cancelled),
Packit ae235b
		  NULL, NULL,
Packit ae235b
		  g_cclosure_marshal_VOID__VOID,
Packit ae235b
		  G_TYPE_NONE, 0);
Packit ae235b
  
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_cancellable_init (GCancellable *cancellable)
Packit ae235b
{
Packit ae235b
  cancellable->priv = g_cancellable_get_instance_private (cancellable);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_cancellable_new:
Packit ae235b
 * 
Packit ae235b
 * Creates a new #GCancellable object.
Packit ae235b
 *
Packit ae235b
 * Applications that want to start one or more operations
Packit ae235b
 * that should be cancellable should create a #GCancellable
Packit ae235b
 * and pass it to the operations.
Packit ae235b
 *
Packit ae235b
 * One #GCancellable can be used in multiple consecutive
Packit ae235b
 * operations or in multiple concurrent operations.
Packit ae235b
 *  
Packit ae235b
 * Returns: a #GCancellable.
Packit ae235b
 **/
Packit ae235b
GCancellable *
Packit ae235b
g_cancellable_new (void)
Packit ae235b
{
Packit ae235b
  return g_object_new (G_TYPE_CANCELLABLE, NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_cancellable_push_current:
Packit ae235b
 * @cancellable: a #GCancellable object
Packit ae235b
 *
Packit ae235b
 * Pushes @cancellable onto the cancellable stack. The current
Packit ae235b
 * cancellable can then be received using g_cancellable_get_current().
Packit ae235b
 *
Packit ae235b
 * This is useful when implementing cancellable operations in
Packit ae235b
 * code that does not allow you to pass down the cancellable object.
Packit ae235b
 *
Packit ae235b
 * This is typically called automatically by e.g. #GFile operations,
Packit ae235b
 * so you rarely have to call this yourself.
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_cancellable_push_current (GCancellable *cancellable)
Packit ae235b
{
Packit ae235b
  GSList *l;
Packit ae235b
Packit ae235b
  g_return_if_fail (cancellable != NULL);
Packit ae235b
Packit ae235b
  l = g_private_get (&current_cancellable);
Packit ae235b
  l = g_slist_prepend (l, cancellable);
Packit ae235b
  g_private_set (&current_cancellable, l);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_cancellable_pop_current:
Packit ae235b
 * @cancellable: a #GCancellable object
Packit ae235b
 *
Packit ae235b
 * Pops @cancellable off the cancellable stack (verifying that @cancellable
Packit ae235b
 * is on the top of the stack).
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_cancellable_pop_current (GCancellable *cancellable)
Packit ae235b
{
Packit ae235b
  GSList *l;
Packit ae235b
Packit ae235b
  l = g_private_get (&current_cancellable);
Packit ae235b
Packit ae235b
  g_return_if_fail (l != NULL);
Packit ae235b
  g_return_if_fail (l->data == cancellable);
Packit ae235b
Packit ae235b
  l = g_slist_delete_link (l, l);
Packit ae235b
  g_private_set (&current_cancellable, l);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_cancellable_get_current:
Packit ae235b
 *
Packit ae235b
 * Gets the top cancellable from the stack.
Packit ae235b
 *
Packit ae235b
 * Returns: (nullable) (transfer none): a #GCancellable from the top
Packit ae235b
 * of the stack, or %NULL if the stack is empty.
Packit ae235b
 **/
Packit ae235b
GCancellable *
Packit ae235b
g_cancellable_get_current  (void)
Packit ae235b
{
Packit ae235b
  GSList *l;
Packit ae235b
Packit ae235b
  l = g_private_get (&current_cancellable);
Packit ae235b
  if (l == NULL)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  return G_CANCELLABLE (l->data);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_cancellable_reset:
Packit ae235b
 * @cancellable: a #GCancellable object.
Packit ae235b
 * 
Packit ae235b
 * Resets @cancellable to its uncancelled state.
Packit ae235b
 *
Packit ae235b
 * If cancellable is currently in use by any cancellable operation
Packit ae235b
 * then the behavior of this function is undefined.
Packit ae235b
 *
Packit ae235b
 * Note that it is generally not a good idea to reuse an existing
Packit ae235b
 * cancellable for more operations after it has been cancelled once,
Packit ae235b
 * as this function might tempt you to do. The recommended practice
Packit ae235b
 * is to drop the reference to a cancellable after cancelling it,
Packit ae235b
 * and let it die with the outstanding async operations. You should
Packit ae235b
 * create a fresh cancellable for further async operations.
Packit ae235b
 **/
Packit ae235b
void 
Packit ae235b
g_cancellable_reset (GCancellable *cancellable)
Packit ae235b
{
Packit ae235b
  GCancellablePrivate *priv;
Packit ae235b
Packit ae235b
  g_return_if_fail (G_IS_CANCELLABLE (cancellable));
Packit ae235b
Packit ae235b
  g_mutex_lock (&cancellable_mutex);
Packit ae235b
Packit ae235b
  priv = cancellable->priv;
Packit ae235b
Packit ae235b
  while (priv->cancelled_running)
Packit ae235b
    {
Packit ae235b
      priv->cancelled_running_waiting = TRUE;
Packit ae235b
      g_cond_wait (&cancellable_cond, &cancellable_mutex);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (priv->cancelled)
Packit ae235b
    {
Packit ae235b
      if (priv->wakeup)
Packit ae235b
        GLIB_PRIVATE_CALL (g_wakeup_acknowledge) (priv->wakeup);
Packit ae235b
Packit ae235b
      priv->cancelled = FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_mutex_unlock (&cancellable_mutex);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_cancellable_is_cancelled:
Packit ae235b
 * @cancellable: (nullable): a #GCancellable or %NULL
Packit ae235b
 *
Packit ae235b
 * Checks if a cancellable job has been cancelled.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if @cancellable is cancelled,
Packit ae235b
 * FALSE if called with %NULL or if item is not cancelled.
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_cancellable_is_cancelled (GCancellable *cancellable)
Packit ae235b
{
Packit ae235b
  return cancellable != NULL && cancellable->priv->cancelled;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_cancellable_set_error_if_cancelled:
Packit ae235b
 * @cancellable: (nullable): a #GCancellable or %NULL
Packit ae235b
 * @error: #GError to append error state to
Packit ae235b
 *
Packit ae235b
 * If the @cancellable is cancelled, sets the error to notify
Packit ae235b
 * that the operation was cancelled.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if @cancellable was cancelled, %FALSE if it was not
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_cancellable_set_error_if_cancelled (GCancellable  *cancellable,
Packit ae235b
                                      GError       **error)
Packit ae235b
{
Packit ae235b
  if (g_cancellable_is_cancelled (cancellable))
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error,
Packit ae235b
                           G_IO_ERROR,
Packit ae235b
                           G_IO_ERROR_CANCELLED,
Packit ae235b
                           _("Operation was cancelled"));
Packit ae235b
      return TRUE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_cancellable_get_fd:
Packit ae235b
 * @cancellable: a #GCancellable.
Packit ae235b
 * 
Packit ae235b
 * Gets the file descriptor for a cancellable job. This can be used to
Packit ae235b
 * implement cancellable operations on Unix systems. The returned fd will
Packit ae235b
 * turn readable when @cancellable is cancelled.
Packit ae235b
 *
Packit ae235b
 * You are not supposed to read from the fd yourself, just check for
Packit ae235b
 * readable status. Reading to unset the readable status is done
Packit ae235b
 * with g_cancellable_reset().
Packit ae235b
 * 
Packit ae235b
 * After a successful return from this function, you should use 
Packit ae235b
 * g_cancellable_release_fd() to free up resources allocated for 
Packit ae235b
 * the returned file descriptor.
Packit ae235b
 *
Packit ae235b
 * See also g_cancellable_make_pollfd().
Packit ae235b
 *
Packit ae235b
 * Returns: A valid file descriptor. %-1 if the file descriptor 
Packit ae235b
 * is not supported, or on errors. 
Packit ae235b
 **/
Packit ae235b
int
Packit ae235b
g_cancellable_get_fd (GCancellable *cancellable)
Packit ae235b
{
Packit ae235b
  GPollFD pollfd;
Packit ae235b
Packit ae235b
  if (cancellable == NULL)
Packit ae235b
	  return -1;
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  pollfd.fd = -1;
Packit ae235b
#else
Packit ae235b
  g_cancellable_make_pollfd (cancellable, &pollfd);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  return pollfd.fd;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_cancellable_make_pollfd:
Packit ae235b
 * @cancellable: (nullable): a #GCancellable or %NULL
Packit ae235b
 * @pollfd: a pointer to a #GPollFD
Packit ae235b
 * 
Packit ae235b
 * Creates a #GPollFD corresponding to @cancellable; this can be passed
Packit ae235b
 * to g_poll() and used to poll for cancellation. This is useful both
Packit ae235b
 * for unix systems without a native poll and for portability to
Packit ae235b
 * windows.
Packit ae235b
 *
Packit ae235b
 * When this function returns %TRUE, you should use 
Packit ae235b
 * g_cancellable_release_fd() to free up resources allocated for the 
Packit ae235b
 * @pollfd. After a %FALSE return, do not call g_cancellable_release_fd().
Packit ae235b
 *
Packit ae235b
 * If this function returns %FALSE, either no @cancellable was given or
Packit ae235b
 * resource limits prevent this function from allocating the necessary 
Packit ae235b
 * structures for polling. (On Linux, you will likely have reached 
Packit ae235b
 * the maximum number of file descriptors.) The suggested way to handle
Packit ae235b
 * these cases is to ignore the @cancellable.
Packit ae235b
 *
Packit ae235b
 * You are not supposed to read from the fd yourself, just check for
Packit ae235b
 * readable status. Reading to unset the readable status is done
Packit ae235b
 * with g_cancellable_reset().
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if @pollfd was successfully initialized, %FALSE on 
Packit ae235b
 *          failure to prepare the cancellable.
Packit ae235b
 * 
Packit ae235b
 * Since: 2.22
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_cancellable_make_pollfd (GCancellable *cancellable, GPollFD *pollfd)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (pollfd != NULL, FALSE);
Packit ae235b
  if (cancellable == NULL)
Packit ae235b
    return FALSE;
Packit ae235b
  g_return_val_if_fail (G_IS_CANCELLABLE (cancellable), FALSE);
Packit ae235b
Packit ae235b
  g_mutex_lock (&cancellable_mutex);
Packit ae235b
Packit ae235b
  cancellable->priv->fd_refcount++;
Packit ae235b
Packit ae235b
  if (cancellable->priv->wakeup == NULL)
Packit ae235b
    {
Packit ae235b
      cancellable->priv->wakeup = GLIB_PRIVATE_CALL (g_wakeup_new) ();
Packit ae235b
Packit ae235b
      if (cancellable->priv->cancelled)
Packit ae235b
        GLIB_PRIVATE_CALL (g_wakeup_signal) (cancellable->priv->wakeup);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  GLIB_PRIVATE_CALL (g_wakeup_get_pollfd) (cancellable->priv->wakeup, pollfd);
Packit ae235b
Packit ae235b
  g_mutex_unlock (&cancellable_mutex);
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_cancellable_release_fd:
Packit ae235b
 * @cancellable: a #GCancellable
Packit ae235b
 *
Packit ae235b
 * Releases a resources previously allocated by g_cancellable_get_fd()
Packit ae235b
 * or g_cancellable_make_pollfd().
Packit ae235b
 *
Packit ae235b
 * For compatibility reasons with older releases, calling this function 
Packit ae235b
 * is not strictly required, the resources will be automatically freed
Packit ae235b
 * when the @cancellable is finalized. However, the @cancellable will
Packit ae235b
 * block scarce file descriptors until it is finalized if this function
Packit ae235b
 * is not called. This can cause the application to run out of file 
Packit ae235b
 * descriptors when many #GCancellables are used at the same time.
Packit ae235b
 * 
Packit ae235b
 * Since: 2.22
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_cancellable_release_fd (GCancellable *cancellable)
Packit ae235b
{
Packit ae235b
  GCancellablePrivate *priv;
Packit ae235b
Packit ae235b
  if (cancellable == NULL)
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  g_return_if_fail (G_IS_CANCELLABLE (cancellable));
Packit ae235b
  g_return_if_fail (cancellable->priv->fd_refcount > 0);
Packit ae235b
Packit ae235b
  priv = cancellable->priv;
Packit ae235b
Packit ae235b
  g_mutex_lock (&cancellable_mutex);
Packit ae235b
Packit ae235b
  priv->fd_refcount--;
Packit ae235b
  if (priv->fd_refcount == 0)
Packit ae235b
    {
Packit ae235b
      GLIB_PRIVATE_CALL (g_wakeup_free) (priv->wakeup);
Packit ae235b
      priv->wakeup = NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_mutex_unlock (&cancellable_mutex);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_cancellable_cancel:
Packit ae235b
 * @cancellable: (nullable): a #GCancellable object.
Packit ae235b
 * 
Packit ae235b
 * Will set @cancellable to cancelled, and will emit the
Packit ae235b
 * #GCancellable::cancelled signal. (However, see the warning about
Packit ae235b
 * race conditions in the documentation for that signal if you are
Packit ae235b
 * planning to connect to it.)
Packit ae235b
 *
Packit ae235b
 * This function is thread-safe. In other words, you can safely call
Packit ae235b
 * it from a thread other than the one running the operation that was
Packit ae235b
 * passed the @cancellable.
Packit ae235b
 *
Packit ae235b
 * If @cancellable is %NULL, this function returns immediately for convenience.
Packit ae235b
 *
Packit ae235b
 * The convention within GIO is that cancelling an asynchronous
Packit ae235b
 * operation causes it to complete asynchronously. That is, if you
Packit ae235b
 * cancel the operation from the same thread in which it is running,
Packit ae235b
 * then the operation's #GAsyncReadyCallback will not be invoked until
Packit ae235b
 * the application returns to the main loop.
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_cancellable_cancel (GCancellable *cancellable)
Packit ae235b
{
Packit ae235b
  GCancellablePrivate *priv;
Packit ae235b
Packit ae235b
  if (cancellable == NULL ||
Packit ae235b
      cancellable->priv->cancelled)
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  priv = cancellable->priv;
Packit ae235b
Packit ae235b
  g_mutex_lock (&cancellable_mutex);
Packit ae235b
Packit ae235b
  if (priv->cancelled)
Packit ae235b
    {
Packit ae235b
      g_mutex_unlock (&cancellable_mutex);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  priv->cancelled = TRUE;
Packit ae235b
  priv->cancelled_running = TRUE;
Packit ae235b
Packit ae235b
  if (priv->wakeup)
Packit ae235b
    GLIB_PRIVATE_CALL (g_wakeup_signal) (priv->wakeup);
Packit ae235b
Packit ae235b
  g_mutex_unlock (&cancellable_mutex);
Packit ae235b
Packit ae235b
  g_object_ref (cancellable);
Packit ae235b
  g_signal_emit (cancellable, signals[CANCELLED], 0);
Packit ae235b
Packit ae235b
  g_mutex_lock (&cancellable_mutex);
Packit ae235b
Packit ae235b
  priv->cancelled_running = FALSE;
Packit ae235b
  if (priv->cancelled_running_waiting)
Packit ae235b
    g_cond_broadcast (&cancellable_cond);
Packit ae235b
  priv->cancelled_running_waiting = FALSE;
Packit ae235b
Packit ae235b
  g_mutex_unlock (&cancellable_mutex);
Packit ae235b
Packit ae235b
  g_object_unref (cancellable);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_cancellable_connect:
Packit ae235b
 * @cancellable: A #GCancellable.
Packit ae235b
 * @callback: The #GCallback to connect.
Packit ae235b
 * @data: Data to pass to @callback.
Packit ae235b
 * @data_destroy_func: (nullable): Free function for @data or %NULL.
Packit ae235b
 *
Packit ae235b
 * Convenience function to connect to the #GCancellable::cancelled
Packit ae235b
 * signal. Also handles the race condition that may happen
Packit ae235b
 * if the cancellable is cancelled right before connecting.
Packit ae235b
 *
Packit ae235b
 * @callback is called at most once, either directly at the
Packit ae235b
 * time of the connect if @cancellable is already cancelled,
Packit ae235b
 * or when @cancellable is cancelled in some thread.
Packit ae235b
 *
Packit ae235b
 * @data_destroy_func will be called when the handler is
Packit ae235b
 * disconnected, or immediately if the cancellable is already
Packit ae235b
 * cancelled.
Packit ae235b
 *
Packit ae235b
 * See #GCancellable::cancelled for details on how to use this.
Packit ae235b
 *
Packit ae235b
 * Since GLib 2.40, the lock protecting @cancellable is not held when
Packit ae235b
 * @callback is invoked.  This lifts a restriction in place for
Packit ae235b
 * earlier GLib versions which now makes it easier to write cleanup
Packit ae235b
 * code that unconditionally invokes e.g. g_cancellable_cancel().
Packit ae235b
 *
Packit ae235b
 * Returns: The id of the signal handler or 0 if @cancellable has already
Packit ae235b
 *          been cancelled.
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
gulong
Packit ae235b
g_cancellable_connect (GCancellable   *cancellable,
Packit ae235b
		       GCallback       callback,
Packit ae235b
		       gpointer        data,
Packit ae235b
		       GDestroyNotify  data_destroy_func)
Packit ae235b
{
Packit ae235b
  gulong id;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_CANCELLABLE (cancellable), 0);
Packit ae235b
Packit ae235b
  g_mutex_lock (&cancellable_mutex);
Packit ae235b
Packit ae235b
  if (cancellable->priv->cancelled)
Packit ae235b
    {
Packit ae235b
      void (*_callback) (GCancellable *cancellable,
Packit ae235b
                         gpointer      user_data);
Packit ae235b
Packit ae235b
      g_mutex_unlock (&cancellable_mutex);
Packit ae235b
Packit ae235b
      _callback = (void *)callback;
Packit ae235b
      id = 0;
Packit ae235b
Packit ae235b
      _callback (cancellable, data);
Packit ae235b
Packit ae235b
      if (data_destroy_func)
Packit ae235b
        data_destroy_func (data);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      id = g_signal_connect_data (cancellable, "cancelled",
Packit ae235b
                                  callback, data,
Packit ae235b
                                  (GClosureNotify) data_destroy_func,
Packit ae235b
                                  0);
Packit ae235b
Packit ae235b
      g_mutex_unlock (&cancellable_mutex);
Packit ae235b
    }
Packit ae235b
Packit ae235b
Packit ae235b
  return id;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_cancellable_disconnect:
Packit ae235b
 * @cancellable: (nullable): A #GCancellable or %NULL.
Packit ae235b
 * @handler_id: Handler id of the handler to be disconnected, or `0`.
Packit ae235b
 *
Packit ae235b
 * Disconnects a handler from a cancellable instance similar to
Packit ae235b
 * g_signal_handler_disconnect().  Additionally, in the event that a
Packit ae235b
 * signal handler is currently running, this call will block until the
Packit ae235b
 * handler has finished.  Calling this function from a
Packit ae235b
 * #GCancellable::cancelled signal handler will therefore result in a
Packit ae235b
 * deadlock.
Packit ae235b
 *
Packit ae235b
 * This avoids a race condition where a thread cancels at the
Packit ae235b
 * same time as the cancellable operation is finished and the
Packit ae235b
 * signal handler is removed. See #GCancellable::cancelled for
Packit ae235b
 * details on how to use this.
Packit ae235b
 *
Packit ae235b
 * If @cancellable is %NULL or @handler_id is `0` this function does
Packit ae235b
 * nothing.
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_cancellable_disconnect (GCancellable  *cancellable,
Packit ae235b
			  gulong         handler_id)
Packit ae235b
{
Packit ae235b
  GCancellablePrivate *priv;
Packit ae235b
Packit ae235b
  if (handler_id == 0 ||  cancellable == NULL)
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  g_mutex_lock (&cancellable_mutex);
Packit ae235b
Packit ae235b
  priv = cancellable->priv;
Packit ae235b
Packit ae235b
  while (priv->cancelled_running)
Packit ae235b
    {
Packit ae235b
      priv->cancelled_running_waiting = TRUE;
Packit ae235b
      g_cond_wait (&cancellable_cond, &cancellable_mutex);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_signal_handler_disconnect (cancellable, handler_id);
Packit ae235b
Packit ae235b
  g_mutex_unlock (&cancellable_mutex);
Packit ae235b
}
Packit ae235b
Packit ae235b
typedef struct {
Packit ae235b
  GSource       source;
Packit ae235b
Packit ae235b
  GCancellable *cancellable;
Packit ae235b
  guint         cancelled_handler;
Packit ae235b
} GCancellableSource;
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * We can't guarantee that the source still has references, so we are
Packit ae235b
 * relying on the fact that g_source_set_ready_time() no longer makes
Packit ae235b
 * assertions about the reference count - the source might be in the
Packit ae235b
 * window between last-unref and finalize, during which its refcount
Packit ae235b
 * is officially 0. However, we *can* guarantee that it's OK to
Packit ae235b
 * dereference it in a limited way, because we know we haven't yet reached
Packit ae235b
 * cancellable_source_finalize() - if we had, then we would have waited
Packit ae235b
 * for signal emission to finish, then disconnected the signal handler
Packit ae235b
 * under the lock.
Packit ae235b
 * See https://bugzilla.gnome.org/show_bug.cgi?id=791754
Packit ae235b
 */
Packit ae235b
static void
Packit ae235b
cancellable_source_cancelled (GCancellable *cancellable,
Packit ae235b
			      gpointer      user_data)
Packit ae235b
{
Packit ae235b
  GSource *source = user_data;
Packit ae235b
Packit ae235b
  g_source_set_ready_time (source, 0);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
cancellable_source_dispatch (GSource     *source,
Packit ae235b
			     GSourceFunc  callback,
Packit ae235b
			     gpointer     user_data)
Packit ae235b
{
Packit ae235b
  GCancellableSourceFunc func = (GCancellableSourceFunc)callback;
Packit ae235b
  GCancellableSource *cancellable_source = (GCancellableSource *)source;
Packit ae235b
Packit ae235b
  g_source_set_ready_time (source, -1);
Packit ae235b
  return (*func) (cancellable_source->cancellable, user_data);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
cancellable_source_finalize (GSource *source)
Packit ae235b
{
Packit ae235b
  GCancellableSource *cancellable_source = (GCancellableSource *)source;
Packit ae235b
Packit ae235b
  if (cancellable_source->cancellable)
Packit ae235b
    {
Packit ae235b
      g_cancellable_disconnect (cancellable_source->cancellable,
Packit ae235b
                                cancellable_source->cancelled_handler);
Packit ae235b
      g_object_unref (cancellable_source->cancellable);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
cancellable_source_closure_callback (GCancellable *cancellable,
Packit ae235b
				     gpointer      data)
Packit ae235b
{
Packit ae235b
  GClosure *closure = data;
Packit ae235b
Packit ae235b
  GValue params = G_VALUE_INIT;
Packit ae235b
  GValue result_value = G_VALUE_INIT;
Packit ae235b
  gboolean result;
Packit ae235b
Packit ae235b
  g_value_init (&result_value, G_TYPE_BOOLEAN);
Packit ae235b
Packit ae235b
  g_value_init (&params, G_TYPE_CANCELLABLE);
Packit ae235b
  g_value_set_object (&params, cancellable);
Packit ae235b
Packit ae235b
  g_closure_invoke (closure, &result_value, 1, &params, NULL);
Packit ae235b
Packit ae235b
  result = g_value_get_boolean (&result_value);
Packit ae235b
  g_value_unset (&result_value);
Packit ae235b
  g_value_unset (&params);
Packit ae235b
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GSourceFuncs cancellable_source_funcs =
Packit ae235b
{
Packit ae235b
  NULL,
Packit ae235b
  NULL,
Packit ae235b
  cancellable_source_dispatch,
Packit ae235b
  cancellable_source_finalize,
Packit ae235b
  (GSourceFunc)cancellable_source_closure_callback,
Packit ae235b
};
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_cancellable_source_new: (skip)
Packit ae235b
 * @cancellable: (nullable): a #GCancellable, or %NULL
Packit ae235b
 *
Packit ae235b
 * Creates a source that triggers if @cancellable is cancelled and
Packit ae235b
 * calls its callback of type #GCancellableSourceFunc. This is
Packit ae235b
 * primarily useful for attaching to another (non-cancellable) source
Packit ae235b
 * with g_source_add_child_source() to add cancellability to it.
Packit ae235b
 *
Packit ae235b
 * For convenience, you can call this with a %NULL #GCancellable,
Packit ae235b
 * in which case the source will never trigger.
Packit ae235b
 *
Packit ae235b
 * The new #GSource will hold a reference to the #GCancellable.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): the new #GSource.
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
GSource *
Packit ae235b
g_cancellable_source_new (GCancellable *cancellable)
Packit ae235b
{
Packit ae235b
  GSource *source;
Packit ae235b
  GCancellableSource *cancellable_source;
Packit ae235b
Packit ae235b
  source = g_source_new (&cancellable_source_funcs, sizeof (GCancellableSource));
Packit ae235b
  g_source_set_name (source, "GCancellable");
Packit ae235b
  cancellable_source = (GCancellableSource *)source;
Packit ae235b
Packit ae235b
  if (cancellable)
Packit ae235b
    {
Packit ae235b
      cancellable_source->cancellable = g_object_ref (cancellable);
Packit ae235b
Packit ae235b
      /* We intentionally don't use g_cancellable_connect() here,
Packit ae235b
       * because we don't want the "at most once" behavior.
Packit ae235b
       */
Packit ae235b
      cancellable_source->cancelled_handler =
Packit ae235b
        g_signal_connect (cancellable, "cancelled",
Packit ae235b
                          G_CALLBACK (cancellable_source_cancelled),
Packit ae235b
                          source);
Packit ae235b
      if (g_cancellable_is_cancelled (cancellable))
Packit ae235b
        g_source_set_ready_time (source, 0);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return source;
Packit ae235b
}