[gparted/+psusi/refactor: 1/19] Remove gdk_threads_enter/exit (#685740)



commit 66f2bf70875ce36c8322e3b16d116d5ca4127b2a
Author: Phillip Susi <psusi ubuntu com>
Date:   Mon Jan 14 20:57:34 2013 -0500

    Remove gdk_threads_enter/exit (#685740)
    
    Use of these functions is depreciated and making gtk calls in a background
    thread still sometimes causes deadlocks or crashes.  Change ped exception
    handler to instead use an idle function to force the main thread to display
    the dialog box.
    
    Part of Bug 685740 - Refactor to use asynchronous command execution

 src/Dialog_Progress.cc |    2 -
 src/GParted_Core.cc    |   57 +++++++++++++++++++++++++++++++++--------------
 src/Win_GParted.cc     |    2 -
 src/main.cc            |    2 -
 4 files changed, 40 insertions(+), 23 deletions(-)
---
diff --git a/src/Dialog_Progress.cc b/src/Dialog_Progress.cc
index 253a378..dd314e8 100644
--- a/src/Dialog_Progress.cc
+++ b/src/Dialog_Progress.cc
@@ -221,9 +221,7 @@ void Dialog_Progress::on_signal_show()
                        
                        while ( Gtk::Main::events_pending() )
                                Gtk::Main::iteration();
-                       gdk_threads_leave();
                        usleep( 100000 ) ;
-                       gdk_threads_enter();
                }
 
                //set status (succes/error) for this operation
diff --git a/src/GParted_Core.cc b/src/GParted_Core.cc
index fd8eaa9..5e174b3 100644
--- a/src/GParted_Core.cc
+++ b/src/GParted_Core.cc
@@ -3545,32 +3545,55 @@ public:
                }
 };
 
-PedExceptionOption GParted_Core::ped_exception_handler( PedException * e ) 
+struct ped_exception_ctx {
+       PedExceptionOption ret;
+       PedException *e;
+       Glib::Threads::Mutex mutex;
+       Glib::Threads::Cond cond;
+};
+
+static bool _ped_exception_handler( struct ped_exception_ctx *ctx )
 {
-       PedExceptionOption ret = PED_EXCEPTION_UNHANDLED;
-        std::cout << e ->message << std::endl ;
+        std::cout << ctx->e->message << std::endl;
 
-        libparted_messages .push_back( e->message ) ;
+        libparted_messages.push_back( ctx->e->message );
        char optcount = 0;
        int opt = 0;
        for( char c = 0; c < 10; c++ )
-               if( e->options & (1 << c) ) {
+               if( ctx->e->options & (1 << c) ) {
                        opt = (1 << c);
                        optcount++;
                }
        // if only one option was given, choose it without popup
-       if( optcount == 1 && e->type != PED_EXCEPTION_BUG && e->type != PED_EXCEPTION_FATAL )
-               return (PedExceptionOption)opt;
-       if (Glib::Thread::self() != GParted_Core::mainthread)
-               gdk_threads_enter();
-       PedExceptionMsg msg( *e );
+       if( optcount == 1 && ctx->e->type != PED_EXCEPTION_BUG && ctx->e->type != PED_EXCEPTION_FATAL )
+       {
+               ctx->ret = (PedExceptionOption)opt;
+               ctx->cond.signal();
+               return false;
+       }
+       PedExceptionMsg msg( *ctx->e );
        msg.show_all();
-       ret = (PedExceptionOption)msg.run();
-       if (Glib::Thread::self() != GParted_Core::mainthread)
-               gdk_threads_leave();
-       if (ret < 0)
-               ret = PED_EXCEPTION_UNHANDLED;
-       return ret;
+       ctx->ret = (PedExceptionOption)msg.run();
+       if (ctx->ret < 0)
+               ctx->ret = PED_EXCEPTION_UNHANDLED;
+       ctx->mutex.lock();
+       ctx->cond.signal();
+       ctx->mutex.unlock();
+       return false;
+}
+
+PedExceptionOption GParted_Core::ped_exception_handler( PedException * e )
+{
+       struct ped_exception_ctx ctx;
+       ctx.ret = PED_EXCEPTION_UNHANDLED;
+       ctx.e = e;
+       if (Glib::Thread::self() != GParted_Core::mainthread) {
+               ctx.mutex.lock();
+               g_idle_add( (GSourceFunc)_ped_exception_handler, &ctx );
+               ctx.cond.wait( ctx.mutex );
+               ctx.mutex.unlock();
+       } else _ped_exception_handler( &ctx );
+       return ctx.ret;
 }
 
 GParted_Core::~GParted_Core() 
@@ -3578,5 +3601,5 @@ GParted_Core::~GParted_Core()
 }
 
 Glib::Thread *GParted_Core::mainthread;
-       
+
 } //GParted
diff --git a/src/Win_GParted.cc b/src/Win_GParted.cc
index ed756e4..0c8302d 100644
--- a/src/Win_GParted.cc
+++ b/src/Win_GParted.cc
@@ -627,9 +627,7 @@ void Win_GParted::show_pulsebar( const Glib::ustring & status_message )
                pulsebar .pulse();
                while ( Gtk::Main::events_pending() )
                        Gtk::Main::iteration();
-               gdk_threads_leave();
                usleep( 100000 );
-               gdk_threads_enter();
                Glib::ustring tmp_msg = gparted_core .get_thread_status_message() ;
                if ( tmp_msg != "" )
                        statusbar .push( tmp_msg ) ;
diff --git a/src/main.cc b/src/main.cc
index 02118d3..7f611fc 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -25,8 +25,6 @@ int main( int argc, char *argv[] )
 {
        //initialize thread system
        Glib::thread_init() ;
-       gdk_threads_init();
-       gdk_threads_enter();
        GParted::GParted_Core::mainthread = Glib::Thread::self();
        
        Gtk::Main kit( argc, argv ) ;


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