[glib/wip/gsource3: 7/7] win32: add HANDLE equivalents of new fd APIs



commit 247de5ea11d069010dde8c7f494368cc9fe2b867
Author: Ryan Lortie <desrt desrt ca>
Date:   Mon Jan 14 15:11:10 2013 -0500

    win32: add HANDLE equivalents of new fd APIs
    
    Add support for adding HANDLEs to a GSource and add API for creating a
    new GSource from a single HANDLE.

 glib/gmain.c  |  133 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 glib/gmain.h  |   12 +++++-
 glib/gwin32.c |  132 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 glib/gwin32.h |   28 ++++++++++++
 4 files changed, 295 insertions(+), 10 deletions(-)
---
diff --git a/glib/gmain.c b/glib/gmain.c
index ff271d5..130f00f 100644
--- a/glib/gmain.c
+++ b/glib/gmain.c
@@ -318,10 +318,6 @@ struct _GSourcePrivate
   GSource *parent_source;
 
   gint64 ready_time;
-
-  /* This is currently only used on UNIX, but we always declare it (and
-   * let it remain empty on Windows) to avoid #ifdef all over the place.
-   */
   GSList *fds;
 };
 
@@ -2404,7 +2400,134 @@ g_source_query_unix_fd (GSource  *source,
 
   return poll_fd->revents;
 }
-#endif /* G_OS_UNIX */
+#else /* G_OS_UNIX */
+/**
+ * g_source_add_handle:
+ * @source: a #GSource
+ * @handle: the HANDLE to monitor
+ * @events: an event mask
+ *
+ * Monitors @handle for the signalled state.
+ *
+ * The tag returned by this function can be used to remove the handle
+ * using g_source_remove_handle().  It is not necessary to remove the
+ * handle before destroying the source; it will be cleaned up
+ * automatically.
+ *
+ * This function is only available on Windows.
+ *
+ * Returns: an opaque tag
+ *
+ * Since: 2.36
+ **/
+gpointer
+g_source_add_handle (GSource      *source,
+                     HANDLE        handle)
+{
+  GMainContext *context;
+  GPollFD *poll_fd;
+
+  g_return_val_if_fail (source != NULL, NULL);
+  g_return_val_if_fail (!SOURCE_DESTROYED (source), NULL);
+
+  poll_fd = g_new (GPollFD, 1);
+  poll_fd->fd = (gssize) handle;
+  poll_fd->events = G_IO_IN;
+  poll_fd->revents = 0;
+
+  context = source->context;
+
+  if (context)
+    LOCK_CONTEXT (context);
+
+  source->priv->fds = g_slist_prepend (source->priv->fds, poll_fd);
+
+  if (context)
+    {
+      if (!SOURCE_BLOCKED (source))
+        g_main_context_add_poll_unlocked (context, source->priority, poll_fd);
+      UNLOCK_CONTEXT (context);
+    }
+
+  return poll_fd;
+}
+
+/**
+ * g_source_remove_handle:
+ * @source: a #GSource
+ * @tag: the tag from g_source_add_handle()
+ *
+ * Reverses the effect of a previous call to g_source_add_handle().
+ *
+ * You only need to call this if you want to remove a handle from being
+ * watched while keeping the same source around.  In the normal case you
+ * will just want to destroy the source.
+ *
+ * This function is only available on Windows.
+ *
+ * Since: 2.36
+ **/
+void
+g_source_remove_handle (GSource  *source,
+                        gpointer  tag)
+{
+  GMainContext *context;
+  GPollFD *poll_fd;
+
+  g_return_if_fail (source != NULL);
+  g_return_if_fail (g_slist_find (source->priv->fds, tag));
+
+  context = source->context;
+  poll_fd = tag;
+
+  if (context)
+    LOCK_CONTEXT (context);
+
+  source->priv->fds = g_slist_remove (source->priv->fds, poll_fd);
+
+  if (context)
+    {
+      if (!SOURCE_BLOCKED (source))
+        g_main_context_remove_poll_unlocked (context, poll_fd);
+
+      UNLOCK_CONTEXT (context);
+    }
+
+  g_free (poll_fd);
+}
+
+/**
+ * g_source_query_handle:
+ * @source: a #GSource
+ * @tag: the tag from g_source_add_handle()
+ *
+ * Queries the events reported for the HANDLE corresponding to @tag on
+ * @source during the last poll.
+ *
+ * The return value of this function is only defined when the function
+ * is called from the check or dispatch functions for @source.
+ *
+ * This function is only available on Windows.
+ *
+ * Returns: %TRUE if the handle is signalled
+ *
+ * Since: 2.36
+ **/
+gboolean
+g_source_query_handle (GSource  *source,
+                       gpointer  tag)
+{
+  GPollFD *poll_fd;
+
+  g_return_val_if_fail (source != NULL, 0);
+  g_return_val_if_fail (g_slist_find (source->priv->fds, tag), 0);
+
+  poll_fd = tag;
+
+  return (poll_fd->revents & G_IO_IN) != 0;
+}
+
+#endif /* !G_OS_UNIX */
 
 /**
  * g_get_current_time:
diff --git a/glib/gmain.h b/glib/gmain.h
index 9fc5d9f..bbde206 100644
--- a/glib/gmain.h
+++ b/glib/gmain.h
@@ -483,7 +483,17 @@ void                 g_source_remove_unix_fd (GSource        *source,
 GLIB_AVAILABLE_IN_2_36
 GIOCondition         g_source_query_unix_fd  (GSource        *source,
                                               gpointer        tag);
-#endif
+#else /* G_OS_UNIX */
+GLIB_AVAILABLE_IN_2_36
+gpointer             g_source_add_handle     (GSource        *source,
+                                              HANDLE          handle);
+GLIB_AVAILABLE_IN_2_36
+void                 g_source_remove_handle  (GSource        *source,
+                                              gpointer        tag);
+GLIB_AVAILABLE_IN_2_36
+gboolean             g_source_query_handle   (GSource        *source,
+                                              gpointer        tag);
+#endif /* !G_OS_UNIX */
 
 /* Used to implement g_source_connect_closure and internally*/
 GLIB_AVAILABLE_IN_ALL
diff --git a/glib/gwin32.c b/glib/gwin32.c
index 97eccd7..3b95602 100644
--- a/glib/gwin32.c
+++ b/glib/gwin32.c
@@ -54,17 +54,17 @@
 #include "glib.h"
 #include "gthreadprivate.h"
 
-#ifdef G_WITH_CYGWIN
+#ihandleef G_WITH_CYGWIN
 #include <sys/cygwin.h>
 #endif
 
 #ifndef G_WITH_CYGWIN
 
 gint
-g_win32_ftruncate (gint  fd,
+g_win32_ftruncate (gint  handle,
 		   guint size)
 {
-  return _chsize (fd, size);
+  return _chsize (handle, size);
 }
 
 #endif
@@ -257,7 +257,7 @@ g_win32_get_package_installation_directory_of_module (gpointer hmodule)
 	    g_ascii_strcasecmp (p + 1, "lib") == 0))
     *p = '\0';
 
-#ifdef G_WITH_CYGWIN
+#ihandleef G_WITH_CYGWIN
   /* In Cygwin we need to have POSIX paths */
   {
     gchar tmp[MAX_PATH];
@@ -575,3 +575,127 @@ g_win32_locale_filename_from_utf8 (const gchar *utf8filename)
     }
   return retval;
 }
+
+typedef struct
+{
+  GSource source;
+
+  gint     handle;
+  gpointer tag;
+} GWin32HandleSource;
+
+static gboolean
+g_win32_handle_source_dispatch (GSource     *source,
+                           GSourceFunc  callback,
+                           gpointer     user_data)
+{
+  GWin32HandleSource *handle_source = (GWin32FdSource *) source;
+  GWin32HandleSourceFunc func = (GWin32FdSourceFunc) callback;
+
+  if (!callback)
+    {
+      g_warning ("GWin32HandleSource dispatched without callback\n"
+                 "You must call g_source_set_callback().");
+      return FALSE;
+    }
+
+  return (* func) (handle_source->handle, g_source_query_win32_handle (source, handle_source->tag), user_data);
+}
+
+
+/**
+ * g_win32_handle_source_new:
+ * @handle: a HANDLE
+ *
+ * Creates a #GSource to watch for @handle being signalled.
+ *
+ * Returns: the newly created #GSource
+ *
+ * Since: 2.36
+ **/
+GSource *
+g_win32_handle_source_new (HANDLE handle)
+{
+  static GSourceFuncs source_funcs = {
+    NULL, NULL, g_win32_handle_source_dispatch, NULL
+  };
+  GWin32HandleSource *handle_source;
+  GSource *source;
+
+  source = g_source_new (&source_funcs, sizeof (GWin32HandleSource));
+  handle_source = (GWin32HandleSource *) source;
+
+  handle_source->handle = handle;
+  handle_source->tag = g_source_add_handle (source, handle);
+
+  return source;
+}
+
+/**
+ * g_win32_handle_add_full:
+ * @priority: the priority of the source
+ * @handle: a HANDLE
+ * @function: a #GWin32HandleSourceFunc
+ * @user_data: data to pass to @function
+ * @notify: function to call when the idle is removed, or %NULL
+ *
+ * Sets a function to be called when @handle becomes signalled.
+ *
+ * This is the same as g_win32_handle_add(), except that it allows you to
+ * specify a non-default priority and a provide a #GDestroyNotify for
+ * @user_data.
+ *
+ * Returns: the ID (greater than 0) of the event source
+ *
+ * Since: 2.36
+ **/
+guint
+g_win32_handle_add_full (gint                   priority,
+                         HANDLE                 handle,
+                         GWin32HandleSourceFunc function,
+                         gpointer               user_data,
+                         GDestroyNotify         notify)
+{
+  GSource *source;
+  guint id;
+
+  g_return_val_if_fail (function != NULL, 0);
+
+  source = g_win32_handle_source_new (handle);
+
+  if (priority != G_PRIORITY_DEFAULT)
+    g_source_set_priority (source, priority);
+
+  g_source_set_callback (source, (GSourceFunc) function, user_data, notify);
+  id = g_source_attach (source, NULL);
+  g_source_unref (source);
+
+  return id;
+}
+
+/**
+ * g_win32_handle_add:
+ * @handle: a HANDLE
+ * @function: a #GPollFDFunc
+ * @user_data: data to pass to @function
+ *
+ * Sets a function to be called when @handle becomes signalled.
+ *
+ * The function is expected to clear whatever event caused the handle to
+ * be signalled and return %TRUE in order to be notified when it happens
+ * again.  If @function returns %FALSE then the watch will be cancelled.
+ *
+ * The return value of this function can be passed to g_source_remove()
+ * to cancel the watch at any time that it exists.
+ *
+ * Returns: the ID (greater than 0) of the event source
+ *
+ * Since: 2.36
+ **/
+guint
+g_win32_handle_add (HANDLE                 handle,
+                    GWin32HandleSourceFunc function,
+                    gpointer               user_data)
+{
+  return g_win32_handle_add_full (G_PRIORITY_DEFAULT, handle, function, user_data, NULL);
+}
diff --git a/glib/gwin32.h b/glib/gwin32.h
index ae87a45..32bd1de 100644
--- a/glib/gwin32.h
+++ b/glib/gwin32.h
@@ -128,6 +128,34 @@ gchar *g_win32_get_package_installation_subdirectory_utf8 (const gchar *package,
                                                            const gchar *dll_name,
                                                            const gchar *subdir);
 
+/**
+ * GWin32HandleSourceFunc:
+ * @handle: the HANDLE that triggered the event
+ * @user_data: user data passed to g_win32_handle_add()
+ *
+ * The type of functions to be called when a Windows HANDLE watch source
+ * triggers.
+ *
+ * Returns: %FALSE if the source should be removed
+ **/
+typedef gboolean (*GWin32HandleSourceFunc) (HANDLE   handle,
+                                            gpointer user_data);
+
+GLIB_AVAILABLE_IN_2_36
+GSource *g_win32_handle_source_new      (HANDLE                 handle);
+
+GLIB_AVAILABLE_IN_2_36
+guint    g_win32_handle_add_full        (gint                   priority,
+                                         HANDLE                 handle,
+                                         GWin32HandleSourceFunc function,
+                                         gpointer               user_data,
+                                         GDestroyNotify         notify);
+
+GLIB_AVAILABLE_IN_2_36
+guint    g_win32_handle_add             (HANDLE                 handle,
+                                         GWin32HandleSourceFunc function,
+                                         gpointer               user_data);
+
 #endif /* G_OS_WIN32 */
 
 #endif /* __G_WIN32_H__ */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]