gsettingsschema: Close directory
[platform/upstream/glib.git] / gio / ginputstream.c
index f7006cb..b0dce76 100644 (file)
@@ -13,9 +13,7 @@
  * 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.
+ * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
  *
  * Author: Alexander Larsson <alexl@redhat.com>
  */
@@ -25,6 +23,7 @@
 #include "glibintl.h"
 
 #include "ginputstream.h"
+#include "gioprivate.h"
 #include "gseekable.h"
 #include "gcancellable.h"
 #include "gasyncresult.h"
  * All of these functions have async variants too.
  **/
 
-G_DEFINE_ABSTRACT_TYPE (GInputStream, g_input_stream, G_TYPE_OBJECT);
-
 struct _GInputStreamPrivate {
   guint closed : 1;
   guint pending : 1;
   GAsyncReadyCallback outstanding_callback;
 };
 
+G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GInputStream, g_input_stream, G_TYPE_OBJECT)
+
 static gssize   g_input_stream_real_skip         (GInputStream         *stream,
                                                  gsize                 count,
                                                  GCancellable         *cancellable,
@@ -87,12 +86,6 @@ static gboolean g_input_stream_real_close_finish (GInputStream         *stream,
                                                  GError              **error);
 
 static void
-g_input_stream_finalize (GObject *object)
-{
-  G_OBJECT_CLASS (g_input_stream_parent_class)->finalize (object);
-}
-
-static void
 g_input_stream_dispose (GObject *object)
 {
   GInputStream *stream;
@@ -111,9 +104,6 @@ g_input_stream_class_init (GInputStreamClass *klass)
 {
   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
   
-  g_type_class_add_private (klass, sizeof (GInputStreamPrivate));
-  
-  gobject_class->finalize = g_input_stream_finalize;
   gobject_class->dispose = g_input_stream_dispose;
   
   klass->skip = g_input_stream_real_skip;
@@ -128,15 +118,14 @@ g_input_stream_class_init (GInputStreamClass *klass)
 static void
 g_input_stream_init (GInputStream *stream)
 {
-  stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
-                                             G_TYPE_INPUT_STREAM,
-                                             GInputStreamPrivate);
+  stream->priv = g_input_stream_get_instance_private (stream);
 }
 
 /**
  * g_input_stream_read:
  * @stream: a #GInputStream.
- * @buffer: a buffer to read data into (which should be at least count bytes long).
+ * @buffer: (array length=count) (element-type guint8): a buffer to
+ *     read data into (which should be at least count bytes long).
  * @count: the number of bytes that will be read from the stream
  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
  * @error: location to store the error occurring, or %NULL to ignore
@@ -152,6 +141,9 @@ g_input_stream_init (GInputStream *stream)
  * can happen e.g. near the end of a file. Zero is returned on end of file
  * (or if @count is zero),  but never otherwise.
  *
+ * The returned @buffer is not a nul-terminated string, it can contain nul bytes
+ * at any position, and this function doesn't nul-terminate the @buffer.
+ *
  * If @cancellable is not %NULL, then the operation can be cancelled by
  * triggering the cancellable object from another thread. If the operation
  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
@@ -160,7 +152,7 @@ g_input_stream_init (GInputStream *stream)
  *
  * On error -1 is returned and @error is set accordingly.
  * 
- * Return value: Number of bytes read, or -1 on error, or 0 on end of file.
+ * Returns: Number of bytes read, or -1 on error, or 0 on end of file.
  **/
 gssize
 g_input_stream_read  (GInputStream  *stream,
@@ -213,7 +205,8 @@ g_input_stream_read  (GInputStream  *stream,
 /**
  * g_input_stream_read_all:
  * @stream: a #GInputStream.
- * @buffer: a buffer to read data into (which should be at least count bytes long).
+ * @buffer: (array length=count) (element-type guint8): a buffer to
+ *     read data into (which should be at least count bytes long).
  * @count: the number of bytes that will be read from the stream
  * @bytes_read: (out): location to store the number of bytes that was read from the stream
  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
@@ -233,7 +226,7 @@ g_input_stream_read  (GInputStream  *stream,
  * is set to indicate the error status, @bytes_read is updated to contain
  * the number of bytes read into @buffer before the error occurred.
  *
- * Return value: %TRUE on success, %FALSE if there was an error
+ * Returns: %TRUE on success, %FALSE if there was an error
  **/
 gboolean
 g_input_stream_read_all (GInputStream  *stream,
@@ -304,7 +297,9 @@ g_input_stream_read_all (GInputStream  *stream,
  *
  * On error %NULL is returned and @error is set accordingly.
  *
- * Return value: a new #GBytes, or %NULL on error
+ * Returns: a new #GBytes, or %NULL on error
+ *
+ * Since: 2.34
  **/
 GBytes *
 g_input_stream_read_bytes (GInputStream  *stream,
@@ -353,7 +348,7 @@ g_input_stream_read_bytes (GInputStream  *stream,
  * operation was partially finished when the operation was cancelled the
  * partial result will be returned, without an error.
  *
- * Return value: Number of bytes skipped, or -1 on error
+ * Returns: Number of bytes skipped, or -1 on error
  **/
 gssize
 g_input_stream_skip (GInputStream  *stream,
@@ -478,7 +473,7 @@ g_input_stream_real_skip (GInputStream  *stream,
  * Cancelling a close will still leave the stream closed, but some streams
  * can use a faster close that doesn't block to e.g. check errors. 
  *
- * Return value: %TRUE on success, %FALSE on failure
+ * Returns: %TRUE on success, %FALSE on failure
  **/
 gboolean
 g_input_stream_close (GInputStream  *stream,
@@ -546,9 +541,10 @@ async_ready_close_callback_wrapper (GObject      *source_object,
 /**
  * g_input_stream_read_async:
  * @stream: A #GInputStream.
- * @buffer: a buffer to read data into (which should be at least count bytes long).
+ * @buffer: (array length=count) (element-type guint8): a buffer to
+ *     read data into (which should be at least count bytes long).
  * @count: the number of bytes that will be read from the stream
- * @io_priority: the <link linkend="io-priority">I/O priority</link> 
+ * @io_priority: the [I/O priority][io-priority]
  * of the request. 
  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
  * @callback: (scope async): callback to call when the request is satisfied
@@ -695,8 +691,7 @@ read_bytes_callback (GObject      *stream,
  * g_input_stream_read_bytes_async:
  * @stream: A #GInputStream.
  * @count: the number of bytes that will be read from the stream
- * @io_priority: the <link linkend="io-priority">I/O priority</link>
- *   of the request.
+ * @io_priority: the [I/O priority][io-priority] of the request
  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
  * @callback: (scope async): callback to call when the request is satisfied
  * @user_data: (closure): the data to pass to callback function
@@ -721,6 +716,8 @@ read_bytes_callback (GObject      *stream,
  * Any outstanding I/O request with higher priority (lower numerical
  * value) will be executed before an outstanding request with lower
  * priority. Default priority is %G_PRIORITY_DEFAULT.
+ *
+ * Since: 2.34
  **/
 void
 g_input_stream_read_bytes_async (GInputStream          *stream,
@@ -752,6 +749,8 @@ g_input_stream_read_bytes_async (GInputStream          *stream,
  * Finishes an asynchronous stream read-into-#GBytes operation.
  *
  * Returns: the newly-allocated #GBytes, or %NULL on error
+ *
+ * Since: 2.34
  **/
 GBytes *
 g_input_stream_read_bytes_finish (GInputStream  *stream,
@@ -768,8 +767,7 @@ g_input_stream_read_bytes_finish (GInputStream  *stream,
  * g_input_stream_skip_async:
  * @stream: A #GInputStream.
  * @count: the number of bytes that will be skipped from the stream
- * @io_priority: the <link linkend="io-priority">I/O priority</link>
- * of the request.
+ * @io_priority: the [I/O priority][io-priority] of the request
  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
  * @callback: (scope async): callback to call when the request is satisfied
  * @user_data: (closure): the data to pass to callback function
@@ -880,8 +878,7 @@ g_input_stream_skip_finish (GInputStream  *stream,
 /**
  * g_input_stream_close_async:
  * @stream: A #GInputStream.
- * @io_priority: the <link linkend="io-priority">I/O priority</link> 
- * of the request. 
+ * @io_priority: the [I/O priority][io-priority] of the request
  * @cancellable: (allow-none): optional cancellable object
  * @callback: (scope async): callback to call when the request is satisfied
  * @user_data: (closure): the data to pass to callback function
@@ -1007,7 +1004,7 @@ g_input_stream_has_pending (GInputStream *stream)
  * already set or @stream is closed, it will return %FALSE and set
  * @error.
  *
- * Return value: %TRUE if pending was previously unset and is now set.
+ * Returns: %TRUE if pending was previously unset and is now set.
  **/
 gboolean
 g_input_stream_set_pending (GInputStream *stream, GError **error)
@@ -1049,6 +1046,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    *
  ********************************************/
@@ -1136,10 +1155,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,
@@ -1160,7 +1175,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);
@@ -1231,7 +1246,7 @@ skip_callback_wrapper (GObject      *source_object,
                              data->buffer, MIN (8192, data->count),
                              g_task_get_priority (task),
                              g_task_get_cancellable (task),
-                             skip_callback_wrapper, data);
+                             skip_callback_wrapper, task);
          return;
        }
     }
@@ -1268,8 +1283,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
@@ -1290,7 +1304,7 @@ g_input_stream_real_skip_async (GInputStream        *stream,
       g_task_set_task_data (task, data, g_free);
       g_task_set_check_cancellable (task, FALSE);
       class->read_async (stream, data->buffer, MIN (8192, count), io_priority, cancellable,
-                        skip_callback_wrapper, data);
+                        skip_callback_wrapper, task);
     }
 
 }