[libgdata] core: Cancel gdata_upload_stream_write() if the entire upload is cancelled



commit 89fbed575e1e22c3e68b9bbc1cc4cb93b9800e21
Author: Philip Withnall <philip tecnocode co uk>
Date:   Fri Dec 17 23:11:19 2010 +0000

    core: Cancel gdata_upload_stream_write() if the entire upload is cancelled
    
    Helps: bgo#637036

 gdata/gdata-upload-stream.c |   34 ++++++++++++++++++----------------
 1 files changed, 18 insertions(+), 16 deletions(-)
---
diff --git a/gdata/gdata-upload-stream.c b/gdata/gdata-upload-stream.c
index 505f172..f684aee 100644
--- a/gdata/gdata-upload-stream.c
+++ b/gdata/gdata-upload-stream.c
@@ -400,31 +400,30 @@ gdata_upload_stream_write (GOutputStream *stream, const void *buffer, gsize coun
 {
 	GDataUploadStreamPrivate *priv = GDATA_UPLOAD_STREAM (stream)->priv;
 	gssize length_written = -1;
-	gulong cancelled_signal = 0;
+	gulong cancelled_signal = 0, global_cancelled_signal = 0;
 	gboolean cancelled = FALSE;
 	gsize old_network_bytes_written;
+	CancelledData data;
 
 	/* Listen for cancellation events */
-	if (cancellable != NULL) {
-		CancelledData data;
+	data.upload_stream = GDATA_UPLOAD_STREAM (stream);
+	data.cancelled = &cancelled;
 
-		data.upload_stream = GDATA_UPLOAD_STREAM (stream);
-		data.cancelled = &cancelled;
+	global_cancelled_signal = g_cancellable_connect (priv->cancellable, (GCallback) write_cancelled_cb, &data, NULL);
 
+	if (cancellable != NULL)
 		cancelled_signal = g_cancellable_connect (cancellable, (GCallback) write_cancelled_cb, &data, NULL);
-	}
 
 	/* Check for an error and return if necessary */
 	g_static_mutex_lock (&(priv->write_mutex));
 
 	if (cancelled == TRUE) {
-		g_assert (cancellable != NULL && g_cancellable_set_error_if_cancelled (cancellable, error) == TRUE);
+		g_assert (g_cancellable_set_error_if_cancelled (cancellable, error) == TRUE ||
+		          g_cancellable_set_error_if_cancelled (priv->cancellable, error) == TRUE);
 		g_static_mutex_unlock (&(priv->write_mutex));
 
-		if (cancelled_signal != 0)
-			g_cancellable_disconnect (cancellable, cancelled_signal);
-
-		return -1;
+		length_written = -1;
+		goto done;
 	}
 
 	/* Increment the number of bytes outstanding for the new write, and keep a record of the old number written so we know if the write's
@@ -469,9 +468,8 @@ gdata_upload_stream_write (GOutputStream *stream, const void *buffer, gsize coun
 	/* Create the thread and let the writing commence! */
 	create_network_thread (GDATA_UPLOAD_STREAM (stream), error);
 	if (priv->network_thread == NULL) {
-		if (cancelled_signal != 0)
-			g_cancellable_disconnect (cancellable, cancelled_signal);
-		return -1;
+		length_written = -1;
+		goto done;
 	}
 
 write:
@@ -490,16 +488,20 @@ write:
 		priv->response_error = NULL;
 		length_written = -1;
 	} else if (cancelled == TRUE && length_written < 1) {
-		g_assert (cancellable != NULL && g_cancellable_set_error_if_cancelled (cancellable, error) == TRUE);
+		g_assert (g_cancellable_set_error_if_cancelled (cancellable, error) == TRUE ||
+		          g_cancellable_set_error_if_cancelled (priv->cancellable, error) == TRUE);
 		length_written = -1;
 	}
 
 	g_static_mutex_unlock (&(priv->response_mutex));
 
-	/* Disconnect from the cancelled signal. Note that we have to do this with @response_mutex not held, as g_cancellable_disconnect() blocks
+done:
+	/* Disconnect from the cancelled signals. Note that we have to do this with @response_mutex not held, as g_cancellable_disconnect() blocks
 	 * until any outstanding cancellation callbacks return, and they will block on @response_mutex. */
 	if (cancelled_signal != 0)
 		g_cancellable_disconnect (cancellable, cancelled_signal);
+	if (global_cancelled_signal != 0)
+		g_cancellable_disconnect (priv->cancellable, global_cancelled_signal);
 
 	g_assert (cancelled == TRUE || length_written > 0);
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]