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"
33 * SECTION:gcancellable
34 * @short_description: Thread-safe Operation Cancellation Stack
35 * @include: gio/gcancellable.h
37 * GCancellable is a thread-safe operation cancellation stack used
38 * throughout GIO to allow for cancellation of asynchronous operations.
48 GObject parent_instance;
51 guint allocated_pipe : 1;
55 static guint signals[LAST_SIGNAL] = { 0 };
57 G_DEFINE_TYPE (GCancellable, g_cancellable, G_TYPE_OBJECT);
59 static GStaticPrivate current_cancellable = G_STATIC_PRIVATE_INIT;
60 G_LOCK_DEFINE_STATIC(cancellable);
63 g_cancellable_finalize (GObject *object)
65 GCancellable *cancellable = G_CANCELLABLE (object);
67 if (cancellable->cancel_pipe[0] != -1)
68 close (cancellable->cancel_pipe[0]);
70 if (cancellable->cancel_pipe[1] != -1)
71 close (cancellable->cancel_pipe[1]);
73 if (G_OBJECT_CLASS (g_cancellable_parent_class)->finalize)
74 (*G_OBJECT_CLASS (g_cancellable_parent_class)->finalize) (object);
78 g_cancellable_class_init (GCancellableClass *klass)
80 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
82 gobject_class->finalize = g_cancellable_finalize;
85 * GCancellable::cancelled:
86 * @cancellable: a #GCancellable.
88 * Emitted when the operation has been cancelled from another thread.
91 g_signal_new (I_("cancelled"),
92 G_TYPE_FROM_CLASS (gobject_class),
94 G_STRUCT_OFFSET (GCancellableClass, cancelled),
96 g_cclosure_marshal_VOID__VOID,
102 set_fd_nonblocking (int fd)
106 fcntl_flags = fcntl (fd, F_GETFL);
109 fcntl_flags |= O_NONBLOCK;
111 fcntl_flags |= O_NDELAY;
114 fcntl (fd, F_SETFL, fcntl_flags);
119 g_cancellable_open_pipe (GCancellable *cancellable)
121 if (pipe (cancellable->cancel_pipe) == 0)
123 /* Make them nonblocking, just to be sure we don't block
124 * on errors and stuff
126 set_fd_nonblocking (cancellable->cancel_pipe[0]);
127 set_fd_nonblocking (cancellable->cancel_pipe[1]);
130 g_warning ("Failed to create pipe for GCancellable. Out of file descriptors?");
134 g_cancellable_init (GCancellable *cancellable)
136 cancellable->cancel_pipe[0] = -1;
137 cancellable->cancel_pipe[1] = -1;
143 * Creates a new #GCancellable object.
145 * Returns: a #GCancellable.
148 g_cancellable_new (void)
150 return g_object_new (G_TYPE_CANCELLABLE, NULL);
154 * g_push_current_cancellable:
155 * @cancellable: optional #GCancellable object, %NULL to ignore.
157 * Pushes @cancellable onto the cancellable stack.
160 g_push_current_cancellable (GCancellable *cancellable)
164 g_assert (cancellable != NULL);
166 l = g_static_private_get (¤t_cancellable);
167 l = g_slist_prepend (l, cancellable);
168 g_static_private_set (¤t_cancellable, l, NULL);
172 * g_pop_current_cancellable:
173 * @cancellable: optional #GCancellable object, %NULL to ignore.
175 * Pops @cancellable off the cancellable stack if @cancellable
176 * is on the top of the stack.
179 g_pop_current_cancellable (GCancellable *cancellable)
183 l = g_static_private_get (¤t_cancellable);
185 g_assert (l != NULL);
186 g_assert (l->data == cancellable);
188 l = g_slist_delete_link (l, l);
189 g_static_private_set (¤t_cancellable, l, NULL);
193 * g_cancellable_get_current:
195 * Gets the top cancellable from the stack.
197 * Returns: a #GCancellable from the top of the stack, or %NULL
198 * if the stack is empty.
201 g_cancellable_get_current (void)
205 l = g_static_private_get (¤t_cancellable);
209 return G_CANCELLABLE (l->data);
213 * g_cancellable_reset:
214 * @cancellable: a #GCancellable object.
216 * Resets @cancellable to its uncancelled state.
219 g_cancellable_reset (GCancellable *cancellable)
221 g_return_if_fail (G_IS_CANCELLABLE (cancellable));
224 /* Make sure we're not leaving old cancel state around */
225 if (cancellable->cancelled)
228 if (cancellable->cancel_pipe[0] != -1)
229 read (cancellable->cancel_pipe[0], &ch, 1);
230 cancellable->cancelled = FALSE;
232 G_UNLOCK(cancellable);
236 * g_cancellable_is_cancelled:
237 * @cancellable: a #GCancellable or NULL.
239 * Checks if a cancellable job has been cancelled.
241 * Returns: %TRUE if @cancellable is is cancelled,
242 * FALSE if called with %NULL or if item is not cancelled.
245 g_cancellable_is_cancelled (GCancellable *cancellable)
247 return cancellable != NULL && cancellable->cancelled;
251 * g_cancellable_set_error_if_cancelled:
252 * @cancellable: a #GCancellable object.
253 * @error: #GError to append error state to.
255 * Sets the current error to notify that the operation was cancelled.
257 * Returns: %TRUE if @cancellable was cancelled, %FALSE if it was not.
260 g_cancellable_set_error_if_cancelled (GCancellable *cancellable,
263 if (g_cancellable_is_cancelled (cancellable))
267 G_IO_ERROR_CANCELLED,
268 _("Operation was cancelled"));
276 * g_cancellable_get_fd:
277 * @cancellable: a #GCancellable.
279 * Gets the file descriptor for a cancellable job.
281 * Returns: A valid file descriptor. %-1 if the file descriptor
282 * is not supported, or on errors.
285 g_cancellable_get_fd (GCancellable *cancellable)
288 if (cancellable == NULL)
292 if (!cancellable->allocated_pipe)
294 cancellable->allocated_pipe = TRUE;
295 g_cancellable_open_pipe (cancellable);
298 fd = cancellable->cancel_pipe[0];
299 G_UNLOCK(cancellable);
305 * g_cancellable_cancel:
306 * @cancellable: a #GCancellable object.
308 * Will set @cancellable to cancelled, and will emit the CANCELLED
309 * signal. This function is thread-safe.
312 g_cancellable_cancel (GCancellable *cancellable)
319 if (cancellable != NULL &&
320 !cancellable->cancelled)
324 cancellable->cancelled = TRUE;
325 if (cancellable->cancel_pipe[1] != -1)
326 write (cancellable->cancel_pipe[1], &ch, 1);
328 G_UNLOCK(cancellable);
332 g_object_ref (cancellable);
333 g_signal_emit (cancellable, signals[CANCELLED], 0);
334 g_object_unref (cancellable);
338 #define __G_CANCELLABLE_C__
339 #include "gioaliasdef.c"