[glib] GOutputStream: Add g_output_stream_async_write_is_via_threads()



commit dec3bfeebc20d8fee9d6ddd6a7187ac762887f2d
Author: Mike Ruprecht <mike ruprecht collabora co uk>
Date:   Sat Feb 23 17:42:49 2013 -0600

    GOutputStream: Add g_output_stream_async_write_is_via_threads()
    
    In implementing a better g_output_stream_splice_async() and possibly
    other situtations it's helpful to know whether the output stream's
    write function internally uses threads. If it and the input stream's
    read async functions use threads, then the splice function could
    spawn a single thread for better efficiency.
    
    This patch adds a function to determine whether an output stream's
    g_output_stream_write_async() function internally uses threads.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=691581

 gio/gioprivate.h    |    2 ++
 gio/goutputstream.c |   26 ++++++++++++++++++++++++--
 2 files changed, 26 insertions(+), 2 deletions(-)
---
diff --git a/gio/gioprivate.h b/gio/gioprivate.h
index fc0fc5c..d190f19 100644
--- a/gio/gioprivate.h
+++ b/gio/gioprivate.h
@@ -22,10 +22,12 @@
 #define __G_IO_PRIVATE_H__
 
 #include "ginputstream.h"
+#include "goutputstream.h"
 
 G_BEGIN_DECLS
 
 gboolean g_input_stream_async_read_is_via_threads (GInputStream *stream);
+gboolean g_output_stream_async_write_is_via_threads (GOutputStream *stream);
 
 G_END_DECLS
 
diff --git a/gio/goutputstream.c b/gio/goutputstream.c
index 69397e2..9655748 100644
--- a/gio/goutputstream.c
+++ b/gio/goutputstream.c
@@ -27,6 +27,7 @@
 #include "gtask.h"
 #include "ginputstream.h"
 #include "gioerror.h"
+#include "gioprivate.h"
 #include "glibintl.h"
 #include "gpollableoutputstream.h"
 
@@ -1352,6 +1353,28 @@ g_output_stream_clear_pending (GOutputStream *stream)
   stream->priv->pending = FALSE;
 }
 
+/**
+ * g_output_stream_async_write_is_via_threads:
+ * @stream: a #GOutputStream.
+ *
+ * Checks if an ouput stream's write_async function uses threads.
+ *
+ * Returns: %TRUE if @stream's write_async function uses threads.
+ **/
+gboolean
+g_output_stream_async_write_is_via_threads (GOutputStream *stream)
+{
+  GOutputStreamClass *class;
+
+  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
+
+  class = G_OUTPUT_STREAM_GET_CLASS (stream);
+
+  return (class->write_async == g_output_stream_real_write_async &&
+      !(G_IS_POLLABLE_OUTPUT_STREAM (stream) &&
+        g_pollable_output_stream_can_poll (G_POLLABLE_OUTPUT_STREAM (stream))));
+}
+
 
 /********************************************
  *   Default implementation of async ops    *
@@ -1456,8 +1479,7 @@ g_output_stream_real_write_async (GOutputStream       *stream,
   op->buffer = buffer;
   op->count_requested = count;
 
-  if (G_IS_POLLABLE_OUTPUT_STREAM (stream) &&
-      g_pollable_output_stream_can_poll (G_POLLABLE_OUTPUT_STREAM (stream)))
+  if (!g_output_stream_async_write_is_via_threads (stream))
     write_async_pollable (G_POLLABLE_OUTPUT_STREAM (stream), task);
   else
     g_task_run_in_thread (task, write_async_thread);


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