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.
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);
81 if (cancellable->cancel_pipe[0] != -1)
82 close (cancellable->cancel_pipe[0]);
84 if (cancellable->cancel_pipe[1] != -1)
85 close (cancellable->cancel_pipe[1]);
88 if (cancellable->event)
89 CloseHandle (cancellable->event);
92 G_OBJECT_CLASS (g_cancellable_parent_class)->finalize (object);
96 g_cancellable_class_init (GCancellableClass *klass)
98 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
100 if (cancellable_cond == NULL && g_thread_supported ())
101 cancellable_cond = g_cond_new ();
103 gobject_class->finalize = g_cancellable_finalize;
106 * GCancellable::cancelled:
107 * @cancellable: a #GCancellable.
109 * Emitted when the operation has been cancelled.
111 * Can be used by implementations of cancellable operations. If the
112 * operation is cancelled from another thread, the signal will be
113 * emitted in the thread that cancelled the operation, not the
114 * thread that is running the operation.
116 * Note that disconnecting from this signal (or any signal) in a
117 * multi-threaded program is prone to race conditions. For instance
118 * it is possible that a signal handler may be invoked even
119 * <emphasis>after</emphasis> a call to
120 * g_signal_handler_disconnect() for that handler has already
123 * There is also a problem when cancellation happen
124 * right before connecting to the signal. If this happens the
125 * signal will unexpectedly not be emitted, and checking before
126 * connecting to the signal leaves a race condition where this is
129 * In order to make it safe and easy to connect handlers there
130 * are two helper functions: g_cancellable_connect() and
131 * g_cancellable_disconnect() which protect against problems
134 * An example of how to us this:
136 * /<!-- -->* Make sure we don't do any unnecessary work if already cancelled *<!-- -->/
137 * if (g_cancellable_set_error_if_cancelled (cancellable))
140 * /<!-- -->* Set up all the data needed to be able to
141 * * handle cancellation of the operation *<!-- -->/
142 * my_data = my_data_new (...);
146 * id = g_cancellable_connect (cancellable,
147 * G_CALLBACK (cancelled_handler)
150 * /<!-- -->* cancellable operation here... *<!-- -->/
152 * g_cancellable_disconnect (cancellable, id);
154 * /<!-- -->* cancelled_handler is never called after this, it
155 * * is now safe to free the data *<!-- -->/
156 * my_data_free (my_data);
159 * Note that the cancelled signal is emitted in the thread that
160 * the user cancelled from, which may be the main thread. So, the
161 * cancellable signal should not do something that can block.
164 g_signal_new (I_("cancelled"),
165 G_TYPE_FROM_CLASS (gobject_class),
167 G_STRUCT_OFFSET (GCancellableClass, cancelled),
169 g_cclosure_marshal_VOID__VOID,
176 set_fd_nonblocking (int fd)
180 fcntl_flags = fcntl (fd, F_GETFL);
183 fcntl_flags |= O_NONBLOCK;
185 fcntl_flags |= O_NDELAY;
188 fcntl (fd, F_SETFL, fcntl_flags);
193 set_fd_close_exec (int fd)
197 flags = fcntl (fd, F_GETFD, 0);
198 if (flags != -1 && (flags & FD_CLOEXEC) == 0)
201 fcntl (fd, F_SETFD, flags);
207 g_cancellable_open_pipe (GCancellable *cancellable)
209 if (pipe (cancellable->cancel_pipe) == 0)
211 /* Make them nonblocking, just to be sure we don't block
212 * on errors and stuff
214 set_fd_nonblocking (cancellable->cancel_pipe[0]);
215 set_fd_nonblocking (cancellable->cancel_pipe[1]);
216 set_fd_close_exec (cancellable->cancel_pipe[0]);
217 set_fd_close_exec (cancellable->cancel_pipe[1]);
220 g_warning ("Failed to create pipe for GCancellable. Out of file descriptors?");
225 g_cancellable_init (GCancellable *cancellable)
227 cancellable->cancel_pipe[0] = -1;
228 cancellable->cancel_pipe[1] = -1;
234 * Creates a new #GCancellable object.
236 * Applications that want to start one or more operations
237 * that should be cancellable should create a #GCancellable
238 * and pass it to the operations.
240 * One #GCancellable can be used in multiple consecutive
241 * operations, but not in multiple concurrent operations.
243 * Returns: a #GCancellable.
246 g_cancellable_new (void)
248 return g_object_new (G_TYPE_CANCELLABLE, NULL);
252 * g_cancellable_push_current:
253 * @cancellable: a #GCancellable object
255 * Pushes @cancellable onto the cancellable stack. The current
256 * cancllable can then be recieved using g_cancellable_get_current().
258 * This is useful when implementing cancellable operations in
259 * code that does not allow you to pass down the cancellable object.
261 * This is typically called automatically by e.g. #GFile operations,
262 * so you rarely have to call this yourself.
265 g_cancellable_push_current (GCancellable *cancellable)
269 g_return_if_fail (cancellable != NULL);
271 l = g_static_private_get (¤t_cancellable);
272 l = g_slist_prepend (l, cancellable);
273 g_static_private_set (¤t_cancellable, l, NULL);
277 * g_cancellable_pop_current:
278 * @cancellable: a #GCancellable object
280 * Pops @cancellable off the cancellable stack (verifying that @cancellable
281 * is on the top of the stack).
284 g_cancellable_pop_current (GCancellable *cancellable)
288 l = g_static_private_get (¤t_cancellable);
290 g_return_if_fail (l != NULL);
291 g_return_if_fail (l->data == cancellable);
293 l = g_slist_delete_link (l, l);
294 g_static_private_set (¤t_cancellable, l, NULL);
298 * g_cancellable_get_current:
300 * Gets the top cancellable from the stack.
302 * Returns: a #GCancellable from the top of the stack, or %NULL
303 * if the stack is empty.
306 g_cancellable_get_current (void)
310 l = g_static_private_get (¤t_cancellable);
314 return G_CANCELLABLE (l->data);
318 * g_cancellable_reset:
319 * @cancellable: a #GCancellable object.
321 * Resets @cancellable to its uncancelled state.
324 g_cancellable_reset (GCancellable *cancellable)
326 g_return_if_fail (G_IS_CANCELLABLE (cancellable));
330 while (cancellable->cancelled_running)
332 cancellable->cancelled_running_waiting = TRUE;
333 g_cond_wait (cancellable_cond,
334 g_static_mutex_get_mutex (& G_LOCK_NAME (cancellable)));
337 if (cancellable->cancelled)
341 /* Make sure we're not leaving old cancel state around */
344 if (cancellable->event)
345 ResetEvent (cancellable->event);
348 if (cancellable->cancel_pipe[0] != -1)
349 read (cancellable->cancel_pipe[0], &ch, 1);
350 cancellable->cancelled = FALSE;
352 G_UNLOCK(cancellable);
356 * g_cancellable_is_cancelled:
357 * @cancellable: a #GCancellable or NULL.
359 * Checks if a cancellable job has been cancelled.
361 * Returns: %TRUE if @cancellable is cancelled,
362 * FALSE if called with %NULL or if item is not cancelled.
365 g_cancellable_is_cancelled (GCancellable *cancellable)
367 return cancellable != NULL && cancellable->cancelled;
371 * g_cancellable_set_error_if_cancelled:
372 * @cancellable: a #GCancellable object.
373 * @error: #GError to append error state to.
375 * If the @cancellable is cancelled, sets the error to notify
376 * that the operation was cancelled.
378 * Returns: %TRUE if @cancellable was cancelled, %FALSE if it was not.
381 g_cancellable_set_error_if_cancelled (GCancellable *cancellable,
384 if (g_cancellable_is_cancelled (cancellable))
386 g_set_error_literal (error,
388 G_IO_ERROR_CANCELLED,
389 _("Operation was cancelled"));
397 * g_cancellable_get_fd:
398 * @cancellable: a #GCancellable.
400 * Gets the file descriptor for a cancellable job. This can be used to
401 * implement cancellable operations on Unix systems. The returned fd will
402 * turn readable when @cancellable is cancelled.
404 * You are not supposed to read from the fd yourself, just check for
405 * readable status. Reading to unset the readable status is done
406 * with g_cancellable_reset().
408 * See also g_cancellable_make_pollfd().
410 * Returns: A valid file descriptor. %-1 if the file descriptor
411 * is not supported, or on errors.
414 g_cancellable_get_fd (GCancellable *cancellable)
417 if (cancellable == NULL)
424 if (!cancellable->allocated_pipe)
426 cancellable->allocated_pipe = TRUE;
427 g_cancellable_open_pipe (cancellable);
430 fd = cancellable->cancel_pipe[0];
431 G_UNLOCK(cancellable);
438 * g_cancellable_make_pollfd:
439 * @cancellable: a #GCancellable.
440 * @pollfd: a pointer to a #GPollFD
442 * Creates a #GPollFD corresponding to @cancellable; this can be passed
443 * to g_poll() and used to poll for cancellation. This is useful both
444 * for unix systems without a native poll and for portability to
447 * You are not supposed to read from the fd yourself, just check for
448 * readable status. Reading to unset the readable status is done
449 * with g_cancellable_reset().
453 g_cancellable_make_pollfd (GCancellable *cancellable, GPollFD *pollfd)
455 g_return_if_fail (G_IS_CANCELLABLE (cancellable));
456 g_return_if_fail (pollfd != NULL);
459 if (!cancellable->event)
461 /* A manual reset anonymous event, starting unset */
462 cancellable->event = CreateEvent (NULL, TRUE, FALSE, NULL);
464 pollfd->fd = (gintptr)cancellable->event;
465 #else /* !G_OS_WIN32 */
466 pollfd->fd = g_cancellable_get_fd (cancellable);
467 #endif /* G_OS_WIN32 */
468 pollfd->events = G_IO_IN;
473 * g_cancellable_cancel:
474 * @cancellable: a #GCancellable object.
476 * Will set @cancellable to cancelled, and will emit the
477 * #GCancellable::cancelled signal. (However, see the warning about
478 * race conditions in the documentation for that signal if you are
479 * planning to connect to it.)
481 * This function is thread-safe. In other words, you can safely call
482 * it from a thread other than the one running the operation that was
483 * passed the @cancellable.
485 * The convention within gio is that cancelling an asynchronous
486 * operation causes it to complete asynchronously. That is, if you
487 * cancel the operation from the same thread in which it is running,
488 * then the operation's #GAsyncReadyCallback will not be invoked until
489 * the application returns to the main loop.
492 g_cancellable_cancel (GCancellable *cancellable)
499 if (cancellable != NULL &&
500 !cancellable->cancelled)
504 cancellable->cancelled = TRUE;
505 cancellable->cancelled_running = TRUE;
507 if (cancellable->event)
508 SetEvent(cancellable->event);
510 if (cancellable->cancel_pipe[1] != -1)
511 write (cancellable->cancel_pipe[1], &ch, 1);
513 G_UNLOCK(cancellable);
517 g_object_ref (cancellable);
518 g_signal_emit (cancellable, signals[CANCELLED], 0);
522 cancellable->cancelled_running = FALSE;
523 if (cancellable->cancelled_running_waiting)
524 g_cond_broadcast (cancellable_cond);
525 cancellable->cancelled_running_waiting = FALSE;
527 G_UNLOCK(cancellable);
529 g_object_unref (cancellable);
534 * g_cancellable_connect:
535 * @cancellable: A #GCancellable.
536 * @callback: The #GCallback to connect.
537 * @data: Data to pass to @callback.
538 * @data_destroy_func: Free function for @data or %NULL.
540 * Convenience function to connect to the #GCancellable::cancelled
541 * signal. Also handles the race condition that may happen
542 * if the cancellable is cancelled right before connecting.
544 * @callback is called at most once, either directly at the
545 * time of the connect if @cancellable is already cancelled,
546 * or when @cancellable is cancelled in some thread.
548 * @data_destroy_func will be called when the handler is
549 * disconnected, or immediately if the cancellable is already
552 * See #GCancellable::cancelled for details on how to use this.
554 * Returns: The id of the signal handler or 0 if @cancellable has already
560 g_cancellable_connect (GCancellable *cancellable,
563 GDestroyNotify data_destroy_func)
567 g_return_val_if_fail (G_IS_CANCELLABLE (cancellable), 0);
569 G_LOCK (cancellable);
571 if (cancellable->cancelled)
573 void (*_callback) (GCancellable *cancellable,
576 _callback = (void *)callback;
579 _callback (cancellable, data);
581 if (data_destroy_func)
582 data_destroy_func (data);
586 id = g_signal_connect_data (cancellable, "cancelled",
588 (GClosureNotify) data_destroy_func,
591 G_UNLOCK (cancellable);
597 * g_cancellable_disconnect:
598 * @cancellable: A #GCancellable or %NULL.
599 * @handler_id: Handler id of the handler to be disconnected, or %0.
601 * Disconnects a handler from an cancellable instance similar to
602 * g_signal_handler_disconnect() but ensures that once this
603 * function returns the handler will not run anymore in any thread.
605 * This avoids a race condition where a thread cancels at the
606 * same time as the cancellable operation is finished and the
607 * signal handler is removed. See #GCancellable::cancelled for
608 * details on how to use this.
610 * If @cancellable is %NULL or @handler_id is %0 this function does
616 g_cancellable_disconnect (GCancellable *cancellable,
619 if (handler_id == 0 || cancellable == NULL)
622 G_LOCK (cancellable);
623 while (cancellable->cancelled_running)
625 cancellable->cancelled_running_waiting = TRUE;
626 g_cond_wait (cancellable_cond,
627 g_static_mutex_get_mutex (& G_LOCK_NAME (cancellable)));
630 g_signal_handler_disconnect (cancellable, handler_id);
631 G_UNLOCK (cancellable);
634 #define __G_CANCELLABLE_C__
635 #include "gioaliasdef.c"