[brasero/gnome-2-28] Use g_cond_timed_wait instead of sleep in thread to allow a more immediate thread cancellation



commit 196ad579d86bd1c9cde3d0fc41838a8d618cf9e1
Author: Philippe Rouquier <bonfire-app wanadoo fr>
Date:   Tue Sep 29 22:13:31 2009 +0200

    Use g_cond_timed_wait instead of sleep in thread to allow a more immediate thread cancellation

 libbrasero-media/brasero-drive.c  |   37 +++++++++++++++++++++++++++++++++++--
 libbrasero-media/brasero-medium.c |   26 +++++++++++++++++++++++++-
 2 files changed, 60 insertions(+), 3 deletions(-)
---
diff --git a/libbrasero-media/brasero-drive.c b/libbrasero-media/brasero-drive.c
index 514933b..2b1b45c 100644
--- a/libbrasero-media/brasero-drive.c
+++ b/libbrasero-media/brasero-drive.c
@@ -72,6 +72,7 @@ struct _BraseroDrivePrivate
 	GThread *probe;
 	GMutex *mutex;
 	GCond *cond;
+	GCond *cond_probe;
 	gint probe_id;
 
 	BraseroMedium *medium;
@@ -196,7 +197,14 @@ brasero_drive_cancel_probing (BraseroDrive *drive)
 
 	g_mutex_lock (priv->mutex);
 	if (priv->probe) {
+		/* This to signal that we are cancelling */
 		priv->probe_cancelled = TRUE;
+
+		/* This is to wake up the thread if it
+		 * was asleep waiting to retry to get
+		 * hold of a handle to probe the drive */
+		g_cond_signal (priv->cond_probe);
+
 		g_cond_wait (priv->cond, priv->mutex);
 	}
 	g_mutex_unlock (priv->mutex);
@@ -839,6 +847,7 @@ static gpointer
 brasero_drive_probe_inside_thread (gpointer data)
 {
 	gint counter = 0;
+	GTimeVal wait_time;
 	const gchar *device;
 	BraseroScsiErrCode code;
 	BraseroDrivePrivate *priv;
@@ -877,6 +886,9 @@ brasero_drive_probe_inside_thread (gpointer data)
 		goto end;
 	}
 
+	g_get_current_time (&wait_time);
+	g_time_val_add (&wait_time, 2000000);
+
 	while (brasero_spc1_test_unit_ready (handle, &code) != BRASERO_SCSI_OK) {
 		if (code == BRASERO_SCSI_NO_MEDIUM) {
 			BRASERO_MEDIA_LOG ("No medium inserted");
@@ -894,7 +906,14 @@ brasero_drive_probe_inside_thread (gpointer data)
 			goto end;
 		}
 
-		sleep (2);
+		g_get_current_time (&wait_time);
+		g_time_val_add (&wait_time, 2000000);
+
+		g_mutex_lock (priv->mutex);
+		g_cond_timed_wait (priv->cond_probe,
+		                   priv->mutex,
+		                   &wait_time);
+		g_mutex_unlock (priv->mutex);
 
 		if (priv->probe_cancelled) {
 			BRASERO_MEDIA_LOG ("Device probing cancelled");
@@ -1181,6 +1200,7 @@ static gpointer
 brasero_drive_probe_thread (gpointer data)
 {
 	gint counter = 0;
+	GTimeVal wait_time;
 	const gchar *device;
 	BraseroScsiResult res;
 	BraseroScsiInquiry hdr;
@@ -1231,7 +1251,14 @@ brasero_drive_probe_thread (gpointer data)
 			goto end;
 		}
 
-		sleep (2);
+		g_get_current_time (&wait_time);
+		g_time_val_add (&wait_time, 2000000);
+
+		g_mutex_lock (priv->mutex);
+		g_cond_timed_wait (priv->cond_probe,
+		                   priv->mutex,
+		                   &wait_time);
+		g_mutex_unlock (priv->mutex);
 
 		if (priv->probe_cancelled) {
 			brasero_device_handle_close (handle);
@@ -1405,6 +1432,7 @@ brasero_drive_init (BraseroDrive *object)
 
 	priv->mutex = g_mutex_new ();
 	priv->cond = g_cond_new ();
+	priv->cond_probe = g_cond_new ();
 }
 
 static void
@@ -1428,6 +1456,11 @@ brasero_drive_finalize (GObject *object)
 		priv->cond = NULL;
 	}
 
+	if (priv->cond_probe) {
+		g_cond_free (priv->cond_probe);
+		priv->cond_probe = NULL;
+	}
+
 	if (priv->medium) {
 		g_signal_emit (object,
 			       drive_signals [MEDIUM_REMOVED],
diff --git a/libbrasero-media/brasero-medium.c b/libbrasero-media/brasero-medium.c
index 4631418..6be5564 100644
--- a/libbrasero-media/brasero-medium.c
+++ b/libbrasero-media/brasero-medium.c
@@ -87,6 +87,8 @@ struct _BraseroMediumPrivate
 	GThread *probe;
 	GMutex *mutex;
 	GCond *cond;
+	GCond *cond_probe;
+
 	gint probe_id;
 
 	GSList *tracks;
@@ -2953,6 +2955,7 @@ static gpointer
 brasero_medium_probe_thread (gpointer self)
 {
 	gint counter = 0;
+	GTimeVal wait_time;
 	const gchar *device;
 	BraseroScsiErrCode code;
 	BraseroMediumPrivate *priv;
@@ -3007,7 +3010,14 @@ brasero_medium_probe_thread (gpointer self)
 			goto end;
 		}
 
-		sleep (2);
+		g_get_current_time (&wait_time);
+		g_time_val_add (&wait_time, 2000000);
+
+		g_mutex_lock (priv->mutex);
+		g_cond_timed_wait (priv->cond_probe,
+		                   priv->mutex,
+		                   &wait_time);
+		g_mutex_unlock (priv->mutex);
 
 		if (priv->probe_cancelled) {
 			BRASERO_MEDIA_LOG ("Device probing cancelled");
@@ -3078,6 +3088,7 @@ brasero_medium_init (BraseroMedium *object)
 
 	priv->mutex = g_mutex_new ();
 	priv->cond = g_cond_new ();
+	priv->cond_probe = g_cond_new ();
 
 	/* we can't do anything here since properties haven't been set yet */
 }
@@ -3093,7 +3104,15 @@ brasero_medium_finalize (GObject *object)
 
 	g_mutex_lock (priv->mutex);
 	if (priv->probe) {
+		/* This to signal that we are cancelling */
 		priv->probe_cancelled = TRUE;
+
+		/* This is to wake up the thread if it
+		 * was asleep waiting to retry to get
+		 * hold of a handle to probe the drive */
+		g_cond_signal (priv->cond_probe);
+
+		/* Wait for the end of the thread */
 		g_cond_wait (priv->cond, priv->mutex);
 	}
 	g_mutex_unlock (priv->mutex);
@@ -3113,6 +3132,11 @@ brasero_medium_finalize (GObject *object)
 		priv->cond = NULL;
 	}
 
+	if (priv->cond_probe) {
+		g_cond_free (priv->cond_probe);
+		priv->cond_probe = NULL;
+	}
+
 	if (priv->id) {
 		g_free (priv->id);
 		priv->id = NULL;



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