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
43 * @include: gio/gcancellable.h
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;
64 static guint signals[LAST_SIGNAL] = { 0 };
66 G_DEFINE_TYPE (GCancellable, g_cancellable, G_TYPE_OBJECT);
68 static GStaticPrivate current_cancellable = G_STATIC_PRIVATE_INIT;
69 G_LOCK_DEFINE_STATIC(cancellable);
72 g_cancellable_finalize (GObject *object)
74 GCancellable *cancellable = G_CANCELLABLE (object);
76 if (cancellable->cancel_pipe[0] != -1)
77 close (cancellable->cancel_pipe[0]);
79 if (cancellable->cancel_pipe[1] != -1)
80 close (cancellable->cancel_pipe[1]);
82 if (G_OBJECT_CLASS (g_cancellable_parent_class)->finalize)
83 (*G_OBJECT_CLASS (g_cancellable_parent_class)->finalize) (object);
87 g_cancellable_class_init (GCancellableClass *klass)
89 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
91 gobject_class->finalize = g_cancellable_finalize;
94 * GCancellable::cancelled:
95 * @cancellable: a #GCancellable.
97 * Emitted when the operation has been cancelled from another thread.
99 * Can be used by implementations of cancellable operations. This will
100 * be emitted in the thread that tried to cancel the operation, not the
101 * thread the is running the operation.
104 g_signal_new (I_("cancelled"),
105 G_TYPE_FROM_CLASS (gobject_class),
107 G_STRUCT_OFFSET (GCancellableClass, cancelled),
109 g_cclosure_marshal_VOID__VOID,
115 set_fd_nonblocking (int fd)
119 fcntl_flags = fcntl (fd, F_GETFL);
122 fcntl_flags |= O_NONBLOCK;
124 fcntl_flags |= O_NDELAY;
127 fcntl (fd, F_SETFL, fcntl_flags);
132 g_cancellable_open_pipe (GCancellable *cancellable)
134 if (pipe (cancellable->cancel_pipe) == 0)
136 /* Make them nonblocking, just to be sure we don't block
137 * on errors and stuff
139 set_fd_nonblocking (cancellable->cancel_pipe[0]);
140 set_fd_nonblocking (cancellable->cancel_pipe[1]);
143 g_warning ("Failed to create pipe for GCancellable. Out of file descriptors?");
147 g_cancellable_init (GCancellable *cancellable)
149 cancellable->cancel_pipe[0] = -1;
150 cancellable->cancel_pipe[1] = -1;
156 * Creates a new #GCancellable object.
158 * Applications that want to start one or more operations
159 * that should be cancellable should create a #GCancellable
160 * and pass it to the operations.
162 * One #GCancellable can be used in multiple consecutive
163 * operations, but not in multiple concurrent operations.
165 * Returns: a #GCancellable.
168 g_cancellable_new (void)
170 return g_object_new (G_TYPE_CANCELLABLE, NULL);
174 * g_push_current_cancellable:
175 * @cancellable: optional #GCancellable object, %NULL to ignore.
177 * Pushes @cancellable onto the cancellable stack. The current
178 * cancllable can then be recieved using g_cancellable_get_current().
180 * This is useful when implementing cancellable operations in
181 * code that does not allow you to pass down the cancellable object.
183 * This is typically called automatically by e.g. #GFile operations,
184 * so you rarely have to call this yourself.
187 g_push_current_cancellable (GCancellable *cancellable)
191 g_return_if_fail (cancellable != NULL);
193 l = g_static_private_get (¤t_cancellable);
194 l = g_slist_prepend (l, cancellable);
195 g_static_private_set (¤t_cancellable, l, NULL);
199 * g_pop_current_cancellable:
200 * @cancellable: optional #GCancellable object, %NULL to ignore.
202 * Pops @cancellable off the cancellable stack (verifying that @cancellable
203 * is on the top of the stack).
206 g_pop_current_cancellable (GCancellable *cancellable)
210 l = g_static_private_get (¤t_cancellable);
212 g_return_if_fail (l != NULL);
213 g_return_if_fail (l->data == cancellable);
215 l = g_slist_delete_link (l, l);
216 g_static_private_set (¤t_cancellable, l, NULL);
220 * g_cancellable_get_current:
222 * Gets the top cancellable from the stack.
224 * Returns: a #GCancellable from the top of the stack, or %NULL
225 * if the stack is empty.
228 g_cancellable_get_current (void)
232 l = g_static_private_get (¤t_cancellable);
236 return G_CANCELLABLE (l->data);
240 * g_cancellable_reset:
241 * @cancellable: a #GCancellable object.
243 * Resets @cancellable to its uncancelled state.
246 g_cancellable_reset (GCancellable *cancellable)
248 g_return_if_fail (G_IS_CANCELLABLE (cancellable));
251 /* Make sure we're not leaving old cancel state around */
252 if (cancellable->cancelled)
255 if (cancellable->cancel_pipe[0] != -1)
256 read (cancellable->cancel_pipe[0], &ch, 1);
257 cancellable->cancelled = FALSE;
259 G_UNLOCK(cancellable);
263 * g_cancellable_is_cancelled:
264 * @cancellable: a #GCancellable or NULL.
266 * Checks if a cancellable job has been cancelled.
268 * Returns: %TRUE if @cancellable is is cancelled,
269 * FALSE if called with %NULL or if item is not cancelled.
272 g_cancellable_is_cancelled (GCancellable *cancellable)
274 return cancellable != NULL && cancellable->cancelled;
278 * g_cancellable_set_error_if_cancelled:
279 * @cancellable: a #GCancellable object.
280 * @error: #GError to append error state to.
282 * If the @cancelalble is cancelled, sets the error to notify
283 * that the operation was cancelled.
285 * Returns: %TRUE if @cancellable was cancelled, %FALSE if it was not.
288 g_cancellable_set_error_if_cancelled (GCancellable *cancellable,
291 if (g_cancellable_is_cancelled (cancellable))
295 G_IO_ERROR_CANCELLED,
296 _("Operation was cancelled"));
304 * g_cancellable_get_fd:
305 * @cancellable: a #GCancellable.
307 * Gets the file descriptor for a cancellable job. This can be used to
308 * implement cancellable operations on Unix systems. The returned fd will
309 * turn readable when @cancellable is cancelled.
311 * Returns: A valid file descriptor. %-1 if the file descriptor
312 * is not supported, or on errors.
315 g_cancellable_get_fd (GCancellable *cancellable)
318 if (cancellable == NULL)
322 if (!cancellable->allocated_pipe)
324 cancellable->allocated_pipe = TRUE;
325 g_cancellable_open_pipe (cancellable);
328 fd = cancellable->cancel_pipe[0];
329 G_UNLOCK(cancellable);
335 * g_cancellable_cancel:
336 * @cancellable: a #GCancellable object.
338 * Will set @cancellable to cancelled, and will emit the CANCELLED
341 * This function is thread-safe. In other words, you can safely call it from
342 * another thread than the one running an operation that was passed
346 g_cancellable_cancel (GCancellable *cancellable)
353 if (cancellable != NULL &&
354 !cancellable->cancelled)
358 cancellable->cancelled = TRUE;
359 if (cancellable->cancel_pipe[1] != -1)
360 write (cancellable->cancel_pipe[1], &ch, 1);
362 G_UNLOCK(cancellable);
366 g_object_ref (cancellable);
367 g_signal_emit (cancellable, signals[CANCELLED], 0);
368 g_object_unref (cancellable);
372 #define __G_CANCELLABLE_C__
373 #include "gioaliasdef.c"