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 asynchronous operations.
56 GObject parent_instance;
59 guint allocated_pipe : 1;
63 static guint signals[LAST_SIGNAL] = { 0 };
65 G_DEFINE_TYPE (GCancellable, g_cancellable, G_TYPE_OBJECT);
67 static GStaticPrivate current_cancellable = G_STATIC_PRIVATE_INIT;
68 G_LOCK_DEFINE_STATIC(cancellable);
71 g_cancellable_finalize (GObject *object)
73 GCancellable *cancellable = G_CANCELLABLE (object);
75 if (cancellable->cancel_pipe[0] != -1)
76 close (cancellable->cancel_pipe[0]);
78 if (cancellable->cancel_pipe[1] != -1)
79 close (cancellable->cancel_pipe[1]);
81 if (G_OBJECT_CLASS (g_cancellable_parent_class)->finalize)
82 (*G_OBJECT_CLASS (g_cancellable_parent_class)->finalize) (object);
86 g_cancellable_class_init (GCancellableClass *klass)
88 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
90 gobject_class->finalize = g_cancellable_finalize;
93 * GCancellable::cancelled:
94 * @cancellable: a #GCancellable.
96 * Emitted when the operation has been cancelled from another thread.
99 g_signal_new (I_("cancelled"),
100 G_TYPE_FROM_CLASS (gobject_class),
102 G_STRUCT_OFFSET (GCancellableClass, cancelled),
104 g_cclosure_marshal_VOID__VOID,
110 set_fd_nonblocking (int fd)
114 fcntl_flags = fcntl (fd, F_GETFL);
117 fcntl_flags |= O_NONBLOCK;
119 fcntl_flags |= O_NDELAY;
122 fcntl (fd, F_SETFL, fcntl_flags);
127 g_cancellable_open_pipe (GCancellable *cancellable)
129 if (pipe (cancellable->cancel_pipe) == 0)
131 /* Make them nonblocking, just to be sure we don't block
132 * on errors and stuff
134 set_fd_nonblocking (cancellable->cancel_pipe[0]);
135 set_fd_nonblocking (cancellable->cancel_pipe[1]);
138 g_warning ("Failed to create pipe for GCancellable. Out of file descriptors?");
142 g_cancellable_init (GCancellable *cancellable)
144 cancellable->cancel_pipe[0] = -1;
145 cancellable->cancel_pipe[1] = -1;
151 * Creates a new #GCancellable object.
153 * Returns: a #GCancellable.
156 g_cancellable_new (void)
158 return g_object_new (G_TYPE_CANCELLABLE, NULL);
162 * g_push_current_cancellable:
163 * @cancellable: optional #GCancellable object, %NULL to ignore.
165 * Pushes @cancellable onto the cancellable stack.
168 g_push_current_cancellable (GCancellable *cancellable)
172 g_return_if_fail (cancellable != NULL);
174 l = g_static_private_get (¤t_cancellable);
175 l = g_slist_prepend (l, cancellable);
176 g_static_private_set (¤t_cancellable, l, NULL);
180 * g_pop_current_cancellable:
181 * @cancellable: optional #GCancellable object, %NULL to ignore.
183 * Pops @cancellable off the cancellable stack if @cancellable
184 * is on the top of the stack.
187 g_pop_current_cancellable (GCancellable *cancellable)
191 l = g_static_private_get (¤t_cancellable);
193 g_return_if_fail (l != NULL);
194 g_return_if_fail (l->data == cancellable);
196 l = g_slist_delete_link (l, l);
197 g_static_private_set (¤t_cancellable, l, NULL);
201 * g_cancellable_get_current:
203 * Gets the top cancellable from the stack.
205 * Returns: a #GCancellable from the top of the stack, or %NULL
206 * if the stack is empty.
209 g_cancellable_get_current (void)
213 l = g_static_private_get (¤t_cancellable);
217 return G_CANCELLABLE (l->data);
221 * g_cancellable_reset:
222 * @cancellable: a #GCancellable object.
224 * Resets @cancellable to its uncancelled state.
227 g_cancellable_reset (GCancellable *cancellable)
229 g_return_if_fail (G_IS_CANCELLABLE (cancellable));
232 /* Make sure we're not leaving old cancel state around */
233 if (cancellable->cancelled)
236 if (cancellable->cancel_pipe[0] != -1)
237 read (cancellable->cancel_pipe[0], &ch, 1);
238 cancellable->cancelled = FALSE;
240 G_UNLOCK(cancellable);
244 * g_cancellable_is_cancelled:
245 * @cancellable: a #GCancellable or NULL.
247 * Checks if a cancellable job has been cancelled.
249 * Returns: %TRUE if @cancellable is is cancelled,
250 * FALSE if called with %NULL or if item is not cancelled.
253 g_cancellable_is_cancelled (GCancellable *cancellable)
255 return cancellable != NULL && cancellable->cancelled;
259 * g_cancellable_set_error_if_cancelled:
260 * @cancellable: a #GCancellable object.
261 * @error: #GError to append error state to.
263 * Sets the current error to notify that the operation was cancelled.
265 * Returns: %TRUE if @cancellable was cancelled, %FALSE if it was not.
268 g_cancellable_set_error_if_cancelled (GCancellable *cancellable,
271 if (g_cancellable_is_cancelled (cancellable))
275 G_IO_ERROR_CANCELLED,
276 _("Operation was cancelled"));
284 * g_cancellable_get_fd:
285 * @cancellable: a #GCancellable.
287 * Gets the file descriptor for a cancellable job.
289 * Returns: A valid file descriptor. %-1 if the file descriptor
290 * is not supported, or on errors.
293 g_cancellable_get_fd (GCancellable *cancellable)
296 if (cancellable == NULL)
300 if (!cancellable->allocated_pipe)
302 cancellable->allocated_pipe = TRUE;
303 g_cancellable_open_pipe (cancellable);
306 fd = cancellable->cancel_pipe[0];
307 G_UNLOCK(cancellable);
313 * g_cancellable_cancel:
314 * @cancellable: a #GCancellable object.
316 * Will set @cancellable to cancelled, and will emit the CANCELLED
317 * signal. This function is thread-safe.
320 g_cancellable_cancel (GCancellable *cancellable)
327 if (cancellable != NULL &&
328 !cancellable->cancelled)
332 cancellable->cancelled = TRUE;
333 if (cancellable->cancel_pipe[1] != -1)
334 write (cancellable->cancel_pipe[1], &ch, 1);
336 G_UNLOCK(cancellable);
340 g_object_ref (cancellable);
341 g_signal_emit (cancellable, signals[CANCELLED], 0);
342 g_object_unref (cancellable);
346 #define __G_CANCELLABLE_C__
347 #include "gioaliasdef.c"