GInputStream: Add g_input_stream_async_read_is_via_threads()

In implementing a better g_output_stream_splice_async() and possibly
other situtations it's helpful to know whether the input stream's
read function internally uses threads. If it and the output stream's
write 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 input stream's
g_input_stream_read_async() function internally uses threads.

https://bugzilla.gnome.org/show_bug.cgi?id=691581
This commit is contained in:
Mike Ruprecht 2013-02-23 17:32:31 -06:00 committed by Dan Winship
parent ab5aa2aa3a
commit 94a232a4ed
3 changed files with 58 additions and 7 deletions

View File

@ -381,6 +381,7 @@ libgio_2_0_la_SOURCES = \
giomodule-priv.h \
gioscheduler.c \
giostream.c \
gioprivate.h \
gloadableicon.c \
gmount.c \
gmemoryinputstream.c \

View File

@ -25,6 +25,7 @@
#include "glibintl.h"
#include "ginputstream.h"
#include "gioprivate.h"
#include "gseekable.h"
#include "gcancellable.h"
#include "gasyncresult.h"
@ -1041,6 +1042,28 @@ g_input_stream_clear_pending (GInputStream *stream)
stream->priv->pending = FALSE;
}
/**
* g_input_stream_async_read_is_via_threads:
* @stream: input stream
*
* Checks if an input stream's read_async function uses threads.
*
* Returns: %TRUE if @stream's read_async function uses threads.
**/
gboolean
g_input_stream_async_read_is_via_threads (GInputStream *stream)
{
GInputStreamClass *class;
g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
class = G_INPUT_STREAM_GET_CLASS (stream);
return (class->read_async == g_input_stream_real_read_async &&
!(G_IS_POLLABLE_INPUT_STREAM (stream) &&
g_pollable_input_stream_can_poll (G_POLLABLE_INPUT_STREAM (stream))));
}
/********************************************
* Default implementation of async ops *
********************************************/
@ -1128,10 +1151,6 @@ read_async_pollable (GPollableInputStream *stream,
/* g_input_stream_real_read_async() unrefs task */
}
#define CAN_DO_NONBLOCKING_READS(stream) \
(G_IS_POLLABLE_INPUT_STREAM (stream) && \
g_pollable_input_stream_can_poll (G_POLLABLE_INPUT_STREAM (stream)))
static void
g_input_stream_real_read_async (GInputStream *stream,
@ -1152,7 +1171,7 @@ g_input_stream_real_read_async (GInputStream *stream,
op->buffer = buffer;
op->count = count;
if (CAN_DO_NONBLOCKING_READS (stream))
if (!g_input_stream_async_read_is_via_threads (stream))
read_async_pollable (G_POLLABLE_INPUT_STREAM (stream), task);
else
g_task_run_in_thread (task, read_async_thread);
@ -1260,8 +1279,7 @@ g_input_stream_real_skip_async (GInputStream *stream,
task = g_task_new (stream, cancellable, callback, user_data);
g_task_set_priority (task, io_priority);
if (class->read_async == g_input_stream_real_read_async &&
!CAN_DO_NONBLOCKING_READS (stream))
if (g_input_stream_async_read_is_via_threads (stream))
{
/* Read is thread-using async fallback.
* Make skip use threads too, so that we can use a possible sync skip

32
gio/gioprivate.h Normal file
View File

@ -0,0 +1,32 @@
/* GIO - GLib Input, Output and Streaming Library
*
* Copyright (C) 2013 Collabora Ltd.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifndef __G_IO_PRIVATE_H__
#define __G_IO_PRIVATE_H__
#include "ginputstream.h"
G_BEGIN_DECLS
gboolean g_input_stream_async_read_is_via_threads (GInputStream *stream);
G_END_DECLS
#endif /* __G_IO_PRIVATE__ */