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>
27 #include "gcancellable.h"
32 * SECTION:gcancellable
33 * @short_description: Thread-safe Operation Cancellation Stack
34 * @include: gio/gcancellable.h
36 * GCancellable is a thread-safe operation cancellation stack used
37 * throughout GIO to allow for cancellation of asynchronous operations.
47 GObject parent_instance;
50 guint allocated_pipe : 1;
54 static guint signals[LAST_SIGNAL] = { 0 };
56 G_DEFINE_TYPE (GCancellable, g_cancellable, G_TYPE_OBJECT);
58 static GStaticPrivate current_cancellable = G_STATIC_PRIVATE_INIT;
59 G_LOCK_DEFINE_STATIC(cancellable);
62 g_cancellable_finalize (GObject *object)
64 GCancellable *cancellable = G_CANCELLABLE (object);
66 if (cancellable->cancel_pipe[0] != -1)
67 close (cancellable->cancel_pipe[0]);
69 if (cancellable->cancel_pipe[1] != -1)
70 close (cancellable->cancel_pipe[1]);
72 if (G_OBJECT_CLASS (g_cancellable_parent_class)->finalize)
73 (*G_OBJECT_CLASS (g_cancellable_parent_class)->finalize) (object);
77 g_cancellable_class_init (GCancellableClass *klass)
79 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
81 gobject_class->finalize = g_cancellable_finalize;
84 * GCancellable::cancelled:
85 * @cancellable: a #GCancellable.
87 * Emitted when the operation has been cancelled from another thread.
90 g_signal_new (I_("cancelled"),
91 G_TYPE_FROM_CLASS (gobject_class),
93 G_STRUCT_OFFSET (GCancellableClass, cancelled),
95 g_cclosure_marshal_VOID__VOID,
101 set_fd_nonblocking (int fd)
105 fcntl_flags = fcntl (fd, F_GETFL);
108 fcntl_flags |= O_NONBLOCK;
110 fcntl_flags |= O_NDELAY;
113 fcntl (fd, F_SETFL, fcntl_flags);
118 g_cancellable_open_pipe (GCancellable *cancellable)
120 if (pipe (cancellable->cancel_pipe) == 0)
122 /* Make them nonblocking, just to be sure we don't block
123 * on errors and stuff
125 set_fd_nonblocking (cancellable->cancel_pipe[0]);
126 set_fd_nonblocking (cancellable->cancel_pipe[1]);
129 g_warning ("Failed to create pipe for GCancellable. Out of file descriptors?");
133 g_cancellable_init (GCancellable *cancellable)
135 cancellable->cancel_pipe[0] = -1;
136 cancellable->cancel_pipe[1] = -1;
142 * Creates a new #GCancellable object.
144 * Returns: a #GCancellable.
147 g_cancellable_new (void)
149 return g_object_new (G_TYPE_CANCELLABLE, NULL);
153 * g_push_current_cancellable:
154 * @cancellable: optional #GCancellable object, %NULL to ignore.
156 * Pushes @cancellable onto the cancellable stack.
159 g_push_current_cancellable (GCancellable *cancellable)
163 g_assert (cancellable != NULL);
165 l = g_static_private_get (¤t_cancellable);
166 l = g_slist_prepend (l, cancellable);
167 g_static_private_set (¤t_cancellable, l, NULL);
171 * g_pop_current_cancellable:
172 * @cancellable: optional #GCancellable object, %NULL to ignore.
174 * Pops @cancellable off the cancellable stack if @cancellable
175 * is on the top of the stack.
178 g_pop_current_cancellable (GCancellable *cancellable)
182 l = g_static_private_get (¤t_cancellable);
184 g_assert (l != NULL);
185 g_assert (l->data == cancellable);
187 l = g_slist_delete_link (l, l);
188 g_static_private_set (¤t_cancellable, l, NULL);
192 * g_cancellable_get_current:
194 * Gets the top cancellable from the stack.
196 * Returns: a #GCancellable from the top of the stack, or %NULL
197 * if the stack is empty.
200 g_cancellable_get_current (void)
204 l = g_static_private_get (¤t_cancellable);
208 return G_CANCELLABLE (l->data);
212 * g_cancellable_reset:
213 * @cancellable: a #GCancellable object.
215 * Resets @cancellable to its uncancelled state.
218 g_cancellable_reset (GCancellable *cancellable)
220 g_return_if_fail (G_IS_CANCELLABLE (cancellable));
223 /* Make sure we're not leaving old cancel state around */
224 if (cancellable->cancelled)
227 if (cancellable->cancel_pipe[0] != -1)
228 read (cancellable->cancel_pipe[0], &ch, 1);
229 cancellable->cancelled = FALSE;
231 G_UNLOCK(cancellable);
235 * g_cancellable_is_cancelled:
236 * @cancellable: a #GCancellable or NULL.
238 * Checks if a cancellable job has been cancelled.
240 * Returns: %TRUE if @cancellable is is cancelled,
241 * FALSE if called with %NULL or if item is not cancelled.
244 g_cancellable_is_cancelled (GCancellable *cancellable)
246 return cancellable != NULL && cancellable->cancelled;
250 * g_cancellable_set_error_if_cancelled:
251 * @cancellable: a #GCancellable object.
252 * @error: #GError to append error state to.
254 * Sets the current error to notify that the operation was cancelled.
256 * Returns: %TRUE if @cancellable was cancelled, %FALSE if it was not.
259 g_cancellable_set_error_if_cancelled (GCancellable *cancellable,
262 if (g_cancellable_is_cancelled (cancellable))
266 G_IO_ERROR_CANCELLED,
267 _("Operation was cancelled"));
275 * g_cancellable_get_fd:
276 * @cancellable: a #GCancellable.
278 * Gets the file descriptor for a cancellable job.
280 * Returns: A valid file descriptor. %-1 if the file descriptor
281 * is not supported, or on errors.
284 g_cancellable_get_fd (GCancellable *cancellable)
287 if (cancellable == NULL)
291 if (!cancellable->allocated_pipe)
293 cancellable->allocated_pipe = TRUE;
294 g_cancellable_open_pipe (cancellable);
297 fd = cancellable->cancel_pipe[0];
298 G_UNLOCK(cancellable);
304 * g_cancellable_cancel:
305 * @cancellable: a #GCancellable object.
307 * Will set @cancellable to cancelled, and will emit the CANCELLED
308 * signal. This function is thread-safe.
311 g_cancellable_cancel (GCancellable *cancellable)
318 if (cancellable != NULL &&
319 !cancellable->cancelled)
323 cancellable->cancelled = TRUE;
324 if (cancellable->cancel_pipe[1] != -1)
325 write (cancellable->cancel_pipe[1], &ch, 1);
327 G_UNLOCK(cancellable);
331 g_object_ref (cancellable);
332 g_signal_emit (cancellable, signals[CANCELLED], 0);
333 g_object_unref (cancellable);