[gparted/psusi/refactor: 2/19] Switch Dialog_Progress to use Glib thread instead of pthread



commit d0c2672cce9213628756098dd89b9b3a0667f811
Author: Phillip Susi <psusi ubuntu com>
Date:   Tue Jan 24 20:15:20 2012 -0500

    Switch Dialog_Progress to use Glib thread instead of pthread
    
    Dialog_Progress was using pthread_create() so that it could later
    pthread_cancel() the thread.  pthread_cancel() is wildly unsafe and full
    of errors.  Changed to use Glib's threads like the rest, and only cancel
    between operations.  Because it can take some time to cancel, disable
    the cancel button once it has been clicked once.

 include/Dialog_Progress.h |    4 ++--
 src/Dialog_Progress.cc    |   28 ++++++++++++----------------
 2 files changed, 14 insertions(+), 18 deletions(-)
---
diff --git a/include/Dialog_Progress.h b/include/Dialog_Progress.h
index e3be49e..1337550 100644
--- a/include/Dialog_Progress.h
+++ b/include/Dialog_Progress.h
@@ -50,7 +50,7 @@ private:
 	void on_signal_show() ;
 	void on_expander_changed() ;
 	void on_cell_data_description( Gtk::CellRenderer * renderer, const Gtk::TreeModel::iterator & iter) ;
-	static void *static_pthread_apply_operation( void * p_dialog_progress ) ;
+	void thread_apply_operation();
 	void on_cancel() ;
 	void on_save() ;
 	void echo_operation_details( const OperationDetail & operation_detail, std::ofstream & out ) ;
@@ -65,6 +65,7 @@ private:
 	Gtk::TreeRow treerow ;
 	Gtk::ScrolledWindow scrolledwindow ;
 	Gtk::Expander expander_details ;
+	Gtk::Button *cancelbutton;
 	
 	Glib::RefPtr<Gdk::Pixbuf> icon_execute ;
 	Glib::RefPtr<Gdk::Pixbuf> icon_succes ;
@@ -92,7 +93,6 @@ private:
 	std::vector<Operation *> operations ;
 	OperationDetail operationdetail ;
 	bool running, succes, cancel, pulse ;
-	pthread_t pthread ;
 	double fraction ;
 	unsigned int t, warnings ;
 
diff --git a/src/Dialog_Progress.cc b/src/Dialog_Progress.cc
index 253a378..3b89c83 100644
--- a/src/Dialog_Progress.cc
+++ b/src/Dialog_Progress.cc
@@ -110,7 +110,7 @@ Dialog_Progress::Dialog_Progress( const std::vector<Operation *> & operations )
 		vbox ->set_spacing(5);
 	}
 
-	this ->add_button( Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL ) ;
+	cancelbutton = this ->add_button( Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL );
 	
 	this ->signal_show() .connect( sigc::mem_fun(*this, &Dialog_Progress::on_signal_show) );
 	this ->show_all_children() ;
@@ -212,7 +212,9 @@ void Dialog_Progress::on_signal_show()
 		
 		//and start..
 		running = true ;
-		pthread_create( & pthread, NULL, Dialog_Progress::static_pthread_apply_operation, this );
+		Glib::Thread::create( sigc::mem_fun(
+					*this, &Dialog_Progress::thread_apply_operation ),
+				      false );
 
 		while ( running )
 		{
@@ -234,8 +236,8 @@ void Dialog_Progress::on_signal_show()
 	this ->add_button( _("_Save Details"), Gtk::RESPONSE_OK ) ; //there's no enum for SAVE
 	
 	//replace 'cancel' with 'close'
-	std::vector<Gtk::Widget *> children = this ->get_action_area() ->get_children() ;
-	this ->get_action_area() ->remove( * children .back() ) ;
+	delete cancelbutton;
+	cancelbutton = 0;
 	this ->add_button( Gtk::Stock::CLOSE, Gtk::RESPONSE_CLOSE );
 
 	if ( cancel )
@@ -301,15 +303,10 @@ void Dialog_Progress::on_cell_data_description( Gtk::CellRenderer * renderer, co
 		static_cast<Gtk::TreeRow>( *iter )[ treeview_operations_columns .operation_description ] ;
 }
 
-void * Dialog_Progress::static_pthread_apply_operation( void * p_dialog_progress ) 
+void Dialog_Progress::thread_apply_operation()
 {
-	Dialog_Progress *dp = static_cast<Dialog_Progress *>( p_dialog_progress ) ;
-	
-	dp ->succes = dp ->signal_apply_operation .emit( dp ->operations[ dp ->t ] ) ;
-	
-	dp ->running = false ;
-
-	return NULL ;
+	succes = signal_apply_operation.emit( operations[t] );
+	running = false;
 }
 
 void Dialog_Progress::on_cancel()
@@ -328,10 +325,8 @@ void Dialog_Progress::on_cancel()
 	
 	if ( dialog .run() == Gtk::RESPONSE_CANCEL )
 	{
-		pthread_cancel( pthread ) ;
-		cancel = true ;
-		running = false ;
-		succes = false ;
+		cancel = true;
+		cancelbutton->set_sensitive( false );
 	}
 }
 
@@ -489,6 +484,7 @@ bool Dialog_Progress::on_delete_event( GdkEventAny * event )
 
 Dialog_Progress::~Dialog_Progress()
 {
+	delete cancelbutton;
 }
 
 



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