#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
+#include <errno.h>
#include <fcntl.h>
#include <gioerror.h>
#ifdef G_OS_WIN32
+#include <windows.h>
#include <io.h>
-#ifndef pipe
-#define pipe(fds) _pipe(fds, 4096, _O_BINARY)
-#endif
#endif
#include "gcancellable.h"
#include "glibintl.h"
-#include "gioalias.h"
/**
* SECTION:gcancellable
LAST_SIGNAL
};
-struct _GCancellable
+struct _GCancellablePrivate
{
- GObject parent_instance;
-
guint cancelled : 1;
- guint allocated_pipe : 1;
guint cancelled_running : 1;
guint cancelled_running_waiting : 1;
+
+ guint fd_refcount;
int cancel_pipe[2];
#ifdef G_OS_WIN32
- GIOChannel *read_channel;
+ HANDLE event;
#endif
};
static GCond *cancellable_cond = NULL;
static void
-g_cancellable_finalize (GObject *object)
+g_cancellable_close_pipe (GCancellable *cancellable)
{
- GCancellable *cancellable = G_CANCELLABLE (object);
+ GCancellablePrivate *priv;
+
+ priv = cancellable->priv;
- if (cancellable->cancel_pipe[0] != -1)
- close (cancellable->cancel_pipe[0]);
+ if (priv->cancel_pipe[0] != -1)
+ {
+ close (priv->cancel_pipe[0]);
+ priv->cancel_pipe[0] = -1;
+ }
- if (cancellable->cancel_pipe[1] != -1)
- close (cancellable->cancel_pipe[1]);
+ if (priv->cancel_pipe[1] != -1)
+ {
+ close (priv->cancel_pipe[1]);
+ priv->cancel_pipe[1] = -1;
+ }
#ifdef G_OS_WIN32
- if (cancellable->read_channel)
- g_io_channel_unref (cancellable->read_channel);
+ if (priv->event)
+ {
+ CloseHandle (priv->event);
+ priv->event = NULL;
+ }
#endif
+}
+
+static void
+g_cancellable_finalize (GObject *object)
+{
+ GCancellable *cancellable = G_CANCELLABLE (object);
+
+ g_cancellable_close_pipe (cancellable);
G_OBJECT_CLASS (g_cancellable_parent_class)->finalize (object);
}
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+ g_type_class_add_private (klass, sizeof (GCancellablePrivate));
+
if (cancellable_cond == NULL && g_thread_supported ())
cancellable_cond = g_cond_new ();
}
+#ifndef G_OS_WIN32
static void
set_fd_nonblocking (int fd)
{
}
static void
+set_fd_close_exec (int fd)
+{
+ int flags;
+
+ flags = fcntl (fd, F_GETFD, 0);
+ if (flags != -1 && (flags & FD_CLOEXEC) == 0)
+ {
+ flags |= FD_CLOEXEC;
+ fcntl (fd, F_SETFD, flags);
+ }
+}
+
+
+static void
g_cancellable_open_pipe (GCancellable *cancellable)
{
- if (pipe (cancellable->cancel_pipe) == 0)
+ GCancellablePrivate *priv;
+
+ priv = cancellable->priv;
+ if (pipe (priv->cancel_pipe) == 0)
{
/* Make them nonblocking, just to be sure we don't block
* on errors and stuff
*/
- set_fd_nonblocking (cancellable->cancel_pipe[0]);
- set_fd_nonblocking (cancellable->cancel_pipe[1]);
+ set_fd_nonblocking (priv->cancel_pipe[0]);
+ set_fd_nonblocking (priv->cancel_pipe[1]);
+ set_fd_close_exec (priv->cancel_pipe[0]);
+ set_fd_close_exec (priv->cancel_pipe[1]);
+
+ if (priv->cancelled)
+ {
+ const char ch = 'x';
+ gssize c;
+
+ do
+ c = write (priv->cancel_pipe[1], &ch, 1);
+ while (c == -1 && errno == EINTR);
+ }
}
- else
- g_warning ("Failed to create pipe for GCancellable. Out of file descriptors?");
}
+#endif
static void
g_cancellable_init (GCancellable *cancellable)
{
- cancellable->cancel_pipe[0] = -1;
- cancellable->cancel_pipe[1] = -1;
+ cancellable->priv = G_TYPE_INSTANCE_GET_PRIVATE (cancellable,
+ G_TYPE_CANCELLABLE,
+ GCancellablePrivate);
+ cancellable->priv->cancel_pipe[0] = -1;
+ cancellable->priv->cancel_pipe[1] = -1;
}
/**
/**
* g_cancellable_push_current:
- * @cancellable: optional #GCancellable object, %NULL to ignore.
- *
+ * @cancellable: a #GCancellable object
+ *
* Pushes @cancellable onto the cancellable stack. The current
* cancllable can then be recieved using g_cancellable_get_current().
*
GSList *l;
g_return_if_fail (cancellable != NULL);
-
+
l = g_static_private_get (¤t_cancellable);
l = g_slist_prepend (l, cancellable);
g_static_private_set (¤t_cancellable, l, NULL);
/**
* g_cancellable_pop_current:
- * @cancellable: optional #GCancellable object, %NULL to ignore.
+ * @cancellable: a #GCancellable object
*
- * Pops @cancellable off the cancellable stack (verifying that @cancellable
+ * Pops @cancellable off the cancellable stack (verifying that @cancellable
* is on the top of the stack).
**/
void
g_cancellable_pop_current (GCancellable *cancellable)
{
GSList *l;
-
+
l = g_static_private_get (¤t_cancellable);
-
+
g_return_if_fail (l != NULL);
g_return_if_fail (l->data == cancellable);
/**
* g_cancellable_get_current:
- *
+ *
* Gets the top cancellable from the stack.
- *
- * Returns: a #GCancellable from the top of the stack, or %NULL
- * if the stack is empty.
+ *
+ * Returns: (transfer none): a #GCancellable from the top of the stack, or %NULL
+ * if the stack is empty.
**/
GCancellable *
g_cancellable_get_current (void)
{
GSList *l;
-
+
l = g_static_private_get (¤t_cancellable);
if (l == NULL)
return NULL;
void
g_cancellable_reset (GCancellable *cancellable)
{
+ GCancellablePrivate *priv;
+
g_return_if_fail (G_IS_CANCELLABLE (cancellable));
G_LOCK(cancellable);
+
+ priv = cancellable->priv;
- if (cancellable->cancelled_running)
+ while (priv->cancelled_running)
{
- g_critical ("Can't reset a cancellable during an active operation");
- G_UNLOCK(cancellable);
- return;
+ priv->cancelled_running_waiting = TRUE;
+ g_cond_wait (cancellable_cond,
+ g_static_mutex_get_mutex (& G_LOCK_NAME (cancellable)));
}
- if (!cancellable->cancelled)
+ if (priv->cancelled)
{
- char ch;
-
/* Make sure we're not leaving old cancel state around */
#ifdef G_OS_WIN32
- if (cancellable->read_channel)
- {
- gsize bytes_read;
- g_io_channel_read_chars (cancellable->read_channel, &ch, 1,
- &bytes_read, NULL);
- }
- else
+ if (priv->event)
+ ResetEvent (priv->event);
#endif
- if (cancellable->cancel_pipe[0] != -1)
- read (cancellable->cancel_pipe[0], &ch, 1);
- cancellable->cancelled = FALSE;
+ if (priv->cancel_pipe[0] != -1)
+ {
+ gssize c;
+ char ch;
+
+ do
+ c = read (priv->cancel_pipe[0], &ch, 1);
+ while (c == -1 && errno == EINTR);
+ }
+
+ priv->cancelled = FALSE;
}
G_UNLOCK(cancellable);
}
gboolean
g_cancellable_is_cancelled (GCancellable *cancellable)
{
- return cancellable != NULL && cancellable->cancelled;
+ return cancellable != NULL && cancellable->priv->cancelled;
}
/**
* readable status. Reading to unset the readable status is done
* with g_cancellable_reset().
*
+ * After a successful return from this function, you should use
+ * g_cancellable_release_fd() to free up resources allocated for
+ * the returned file descriptor.
+ *
* See also g_cancellable_make_pollfd().
*
* Returns: A valid file descriptor. %-1 if the file descriptor
int
g_cancellable_get_fd (GCancellable *cancellable)
{
+ GCancellablePrivate *priv;
int fd;
+
if (cancellable == NULL)
return -1;
-
+
+ priv = cancellable->priv;
+
+#ifdef G_OS_WIN32
+ return -1;
+#else
G_LOCK(cancellable);
- if (!cancellable->allocated_pipe)
- {
- cancellable->allocated_pipe = TRUE;
- g_cancellable_open_pipe (cancellable);
- }
-
- fd = cancellable->cancel_pipe[0];
+ if (priv->cancel_pipe[0] == -1)
+ g_cancellable_open_pipe (cancellable);
+ fd = priv->cancel_pipe[0];
+ if (fd != -1)
+ priv->fd_refcount++;
G_UNLOCK(cancellable);
-
+#endif
+
return fd;
}
/**
* g_cancellable_make_pollfd:
- * @cancellable: a #GCancellable.
+ * @cancellable: a #GCancellable or %NULL
* @pollfd: a pointer to a #GPollFD
*
* Creates a #GPollFD corresponding to @cancellable; this can be passed
- * to g_poll() and used to poll for cancellation.
+ * to g_poll() and used to poll for cancellation. This is useful both
+ * for unix systems without a native poll and for portability to
+ * windows.
+ *
+ * When this function returns %TRUE, you should use
+ * g_cancellable_release_fd() to free up resources allocated for the
+ * @pollfd. After a %FALSE return, do not call g_cancellable_release_fd().
+ *
+ * If this function returns %FALSE, either no @cancellable was given or
+ * resource limits prevent this function from allocating the necessary
+ * structures for polling. (On Linux, you will likely have reached
+ * the maximum number of file descriptors.) The suggested way to handle
+ * these cases is to ignore the @cancellable.
*
* You are not supposed to read from the fd yourself, just check for
* readable status. Reading to unset the readable status is done
* with g_cancellable_reset().
+ *
+ * Returns: %TRUE if @pollfd was successfully initialized, %FALSE on
+ * failure to prepare the cancellable.
*
+ * Since: 2.22
**/
-void
+gboolean
g_cancellable_make_pollfd (GCancellable *cancellable, GPollFD *pollfd)
{
- g_return_if_fail (G_IS_CANCELLABLE (cancellable));
- g_return_if_fail (pollfd != NULL);
+ g_return_val_if_fail (pollfd != NULL, FALSE);
+ if (cancellable == NULL)
+ return FALSE;
+ g_return_val_if_fail (G_IS_CANCELLABLE (cancellable), FALSE);
+ {
#ifdef G_OS_WIN32
- if (!cancellable->read_channel)
- {
- int fd = g_cancellable_get_fd (cancellable);
- cancellable->read_channel = g_io_channel_win32_new_fd (fd);
- g_io_channel_set_buffered (cancellable->read_channel, FALSE);
- g_io_channel_set_flags (cancellable->read_channel,
- G_IO_FLAG_NONBLOCK, NULL);
- g_io_channel_set_encoding (cancellable->read_channel, NULL, NULL);
- }
- g_io_channel_win32_make_pollfd (cancellable->read_channel, G_IO_IN, pollfd);
- /* (We need to keep cancellable->read_channel around, because it's
- * keeping track of state related to the pollfd.)
- */
+ GCancellablePrivate *priv;
+
+ priv = cancellable->priv;
+ G_LOCK(cancellable);
+ if (priv->event == NULL)
+ {
+ /* A manual reset anonymous event, starting unset */
+ priv->event = CreateEvent (NULL, TRUE, FALSE, NULL);
+ if (priv->event == NULL)
+ {
+ G_UNLOCK(cancellable);
+ return FALSE;
+ }
+ if (priv->cancelled)
+ SetEvent(priv->event);
+ }
+ priv->fd_refcount++;
+ G_UNLOCK(cancellable);
+
+ pollfd->fd = (gintptr)priv->event;
#else /* !G_OS_WIN32 */
- pollfd->fd = g_cancellable_get_fd (cancellable);
- pollfd->events = G_IO_IN;
+ int fd = g_cancellable_get_fd (cancellable);
+
+ if (fd == -1)
+ return FALSE;
+ pollfd->fd = fd;
#endif /* G_OS_WIN32 */
+ }
+
+ pollfd->events = G_IO_IN;
pollfd->revents = 0;
+
+ return TRUE;
+}
+
+/**
+ * g_cancellable_release_fd:
+ * @cancellable: a #GCancellable
+ *
+ * Releases a resources previously allocated by g_cancellable_get_fd()
+ * or g_cancellable_make_pollfd().
+ *
+ * For compatibility reasons with older releases, calling this function
+ * is not strictly required, the resources will be automatically freed
+ * when the @cancellable is finalized. However, the @cancellable will
+ * block scarce file descriptors until it is finalized if this function
+ * is not called. This can cause the application to run out of file
+ * descriptors when many #GCancellables are used at the same time.
+ *
+ * Since: 2.22
+ **/
+void
+g_cancellable_release_fd (GCancellable *cancellable)
+{
+ GCancellablePrivate *priv;
+
+ if (cancellable == NULL)
+ return;
+
+ g_return_if_fail (G_IS_CANCELLABLE (cancellable));
+ g_return_if_fail (cancellable->priv->fd_refcount > 0);
+
+ priv = cancellable->priv;
+
+ G_LOCK (cancellable);
+ priv->fd_refcount--;
+ if (priv->fd_refcount == 0)
+ g_cancellable_close_pipe (cancellable);
+ G_UNLOCK (cancellable);
}
/**
void
g_cancellable_cancel (GCancellable *cancellable)
{
- gboolean cancel;
+ GCancellablePrivate *priv;
+
+ if (cancellable == NULL ||
+ cancellable->priv->cancelled)
+ return;
+
+ priv = cancellable->priv;
- cancel = FALSE;
-
G_LOCK(cancellable);
- if (cancellable != NULL &&
- !cancellable->cancelled)
+ if (priv->cancelled)
{
- char ch = 'x';
- cancel = TRUE;
- cancellable->cancelled = TRUE;
- cancellable->cancelled_running = TRUE;
- if (cancellable->cancel_pipe[1] != -1)
- write (cancellable->cancel_pipe[1], &ch, 1);
+ G_UNLOCK (cancellable);
+ return;
}
- G_UNLOCK(cancellable);
- if (cancel)
+ priv->cancelled = TRUE;
+ priv->cancelled_running = TRUE;
+#ifdef G_OS_WIN32
+ if (priv->event)
+ SetEvent (priv->event);
+#endif
+ if (priv->cancel_pipe[1] != -1)
{
- g_object_ref (cancellable);
- g_signal_emit (cancellable, signals[CANCELLED], 0);
+ const char ch = 'x';
+ gssize c;
- G_LOCK(cancellable);
-
- cancellable->cancelled_running = FALSE;
- if (cancellable->cancelled_running_waiting)
- g_cond_broadcast (cancellable_cond);
- cancellable->cancelled_running_waiting = FALSE;
-
- G_UNLOCK(cancellable);
-
- g_object_unref (cancellable);
+ do
+ c = write (priv->cancel_pipe[1], &ch, 1);
+ while (c == -1 && errno == EINTR);
}
+ G_UNLOCK(cancellable);
+
+ g_object_ref (cancellable);
+ g_signal_emit (cancellable, signals[CANCELLED], 0);
+
+ G_LOCK(cancellable);
+
+ priv->cancelled_running = FALSE;
+ if (priv->cancelled_running_waiting)
+ g_cond_broadcast (cancellable_cond);
+ priv->cancelled_running_waiting = FALSE;
+
+ G_UNLOCK(cancellable);
+
+ g_object_unref (cancellable);
}
/**
*
* Convenience function to connect to the #GCancellable::cancelled
* signal. Also handles the race condition that may happen
- * if the cancellable is cancelled right before connecting.
- *
+ * if the cancellable is cancelled right before connecting.
+ *
* @callback is called at most once, either directly at the
* time of the connect if @cancellable is already cancelled,
* or when @cancellable is cancelled in some thread.
* cancelled.
*
* See #GCancellable::cancelled for details on how to use this.
- *
+ *
* Returns: The id of the signal handler or 0 if @cancellable has already
* been cancelled.
*
- * Since: 2.20
+ * Since: 2.22
*/
gulong
g_cancellable_connect (GCancellable *cancellable,
gulong id;
g_return_val_if_fail (G_IS_CANCELLABLE (cancellable), 0);
-
- G_LOCK(cancellable);
-
- if (cancellable->cancelled)
+
+ G_LOCK (cancellable);
+
+ if (cancellable->priv->cancelled)
{
void (*_callback) (GCancellable *cancellable,
- gpointer user_data);
-
+ gpointer user_data);
+
_callback = (void *)callback;
id = 0;
-
+
_callback (cancellable, data);
-
+
if (data_destroy_func)
- data_destroy_func (data);
+ data_destroy_func (data);
}
else
{
id = g_signal_connect_data (cancellable, "cancelled",
- callback, data,
- (GClosureNotify) data_destroy_func,
- 0);
+ callback, data,
+ (GClosureNotify) data_destroy_func,
+ 0);
}
- G_UNLOCK(cancellable);
+ G_UNLOCK (cancellable);
return id;
}
* @cancellable: A #GCancellable or %NULL.
* @handler_id: Handler id of the handler to be disconnected, or %0.
*
- * Disconnects a handler from an cancellable instance similar to
- * g_signal_handler_disconnect() but ensures that once this
- * function returns the handler will not run anymore in any thread.
+ * Disconnects a handler from a cancellable instance similar to
+ * g_signal_handler_disconnect(). Additionally, in the event that a
+ * signal handler is currently running, this call will block until the
+ * handler has finished. Calling this function from a
+ * #GCancellable::cancelled signal handler will therefore result in a
+ * deadlock.
*
* This avoids a race condition where a thread cancels at the
- * same time as the cancellable operation is finished and the
+ * same time as the cancellable operation is finished and the
* signal handler is removed. See #GCancellable::cancelled for
* details on how to use this.
*
* If @cancellable is %NULL or @handler_id is %0 this function does
* nothing.
- *
- * Since: 2.20
+ *
+ * Since: 2.22
*/
void
g_cancellable_disconnect (GCancellable *cancellable,
gulong handler_id)
{
+ GCancellablePrivate *priv;
+
if (handler_id == 0 || cancellable == NULL)
return;
- G_LOCK(cancellable);
- while (cancellable->cancelled_running)
+ G_LOCK (cancellable);
+
+ priv = cancellable->priv;
+
+ while (priv->cancelled_running)
{
- cancellable->cancelled_running_waiting = TRUE;
+ priv->cancelled_running_waiting = TRUE;
g_cond_wait (cancellable_cond,
- g_static_mutex_get_mutex (& G_LOCK_NAME (cancellable)));
+ g_static_mutex_get_mutex (& G_LOCK_NAME (cancellable)));
}
-
+
g_signal_handler_disconnect (cancellable, handler_id);
- G_UNLOCK(cancellable);
+ G_UNLOCK (cancellable);
}
-
-#define __G_CANCELLABLE_C__
-#include "gioaliasdef.c"