mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-08-02 07:23:41 +02:00
gio: port basic I/O classes from GSimpleAsyncResult to GTask
https://bugzilla.gnome.org/show_bug.cgi?id=661767
This commit is contained in:
175
gio/giostream.c
175
gio/giostream.c
@@ -27,9 +27,8 @@
|
||||
#include "glibintl.h"
|
||||
|
||||
#include "giostream.h"
|
||||
#include <gio/gsimpleasyncresult.h>
|
||||
#include <gio/gasyncresult.h>
|
||||
|
||||
#include "gasyncresult.h"
|
||||
#include "gtask.h"
|
||||
|
||||
G_DEFINE_ABSTRACT_TYPE (GIOStream, g_io_stream, G_TYPE_OBJECT);
|
||||
|
||||
@@ -465,28 +464,26 @@ g_io_stream_close_async (GIOStream *stream,
|
||||
gpointer user_data)
|
||||
{
|
||||
GIOStreamClass *class;
|
||||
GSimpleAsyncResult *simple;
|
||||
GError *error = NULL;
|
||||
|
||||
g_return_if_fail (G_IS_IO_STREAM (stream));
|
||||
|
||||
if (stream->priv->closed)
|
||||
{
|
||||
simple = g_simple_async_result_new (G_OBJECT (stream),
|
||||
callback,
|
||||
user_data,
|
||||
g_io_stream_close_async);
|
||||
g_simple_async_result_complete_in_idle (simple);
|
||||
g_object_unref (simple);
|
||||
GTask *task;
|
||||
|
||||
task = g_task_new (stream, cancellable, callback, user_data);
|
||||
g_task_set_source_tag (task, g_io_stream_close_async);
|
||||
g_task_return_boolean (task, TRUE);
|
||||
g_object_unref (task);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!g_io_stream_set_pending (stream, &error))
|
||||
{
|
||||
g_simple_async_report_take_gerror_in_idle (G_OBJECT (stream),
|
||||
callback,
|
||||
user_data,
|
||||
error);
|
||||
g_task_report_error (stream, callback, user_data,
|
||||
g_io_stream_close_async,
|
||||
error);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -523,10 +520,7 @@ g_io_stream_close_finish (GIOStream *stream,
|
||||
if (g_async_result_legacy_propagate_error (result, error))
|
||||
return FALSE;
|
||||
else if (g_async_result_is_tagged (result, g_io_stream_close_async))
|
||||
{
|
||||
/* Special case already closed */
|
||||
return TRUE;
|
||||
}
|
||||
return g_task_propagate_boolean (G_TASK (result), error);
|
||||
|
||||
class = G_IO_STREAM_GET_CLASS (stream);
|
||||
return class->close_finish (stream, result, error);
|
||||
@@ -534,25 +528,30 @@ g_io_stream_close_finish (GIOStream *stream,
|
||||
|
||||
|
||||
static void
|
||||
close_async_thread (GSimpleAsyncResult *res,
|
||||
GObject *object,
|
||||
GCancellable *cancellable)
|
||||
close_async_thread (GTask *task,
|
||||
gpointer source_object,
|
||||
gpointer task_data,
|
||||
GCancellable *cancellable)
|
||||
{
|
||||
GIOStream *stream = source_object;
|
||||
GIOStreamClass *class;
|
||||
GError *error = NULL;
|
||||
gboolean result;
|
||||
|
||||
/* Auto handling of cancelation disabled, and ignore cancellation,
|
||||
* since we want to close things anyway, although possibly in a
|
||||
* quick-n-dirty way. At least we never want to leak open handles
|
||||
*/
|
||||
class = G_IO_STREAM_GET_CLASS (object);
|
||||
class = G_IO_STREAM_GET_CLASS (stream);
|
||||
if (class->close_fn)
|
||||
{
|
||||
result = class->close_fn (G_IO_STREAM (object), cancellable, &error);
|
||||
result = class->close_fn (stream,
|
||||
g_task_get_cancellable (task),
|
||||
&error);
|
||||
if (!result)
|
||||
g_simple_async_result_take_error (res, error);
|
||||
{
|
||||
g_task_return_error (task, error);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
g_task_return_boolean (task, TRUE);
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -562,20 +561,14 @@ g_io_stream_real_close_async (GIOStream *stream,
|
||||
GAsyncReadyCallback callback,
|
||||
gpointer user_data)
|
||||
{
|
||||
GSimpleAsyncResult *res;
|
||||
GTask *task;
|
||||
|
||||
res = g_simple_async_result_new (G_OBJECT (stream),
|
||||
callback,
|
||||
user_data,
|
||||
g_io_stream_real_close_async);
|
||||
|
||||
g_simple_async_result_set_handle_cancellation (res, FALSE);
|
||||
|
||||
g_simple_async_result_run_in_thread (res,
|
||||
close_async_thread,
|
||||
io_priority,
|
||||
cancellable);
|
||||
g_object_unref (res);
|
||||
task = g_task_new (stream, cancellable, callback, user_data);
|
||||
g_task_set_check_cancellable (task, FALSE);
|
||||
g_task_set_priority (task, io_priority);
|
||||
|
||||
g_task_run_in_thread (task, close_async_thread);
|
||||
g_object_unref (task);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
@@ -583,15 +576,9 @@ g_io_stream_real_close_finish (GIOStream *stream,
|
||||
GAsyncResult *result,
|
||||
GError **error)
|
||||
{
|
||||
GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
|
||||
g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
|
||||
|
||||
g_warn_if_fail (g_simple_async_result_get_source_tag (simple) ==
|
||||
g_io_stream_real_close_async);
|
||||
|
||||
if (g_simple_async_result_propagate_error (simple, error))
|
||||
return FALSE;
|
||||
|
||||
return TRUE;
|
||||
return g_task_propagate_boolean (G_TASK (result), error);
|
||||
}
|
||||
|
||||
typedef struct
|
||||
@@ -622,16 +609,20 @@ splice_context_free (SpliceContext *ctx)
|
||||
}
|
||||
|
||||
static void
|
||||
splice_complete (GSimpleAsyncResult *simple,
|
||||
SpliceContext *ctx)
|
||||
splice_complete (GTask *task,
|
||||
SpliceContext *ctx)
|
||||
{
|
||||
if (ctx->cancelled_id != 0)
|
||||
g_cancellable_disconnect (ctx->cancellable, ctx->cancelled_id);
|
||||
ctx->cancelled_id = 0;
|
||||
|
||||
if (ctx->error != NULL)
|
||||
g_simple_async_result_set_from_error (simple, ctx->error);
|
||||
g_simple_async_result_complete (simple);
|
||||
{
|
||||
g_task_return_error (task, ctx->error);
|
||||
ctx->error = NULL;
|
||||
}
|
||||
else
|
||||
g_task_return_boolean (task, TRUE);
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -639,13 +630,12 @@ splice_close_cb (GObject *iostream,
|
||||
GAsyncResult *res,
|
||||
gpointer user_data)
|
||||
{
|
||||
GSimpleAsyncResult *simple = user_data;
|
||||
SpliceContext *ctx;
|
||||
GTask *task = user_data;
|
||||
SpliceContext *ctx = g_task_get_task_data (task);
|
||||
GError *error = NULL;
|
||||
|
||||
g_io_stream_close_finish (G_IO_STREAM (iostream), res, &error);
|
||||
|
||||
ctx = g_simple_async_result_get_op_res_gpointer (simple);
|
||||
ctx->completed++;
|
||||
|
||||
/* Keep the first error that occurred */
|
||||
@@ -656,9 +646,9 @@ splice_close_cb (GObject *iostream,
|
||||
|
||||
/* If all operations are done, complete now */
|
||||
if (ctx->completed == 4)
|
||||
splice_complete (simple, ctx);
|
||||
splice_complete (task, ctx);
|
||||
|
||||
g_object_unref (simple);
|
||||
g_object_unref (task);
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -666,13 +656,12 @@ splice_cb (GObject *ostream,
|
||||
GAsyncResult *res,
|
||||
gpointer user_data)
|
||||
{
|
||||
GSimpleAsyncResult *simple = user_data;
|
||||
SpliceContext *ctx;
|
||||
GTask *task = user_data;
|
||||
SpliceContext *ctx = g_task_get_task_data (task);
|
||||
GError *error = NULL;
|
||||
|
||||
g_output_stream_splice_finish (G_OUTPUT_STREAM (ostream), res, &error);
|
||||
|
||||
ctx = g_simple_async_result_get_op_res_gpointer (simple);
|
||||
ctx->completed++;
|
||||
|
||||
/* ignore cancellation error if it was not requested by the user */
|
||||
@@ -706,32 +695,40 @@ splice_cb (GObject *ostream,
|
||||
|
||||
/* Close the IO streams if needed */
|
||||
if ((ctx->flags & G_IO_STREAM_SPLICE_CLOSE_STREAM1) != 0)
|
||||
g_io_stream_close_async (ctx->stream1, ctx->io_priority,
|
||||
ctx->op1_cancellable, splice_close_cb, g_object_ref (simple));
|
||||
{
|
||||
g_io_stream_close_async (ctx->stream1,
|
||||
g_task_get_priority (task),
|
||||
ctx->op1_cancellable,
|
||||
splice_close_cb, g_object_ref (task));
|
||||
}
|
||||
else
|
||||
ctx->completed++;
|
||||
|
||||
if ((ctx->flags & G_IO_STREAM_SPLICE_CLOSE_STREAM2) != 0)
|
||||
g_io_stream_close_async (ctx->stream2, ctx->io_priority,
|
||||
ctx->op2_cancellable, splice_close_cb, g_object_ref (simple));
|
||||
{
|
||||
g_io_stream_close_async (ctx->stream2,
|
||||
g_task_get_priority (task),
|
||||
ctx->op2_cancellable,
|
||||
splice_close_cb, g_object_ref (task));
|
||||
}
|
||||
else
|
||||
ctx->completed++;
|
||||
|
||||
/* If all operations are done, complete now */
|
||||
if (ctx->completed == 4)
|
||||
splice_complete (simple, ctx);
|
||||
splice_complete (task, ctx);
|
||||
}
|
||||
|
||||
g_object_unref (simple);
|
||||
g_object_unref (task);
|
||||
}
|
||||
|
||||
static void
|
||||
splice_cancelled_cb (GCancellable *cancellable,
|
||||
GSimpleAsyncResult *simple)
|
||||
splice_cancelled_cb (GCancellable *cancellable,
|
||||
GTask *task)
|
||||
{
|
||||
SpliceContext *ctx;
|
||||
|
||||
ctx = g_simple_async_result_get_op_res_gpointer (simple);
|
||||
ctx = g_task_get_task_data (task);
|
||||
g_cancellable_cancel (ctx->op1_cancellable);
|
||||
g_cancellable_cancel (ctx->op2_cancellable);
|
||||
}
|
||||
@@ -765,16 +762,17 @@ g_io_stream_splice_async (GIOStream *stream1,
|
||||
GAsyncReadyCallback callback,
|
||||
gpointer user_data)
|
||||
{
|
||||
GSimpleAsyncResult *simple;
|
||||
GTask *task;
|
||||
SpliceContext *ctx;
|
||||
GInputStream *istream;
|
||||
GOutputStream *ostream;
|
||||
|
||||
if (cancellable != NULL && g_cancellable_is_cancelled (cancellable))
|
||||
{
|
||||
g_simple_async_report_error_in_idle (NULL, callback,
|
||||
user_data, G_IO_ERROR, G_IO_ERROR_CANCELLED,
|
||||
"Operation has been cancelled");
|
||||
g_task_report_new_error (NULL, callback, user_data,
|
||||
g_io_stream_splice_async,
|
||||
G_IO_ERROR, G_IO_ERROR_CANCELLED,
|
||||
"Operation has been cancelled");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -782,21 +780,18 @@ g_io_stream_splice_async (GIOStream *stream1,
|
||||
ctx->stream1 = g_object_ref (stream1);
|
||||
ctx->stream2 = g_object_ref (stream2);
|
||||
ctx->flags = flags;
|
||||
ctx->io_priority = io_priority;
|
||||
ctx->op1_cancellable = g_cancellable_new ();
|
||||
ctx->op2_cancellable = g_cancellable_new ();
|
||||
ctx->completed = 0;
|
||||
|
||||
simple = g_simple_async_result_new (NULL, callback, user_data,
|
||||
g_io_stream_splice_finish);
|
||||
g_simple_async_result_set_op_res_gpointer (simple, ctx,
|
||||
(GDestroyNotify) splice_context_free);
|
||||
task = g_task_new (NULL, cancellable, callback, user_data);
|
||||
g_task_set_task_data (task, ctx, (GDestroyNotify) splice_context_free);
|
||||
|
||||
if (cancellable != NULL)
|
||||
{
|
||||
ctx->cancellable = g_object_ref (cancellable);
|
||||
ctx->cancelled_id = g_cancellable_connect (cancellable,
|
||||
G_CALLBACK (splice_cancelled_cb), g_object_ref (simple),
|
||||
G_CALLBACK (splice_cancelled_cb), g_object_ref (task),
|
||||
g_object_unref);
|
||||
}
|
||||
|
||||
@@ -804,15 +799,15 @@ g_io_stream_splice_async (GIOStream *stream1,
|
||||
ostream = g_io_stream_get_output_stream (stream2);
|
||||
g_output_stream_splice_async (ostream, istream, G_OUTPUT_STREAM_SPLICE_NONE,
|
||||
io_priority, ctx->op1_cancellable, splice_cb,
|
||||
g_object_ref (simple));
|
||||
g_object_ref (task));
|
||||
|
||||
istream = g_io_stream_get_input_stream (stream2);
|
||||
ostream = g_io_stream_get_output_stream (stream1);
|
||||
g_output_stream_splice_async (ostream, istream, G_OUTPUT_STREAM_SPLICE_NONE,
|
||||
io_priority, ctx->op2_cancellable, splice_cb,
|
||||
g_object_ref (simple));
|
||||
g_object_ref (task));
|
||||
|
||||
g_object_unref (simple);
|
||||
g_object_unref (task);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -831,17 +826,7 @@ gboolean
|
||||
g_io_stream_splice_finish (GAsyncResult *result,
|
||||
GError **error)
|
||||
{
|
||||
GSimpleAsyncResult *simple;
|
||||
g_return_val_if_fail (g_task_is_valid (result, NULL), FALSE);
|
||||
|
||||
g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (result), FALSE);
|
||||
|
||||
simple = G_SIMPLE_ASYNC_RESULT (result);
|
||||
|
||||
if (g_simple_async_result_propagate_error (simple, error))
|
||||
return FALSE;
|
||||
|
||||
g_return_val_if_fail (g_simple_async_result_is_valid (result, NULL,
|
||||
g_io_stream_splice_finish), FALSE);
|
||||
|
||||
return TRUE;
|
||||
return g_task_propagate_boolean (G_TASK (result), error);
|
||||
}
|
||||
|
Reference in New Issue
Block a user