gio: minor GPollableInputStream / GPollableOutputStream fixes

Make g_pollable_input_stream_read() and
g_pollable_output_stream_write() look a little bit more like the
non-pollable versions in terms of error handling, etc. Also, use the
read_fn and write_fn virtual methods directly rather than calling
g_input_stream_read()/g_output_stream_write(), to avoid problems with
re-entrancy involving the "pending" flag.

Also belatedly add single-include guards to the header files.

https://bugzilla.gnome.org/show_bug.cgi?id=673997
This commit is contained in:
Dan Winship 2012-04-01 14:47:19 -04:00
parent adea9fb252
commit 7e95777a6a
4 changed files with 74 additions and 24 deletions

View File

@ -45,7 +45,7 @@ G_DEFINE_INTERFACE (GPollableInputStream, g_pollable_input_stream, G_TYPE_INPUT_
static gboolean g_pollable_input_stream_default_can_poll (GPollableInputStream *stream);
static gssize g_pollable_input_stream_default_read_nonblocking (GPollableInputStream *stream,
void *buffer,
gsize size,
gsize count,
GError **error);
static void
@ -144,7 +144,7 @@ g_pollable_input_stream_create_source (GPollableInputStream *stream,
static gssize
g_pollable_input_stream_default_read_nonblocking (GPollableInputStream *stream,
void *buffer,
gsize size,
gsize count,
GError **error)
{
if (!g_pollable_input_stream_is_readable (stream))
@ -154,20 +154,20 @@ g_pollable_input_stream_default_read_nonblocking (GPollableInputStream *stream,
return -1;
}
return g_input_stream_read (G_INPUT_STREAM (stream), buffer, size,
NULL, error);
return G_INPUT_STREAM_GET_CLASS (stream)->
read_fn (G_INPUT_STREAM (stream), buffer, count, NULL, error);
}
/**
* g_pollable_input_stream_read_nonblocking:
* @stream: a #GPollableInputStream
* @buffer: a buffer to read data into (which should be at least @size
* @buffer: a buffer to read data into (which should be at least @count
* bytes long).
* @size: the number of bytes you want to read
* @count: the number of bytes you want to read
* @cancellable: (allow-none): a #GCancellable, or %NULL
* @error: #GError for error reporting, or %NULL to ignore.
*
* Attempts to read up to @size bytes from @stream into @buffer, as
* Attempts to read up to @count bytes from @stream into @buffer, as
* with g_input_stream_read(). If @stream is not currently readable,
* this will immediately return %G_IO_ERROR_WOULD_BLOCK, and you can
* use g_pollable_input_stream_create_source() to create a #GSource
@ -186,17 +186,38 @@ g_pollable_input_stream_default_read_nonblocking (GPollableInputStream *stream,
gssize
g_pollable_input_stream_read_nonblocking (GPollableInputStream *stream,
void *buffer,
gsize size,
gsize count,
GCancellable *cancellable,
GError **error)
{
gssize res;
g_return_val_if_fail (G_IS_POLLABLE_INPUT_STREAM (stream), -1);
g_return_val_if_fail (buffer != NULL, 0);
if (g_cancellable_set_error_if_cancelled (cancellable, error))
return -1;
return G_POLLABLE_INPUT_STREAM_GET_INTERFACE (stream)->
read_nonblocking (stream, buffer, size, error);
if (count == 0)
return 0;
if (((gssize) count) < 0)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
_("Too large count value passed to %s"), G_STRFUNC);
return -1;
}
if (cancellable)
g_cancellable_push_current (cancellable);
res = G_POLLABLE_INPUT_STREAM_GET_INTERFACE (stream)->
read_nonblocking (stream, buffer, count, error);
if (cancellable)
g_cancellable_pop_current (cancellable);
return res;
}
/* GPollableSource */

View File

@ -18,6 +18,10 @@
* Boston, MA 02111-1307, USA.
*/
#if !defined (__GIO_GIO_H_INSIDE__) && !defined (GIO_COMPILATION)
#error "Only <gio/gio.h> can be included directly."
#endif
#ifndef __G_POLLABLE_INPUT_STREAM_H__
#define __G_POLLABLE_INPUT_STREAM_H__
@ -73,7 +77,7 @@ struct _GPollableInputStreamInterface
GCancellable *cancellable);
gssize (*read_nonblocking) (GPollableInputStream *stream,
void *buffer,
gsize size,
gsize count,
GError **error);
};
@ -87,7 +91,7 @@ GSource *g_pollable_input_stream_create_source (GPollableInputStream *stream
gssize g_pollable_input_stream_read_nonblocking (GPollableInputStream *stream,
void *buffer,
gsize size,
gsize count,
GCancellable *cancellable,
GError **error);

View File

@ -46,7 +46,7 @@ G_DEFINE_INTERFACE (GPollableOutputStream, g_pollable_output_stream, G_TYPE_OUTP
static gboolean g_pollable_output_stream_default_can_poll (GPollableOutputStream *stream);
static gssize g_pollable_output_stream_default_write_nonblocking (GPollableOutputStream *stream,
const void *buffer,
gsize size,
gsize count,
GError **error);
static void
@ -145,7 +145,7 @@ g_pollable_output_stream_create_source (GPollableOutputStream *stream,
static gssize
g_pollable_output_stream_default_write_nonblocking (GPollableOutputStream *stream,
const void *buffer,
gsize size,
gsize count,
GError **error)
{
if (!g_pollable_output_stream_is_writable (stream))
@ -155,20 +155,20 @@ g_pollable_output_stream_default_write_nonblocking (GPollableOutputStream *stre
return -1;
}
return g_output_stream_write (G_OUTPUT_STREAM (stream), buffer, size,
NULL, error);
return G_OUTPUT_STREAM_GET_CLASS (stream)->
write_fn (G_OUTPUT_STREAM (stream), buffer, count, NULL, error);
}
/**
* g_pollable_output_stream_write_nonblocking:
* @stream: a #GPollableOutputStream
* @buffer: (array length=size) (element-type guint8): a buffer to write
* @buffer: (array length=count) (element-type guint8): a buffer to write
* data from
* @size: the number of bytes you want to write
* @count: the number of bytes you want to write
* @cancellable: (allow-none): a #GCancellable, or %NULL
* @error: #GError for error reporting, or %NULL to ignore.
*
* Attempts to write up to @size bytes from @buffer to @stream, as
* Attempts to write up to @count bytes from @buffer to @stream, as
* with g_output_stream_write(). If @stream is not currently writable,
* this will immediately return %G_IO_ERROR_WOULD_BLOCK, and you can
* use g_pollable_output_stream_create_source() to create a #GSource
@ -187,15 +187,36 @@ g_pollable_output_stream_default_write_nonblocking (GPollableOutputStream *stre
gssize
g_pollable_output_stream_write_nonblocking (GPollableOutputStream *stream,
const void *buffer,
gsize size,
gsize count,
GCancellable *cancellable,
GError **error)
{
gssize res;
g_return_val_if_fail (G_IS_POLLABLE_OUTPUT_STREAM (stream), -1);
g_return_val_if_fail (buffer != NULL, 0);
if (g_cancellable_set_error_if_cancelled (cancellable, error))
return -1;
return G_POLLABLE_OUTPUT_STREAM_GET_INTERFACE (stream)->
write_nonblocking (stream, buffer, size, error);
if (count == 0)
return 0;
if (((gssize) count) < 0)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
_("Too large count value passed to %s"), G_STRFUNC);
return -1;
}
if (cancellable)
g_cancellable_push_current (cancellable);
res = G_POLLABLE_OUTPUT_STREAM_GET_INTERFACE (stream)->
write_nonblocking (stream, buffer, count, error);
if (cancellable)
g_cancellable_pop_current (cancellable);
return res;
}

View File

@ -18,6 +18,10 @@
* Boston, MA 02111-1307, USA.
*/
#if !defined (__GIO_GIO_H_INSIDE__) && !defined (GIO_COMPILATION)
#error "Only <gio/gio.h> can be included directly."
#endif
#ifndef __G_POLLABLE_OUTPUT_STREAM_H__
#define __G_POLLABLE_OUTPUT_STREAM_H__
@ -73,7 +77,7 @@ struct _GPollableOutputStreamInterface
GCancellable *cancellable);
gssize (*write_nonblocking) (GPollableOutputStream *stream,
const void *buffer,
gsize size,
gsize count,
GError **error);
};
@ -87,7 +91,7 @@ GSource *g_pollable_output_stream_create_source (GPollableOutputStream *str
gssize g_pollable_output_stream_write_nonblocking (GPollableOutputStream *stream,
const void *buffer,
gsize size,
gsize count,
GCancellable *cancellable,
GError **error);