Author: sh-sh-sh Date: Wed Aug 1 15:04:53 2007 New Revision: 39871 URL: http://svn.opensuse.org/viewcvs/yast?rev=39871&view=rev Log: generic thread handling Added: branches/tmp/sh/qt4-port/core/libyui/src/YUIThreadHandler.cc branches/tmp/sh/qt4-port/core/libyui/src/YUIThreadHandler.h Modified: branches/tmp/sh/qt4-port/core/libyui/src/Makefile.am branches/tmp/sh/qt4-port/core/libyui/src/YUI.h branches/tmp/sh/qt4-port/core/libyui/src/YUI_core.cc Modified: branches/tmp/sh/qt4-port/core/libyui/src/Makefile.am URL: http://svn.opensuse.org/viewcvs/yast/branches/tmp/sh/qt4-port/core/libyui/src/Makefile.am?rev=39871&r1=39870&r2=39871&view=diff ============================================================================== --- branches/tmp/sh/qt4-port/core/libyui/src/Makefile.am (original) +++ branches/tmp/sh/qt4-port/core/libyui/src/Makefile.am Wed Aug 1 15:04:53 2007 @@ -17,6 +17,7 @@ YUI_special_widgets.cc \ YUI_bindings.cc \ YUI_util.cc \ + YUIThreadHandler.cc \ YUIPlugin.cc \ \ YAlignment.cc \ @@ -76,6 +77,7 @@ YUI_util.h \ UI.h \ YUISymbols.h \ + YUIThreadHandler.h \ YUIPlugin.h \ YPackageSelectorPlugin.h \ \ Modified: branches/tmp/sh/qt4-port/core/libyui/src/YUI.h URL: http://svn.opensuse.org/viewcvs/yast/branches/tmp/sh/qt4-port/core/libyui/src/YUI.h?rev=39871&r1=39870&r2=39871&view=diff ============================================================================== --- branches/tmp/sh/qt4-port/core/libyui/src/YUI.h (original) +++ branches/tmp/sh/qt4-port/core/libyui/src/YUI.h Wed Aug 1 15:04:53 2007 @@ -51,6 +51,7 @@ class YTree; class YTreeItem; class Y2Component; +class YUIThreadHandler; typedef struct @@ -90,6 +91,9 @@ * or override @ref #pollInput and @ref #waitForEvent, whichever is * easier for you. * + * Derived classes can pass their own YUIThreadHandler. 0 means the standard + * YUIThreadHandler based on "pthread" will be used. + * * This class is an abstract base class that contains pure virtuals. * It is not intended for direct instantiation, only for inheritance. */ @@ -99,7 +103,7 @@ /** * Constructor. */ - YUI( bool with_threads ); + YUI( bool with_threads, YUIThreadHandler * uiThreadHandler = 0 ); public: @@ -366,9 +370,6 @@ protected: - - - const char *moduleName(); // Event handling, execution flow @@ -390,6 +391,15 @@ virtual void idleLoop( int fd_ycp ); /** + * Set a new UI thread handler. UI thread handling is moved to a separate + * class and object so it can be exchanged in a derived class's constructor + * (virtual functions can't be used in constructors or destructors). + * + * Any previously existing UI thread handler will be deleted. + **/ + virtual void setUIThreadHandler( YUIThreadHandler * newHandler ); + + /** * UI-specific UserInput() method. * * This is called upon evaluating the UI::UserInput() builtin command. @@ -1002,7 +1012,6 @@ /** * Creates and launches the ui thread. */ - void createUIThread(); friend void *start_ui_thread( void *ui_int ); /** @@ -1597,6 +1606,11 @@ **/ Y2Component * _callback; + /** + * Handler for UI threads. + **/ + YUIThreadHandler * _uiThreadHandler; + /** * Global reference to the UI Added: branches/tmp/sh/qt4-port/core/libyui/src/YUIThreadHandler.cc URL: http://svn.opensuse.org/viewcvs/yast/branches/tmp/sh/qt4-port/core/libyui/src/YUIThreadHandler.cc?rev=39871&view=auto ============================================================================== --- branches/tmp/sh/qt4-port/core/libyui/src/YUIThreadHandler.cc (added) +++ branches/tmp/sh/qt4-port/core/libyui/src/YUIThreadHandler.cc Wed Aug 1 15:04:53 2007 @@ -0,0 +1,58 @@ +/*---------------------------------------------------------------------\ +| | +| __ __ ____ _____ ____ | +| \ \ / /_ _/ ___|_ _|___ \ | +| \ V / _` \___ \ | | __) | | +| | | (_| |___) || | / __/ | +| |_|\__,_|____/ |_| |_____| | +| | +| core system | +| (C) SuSE GmbH | +\----------------------------------------------------------------------/ + + File: YUIThreadHandler.h + + Authors: Stefan Hundhammer <sh@suse.de> + +/-*/ + +#define y2log_component "ui" +#include <ycp/y2log.h> + +#include "YUIThreadHandler.h" + + +YUIThreadHandler::YUIThreadHandler( voidPtrFunction startFunc, + void * startFuncArg ) + : _startFunc( startFunc ) + , _startFuncArg( startFuncArg ) + , _uiThreadRunning( false ) +{ +} + + +YUIThreadHandler::~YUIThreadHandler() +{ + if ( _uiThreadRunning ) + y2error( "Call terminateUIThread() before the destructor!" ); +} + + +void YUIThreadHandler::startUIThread() +{ + pthread_attr_t attr; + pthread_attr_init( &attr ); + + y2debug( "Starting UI thread" ); + pthread_create( &_uiThread, &attr, _startFunc, _startFuncArg ); + _uiThreadRunning = true; +} + + +void YUIThreadHandler::terminateUIThread() +{ + y2debug( "Terminating UI thread" ); + pthread_join( _uiThread, 0 ); + _uiThreadRunning = false; +} + Added: branches/tmp/sh/qt4-port/core/libyui/src/YUIThreadHandler.h URL: http://svn.opensuse.org/viewcvs/yast/branches/tmp/sh/qt4-port/core/libyui/src/YUIThreadHandler.h?rev=39871&view=auto ============================================================================== --- branches/tmp/sh/qt4-port/core/libyui/src/YUIThreadHandler.h (added) +++ branches/tmp/sh/qt4-port/core/libyui/src/YUIThreadHandler.h Wed Aug 1 15:04:53 2007 @@ -0,0 +1,78 @@ +/*---------------------------------------------------------------------\ +| | +| __ __ ____ _____ ____ | +| \ \ / /_ _/ ___|_ _|___ \ | +| \ V / _` \___ \ | | __) | | +| | | (_| |___) || | / __/ | +| |_|\__,_|____/ |_| |_____| | +| | +| core system | +| (C) SuSE GmbH | +\----------------------------------------------------------------------/ + + File: YUIThreadHandler.h + + Authors: Stefan Hundhammer <sh@suse.de> + +/-*/ + +#ifndef YUIThreadHandler_h +#define YUIThreadHandler_h + +#include <pthread.h> + + +// Function that accepts a void * and returns a void * +typedef void * (*voidPtrFunction) (void * arg); + +/** + * Helper class to manage UI threads based on pthread. + **/ +class YUIThreadHandler +{ +public: + + /** + * Constructor. Takes the function to be started upon startThread() and the + * sole argument to that function. The constructor just stores both + * parameters; it will neither create a thread nor call the function. + * Use startThread() for that at some later time. + **/ + YUIThreadHandler( voidPtrFunction startFunc, void * startFuncArg ); + + /** + * Destructor. + * + * Notice that this will NOT call terminateUIThread() since + * virtual functions can't be used (as virtual functions) in the + * destructor. Remember to call terminateUIThread() manually. + **/ + virtual ~YUIThreadHandler(); + + /*** + * Start the UI thread with the function (and the argument to that + * function) passed in the constructor. + **/ + virtual void startUIThread(); + + /** + * Terminate the UI thread if it running. + **/ + virtual void terminateUIThread(); + + /** + * Return 'true' if the UI thread is currently running. + **/ + bool uiThreadRunning() const { return _uiThreadRunning; } + + +protected: + + voidPtrFunction _startFunc; + void * _startFuncArg; + pthread_t _uiThread; + bool _uiThreadRunning; +}; + + +#endif // YUIThreadHandler_h Modified: branches/tmp/sh/qt4-port/core/libyui/src/YUI_core.cc URL: http://svn.opensuse.org/viewcvs/yast/branches/tmp/sh/qt4-port/core/libyui/src/YUI_core.cc?rev=39871&r1=39870&r2=39871&view=diff ============================================================================== --- branches/tmp/sh/qt4-port/core/libyui/src/YUI_core.cc (original) +++ branches/tmp/sh/qt4-port/core/libyui/src/YUI_core.cc Wed Aug 1 15:04:53 2007 @@ -32,7 +32,6 @@ #include <fcntl.h> // fcntl() #include <errno.h> #include <locale.h> // setlocale() -#include <pthread.h> #include <assert.h> #include <string.h> @@ -42,6 +41,7 @@ #include "YUI.h" #include "YUISymbols.h" +#include "YUIThreadHandler.h" #include "YWidget.h" #include "YDialog.h" @@ -63,7 +63,7 @@ extern void *start_ui_thread( void * yui ); -YUI::YUI( bool with_threads ) +YUI::YUI( bool with_threads, YUIThreadHandler * uiThreadHandler ) : id_counter(0) , with_threads( with_threads ) , _moduleName( "yast2" ) @@ -72,8 +72,12 @@ , macroPlayer (0) , _events_blocked( false ) , _callback( 0 ) + , _uiThreadHandler( uiThreadHandler ) { _yui = this; + + if ( with_threads && ! _uiThreadHandler ) + _uiThreadHandler = new YUIThreadHandler( start_ui_thread, this ); } @@ -101,6 +105,9 @@ removeDialog(); } + if ( _uiThreadHandler ) + delete _uiThreadHandler; + deleteMacroRecorder(); deleteMacroPlayer(); } @@ -146,7 +153,9 @@ else { terminate_ui_thread = false; - createUIThread(); + + if ( _uiThreadHandler ) + _uiThreadHandler->startUIThread(); } } else @@ -156,21 +165,22 @@ } -void YUI::createUIThread() +void YUI::terminateUIThread() { - pthread_attr_t attr; - pthread_attr_init( & attr ); - pthread_create( & ui_thread, & attr, start_ui_thread, this ); + terminate_ui_thread = true; + signalUIThread(); + + if ( _uiThreadHandler ) + _uiThreadHandler->terminateUIThread(); } -void YUI::terminateUIThread() +void YUI::setUIThreadHandler( YUIThreadHandler * newHandler ) { - y2debug( "Telling UI thread to shut down" ); - terminate_ui_thread = true; - signalUIThread(); - pthread_join( ui_thread, 0 ); - y2debug( "UI thread shut down correctly" ); + if ( _uiThreadHandler ) + delete _uiThreadHandler; + + _uiThreadHandler = newHandler; } @@ -425,16 +435,16 @@ // ---------------------------------------------------------------------- -void *start_ui_thread( void * yui ) +void *start_ui_thread( void * ui ) { - YUI * ui= (YUI *) yui; - #if VERBOSE_COMM y2debug( "Starting UI thread" ); #endif - if ( ui ) - ui->uiThreadMainLoop(); + YUI * yui = (YUI *) ui; + + if ( yui ) + yui->uiThreadMainLoop(); return 0; } -- To unsubscribe, e-mail: yast-commit+unsubscribe@opensuse.org For additional commands, e-mail: yast-commit+help@opensuse.org