Effort/Make Dialogs Asynchronous

From Apache OpenOffice Wiki
Jump to: navigation, search

As we proceed with making VCL thread-transparent, it becomes necessary to fix asynchronous dialogs so as to avoid deadlocks. This page documents the background, necessary tasks and the status of Modal OpenOffice.org Dialogs in Multi-threaded Environments.

For Current Status, please use this spreadsheet

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 Solar Mutex. But then another problem arises.

The thread calling Dialog::Execute() will acquire the VCL Solar 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 thread-transparent VCL. There, the Solar Mutex will be retired and replaced by an outer, global mutex that will be used to synchronize access to all thread-unsafe components (incl. thread-unsafe 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

VCL

(vcl/inc/dialog.hxx)

[cpp] class VCL_DLLPUBLIC Dialog : public SystemWindow { ... public:

   virtual short Execute();

... };

Uno

(offapi/com/sun/star/ui/dialogs/XExecutableDialog.idl)

[cpp] published interface XExecutableDialog: com::sun::star::uno::XInterface {

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

};

New Dialog Interfaces

VCL

[cpp] 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;

... };

Uno

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

[cpp] 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;

};

Status

We already created a Child Workspace (CWS) for changing all dialogs that must be changed in order to make them compatible with the upcoming thread-transparent VCL. 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.

The latest changes were the replacement of SfxApplication::InsertDocumentDialog(). Now we want to close this CWS because the amount of changes are already big and our Quality Assurance needs much time to approve them. The CWS is resynced to m171. All platforms are built. After this CWS is integrated we will open a new CWS (asyncdialogs2). It is planned to integrate asyncdialog early in OOo 2.1.

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 thread-transparent VCL 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...)

To help find the classes that use SvtURLBox or use a dialog that has a SvtURLBox embedded in it, this (hopefully correct) UML diagram was created: SvtURLBox_UML.jpg (The Visual Paradigm file would be here: SvtURLBox.vpp ...but I couldn't upload .vpp files)

AntoXu has made this spreadsheet which lists all the dialog cases that need to be fixed, location, how to fix and how to test.

About Development

Currently, we have fixed about 50 cases, to call for more people to join us, we divided all cases we found into 6 categories and wrote a development guide to help people understand our working process and the possible clue of how to fix a case under a particular category. If any people want to contribute to this project, we suggest that he or she could start from this page and read the development guide and the example fixes, and selects an unfixed case in cases list and create patches.

Conclusion

Much work is already done in order to solve the problem that certain OOo dialogs will have in conjunction with the new OOo thread-transparent VCL. 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 thread-unsafe 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 thread-unsafe environment ...

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

 Another solution would be to deliver the results of the SearchThread asynchronously - wouldn't it?
 That is, instead of calling InsertEntry directly, the search thread could post an asynchronous event
 to the thread currently doing the message loop, and in this event, the entry would be inserted.
 This way, only the dialog itself would need to be fixed, not all of its clients.
 (Frank Schönheit)
You mean, would have been, don't you?! ;-). Your suggested solution probably would have worked to fix this one dialog again, but would have left the following points open:
  • While a synchronous (AKA ::execute) dialog is open, no function call can enter the office over the Uno API.
  • Synchronous dialogs keep staying on the stack, therefor enforcing that the opened dialogs need to be closed in the reverse order (just try to open some dialogs in otherwise unrelated documents, dependent on the a/synchrnous nature of the involved dialogs, you need to close these in the reverse order).
  • Depending on the actual call stack, mutexes etc. may be kept locked, while the dialog is open, potentially introducing long lasting locks respectively deadlocks.
  • Last but not least: "::execute" is a blocking and potentially long lasting call, conflicting with a potential event based architecture I think we should head for.
So, basically converting all dialogs to asynchronous is the "right thing" anyway :-). Kr 13:27, 19 July 2006 (CEST)
The first item is not really convinicing, as it affects only the Uno API which is "protected" using the Solar Mutex. Other API would continue to work fine. The other items sounds more convincing :) (fs)

To DO

List of things to do in CWS asyncdialogs

General

  • Find and change all dialogs that are executed via Dialog::Execute() and that indirectly or directly spawn threads that need to call back to thread-unsafe code (for example VCL API, thread-unsafe 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 [[Uno/Term/Thread Unsafe|thread-unsafe 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
      • IN PROGRESS
  • 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