Effort/Make Dialogs Asynchronous

From Apache OpenOffice Wiki
< Effort
Revision as of 16:43, 19 April 2006 by Dkeskar (Talk | contribs)

Jump to: navigation, search

As we proceed with the UNO Threading Framework, it becomes necessary to fix asynchronous dialogs so as to avoid deadlocks. This page documents the background, necessary tasks and their status.

Modal Dialogs in Multi-threaded Environments =

  • Author: Kai Sommerfeld
  • Version: 1.0
  • Feb. 13, 2006

The Problem

Currently VCL dialogs are executed using synchronous Dialog::Execute(). Dialog::Execute() calls Application::Yield() periodically, until dialog is about to be ended. Today, Application::Yield() releases the VCL mutex (the Solar Mutex), which is wrong, because due to this a second thread can enter VCL while another thread is already within VCL (Dialog::Execute()). The logical consequence for a fix is that Application::Yield() must no longer release the VCL mutex. But then another problem arises.

The thread calling Dialog::Execute() will acquire the VCL mutex once it and not release it until Dialog::Execute() has finished. This means, that during the time a thread executes a dialog, no other thread can enter VCL. If a dialog implementation creates (or uses) a thread in order to obtain data it needs itself for being able to end sometimes and this thread needs to call back to VCL in order to fullfil its work (example: deliver search results to a VCL control contained in a search dialog), this will no longer work. The worker thread will never be able to enter VCL and therefore never end. The dialog thread, staying in VCL all the time, will never get results from the worker thread and therefore never end.

Unfortunately, in the current OOo code base there are some dialogs that suffer from the problem just described.

The demand to fix those dialogs soon comes from the upcoming UNO Threading Framework. There, the Solar Mutex will be retired and replaced by another global mutex that will be used to synchronize access to all non-threadsafe components (incl. non-threadsafe UNO components). Additionally, Application::Yield() will be fixed not to release this new mutex for the reasons discussed earlier.

The Solution

We want to change the dialog execution model to an asynchronous one. The code that wants to display a dialog simply triggers execution and enters VCL just for a short time, leaves VCL immediately after having triggered the dialog. Results will be delivered asynchronously using a callback.

Unfortunately, we also have chosen the broken dialog API design for the UNO API that models dialogs. We need to introduce a new API for UNO dialogs as well and to extend (not replace: compatibility!!!) the existing components that implement the old interface.

Old Dialog Interfaces



class VCL_DLLPUBLIC Dialog : public SystemWindow { ... public:

   virtual short Execute();

... };



published interface XExecutableDialog: com::sun::star::uno::XInterface {

       void setTitle( [in] string aTitle );
       short execute();


New dialog interfaces


class VCL_DLLPUBLIC Dialog : public SystemWindow { ... public:

   // Link impl: DECL_LINK( MyEndDialogHdl, Dialog* ); 
   // - param is a pointer to the dialog just ended
   virtual void StartExecuteModal( const Link& rEndDialogHdl );
   long         GetResult() const;

... };


  • (offapi/com/sun/star/ui/dialogs/XAsynchronousExecutableDialog.idl)
  • (offapi/com/sun/star/ui/dialogs/XDialogClosedListener.idl)
  • (offapi/com/sun/star/ui/dialogs/DialogClosedEvent.idl)

interface XAsynchronousExecutableDialog: com::sun::star::uno::XInterface {

   void setDialogTitle( [in] string aTitle );
   void startExecuteModal( [in] XDialogClosedListener xListener );


interface XDialogClosedListener: com::sun::star::lang::XEventListener {

   void dialogClosed( [in] DialogClosedEvent aEvent );


struct DialogClosedEvent: com::sun::star::lang::EventObject {

      @see <type>ExecutableDialogResults</type>
   short DialogResult;



We already created a Child Workspace (CWS) for changing all dialogs that must be changed in order to make them compatible with the upcoming Threading Framework. The name of the CWS is 'asyncdialogs'.

The new dialog API (VCL and UNO) is available in this CWS.

Many affected dialogs are already adapted. Although the number of affected dialogs is not that big, lots of work is to be done because not only the dialog itself but all its callers that rely on data that are synchronously returned by the dialog must be adapted, too. This is like the well-known snowball effect.

Work Required

It is necessary to find and adapt all dialogs that are executed synchronously and that use SvtURLBox (svtools/source/control/inettbc.cxx). SvtURLBox creates a thread for filename autocompletion which will no longer work once the Threading Framework is in place. We need to adapt all callers of the dialogs just mentioned, if those callers rely directly or indirectly on data synchronously delivered by the dialogs.

Important affected dialogs are the OOo Filepicker and the OOo Folderpicker dialogs (fpicker/source/office/*). Those are heavily used throughout the whole OOo code. They must be changed, because they use SvtURLBox. Changing all affected code, especially the dozens of direct and indirect clients of the picker dialogs, is really much work.

Currently, we are in the middle of adapting sfx2::FileDialogHelper (sfx2/include/filedlghelper.hxx) that is a wrapper for the file dialog and that is heavily used in the OOo code.

Other affected SvtURLBox clients are subject of further investigations (LXR is your friend...)


Much work is already done in order to solve the problem that certain OOo dialogs will have in conjunction with the new OOo Threading Framework. All conceptional work is done. What's open is to finish adapting affected OOo code.

Typical Changes - Example

IMO a simple example for the code changes typical for asyncdialog can be found in /svx/source/dialog/cuigaldlg.cxx and the respective header file in the same directory.

There is a class SearchProgress which creates an instance of class SearchThread. The SearchThread worker thread calls back into VCL (e.g. SearchThread::ImplSearch(): mpBrowser->aLbxFound.InsertEntry()).

In asyncdialogs the latter will lead to a deadlock, because:

  • The thread that calls SearchProgress::Execute() thereby enters the Office Mutex threading environment
  • That thread stays there at least until Execute() returns.
  • But Execute() will never return, because it waits for the SearchThread to end.

In fact it waits indefinetly that CleanUpHdl will be called (handler calls EndDialog() and this will let Execute() return), which is only done from inside SearchThread::onTerminated(). But the worker thread only terminates if it is ably to deliver search results to VCL, but it will never be able to do so because every VCL call will automatically guarded by the Office Mutex threading environment...

The solution was to switch to the new asyncdialog VCL Dialog execution API.


List of things to do in CWS asyncdialogs


  • Find and change all dialogs that are executed via Dialog::Execute() and that indirectly or directly spawn threads that need to call back to non-threadsafe code (for example VCL API, non-threadsafe UNO objects) in order to get the dialog finished.
    • VCL: deprecate Dialog::Execute() => changes mail!
    • UNO: deprecate css::ui::dialogs::XExecutableDialog => changes mail!

Affected dialogs

  • unopkg binary -- gui mode
    • desktop/source/deployment/gui/dp_gui_service.cxx => ServiceImpl::execute()
    • dialog creates threads.
    • dialog is a non-threadsafe UNO service => does not use Dialog::Execute, but Application::Execute()
      • BUG: Application::Execute() enters apartment at the beginning and remains in apartment all the time
      • dialog worker threads will never be able to enter apartment => deadlock
      • UTF2: Application::Execute() must be fixed - DONE (by KR)
      • asyncdialogs: com.sun.star.comp.deployment.ui.PackageManagerDialog service: - does no longer implement XExecutableDialog, but XAsynchronousExecutableDialog DONE: changed
  • fpicker/source/unx/gnome/asynceventnotifier.hxx: #include <osl/thread.hxx>
    • triggered by dialog - ./fpicker/source/unx/gnome/SalGtkFilePicker.cxx => SalGtkFilePicker::execute()
    • 'execute' problem must be solved
    • DONE: no change (asynceventnotifier no longer used in m124)
  • svtools/source/control/inettbc.cxx:#include <vos/thread.hxx>
    • triggered by misc dialogs using SvtURLBox => 'execute' problem must be solved
    • class XMLFilterTabPageXSLT (/framework/filter/source/xsltdialog/xmlfiltertabpagexslt.hxx)
      • @@@ todo => investigate if change needed
    • class SvxIMapDlg (/graphics/svx/inc/imapdlg.hxx)
      • @@@ todo => investigate if change needed
    • class SvxHyperURLBox : public SvtURLBox (/graphics/svx/source/dialog/hltpbase.hxx)
      • @@@ todo: callers? => investigate
    • class OFileURLControl : public SvtURLBox (/graphics/svx/source/dialog/urlcontrol.hxx)
      • @@@ todo: callers? => investigate
    • class AddInstanceDialog (/graphics/svx/source/inc/datanavi.hxx)
      • @@@ todo => investigate if change needed
    • class SvtExpFileDlg_Impl (/gsl/fpicker/source/office/iodlgimp.hxx)
      • part of implementation of class SvtFileDialog (/gsl/fpicker/source/office/iodlg.hxx)
      • lots of direct and indirect callers!
      • @@@ IN PROGRESS (Peter Burow)
    • class ScLinkedAreaDlg (/sc/sc/source/ui/inc/linkarea.hxx)
      • @@@ todo => investigate if change needed
    • class FileURLBox : public SvtURLBox (/util/svtools/inc/fileurlbox.hxx)
      • @@@ todo: callers? => investigate
    • class OFileURLControl : public SvtURLBox (/util/svtools/inc/urlcontrol.hxx)
      • @@@ todo: callers? => investigate
  • sw/source/ui/inc/maildispatcher.hxx:#include <osl/thread.hxx>
    • class MailDispatcher (sw/source/ui/inc/maildispatcher.hxx) is an osl::Thread
      • Instances are created in:
        1. sw/source/ui/dbui/dbmgr.cxx
        2. sw/source/ui/dbui/mailmergechildwindow.cxx
      • MailDispatcher has interface for regsistration of UNO listeners
        1. no Dialog::Execute in any possible callstack (currently only reachable via UNO API) ==> DONE: no change needed
        2. SwMailSendDialog is a modeless dialog, does not need to be changed therfore, but is called by MailMergeWizard which has overridden Dialog::Execute()
          • MailMergeWizard and calling code must be changed ==> DONE: changed
    • DONE
  • svx/source/dialog/cuigaldlg.hxx:#include <vos/thread.hxx>
    • @@@ triggered by dialog => 'execute' problem must be solved
    • DONE: changed
Personal tools