GSettings: properly support 'extends'
[glib.git] / gio / gcancellable.c
blobfc56b87ce350e202be5f8cda5e57ba41ed1bbc83
1 /* GIO - GLib Input, Output and Streaming Library
2 *
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>
23 #include "config.h"
24 #include "glib.h"
25 #include <gioerror.h>
26 #include "glib-private.h"
27 #include "gcancellable.h"
28 #include "glibintl.h"
31 /**
32 * SECTION:gcancellable
33 * @short_description: Thread-safe Operation Cancellation Stack
34 * @include: gio/gio.h
36 * GCancellable is a thread-safe operation cancellation stack used
37 * throughout GIO to allow for cancellation of synchronous and
38 * asynchronous operations.
41 enum {
42 CANCELLED,
43 LAST_SIGNAL
46 struct _GCancellablePrivate
48 guint cancelled : 1;
49 guint cancelled_running : 1;
50 guint cancelled_running_waiting : 1;
52 guint fd_refcount;
53 GWakeup *wakeup;
56 static guint signals[LAST_SIGNAL] = { 0 };
58 G_DEFINE_TYPE_WITH_PRIVATE (GCancellable, g_cancellable, G_TYPE_OBJECT)
60 static GPrivate current_cancellable;
61 static GMutex cancellable_mutex;
62 static GCond cancellable_cond;
64 static void
65 g_cancellable_finalize (GObject *object)
67 GCancellable *cancellable = G_CANCELLABLE (object);
69 if (cancellable->priv->wakeup)
70 GLIB_PRIVATE_CALL (g_wakeup_free) (cancellable->priv->wakeup);
72 G_OBJECT_CLASS (g_cancellable_parent_class)->finalize (object);
75 static void
76 g_cancellable_class_init (GCancellableClass *klass)
78 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
80 gobject_class->finalize = g_cancellable_finalize;
82 /**
83 * GCancellable::cancelled:
84 * @cancellable: a #GCancellable.
86 * Emitted when the operation has been cancelled.
88 * Can be used by implementations of cancellable operations. If the
89 * operation is cancelled from another thread, the signal will be
90 * emitted in the thread that cancelled the operation, not the
91 * thread that is running the operation.
93 * Note that disconnecting from this signal (or any signal) in a
94 * multi-threaded program is prone to race conditions. For instance
95 * it is possible that a signal handler may be invoked even
96 * <emphasis>after</emphasis> a call to
97 * g_signal_handler_disconnect() for that handler has already
98 * returned.
100 * There is also a problem when cancellation happen
101 * right before connecting to the signal. If this happens the
102 * signal will unexpectedly not be emitted, and checking before
103 * connecting to the signal leaves a race condition where this is
104 * still happening.
106 * In order to make it safe and easy to connect handlers there
107 * are two helper functions: g_cancellable_connect() and
108 * g_cancellable_disconnect() which protect against problems
109 * like this.
111 * An example of how to us this:
112 * |[
113 * /<!-- -->* Make sure we don't do any unnecessary work if already cancelled *<!-- -->/
114 * if (g_cancellable_set_error_if_cancelled (cancellable))
115 * return;
117 * /<!-- -->* Set up all the data needed to be able to
118 * * handle cancellation of the operation *<!-- -->/
119 * my_data = my_data_new (...);
121 * id = 0;
122 * if (cancellable)
123 * id = g_cancellable_connect (cancellable,
124 * G_CALLBACK (cancelled_handler)
125 * data, NULL);
127 * /<!-- -->* cancellable operation here... *<!-- -->/
129 * g_cancellable_disconnect (cancellable, id);
131 * /<!-- -->* cancelled_handler is never called after this, it
132 * * is now safe to free the data *<!-- -->/
133 * my_data_free (my_data);
134 * ]|
136 * Note that the cancelled signal is emitted in the thread that
137 * the user cancelled from, which may be the main thread. So, the
138 * cancellable signal should not do something that can block.
140 signals[CANCELLED] =
141 g_signal_new (I_("cancelled"),
142 G_TYPE_FROM_CLASS (gobject_class),
143 G_SIGNAL_RUN_LAST,
144 G_STRUCT_OFFSET (GCancellableClass, cancelled),
145 NULL, NULL,
146 g_cclosure_marshal_VOID__VOID,
147 G_TYPE_NONE, 0);
151 static void
152 g_cancellable_init (GCancellable *cancellable)
154 cancellable->priv = g_cancellable_get_instance_private (cancellable);
158 * g_cancellable_new:
160 * Creates a new #GCancellable object.
162 * Applications that want to start one or more operations
163 * that should be cancellable should create a #GCancellable
164 * and pass it to the operations.
166 * One #GCancellable can be used in multiple consecutive
167 * operations or in multiple concurrent operations.
169 * Returns: a #GCancellable.
171 GCancellable *
172 g_cancellable_new (void)
174 return g_object_new (G_TYPE_CANCELLABLE, NULL);
178 * g_cancellable_push_current:
179 * @cancellable: a #GCancellable object
181 * Pushes @cancellable onto the cancellable stack. The current
182 * cancellable can then be received using g_cancellable_get_current().
184 * This is useful when implementing cancellable operations in
185 * code that does not allow you to pass down the cancellable object.
187 * This is typically called automatically by e.g. #GFile operations,
188 * so you rarely have to call this yourself.
190 void
191 g_cancellable_push_current (GCancellable *cancellable)
193 GSList *l;
195 g_return_if_fail (cancellable != NULL);
197 l = g_private_get (&current_cancellable);
198 l = g_slist_prepend (l, cancellable);
199 g_private_set (&current_cancellable, l);
203 * g_cancellable_pop_current:
204 * @cancellable: a #GCancellable object
206 * Pops @cancellable off the cancellable stack (verifying that @cancellable
207 * is on the top of the stack).
209 void
210 g_cancellable_pop_current (GCancellable *cancellable)
212 GSList *l;
214 l = g_private_get (&current_cancellable);
216 g_return_if_fail (l != NULL);
217 g_return_if_fail (l->data == cancellable);
219 l = g_slist_delete_link (l, l);
220 g_private_set (&current_cancellable, l);
224 * g_cancellable_get_current:
226 * Gets the top cancellable from the stack.
228 * Returns: (transfer none): a #GCancellable from the top of the stack, or %NULL
229 * if the stack is empty.
231 GCancellable *
232 g_cancellable_get_current (void)
234 GSList *l;
236 l = g_private_get (&current_cancellable);
237 if (l == NULL)
238 return NULL;
240 return G_CANCELLABLE (l->data);
244 * g_cancellable_reset:
245 * @cancellable: a #GCancellable object.
247 * Resets @cancellable to its uncancelled state.
249 * If cancellable is currently in use by any cancellable operation
250 * then the behavior of this function is undefined.
252 void
253 g_cancellable_reset (GCancellable *cancellable)
255 GCancellablePrivate *priv;
257 g_return_if_fail (G_IS_CANCELLABLE (cancellable));
259 g_mutex_lock (&cancellable_mutex);
261 priv = cancellable->priv;
263 while (priv->cancelled_running)
265 priv->cancelled_running_waiting = TRUE;
266 g_cond_wait (&cancellable_cond, &cancellable_mutex);
269 if (priv->cancelled)
271 if (priv->wakeup)
272 GLIB_PRIVATE_CALL (g_wakeup_acknowledge) (priv->wakeup);
274 priv->cancelled = FALSE;
277 g_mutex_unlock (&cancellable_mutex);
281 * g_cancellable_is_cancelled:
282 * @cancellable: (allow-none): a #GCancellable or %NULL
284 * Checks if a cancellable job has been cancelled.
286 * Returns: %TRUE if @cancellable is cancelled,
287 * FALSE if called with %NULL or if item is not cancelled.
289 gboolean
290 g_cancellable_is_cancelled (GCancellable *cancellable)
292 return cancellable != NULL && cancellable->priv->cancelled;
296 * g_cancellable_set_error_if_cancelled:
297 * @cancellable: (allow-none): a #GCancellable or %NULL
298 * @error: #GError to append error state to
300 * If the @cancellable is cancelled, sets the error to notify
301 * that the operation was cancelled.
303 * Returns: %TRUE if @cancellable was cancelled, %FALSE if it was not
305 gboolean
306 g_cancellable_set_error_if_cancelled (GCancellable *cancellable,
307 GError **error)
309 if (g_cancellable_is_cancelled (cancellable))
311 g_set_error_literal (error,
312 G_IO_ERROR,
313 G_IO_ERROR_CANCELLED,
314 _("Operation was cancelled"));
315 return TRUE;
318 return FALSE;
322 * g_cancellable_get_fd:
323 * @cancellable: a #GCancellable.
325 * Gets the file descriptor for a cancellable job. This can be used to
326 * implement cancellable operations on Unix systems. The returned fd will
327 * turn readable when @cancellable is cancelled.
329 * You are not supposed to read from the fd yourself, just check for
330 * readable status. Reading to unset the readable status is done
331 * with g_cancellable_reset().
333 * After a successful return from this function, you should use
334 * g_cancellable_release_fd() to free up resources allocated for
335 * the returned file descriptor.
337 * See also g_cancellable_make_pollfd().
339 * Returns: A valid file descriptor. %-1 if the file descriptor
340 * is not supported, or on errors.
343 g_cancellable_get_fd (GCancellable *cancellable)
345 GPollFD pollfd;
347 if (cancellable == NULL)
348 return -1;
350 #ifdef G_OS_WIN32
351 pollfd.fd = -1;
352 #else
353 g_cancellable_make_pollfd (cancellable, &pollfd);
354 #endif
356 return pollfd.fd;
360 * g_cancellable_make_pollfd:
361 * @cancellable: (allow-none): a #GCancellable or %NULL
362 * @pollfd: a pointer to a #GPollFD
364 * Creates a #GPollFD corresponding to @cancellable; this can be passed
365 * to g_poll() and used to poll for cancellation. This is useful both
366 * for unix systems without a native poll and for portability to
367 * windows.
369 * When this function returns %TRUE, you should use
370 * g_cancellable_release_fd() to free up resources allocated for the
371 * @pollfd. After a %FALSE return, do not call g_cancellable_release_fd().
373 * If this function returns %FALSE, either no @cancellable was given or
374 * resource limits prevent this function from allocating the necessary
375 * structures for polling. (On Linux, you will likely have reached
376 * the maximum number of file descriptors.) The suggested way to handle
377 * these cases is to ignore the @cancellable.
379 * You are not supposed to read from the fd yourself, just check for
380 * readable status. Reading to unset the readable status is done
381 * with g_cancellable_reset().
383 * Returns: %TRUE if @pollfd was successfully initialized, %FALSE on
384 * failure to prepare the cancellable.
386 * Since: 2.22
388 gboolean
389 g_cancellable_make_pollfd (GCancellable *cancellable, GPollFD *pollfd)
391 g_return_val_if_fail (pollfd != NULL, FALSE);
392 if (cancellable == NULL)
393 return FALSE;
394 g_return_val_if_fail (G_IS_CANCELLABLE (cancellable), FALSE);
396 g_mutex_lock (&cancellable_mutex);
398 cancellable->priv->fd_refcount++;
400 if (cancellable->priv->wakeup == NULL)
402 cancellable->priv->wakeup = GLIB_PRIVATE_CALL (g_wakeup_new) ();
404 if (cancellable->priv->cancelled)
405 GLIB_PRIVATE_CALL (g_wakeup_signal) (cancellable->priv->wakeup);
408 GLIB_PRIVATE_CALL (g_wakeup_get_pollfd) (cancellable->priv->wakeup, pollfd);
410 g_mutex_unlock (&cancellable_mutex);
412 return TRUE;
416 * g_cancellable_release_fd:
417 * @cancellable: a #GCancellable
419 * Releases a resources previously allocated by g_cancellable_get_fd()
420 * or g_cancellable_make_pollfd().
422 * For compatibility reasons with older releases, calling this function
423 * is not strictly required, the resources will be automatically freed
424 * when the @cancellable is finalized. However, the @cancellable will
425 * block scarce file descriptors until it is finalized if this function
426 * is not called. This can cause the application to run out of file
427 * descriptors when many #GCancellables are used at the same time.
429 * Since: 2.22
431 void
432 g_cancellable_release_fd (GCancellable *cancellable)
434 GCancellablePrivate *priv;
436 if (cancellable == NULL)
437 return;
439 g_return_if_fail (G_IS_CANCELLABLE (cancellable));
440 g_return_if_fail (cancellable->priv->fd_refcount > 0);
442 priv = cancellable->priv;
444 g_mutex_lock (&cancellable_mutex);
446 priv->fd_refcount--;
447 if (priv->fd_refcount == 0)
449 GLIB_PRIVATE_CALL (g_wakeup_free) (priv->wakeup);
450 priv->wakeup = NULL;
453 g_mutex_unlock (&cancellable_mutex);
457 * g_cancellable_cancel:
458 * @cancellable: a #GCancellable object.
460 * Will set @cancellable to cancelled, and will emit the
461 * #GCancellable::cancelled signal. (However, see the warning about
462 * race conditions in the documentation for that signal if you are
463 * planning to connect to it.)
465 * This function is thread-safe. In other words, you can safely call
466 * it from a thread other than the one running the operation that was
467 * passed the @cancellable.
469 * The convention within gio is that cancelling an asynchronous
470 * operation causes it to complete asynchronously. That is, if you
471 * cancel the operation from the same thread in which it is running,
472 * then the operation's #GAsyncReadyCallback will not be invoked until
473 * the application returns to the main loop.
475 void
476 g_cancellable_cancel (GCancellable *cancellable)
478 GCancellablePrivate *priv;
480 if (cancellable == NULL ||
481 cancellable->priv->cancelled)
482 return;
484 priv = cancellable->priv;
486 g_mutex_lock (&cancellable_mutex);
488 if (priv->cancelled)
490 g_mutex_unlock (&cancellable_mutex);
491 return;
494 priv->cancelled = TRUE;
495 priv->cancelled_running = TRUE;
497 if (priv->wakeup)
498 GLIB_PRIVATE_CALL (g_wakeup_signal) (priv->wakeup);
500 g_mutex_unlock (&cancellable_mutex);
502 g_object_ref (cancellable);
503 g_signal_emit (cancellable, signals[CANCELLED], 0);
505 g_mutex_lock (&cancellable_mutex);
507 priv->cancelled_running = FALSE;
508 if (priv->cancelled_running_waiting)
509 g_cond_broadcast (&cancellable_cond);
510 priv->cancelled_running_waiting = FALSE;
512 g_mutex_unlock (&cancellable_mutex);
514 g_object_unref (cancellable);
518 * g_cancellable_connect:
519 * @cancellable: A #GCancellable.
520 * @callback: The #GCallback to connect.
521 * @data: Data to pass to @callback.
522 * @data_destroy_func: (allow-none): Free function for @data or %NULL.
524 * Convenience function to connect to the #GCancellable::cancelled
525 * signal. Also handles the race condition that may happen
526 * if the cancellable is cancelled right before connecting.
528 * @callback is called at most once, either directly at the
529 * time of the connect if @cancellable is already cancelled,
530 * or when @cancellable is cancelled in some thread.
532 * @data_destroy_func will be called when the handler is
533 * disconnected, or immediately if the cancellable is already
534 * cancelled.
536 * See #GCancellable::cancelled for details on how to use this.
538 * Returns: The id of the signal handler or 0 if @cancellable has already
539 * been cancelled.
541 * Since: 2.22
543 gulong
544 g_cancellable_connect (GCancellable *cancellable,
545 GCallback callback,
546 gpointer data,
547 GDestroyNotify data_destroy_func)
549 gulong id;
551 g_return_val_if_fail (G_IS_CANCELLABLE (cancellable), 0);
553 g_mutex_lock (&cancellable_mutex);
555 if (cancellable->priv->cancelled)
557 void (*_callback) (GCancellable *cancellable,
558 gpointer user_data);
560 _callback = (void *)callback;
561 id = 0;
563 _callback (cancellable, data);
565 if (data_destroy_func)
566 data_destroy_func (data);
568 else
570 id = g_signal_connect_data (cancellable, "cancelled",
571 callback, data,
572 (GClosureNotify) data_destroy_func,
576 g_mutex_unlock (&cancellable_mutex);
578 return id;
582 * g_cancellable_disconnect:
583 * @cancellable: (allow-none): A #GCancellable or %NULL.
584 * @handler_id: Handler id of the handler to be disconnected, or %0.
586 * Disconnects a handler from a cancellable instance similar to
587 * g_signal_handler_disconnect(). Additionally, in the event that a
588 * signal handler is currently running, this call will block until the
589 * handler has finished. Calling this function from a
590 * #GCancellable::cancelled signal handler will therefore result in a
591 * deadlock.
593 * This avoids a race condition where a thread cancels at the
594 * same time as the cancellable operation is finished and the
595 * signal handler is removed. See #GCancellable::cancelled for
596 * details on how to use this.
598 * If @cancellable is %NULL or @handler_id is %0 this function does
599 * nothing.
601 * Since: 2.22
603 void
604 g_cancellable_disconnect (GCancellable *cancellable,
605 gulong handler_id)
607 GCancellablePrivate *priv;
609 if (handler_id == 0 || cancellable == NULL)
610 return;
612 g_mutex_lock (&cancellable_mutex);
614 priv = cancellable->priv;
616 while (priv->cancelled_running)
618 priv->cancelled_running_waiting = TRUE;
619 g_cond_wait (&cancellable_cond, &cancellable_mutex);
622 g_signal_handler_disconnect (cancellable, handler_id);
624 g_mutex_unlock (&cancellable_mutex);
627 typedef struct {
628 GSource source;
630 GCancellable *cancellable;
631 guint cancelled_handler;
632 } GCancellableSource;
634 static void
635 cancellable_source_cancelled (GCancellable *cancellable,
636 gpointer user_data)
638 GSource *source = user_data;
640 if (!g_source_is_destroyed (source))
641 g_source_set_ready_time (source, 0);
644 static gboolean
645 cancellable_source_dispatch (GSource *source,
646 GSourceFunc callback,
647 gpointer user_data)
649 GCancellableSourceFunc func = (GCancellableSourceFunc)callback;
650 GCancellableSource *cancellable_source = (GCancellableSource *)source;
652 g_source_set_ready_time (source, -1);
653 return (*func) (cancellable_source->cancellable, user_data);
656 static void
657 cancellable_source_finalize (GSource *source)
659 GCancellableSource *cancellable_source = (GCancellableSource *)source;
661 if (cancellable_source->cancellable)
663 g_cancellable_disconnect (cancellable_source->cancellable,
664 cancellable_source->cancelled_handler);
665 g_object_unref (cancellable_source->cancellable);
669 static gboolean
670 cancellable_source_closure_callback (GCancellable *cancellable,
671 gpointer data)
673 GClosure *closure = data;
675 GValue params = G_VALUE_INIT;
676 GValue result_value = G_VALUE_INIT;
677 gboolean result;
679 g_value_init (&result_value, G_TYPE_BOOLEAN);
681 g_value_init (&params, G_TYPE_CANCELLABLE);
682 g_value_set_object (&params, cancellable);
684 g_closure_invoke (closure, &result_value, 1, &params, NULL);
686 result = g_value_get_boolean (&result_value);
687 g_value_unset (&result_value);
688 g_value_unset (&params);
690 return result;
693 static GSourceFuncs cancellable_source_funcs =
695 NULL,
696 NULL,
697 cancellable_source_dispatch,
698 cancellable_source_finalize,
699 (GSourceFunc)cancellable_source_closure_callback,
703 * g_cancellable_source_new: (skip)
704 * @cancellable: (allow-none): a #GCancellable, or %NULL
706 * Creates a source that triggers if @cancellable is cancelled and
707 * calls its callback of type #GCancellableSourceFunc. This is
708 * primarily useful for attaching to another (non-cancellable) source
709 * with g_source_add_child_source() to add cancellability to it.
711 * For convenience, you can call this with a %NULL #GCancellable,
712 * in which case the source will never trigger.
714 * Return value: (transfer full): the new #GSource.
716 * Since: 2.28
718 GSource *
719 g_cancellable_source_new (GCancellable *cancellable)
721 GSource *source;
722 GCancellableSource *cancellable_source;
724 source = g_source_new (&cancellable_source_funcs, sizeof (GCancellableSource));
725 g_source_set_name (source, "GCancellable");
726 cancellable_source = (GCancellableSource *)source;
728 if (cancellable)
730 cancellable_source->cancellable = g_object_ref (cancellable);
731 cancellable_source->cancelled_handler =
732 g_cancellable_connect (cancellable,
733 G_CALLBACK (cancellable_source_cancelled),
734 source,
735 NULL);
738 return source;