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>
32 #define pipe(fds) _pipe(fds, 4096, _O_BINARY)
35 #include "gcancellable.h"
41 * SECTION:gcancellable
42 * @short_description: Thread-safe Operation Cancellation Stack
45 * GCancellable is a thread-safe operation cancellation stack used
46 * throughout GIO to allow for cancellation of synchronous and
47 * asynchronous operations.
57 GObject parent_instance;
60 guint allocated_pipe : 1;
61 guint cancelled_running : 1;
62 guint cancelled_running_waiting : 1;
66 GIOChannel *read_channel;
70 static guint signals[LAST_SIGNAL] = { 0 };
72 G_DEFINE_TYPE (GCancellable, g_cancellable, G_TYPE_OBJECT);
74 static GStaticPrivate current_cancellable = G_STATIC_PRIVATE_INIT;
75 G_LOCK_DEFINE_STATIC(cancellable);
76 static GCond *cancellable_cond = NULL;
79 g_cancellable_finalize (GObject *object)
81 GCancellable *cancellable = G_CANCELLABLE (object);
83 if (cancellable->cancel_pipe[0] != -1)
84 close (cancellable->cancel_pipe[0]);
86 if (cancellable->cancel_pipe[1] != -1)
87 close (cancellable->cancel_pipe[1]);
90 if (cancellable->read_channel)
91 g_io_channel_unref (cancellable->read_channel);
94 G_OBJECT_CLASS (g_cancellable_parent_class)->finalize (object);
98 g_cancellable_class_init (GCancellableClass *klass)
100 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
102 if (cancellable_cond == NULL && g_thread_supported ())
103 cancellable_cond = g_cond_new ();
105 gobject_class->finalize = g_cancellable_finalize;
108 * GCancellable::cancelled:
109 * @cancellable: a #GCancellable.
111 * Emitted when the operation has been cancelled.
113 * Can be used by implementations of cancellable operations. If the
114 * operation is cancelled from another thread, the signal will be
115 * emitted in the thread that cancelled the operation, not the
116 * thread that is running the operation.
118 * Note that disconnecting from this signal (or any signal) in a
119 * multi-threaded program is prone to race conditions. For instance
120 * it is possible that a signal handler may be invoked even
121 * <emphasis>after</emphasis> a call to
122 * g_signal_handler_disconnect() for that handler has already
125 * There is also a problem when cancellation happen
126 * right before connecting to the signal. If this happens the
127 * signal will unexpectedly not be emitted, and checking before
128 * connecting to the signal leaves a race condition where this is
131 * In order to make it safe and easy to connect handlers there
132 * are two helper functions: g_cancellable_connect() and
133 * g_cancellable_disconnect() which protect against problems
136 * An example of how to us this:
138 * /<!-- -->* Make sure we don't do any unnecessary work if already cancelled *<!-- -->/
139 * if (g_cancellable_set_error_if_cancelled (cancellable))
142 * /<!-- -->* Set up all the data needed to be able to
143 * * handle cancellation of the operation *<!-- -->/
144 * my_data = my_data_new (...);
148 * id = g_cancellable_connect (cancellable,
149 * G_CALLBACK (cancelled_handler)
152 * /<!-- -->* cancellable operation here... *<!-- -->/
154 * g_cancellable_disconnect (cancellable, id);
156 * /<!-- -->* cancelled_handler is never called after this, it
157 * * is now safe to free the data *<!-- -->/
158 * my_data_free (my_data);
161 * Note that the cancelled signal is emitted in the thread that
162 * the user cancelled from, which may be the main thread. So, the
163 * cancellable signal should not do something that can block.
166 g_signal_new (I_("cancelled"),
167 G_TYPE_FROM_CLASS (gobject_class),
169 G_STRUCT_OFFSET (GCancellableClass, cancelled),
171 g_cclosure_marshal_VOID__VOID,
177 set_fd_nonblocking (int fd)
181 fcntl_flags = fcntl (fd, F_GETFL);
184 fcntl_flags |= O_NONBLOCK;
186 fcntl_flags |= O_NDELAY;
189 fcntl (fd, F_SETFL, fcntl_flags);
194 g_cancellable_open_pipe (GCancellable *cancellable)
196 if (pipe (cancellable->cancel_pipe) == 0)
198 /* Make them nonblocking, just to be sure we don't block
199 * on errors and stuff
201 set_fd_nonblocking (cancellable->cancel_pipe[0]);
202 set_fd_nonblocking (cancellable->cancel_pipe[1]);
205 g_warning ("Failed to create pipe for GCancellable. Out of file descriptors?");
209 g_cancellable_init (GCancellable *cancellable)
211 cancellable->cancel_pipe[0] = -1;
212 cancellable->cancel_pipe[1] = -1;
218 * Creates a new #GCancellable object.
220 * Applications that want to start one or more operations
221 * that should be cancellable should create a #GCancellable
222 * and pass it to the operations.
224 * One #GCancellable can be used in multiple consecutive
225 * operations, but not in multiple concurrent operations.
227 * Returns: a #GCancellable.
230 g_cancellable_new (void)
232 return g_object_new (G_TYPE_CANCELLABLE, NULL);
236 * g_cancellable_push_current:
237 * @cancellable: optional #GCancellable object, %NULL to ignore.
239 * Pushes @cancellable onto the cancellable stack. The current
240 * cancllable can then be recieved using g_cancellable_get_current().
242 * This is useful when implementing cancellable operations in
243 * code that does not allow you to pass down the cancellable object.
245 * This is typically called automatically by e.g. #GFile operations,
246 * so you rarely have to call this yourself.
249 g_cancellable_push_current (GCancellable *cancellable)
253 g_return_if_fail (cancellable != NULL);
255 l = g_static_private_get (¤t_cancellable);
256 l = g_slist_prepend (l, cancellable);
257 g_static_private_set (¤t_cancellable, l, NULL);
261 * g_cancellable_pop_current:
262 * @cancellable: optional #GCancellable object, %NULL to ignore.
264 * Pops @cancellable off the cancellable stack (verifying that @cancellable
265 * is on the top of the stack).
268 g_cancellable_pop_current (GCancellable *cancellable)
272 l = g_static_private_get (¤t_cancellable);
274 g_return_if_fail (l != NULL);
275 g_return_if_fail (l->data == cancellable);
277 l = g_slist_delete_link (l, l);
278 g_static_private_set (¤t_cancellable, l, NULL);
282 * g_cancellable_get_current:
284 * Gets the top cancellable from the stack.
286 * Returns: a #GCancellable from the top of the stack, or %NULL
287 * if the stack is empty.
290 g_cancellable_get_current (void)
294 l = g_static_private_get (¤t_cancellable);
298 return G_CANCELLABLE (l->data);
302 * g_cancellable_reset:
303 * @cancellable: a #GCancellable object.
305 * Resets @cancellable to its uncancelled state.
308 g_cancellable_reset (GCancellable *cancellable)
310 g_return_if_fail (G_IS_CANCELLABLE (cancellable));
314 if (cancellable->cancelled_running)
316 g_critical ("Can't reset a cancellable during an active operation");
317 G_UNLOCK(cancellable);
321 if (!cancellable->cancelled)
325 /* Make sure we're not leaving old cancel state around */
328 if (cancellable->read_channel)
331 g_io_channel_read_chars (cancellable->read_channel, &ch, 1,
336 if (cancellable->cancel_pipe[0] != -1)
337 read (cancellable->cancel_pipe[0], &ch, 1);
338 cancellable->cancelled = FALSE;
340 G_UNLOCK(cancellable);
344 * g_cancellable_is_cancelled:
345 * @cancellable: a #GCancellable or NULL.
347 * Checks if a cancellable job has been cancelled.
349 * Returns: %TRUE if @cancellable is cancelled,
350 * FALSE if called with %NULL or if item is not cancelled.
353 g_cancellable_is_cancelled (GCancellable *cancellable)
355 return cancellable != NULL && cancellable->cancelled;
359 * g_cancellable_set_error_if_cancelled:
360 * @cancellable: a #GCancellable object.
361 * @error: #GError to append error state to.
363 * If the @cancellable is cancelled, sets the error to notify
364 * that the operation was cancelled.
366 * Returns: %TRUE if @cancellable was cancelled, %FALSE if it was not.
369 g_cancellable_set_error_if_cancelled (GCancellable *cancellable,
372 if (g_cancellable_is_cancelled (cancellable))
374 g_set_error_literal (error,
376 G_IO_ERROR_CANCELLED,
377 _("Operation was cancelled"));
385 * g_cancellable_get_fd:
386 * @cancellable: a #GCancellable.
388 * Gets the file descriptor for a cancellable job. This can be used to
389 * implement cancellable operations on Unix systems. The returned fd will
390 * turn readable when @cancellable is cancelled.
392 * You are not supposed to read from the fd yourself, just check for
393 * readable status. Reading to unset the readable status is done
394 * with g_cancellable_reset().
396 * See also g_cancellable_make_pollfd().
398 * Returns: A valid file descriptor. %-1 if the file descriptor
399 * is not supported, or on errors.
402 g_cancellable_get_fd (GCancellable *cancellable)
405 if (cancellable == NULL)
409 if (!cancellable->allocated_pipe)
411 cancellable->allocated_pipe = TRUE;
412 g_cancellable_open_pipe (cancellable);
415 fd = cancellable->cancel_pipe[0];
416 G_UNLOCK(cancellable);
422 * g_cancellable_make_pollfd:
423 * @cancellable: a #GCancellable.
424 * @pollfd: a pointer to a #GPollFD
426 * Creates a #GPollFD corresponding to @cancellable; this can be passed
427 * to g_poll() and used to poll for cancellation.
429 * You are not supposed to read from the fd yourself, just check for
430 * readable status. Reading to unset the readable status is done
431 * with g_cancellable_reset().
435 g_cancellable_make_pollfd (GCancellable *cancellable, GPollFD *pollfd)
437 g_return_if_fail (G_IS_CANCELLABLE (cancellable));
438 g_return_if_fail (pollfd != NULL);
441 if (!cancellable->read_channel)
443 int fd = g_cancellable_get_fd (cancellable);
444 cancellable->read_channel = g_io_channel_win32_new_fd (fd);
445 g_io_channel_set_buffered (cancellable->read_channel, FALSE);
446 g_io_channel_set_flags (cancellable->read_channel,
447 G_IO_FLAG_NONBLOCK, NULL);
448 g_io_channel_set_encoding (cancellable->read_channel, NULL, NULL);
450 g_io_channel_win32_make_pollfd (cancellable->read_channel, G_IO_IN, pollfd);
451 /* (We need to keep cancellable->read_channel around, because it's
452 * keeping track of state related to the pollfd.)
454 #else /* !G_OS_WIN32 */
455 pollfd->fd = g_cancellable_get_fd (cancellable);
456 pollfd->events = G_IO_IN;
457 #endif /* G_OS_WIN32 */
462 * g_cancellable_cancel:
463 * @cancellable: a #GCancellable object.
465 * Will set @cancellable to cancelled, and will emit the
466 * #GCancellable::cancelled signal. (However, see the warning about
467 * race conditions in the documentation for that signal if you are
468 * planning to connect to it.)
470 * This function is thread-safe. In other words, you can safely call
471 * it from a thread other than the one running the operation that was
472 * passed the @cancellable.
474 * The convention within gio is that cancelling an asynchronous
475 * operation causes it to complete asynchronously. That is, if you
476 * cancel the operation from the same thread in which it is running,
477 * then the operation's #GAsyncReadyCallback will not be invoked until
478 * the application returns to the main loop.
481 g_cancellable_cancel (GCancellable *cancellable)
488 if (cancellable != NULL &&
489 !cancellable->cancelled)
493 cancellable->cancelled = TRUE;
494 cancellable->cancelled_running = TRUE;
495 if (cancellable->cancel_pipe[1] != -1)
496 write (cancellable->cancel_pipe[1], &ch, 1);
498 G_UNLOCK(cancellable);
502 g_object_ref (cancellable);
503 g_signal_emit (cancellable, signals[CANCELLED], 0);
507 cancellable->cancelled_running = FALSE;
508 if (cancellable->cancelled_running_waiting)
509 g_cond_broadcast (cancellable_cond);
510 cancellable->cancelled_running_waiting = FALSE;
512 G_UNLOCK(cancellable);
514 g_object_unref (cancellable);
519 * g_cancellable_connect:
520 * @cancellable: A #GCancellable.
521 * @callback: The #GCallback to connect.
522 * @data: Data to pass to @callback.
523 * @data_destroy_func: Free function for @data or %NULL.
525 * Convenience function to connect to the #GCancellable::cancelled
526 * signal. Also handles the race condition that may happen
527 * if the cancellable is cancelled right before connecting.
529 * @callback is called at most once, either directly at the
530 * time of the connect if @cancellable is already cancelled,
531 * or when @cancellable is cancelled in some thread.
533 * @data_destroy_func will be called when the handler is
534 * disconnected, or immediately if the cancellable is already
537 * See #GCancellable::cancelled for details on how to use this.
539 * Returns: The id of the signal handler or 0 if @cancellable has already
545 g_cancellable_connect (GCancellable *cancellable,
548 GDestroyNotify data_destroy_func)
552 g_return_val_if_fail (G_IS_CANCELLABLE (cancellable), 0);
554 G_LOCK (cancellable);
556 if (cancellable->cancelled)
558 void (*_callback) (GCancellable *cancellable,
561 _callback = (void *)callback;
564 _callback (cancellable, data);
566 if (data_destroy_func)
567 data_destroy_func (data);
571 id = g_signal_connect_data (cancellable, "cancelled",
573 (GClosureNotify) data_destroy_func,
576 G_UNLOCK (cancellable);
582 * g_cancellable_disconnect:
583 * @cancellable: A #GCancellable or %NULL.
584 * @handler_id: Handler id of the handler to be disconnected, or %0.
586 * Disconnects a handler from an cancellable instance similar to
587 * g_signal_handler_disconnect() but ensures that once this
588 * function returns the handler will not run anymore in any thread.
590 * This avoids a race condition where a thread cancels at the
591 * same time as the cancellable operation is finished and the
592 * signal handler is removed. See #GCancellable::cancelled for
593 * details on how to use this.
595 * If @cancellable is %NULL or @handler_id is %0 this function does
601 g_cancellable_disconnect (GCancellable *cancellable,
604 if (handler_id == 0 || cancellable == NULL)
607 G_LOCK (cancellable);
608 while (cancellable->cancelled_running)
610 cancellable->cancelled_running_waiting = TRUE;
611 g_cond_wait (cancellable_cond,
612 g_static_mutex_get_mutex (& G_LOCK_NAME (cancellable)));
615 g_signal_handler_disconnect (cancellable, handler_id);
616 G_UNLOCK (cancellable);
619 #define __G_CANCELLABLE_C__
620 #include "gioaliasdef.c"