1 /* GIO - GLib Input, Output and Streaming Library
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>
33 #include "gcancellable.h"
39 * SECTION:gcancellable
40 * @short_description: Thread-safe Operation Cancellation Stack
43 * GCancellable is a thread-safe operation cancellation stack used
44 * throughout GIO to allow for cancellation of synchronous and
45 * asynchronous operations.
53 struct _GCancellablePrivate
55 GObject parent_instance;
58 guint allocated_pipe : 1;
59 guint cancelled_running : 1;
60 guint cancelled_running_waiting : 1;
68 static guint signals[LAST_SIGNAL] = { 0 };
70 G_DEFINE_TYPE (GCancellable, g_cancellable, G_TYPE_OBJECT);
72 static GStaticPrivate current_cancellable = G_STATIC_PRIVATE_INIT;
73 G_LOCK_DEFINE_STATIC(cancellable);
74 static GCond *cancellable_cond = NULL;
77 g_cancellable_finalize (GObject *object)
79 GCancellable *cancellable = G_CANCELLABLE (object);
80 GCancellablePrivate *priv;
82 priv = cancellable->priv;
84 if (priv->cancel_pipe[0] != -1)
85 close (priv->cancel_pipe[0]);
87 if (priv->cancel_pipe[1] != -1)
88 close (priv->cancel_pipe[1]);
92 CloseHandle (priv->event);
95 G_OBJECT_CLASS (g_cancellable_parent_class)->finalize (object);
99 g_cancellable_class_init (GCancellableClass *klass)
101 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
103 g_type_class_add_private (klass, sizeof (GCancellablePrivate));
105 if (cancellable_cond == NULL && g_thread_supported ())
106 cancellable_cond = g_cond_new ();
108 gobject_class->finalize = g_cancellable_finalize;
111 * GCancellable::cancelled:
112 * @cancellable: a #GCancellable.
114 * Emitted when the operation has been cancelled.
116 * Can be used by implementations of cancellable operations. If the
117 * operation is cancelled from another thread, the signal will be
118 * emitted in the thread that cancelled the operation, not the
119 * thread that is running the operation.
121 * Note that disconnecting from this signal (or any signal) in a
122 * multi-threaded program is prone to race conditions. For instance
123 * it is possible that a signal handler may be invoked even
124 * <emphasis>after</emphasis> a call to
125 * g_signal_handler_disconnect() for that handler has already
128 * There is also a problem when cancellation happen
129 * right before connecting to the signal. If this happens the
130 * signal will unexpectedly not be emitted, and checking before
131 * connecting to the signal leaves a race condition where this is
134 * In order to make it safe and easy to connect handlers there
135 * are two helper functions: g_cancellable_connect() and
136 * g_cancellable_disconnect() which protect against problems
139 * An example of how to us this:
141 * /<!-- -->* Make sure we don't do any unnecessary work if already cancelled *<!-- -->/
142 * if (g_cancellable_set_error_if_cancelled (cancellable))
145 * /<!-- -->* Set up all the data needed to be able to
146 * * handle cancellation of the operation *<!-- -->/
147 * my_data = my_data_new (...);
151 * id = g_cancellable_connect (cancellable,
152 * G_CALLBACK (cancelled_handler)
155 * /<!-- -->* cancellable operation here... *<!-- -->/
157 * g_cancellable_disconnect (cancellable, id);
159 * /<!-- -->* cancelled_handler is never called after this, it
160 * * is now safe to free the data *<!-- -->/
161 * my_data_free (my_data);
164 * Note that the cancelled signal is emitted in the thread that
165 * the user cancelled from, which may be the main thread. So, the
166 * cancellable signal should not do something that can block.
169 g_signal_new (I_("cancelled"),
170 G_TYPE_FROM_CLASS (gobject_class),
172 G_STRUCT_OFFSET (GCancellableClass, cancelled),
174 g_cclosure_marshal_VOID__VOID,
181 set_fd_nonblocking (int fd)
185 fcntl_flags = fcntl (fd, F_GETFL);
188 fcntl_flags |= O_NONBLOCK;
190 fcntl_flags |= O_NDELAY;
193 fcntl (fd, F_SETFL, fcntl_flags);
198 set_fd_close_exec (int fd)
202 flags = fcntl (fd, F_GETFD, 0);
203 if (flags != -1 && (flags & FD_CLOEXEC) == 0)
206 fcntl (fd, F_SETFD, flags);
212 g_cancellable_open_pipe (GCancellable *cancellable)
214 GCancellablePrivate *priv;
216 priv = cancellable->priv;
217 if (pipe (priv->cancel_pipe) == 0)
219 /* Make them nonblocking, just to be sure we don't block
220 * on errors and stuff
222 set_fd_nonblocking (priv->cancel_pipe[0]);
223 set_fd_nonblocking (priv->cancel_pipe[1]);
224 set_fd_close_exec (priv->cancel_pipe[0]);
225 set_fd_close_exec (priv->cancel_pipe[1]);
228 g_warning ("Failed to create pipe for GCancellable. Out of file descriptors?");
233 g_cancellable_init (GCancellable *cancellable)
235 cancellable->priv = G_TYPE_INSTANCE_GET_PRIVATE (cancellable,
237 GCancellablePrivate);
238 cancellable->priv->cancel_pipe[0] = -1;
239 cancellable->priv->cancel_pipe[1] = -1;
245 * Creates a new #GCancellable object.
247 * Applications that want to start one or more operations
248 * that should be cancellable should create a #GCancellable
249 * and pass it to the operations.
251 * One #GCancellable can be used in multiple consecutive
252 * operations, but not in multiple concurrent operations.
254 * Returns: a #GCancellable.
257 g_cancellable_new (void)
259 return g_object_new (G_TYPE_CANCELLABLE, NULL);
263 * g_cancellable_push_current:
264 * @cancellable: a #GCancellable object
266 * Pushes @cancellable onto the cancellable stack. The current
267 * cancllable can then be recieved using g_cancellable_get_current().
269 * This is useful when implementing cancellable operations in
270 * code that does not allow you to pass down the cancellable object.
272 * This is typically called automatically by e.g. #GFile operations,
273 * so you rarely have to call this yourself.
276 g_cancellable_push_current (GCancellable *cancellable)
280 g_return_if_fail (cancellable != NULL);
282 l = g_static_private_get (¤t_cancellable);
283 l = g_slist_prepend (l, cancellable);
284 g_static_private_set (¤t_cancellable, l, NULL);
288 * g_cancellable_pop_current:
289 * @cancellable: a #GCancellable object
291 * Pops @cancellable off the cancellable stack (verifying that @cancellable
292 * is on the top of the stack).
295 g_cancellable_pop_current (GCancellable *cancellable)
299 l = g_static_private_get (¤t_cancellable);
301 g_return_if_fail (l != NULL);
302 g_return_if_fail (l->data == cancellable);
304 l = g_slist_delete_link (l, l);
305 g_static_private_set (¤t_cancellable, l, NULL);
309 * g_cancellable_get_current:
311 * Gets the top cancellable from the stack.
313 * Returns: a #GCancellable from the top of the stack, or %NULL
314 * if the stack is empty.
317 g_cancellable_get_current (void)
321 l = g_static_private_get (¤t_cancellable);
325 return G_CANCELLABLE (l->data);
329 * g_cancellable_reset:
330 * @cancellable: a #GCancellable object.
332 * Resets @cancellable to its uncancelled state.
335 g_cancellable_reset (GCancellable *cancellable)
337 GCancellablePrivate *priv;
339 g_return_if_fail (G_IS_CANCELLABLE (cancellable));
343 priv = cancellable->priv;
345 while (priv->cancelled_running)
347 priv->cancelled_running_waiting = TRUE;
348 g_cond_wait (cancellable_cond,
349 g_static_mutex_get_mutex (& G_LOCK_NAME (cancellable)));
356 /* Make sure we're not leaving old cancel state around */
360 ResetEvent (priv->event);
363 if (priv->cancel_pipe[0] != -1)
364 read (priv->cancel_pipe[0], &ch, 1);
365 priv->cancelled = FALSE;
367 G_UNLOCK(cancellable);
371 * g_cancellable_is_cancelled:
372 * @cancellable: a #GCancellable or NULL.
374 * Checks if a cancellable job has been cancelled.
376 * Returns: %TRUE if @cancellable is cancelled,
377 * FALSE if called with %NULL or if item is not cancelled.
380 g_cancellable_is_cancelled (GCancellable *cancellable)
382 return cancellable != NULL && cancellable->priv->cancelled;
386 * g_cancellable_set_error_if_cancelled:
387 * @cancellable: a #GCancellable object.
388 * @error: #GError to append error state to.
390 * If the @cancellable is cancelled, sets the error to notify
391 * that the operation was cancelled.
393 * Returns: %TRUE if @cancellable was cancelled, %FALSE if it was not.
396 g_cancellable_set_error_if_cancelled (GCancellable *cancellable,
399 if (g_cancellable_is_cancelled (cancellable))
401 g_set_error_literal (error,
403 G_IO_ERROR_CANCELLED,
404 _("Operation was cancelled"));
412 * g_cancellable_get_fd:
413 * @cancellable: a #GCancellable.
415 * Gets the file descriptor for a cancellable job. This can be used to
416 * implement cancellable operations on Unix systems. The returned fd will
417 * turn readable when @cancellable is cancelled.
419 * You are not supposed to read from the fd yourself, just check for
420 * readable status. Reading to unset the readable status is done
421 * with g_cancellable_reset().
423 * See also g_cancellable_make_pollfd().
425 * Returns: A valid file descriptor. %-1 if the file descriptor
426 * is not supported, or on errors.
429 g_cancellable_get_fd (GCancellable *cancellable)
431 GCancellablePrivate *priv;
434 if (cancellable == NULL)
437 priv = cancellable->priv;
443 if (!priv->allocated_pipe)
445 priv->allocated_pipe = TRUE;
446 g_cancellable_open_pipe (cancellable);
449 fd = priv->cancel_pipe[0];
450 G_UNLOCK(cancellable);
457 * g_cancellable_make_pollfd:
458 * @cancellable: a #GCancellable.
459 * @pollfd: a pointer to a #GPollFD
461 * Creates a #GPollFD corresponding to @cancellable; this can be passed
462 * to g_poll() and used to poll for cancellation. This is useful both
463 * for unix systems without a native poll and for portability to
466 * You are not supposed to read from the fd yourself, just check for
467 * readable status. Reading to unset the readable status is done
468 * with g_cancellable_reset().
472 g_cancellable_make_pollfd (GCancellable *cancellable, GPollFD *pollfd)
474 GCancellablePrivate *priv;
476 g_return_if_fail (G_IS_CANCELLABLE (cancellable));
477 g_return_if_fail (pollfd != NULL);
479 priv = cancellable->priv;
484 /* A manual reset anonymous event, starting unset */
485 priv->event = CreateEvent (NULL, TRUE, FALSE, NULL);
487 pollfd->fd = (gintptr)priv->event;
488 #else /* !G_OS_WIN32 */
489 pollfd->fd = g_cancellable_get_fd (cancellable);
490 #endif /* G_OS_WIN32 */
491 pollfd->events = G_IO_IN;
496 * g_cancellable_cancel:
497 * @cancellable: a #GCancellable object.
499 * Will set @cancellable to cancelled, and will emit the
500 * #GCancellable::cancelled signal. (However, see the warning about
501 * race conditions in the documentation for that signal if you are
502 * planning to connect to it.)
504 * This function is thread-safe. In other words, you can safely call
505 * it from a thread other than the one running the operation that was
506 * passed the @cancellable.
508 * The convention within gio is that cancelling an asynchronous
509 * operation causes it to complete asynchronously. That is, if you
510 * cancel the operation from the same thread in which it is running,
511 * then the operation's #GAsyncReadyCallback will not be invoked until
512 * the application returns to the main loop.
515 g_cancellable_cancel (GCancellable *cancellable)
518 GCancellablePrivate *priv;
520 priv = cancellable->priv;
524 if (cancellable != NULL &&
529 priv->cancelled = TRUE;
530 priv->cancelled_running = TRUE;
533 SetEvent(priv->event);
535 if (priv->cancel_pipe[1] != -1)
536 write (priv->cancel_pipe[1], &ch, 1);
538 G_UNLOCK(cancellable);
542 g_object_ref (cancellable);
543 g_signal_emit (cancellable, signals[CANCELLED], 0);
547 priv->cancelled_running = FALSE;
548 if (priv->cancelled_running_waiting)
549 g_cond_broadcast (cancellable_cond);
550 priv->cancelled_running_waiting = FALSE;
552 G_UNLOCK(cancellable);
554 g_object_unref (cancellable);
559 * g_cancellable_connect:
560 * @cancellable: A #GCancellable.
561 * @callback: The #GCallback to connect.
562 * @data: Data to pass to @callback.
563 * @data_destroy_func: Free function for @data or %NULL.
565 * Convenience function to connect to the #GCancellable::cancelled
566 * signal. Also handles the race condition that may happen
567 * if the cancellable is cancelled right before connecting.
569 * @callback is called at most once, either directly at the
570 * time of the connect if @cancellable is already cancelled,
571 * or when @cancellable is cancelled in some thread.
573 * @data_destroy_func will be called when the handler is
574 * disconnected, or immediately if the cancellable is already
577 * See #GCancellable::cancelled for details on how to use this.
579 * Returns: The id of the signal handler or 0 if @cancellable has already
585 g_cancellable_connect (GCancellable *cancellable,
588 GDestroyNotify data_destroy_func)
592 g_return_val_if_fail (G_IS_CANCELLABLE (cancellable), 0);
594 G_LOCK (cancellable);
596 if (cancellable->priv->cancelled)
598 void (*_callback) (GCancellable *cancellable,
601 _callback = (void *)callback;
604 _callback (cancellable, data);
606 if (data_destroy_func)
607 data_destroy_func (data);
611 id = g_signal_connect_data (cancellable, "cancelled",
613 (GClosureNotify) data_destroy_func,
616 G_UNLOCK (cancellable);
622 * g_cancellable_disconnect:
623 * @cancellable: A #GCancellable or %NULL.
624 * @handler_id: Handler id of the handler to be disconnected, or %0.
626 * Disconnects a handler from an cancellable instance similar to
627 * g_signal_handler_disconnect() but ensures that once this
628 * function returns the handler will not run anymore in any thread.
630 * This avoids a race condition where a thread cancels at the
631 * same time as the cancellable operation is finished and the
632 * signal handler is removed. See #GCancellable::cancelled for
633 * details on how to use this.
635 * If @cancellable is %NULL or @handler_id is %0 this function does
641 g_cancellable_disconnect (GCancellable *cancellable,
644 GCancellablePrivate *priv;
646 if (handler_id == 0 || cancellable == NULL)
649 G_LOCK (cancellable);
651 priv = cancellable->priv;
653 while (priv->cancelled_running)
655 priv->cancelled_running_waiting = TRUE;
656 g_cond_wait (cancellable_cond,
657 g_static_mutex_get_mutex (& G_LOCK_NAME (cancellable)));
660 g_signal_handler_disconnect (cancellable, handler_id);
661 G_UNLOCK (cancellable);
664 #define __G_CANCELLABLE_C__
665 #include "gioaliasdef.c"