[gparted/psusi/refactor: 3/19] Switch Dialog_Progress to use Glib thread instead of pthread (#601239)



commit 45a80cfd690e078567276ac948a6656c9de44d77
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 (#601239)
    
    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.
    
    Bug 601239 - Please allow 'Cancel after current operation'

 include/Dialog_Progress.h |    4 ++--
 src/Dialog_Progress.cc    |   27 ++++++++++++---------------
 2 files changed, 14 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 6e3fe2f..822ac94 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,8 +217,11 @@ 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 );
+               Glib::Thread::create( sigc::mem_fun(
+                                       *this, &Dialog_Progress::thread_apply_operation ),
+                                     false );
                Gtk::Main::run();
+
                //set status (succes/error) for this operation
                operations[ t ] ->operation_detail .set_status( succes ? STATUS_SUCCES : STATUS_ERROR ) ;
        }
@@ -227,8 +230,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 );
 
        pulsetimer.disconnect();
@@ -302,15 +305,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 +327,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 +486,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]