|
|
IntroductionThe goals of the threading solution research are:
Contents
Current StarBasic script threading behaviourRun the macro from the "Macro" dialogbox.
Current Ms Basic script threading behaviourRun the macro from the "Macro" dialogbox.
Event HandlingThe OpenOffice process has multiple threads. Any thread that calls Application::Yield or Application::Reschedule dispatches GUI events. Dispatching GUI events requires the SolarMutex, so only one thread at a time can dispatch GUI events. In practice the "main" OpenOffice thread is the thread that dispatches most GUI events. Events can be either synchronous or asynchronous. Synchronous events are handled immediately, that means that the functionality bound to the event is invoked immediately from the call stack of the thread where the event occurred. The thread is therefore blocked until the event has been handled. Asynchronous events are placed in a message queue by the thread handling the event, and the thread is not blocked, it continues executing. The message queue is serviced by any thread which calls Application::Yield or Application::Reschedule. Options for script threading behaviourThere are two possible threading models that we could use:
The best solution is to implement option 2, and add the possibility to configure the threading behaviour so that it acts like option 1 if the user wishes to use this behaviour (see Configuration section below). So, how do we implement option 2? From discussions with Mathias Bauer and Joerg Budischweski they feel that OpenOffice should manage the spawning of the threads which would invoke the scripts. In the case of synchronous events the thread handling the event would spawn a thread in which it would then invoke the script. In the case of asynchronous threads the thread which had grabbed the event off the message queue would spawn a new thread when it wanted to invoke a script. In other words the responsibility for managing the per document script threads would lie with OpenOffice, not the scripting framework. Changes needed:
See also: "Threading model for an office scripting framework" discussion on openoffice.udk.dev newsgroup Issues with allowing one script per documentBecause of problems with thread safety in the OpenOffice API it is currently unsafe to allow more than one thread to make API calls at the same time. For this reason there is a global SolarMutex which thread unsafe API implementations automatically lock before executing any functionality, and unlock before returning. The thread calling the OpenOffice API is not aware of this SolarMutex. While the SolarMutex is grabbed, OpenOffice API calls from other threads will block until the SolarMutex becomes free. The OpenOffice UI will be frozen while the SolarMutex is locked. If a script calls a OpenOffice API, the solarmutex will be grabbed until the API call returns which would temporarily freeze all document windows. These freezes may be an unacceptable annoyance to users and they may prefer that the UI be completely frozen for the duration of script execution rather than having it freeze unexpectedly. See also: "Office API and thread safety" discussion on openoffice.framework.dev newsgroup Configuration of threading behaviourThe threading behaviour of Office should be configurable so that the user can switch on/off the one script per document behaviour as they wish. A radio box should be added to the Scripting pane of the Office options dialog with the options: Allow only one script to be run by OpenOffice at a time. Identifying script threads in a debuggerThe issue here is how we can identify which thread in a OpenOffice process is running the script we are interested in debugging. This is possible in java using the java.lang.Thread.setName() method. The pairing of script name and document name should be sufficient to uniquely identify the appropriate thread. SolarMutex A problem could arise if a thread that does not possess the SolarMutex
tries to execute a script synchronously, because any API call in the script
can possibly acquire the SolarMutex. This may cause a deadlock, f.e. if the
thread possessing the SolarMutex waits for a resource that is locked by the
thread that tries to execute a script synchronously. So we could add some
code here, that denies script execution, if it should be done synchronously
and the SolarMutex is locked. It's better to treat this as an error than taking
the risk of getting a deadlock. |


