Merge remote branch 'gvdb/master'
[platform/upstream/glib.git] / gio / gcancellable.c
index be712c4..3a99532 100644 (file)
@@ -24,6 +24,7 @@
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
+#include <errno.h>
 #include <fcntl.h>
 #include <gioerror.h>
 #ifdef G_OS_WIN32
@@ -33,7 +34,6 @@
 #include "gcancellable.h"
 #include "glibintl.h"
 
-#include "gioalias.h"
 
 /**
  * SECTION:gcancellable
@@ -52,11 +52,11 @@ enum {
 
 struct _GCancellablePrivate
 {
-  GObject parent_instance;
-
   guint cancelled : 1;
   guint cancelled_running : 1;
   guint cancelled_running_waiting : 1;
+
+  guint fd_refcount;
   int cancel_pipe[2];
 
 #ifdef G_OS_WIN32
@@ -73,23 +73,39 @@ G_LOCK_DEFINE_STATIC(cancellable);
 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 (priv->cancel_pipe[0] != -1)
-    close (priv->cancel_pipe[0]);
+    {
+      close (priv->cancel_pipe[0]);
+      priv->cancel_pipe[0] = -1;
+    }
   
   if (priv->cancel_pipe[1] != -1)
-    close (priv->cancel_pipe[1]);
+    {
+      close (priv->cancel_pipe[1]);
+      priv->cancel_pipe[1] = -1;
+    }
 
 #ifdef G_OS_WIN32
   if (priv->event)
-    CloseHandle (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);
 }
@@ -222,6 +238,16 @@ g_cancellable_open_pipe (GCancellable *cancellable)
       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);
+        }
     }
 }
 #endif
@@ -307,7 +333,7 @@ g_cancellable_pop_current (GCancellable *cancellable)
  *
  * Gets the top cancellable from the stack.
  *
- * Returns: a #GCancellable from the top of the stack, or %NULL
+ * Returns: (transfer none): a #GCancellable from the top of the stack, or %NULL
  * if the stack is empty.
  **/
 GCancellable *
@@ -348,17 +374,22 @@ g_cancellable_reset (GCancellable *cancellable)
   
   if (priv->cancelled)
     {
-      char ch;
-      
     /* Make sure we're not leaving old cancel state around */
       
 #ifdef G_OS_WIN32
       if (priv->event)
        ResetEvent (priv->event);
-      else
 #endif
       if (priv->cancel_pipe[0] != -1)
-       read (priv->cancel_pipe[0], &ch, 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);
@@ -430,8 +461,8 @@ int
 g_cancellable_get_fd (GCancellable *cancellable)
 {
   GCancellablePrivate *priv;
-
   int fd;
+
   if (cancellable == NULL)
     return -1;
 
@@ -443,8 +474,9 @@ g_cancellable_get_fd (GCancellable *cancellable)
   G_LOCK(cancellable);
   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
 
@@ -475,41 +507,54 @@ g_cancellable_get_fd (GCancellable *cancellable)
  * 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.
+ * Returns: %TRUE if @pollfd was successfully initialized, %FALSE on 
+ *          failure to prepare the cancellable.
  * 
- * @Since: 2.22
+ * Since: 2.22
  **/
 gboolean
 g_cancellable_make_pollfd (GCancellable *cancellable, GPollFD *pollfd)
 {
-  GCancellablePrivate *priv;
-  int fd;
-
   g_return_val_if_fail (pollfd != NULL, FALSE);
   if (cancellable == NULL)
     return FALSE;
   g_return_val_if_fail (G_IS_CANCELLABLE (cancellable), FALSE);
 
-  priv = cancellable->priv;
-
+  {
 #ifdef G_OS_WIN32
-  if (!priv->event)
-    {
-      /* A manual reset anonymous event, starting unset */
-      priv->event = CreateEvent (NULL, TRUE, FALSE, NULL);
-      if (priv->event == NULL)
-        return FALSE;
-    }
-  pollfd->fd = (gintptr)priv->event;
+    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 */
-  fd = g_cancellable_get_fd (cancellable);
-  if (fd == -1)
-    return -1;
-  pollfd->fd = fd;
+    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;
 }
 
 /**
@@ -526,12 +571,26 @@ g_cancellable_make_pollfd (GCancellable *cancellable, GPollFD *pollfd)
  * 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
+ * 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);
 }
 
 /**
@@ -556,8 +615,6 @@ g_cancellable_release_fd (GCancellable *cancellable)
 void
 g_cancellable_cancel (GCancellable *cancellable)
 {
-  static const char ch = 'x';
-  gboolean cancel;
   GCancellablePrivate *priv;
 
   if (cancellable == NULL ||
@@ -565,36 +622,44 @@ g_cancellable_cancel (GCancellable *cancellable)
     return;
 
   priv = cancellable->priv;
-  cancel = FALSE;
 
   G_LOCK(cancellable);
-  cancel = TRUE;
+  if (priv->cancelled)
+    {
+      G_UNLOCK (cancellable);
+      return;
+    }
+
   priv->cancelled = TRUE;
   priv->cancelled_running = TRUE;
 #ifdef G_OS_WIN32
   if (priv->event)
-    SetEvent(priv->event);
+    SetEvent (priv->event);
 #endif
   if (priv->cancel_pipe[1] != -1)
-    write (priv->cancel_pipe[1], &ch, 1);
+    {
+      const char ch = 'x';
+      gssize c;
+
+      do
+        c = write (priv->cancel_pipe[1], &ch, 1);
+      while (c == -1 && errno == EINTR);
+    }
   G_UNLOCK(cancellable);
 
-  if (cancel)
-    {
-      g_object_ref (cancellable);
-      g_signal_emit (cancellable, signals[CANCELLED], 0);
+  g_object_ref (cancellable);
+  g_signal_emit (cancellable, signals[CANCELLED], 0);
 
-      G_LOCK(cancellable);
+  G_LOCK(cancellable);
 
-      priv->cancelled_running = FALSE;
-      if (priv->cancelled_running_waiting)
-       g_cond_broadcast (cancellable_cond);
-      priv->cancelled_running_waiting = FALSE;
+  priv->cancelled_running = FALSE;
+  if (priv->cancelled_running_waiting)
+    g_cond_broadcast (cancellable_cond);
+  priv->cancelled_running_waiting = FALSE;
 
-      G_UNLOCK(cancellable);
+  G_UNLOCK(cancellable);
 
-      g_object_unref (cancellable);
-    }
+  g_object_unref (cancellable);
 }
 
 /**
@@ -665,9 +730,12 @@ g_cancellable_connect (GCancellable   *cancellable,
  * @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
@@ -702,6 +770,3 @@ g_cancellable_disconnect (GCancellable  *cancellable,
   g_signal_handler_disconnect (cancellable, handler_id);
   G_UNLOCK (cancellable);
 }
-
-#define __G_CANCELLABLE_C__
-#include "gioaliasdef.c"