[gparted/psusi/refactor: 3/7] Switch Dialog_Progress to use Glib thread instead of pthread
- From: Phillip Susi <psusi src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gparted/psusi/refactor: 3/7] Switch Dialog_Progress to use Glib thread instead of pthread
- Date: Sun, 20 Jan 2013 18:33:08 +0000 (UTC)
commit 78474309d7beece8b52155e47b1b86c42949fab6
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, 15 insertions(+), 17 deletions(-)
---
diff --git a/include/Dialog_Progress.h b/include/Dialog_Progress.h
index a580ddb..e4ede18 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 ) ;
@@ -66,6 +66,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 ;
@@ -93,7 +94,6 @@ private:
std::vector<Operation *> operations ;
OperationDetail operationdetail ;
bool succes, cancel;
- pthread_t pthread ;
double fraction ;
unsigned int t, warnings ;
sigc::connection pulsetimer;
diff --git a/src/Dialog_Progress.cc b/src/Dialog_Progress.cc
index 314fce4..2b4e02e 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() ;
@@ -217,10 +217,14 @@ void Dialog_Progress::on_signal_show()
treeview_operations .set_cursor( static_cast<Gtk::TreePath>( treerow ) ) ;
//and start..
- pthread_create( & pthread, NULL, Dialog_Progress::static_pthread_apply_operation, this );
+
pulsetimer = Glib::signal_timeout().connect( sigc::mem_fun(*this, &Dialog_Progress::pulsebar_pulse), 100 );
+ Glib::Thread::create( sigc::mem_fun(
+ *this, &Dialog_Progress::thread_apply_operation ),
+ false );
Gtk::Main::run();
pulsetimer.disconnect();
+
//set status (succes/error) for this operation
operations[ t ] ->operation_detail .set_status( succes ? STATUS_SUCCES : STATUS_ERROR ) ;
}
@@ -229,8 +233,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 )
@@ -302,15 +306,10 @@ static bool _mainquit( void *dummy )
return false;
}
-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 ] ) ;
-
+ succes = signal_apply_operation.emit( operations[t] );
g_idle_add( (GSourceFunc)_mainquit, NULL );
-
- return NULL ;
}
void Dialog_Progress::on_cancel()
@@ -329,10 +328,8 @@ void Dialog_Progress::on_cancel()
if ( dialog .run() == Gtk::RESPONSE_CANCEL )
{
- pthread_cancel( pthread ) ;
- cancel = true ;
- Gtk::Main::quit();
- succes = false ;
+ cancel = true;
+ cancelbutton->set_sensitive( false );
}
}
@@ -490,6 +487,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]