GOutputStream: Add g_output_stream_async_write_is_via_threads()
authorMike Ruprecht <mike.ruprecht@collabora.co.uk>
Sat, 23 Feb 2013 23:42:49 +0000 (17:42 -0600)
committerDan Winship <danw@gnome.org>
Sun, 29 Sep 2013 21:48:40 +0000 (17:48 -0400)
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
gio/goutputstream.c

index fc0fc5c..d190f19 100644 (file)
 #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
 
index 69397e2..9655748 100644 (file)
@@ -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);