Difference between revisions of "Uno/Article/Multi-Thread Programming"

From Apache OpenOffice Wiki
< Uno
Jump to: navigation, search
m (Improved wording.)
(Restructured.)
Line 5: Line 5:
 
UNO is inherently multi threaded. Every instance of a UNO component may be accessed by multiple threads concurrently. The UNO threading framework provides support for simplifying multi thread programming.
 
UNO is inherently multi threaded. Every instance of a UNO component may be accessed by multiple threads concurrently. The UNO threading framework provides support for simplifying multi thread programming.
  
Every UNO reference points to an object with particular characteristics. Among implementing a concrete interface, the object may belong to a particular [[Uno/Spec/Purpose Environment | Purpose Environment]]. The [[Uno/Effort/Creating the Uno Threading Framework | UNO Threading Framework]] introduces the [[Uno/Spec/Thread Affinity Bridge | Thread Affine Purpose Environment]] and [[Uno/Spec/Thread Unsafety Bridge | Thread Unsafe Purpose Environments]]. Objects not belonging to one of these two [[Uno/Spec/Purpose Environment | Purpose Environments]] are assumed to be [[Uno/Term/Thread Safe | Thread Safe]]. Depending on the type of the planned implementation, which either is a
+
Every UNO reference points to an object with particular characteristics. Among implementing a concrete interface, the object may belong to a particular [[Uno/Spec/Purpose Environment | Purpose Environment]]. The [[Uno/Effort/Creating the Uno Threading Framework | UNO Threading Framework]] introduces the [[Uno/Spec/Thread Affinity Bridge | Thread Affine Purpose Environment]] and [[Uno/Spec/Thread Unsafety Bridge | Thread Unsafe Purpose Environments]]. Objects not belonging to one of these two [[Uno/Spec/Purpose Environment | Purpose Environments]] are assumed to be [[Uno/Term/Thread Safe | Thread Safe]].  
* component, or a
+
* library, or an
+
* application,
+
care of the "purpose" characteristic of the involved objects needs to be taken.
+
  
'''Note: '''Unfortunately, no type safe specialized purpose references are yet available for any UNO language binding. So, this is planned. Currently, the only possible way to detect the purpose of a reference is at runtime, via the <code>getCurrentEnvironment</code> [[Uno/Spec/Runtime | Runtime]] function.
+
=== Implementing Objects ===
 
+
Going to implement an UNO object, you need to decide on the threading architecture. You basically have three choices, it can be
'''Note:''' The UNO [[Uno/Spec/Runtime | Runtime]] provides APIs for implementing any whished scenario. Including objects shared between different [[Uno/Spec/Purpose Environment | Purpose Environments]] or the instantiation of a [[Uno/Term/Thread Safe | Thread Safe]] component in a [[Uno/Term/Thread Unsafe | Thread Unsafe]] [[Uno/Spec/Environment | Environment]]. These mechanisms are in place to implement the Threading Framework itself, and to allow the programmer to gain full control, if needed.
+
* [[Uno/Term/Thread Unsafe | Thread Unsafe]], or
 
+
* [[Uno/Term/Thread Safe | Thread Safe]], or
=== Components ===
+
* [[Uno/Term/Thread Affine | Thread Affine]], or
If you are going to implement a UNO component, you need to decide on the threading architecture. You basically have three choices:
+
* [[Uno/Term/Thread Free | Thread Free]].
* [[Uno/Term/Thread Unsafe | Thread Unsafe]]
+
* [[Uno/Term/Thread Safe | Thread Safe]]
+
* [[Uno/Term/Thread Affine | Thread Affine]]
+
* [[Uno/Term/Thread Free | Thread Free]]
+
 
+
==== C++ Example Components ====
+
* [[Uno/Example/Thread Unsafe Component]]
+
* [[Uno/Example/Thread Safe Component]]
+
* [[Uno/Example/Thread Affine Component]]
+
* [[Uno/Example/Thread Free Component]]
+
 
+
'''Note:''' It is planned, to support the selection of an [[Uno/Spec/Implementation Environment | Implementation Environment]] (determining the components [[Uno/Spec/Purpose Environment | Purpose Environment]]) by a macro or an include. Some experiments have been done, so no final decisions have been made yet.
+
  
 
==== Thread Unsafe ====
 
==== Thread Unsafe ====
Line 34: Line 18:
  
 
==== Thread Safe ====
 
==== Thread Safe ====
There are only rare cases where you want to implement your component [[Uno/Term/Thread Safe | Thread Safe]]. Either
+
There are only rare cases where you want to implement your object [[Uno/Term/Thread Safe | Thread Safe]]. Either
* your component should or must allow the concurrent execution of some methods, or
+
* your component should or must allow the concurrent execution of some of its methods, or
 
* your component wants to avoid the overhead associated with leaving synchronization to the [[Uno/Spec/Runtime | Runtime]].
 
* your component wants to avoid the overhead associated with leaving synchronization to the [[Uno/Spec/Runtime | Runtime]].
  
One case, where your component must allow the concurrent execution of methods is, when you want to abort a running invocation. UNO currently does not offer a mechanism to do this generically, so that particular objects must provide dedicated methods for abortion. An example for this is the [http://util.openoffice.org/source/browse/util/io/source/acceptor/ util/io/Acceptor] implementation.
+
One case, where your component must allow the concurrent execution of methods is, when you want to be able to abort a running invocation. UNO currently does not offer a mechanism to do this generically, so that particular objects must provide dedicated methods for abortion. An example for this is the [http://util.openoffice.org/source/browse/util/io/source/acceptor/ util/io/Acceptor] implementation.
  
 
The overhead for automatic synchronization only affects inter-environment calls. The threading architecture of a particular application should be designed in a way, that closely connected objects happen to exist in the same [[Uno/Spec/Environment | Environment]]. Basically ensuring a low inter-environment call frequency, converting the potential advantage of self synchronized methods to the reverse.
 
The overhead for automatic synchronization only affects inter-environment calls. The threading architecture of a particular application should be designed in a way, that closely connected objects happen to exist in the same [[Uno/Spec/Environment | Environment]]. Basically ensuring a low inter-environment call frequency, converting the potential advantage of self synchronized methods to the reverse.
  
'''Note:''' Scaling may be achieved by the introduction of [[Uno/Spec/Named Environment | Named Environments]], actually allowing any number of Thread Unsafe Purpose Environments to exist simultanesously and to be entered by multiple threads concurrently.
+
'''Note:''' Scalability may be achieved by the introduction of [[Uno/Spec/Named Environment | Named Environments]], actually allowing any number of Thread Unsafe Purpose Environments to exist simultanesously and to be entered by multiple threads concurrently.
  
 
==== Thread Affine ====
 
==== Thread Affine ====
[[Uno/Term/Thread Affine | Thread Affine]] components are rare. In OOo they are needed to encapsulate the Win32 thread affinity.
+
[[Uno/Term/Thread Affine | Thread Affine]] objects are rare. In OOo they are needed to encapsulate the Win32 thread affinity.
  
=== Libraries ===
+
==== Thread Free ====
 +
[[Uno/Term/Thread Free | Thread Free]] objects may be used in any threading environment. They certainly need to respect particular environment related constraints.
 +
 
 +
'''Note: '''Unfortunately, no type safe specialized purpose references are yet available for any UNO language binding. So, this is planned. Currently, the only possible way to detect the purpose of a reference is at runtime, via the <code>getCurrentEnvironment</code> [[Uno/Spec/Runtime | Runtime]] function.
 +
 
 +
'''Note:''' The UNO [[Uno/Spec/Runtime | Runtime]] provides APIs for implementing any whished scenario. Including objects shared between different [[Uno/Spec/Purpose Environment | Purpose Environments]] or the instantiation of a [[Uno/Term/Thread Safe | Thread Safe]] component in a [[Uno/Term/Thread Unsafe | Thread Unsafe]] [[Uno/Spec/Environment | Environment]]. These mechanisms are in place to implement the Threading Framework itself, and to allow the programmer to gain full control, if needed.
 +
 
 +
=== Using Threads ===
 +
You may create threads in your implementation. Depending on the selected threading architecture, these threads may are allowed to be visible from the outside or not. If your implementation is
 +
* [[Uno/Term/Thread Unsafe | Thread Unsafe]], than it must be [[Uno/Term/Thread Transparent | Thread Transparent]], if it is
 +
* [[Uno/Term/Thread Affine | Thread Affine]], than is must be [[Uno/Term/Thread Transparent | Thread Transparent]] as well, if it is
 +
* [[Uno/Term/Thread Safe | Thread Safe]], it may create visible threads and be [[Uno/Term/Thread Aware]] as needed,
 +
* [[Uno/Term/Thread Free | Thread Free]], it may behave according to the surrounding environment.
 +
Obviously, invisible threads can be used and created anytime anyway. Invisible threads do not harm [[Uno/Term/Thread Transparent | Thread Transparency]].
 +
 
 +
=== Implementation Types ===
 +
Depending on the type of the planned implementation, which either is a
 +
* component, or a
 +
* library, or an
 +
* application,
 +
care of the "purpose" characteristic of the involved objects needs to be taken.
 +
 
 +
'''Note:''' It is planned, to support the selection of an [[Uno/Spec/Implementation Environment | Implementation Environment]] (determining the implementations [[Uno/Spec/Purpose Environment | Purpose Environment]]) by a macro or an include. Some experiments have been done, so no final decisions have been made yet.
 +
 
 +
==== Components ====
 +
 
 +
===== C++ Examples =====
 +
* [[Uno/Example/Thread Unsafe Component]]
 +
* [[Uno/Example/Thread Safe Component]]
 +
* [[Uno/Example/Thread Affine Component]]
 +
* [[Uno/Example/Thread Free Component]]
 +
 
 +
==== Libraries ====
 
Libraries providing functions dealing with UNO objects may be implemented as
 
Libraries providing functions dealing with UNO objects may be implemented as
 
* ''free'', meaning that the library can deal with any kind of objects, or as
 
* ''free'', meaning that the library can deal with any kind of objects, or as
Line 57: Line 73:
 
The cppuhelper library dynamically checks the current environment, and returns matching references, e.g. guaranteeing that [[Uno/Term/Thread Safe | Thread Safe]] and [[Uno/Term/Thread Unsafe | Thread Unsafe]] objects do not get mixed.
 
The cppuhelper library dynamically checks the current environment, and returns matching references, e.g. guaranteeing that [[Uno/Term/Thread Safe | Thread Safe]] and [[Uno/Term/Thread Unsafe | Thread Unsafe]] objects do not get mixed.
  
=== Applications ===
+
==== Applications ====
 
Application code implemented other than in the Thread Safe environment needs to explicitly enter the wished [[Uno/Spec/Purpose Environment | Purpose Environment]] before calling any UNO aware library. UNO aware libraries may than provide matching objects, dependent on the implementation, again ensuring that objects with different "purposes" do not get mixed.
 
Application code implemented other than in the Thread Safe environment needs to explicitly enter the wished [[Uno/Spec/Purpose Environment | Purpose Environment]] before calling any UNO aware library. UNO aware libraries may than provide matching objects, dependent on the implementation, again ensuring that objects with different "purposes" do not get mixed.
  
 
'''Note:''' The planned introduction of [[Uno/Spec/Implementation Environment | Implementation Environments]] is going to be usable by the application programmer as well. It basically removes the necessity to explicitly enter a particular [[Uno/Spec/Purpose Environment | Purpose Environment]].
 
'''Note:''' The planned introduction of [[Uno/Spec/Implementation Environment | Implementation Environments]] is going to be usable by the application programmer as well. It basically removes the necessity to explicitly enter a particular [[Uno/Spec/Purpose Environment | Purpose Environment]].
 
=== Threads ===
 
You may create threads in your implementation. Depending on the selected threading architecture, these threads may allowed to be visible from the outside or not. If your code is
 
* [[Uno/Term/Thread Unsafe | Thread Unsafe]], than it must be [[Uno/Term/Thread Transparent | Thread Transparent]], if it is
 
* [[Uno/Term/Thread Affine | Thread Affine]], than is must be [[Uno/Term/Thread Transparent | Thread Transparent]] as well, if it is
 
* [[Uno/Term/Thread Safe | Thread Safe]], it may create visible threads as needed.
 
Obviously, invisible threads can be used and created anytime anyway.
 
 
  
 
[[Category:Uno:Article]]
 
[[Category:Uno:Article]]

Revision as of 09:11, 15 June 2006

Preface

The technologie described in this article depends on the presence of the Uno/Effort/Creating the Uno Threading Framework.

Multi Threading Programming

UNO is inherently multi threaded. Every instance of a UNO component may be accessed by multiple threads concurrently. The UNO threading framework provides support for simplifying multi thread programming.

Every UNO reference points to an object with particular characteristics. Among implementing a concrete interface, the object may belong to a particular Purpose Environment. The UNO Threading Framework introduces the Thread Affine Purpose Environment and Thread Unsafe Purpose Environments. Objects not belonging to one of these two Purpose Environments are assumed to be Thread Safe.

Implementing Objects

Going to implement an UNO object, you need to decide on the threading architecture. You basically have three choices, it can be

Thread Unsafe

Thread Unsafe is the choice for most cases. Actually leaving proper synchronization of method calls to the Runtime.

Thread Safe

There are only rare cases where you want to implement your object Thread Safe. Either

  • your component should or must allow the concurrent execution of some of its methods, or
  • your component wants to avoid the overhead associated with leaving synchronization to the Runtime.

One case, where your component must allow the concurrent execution of methods is, when you want to be able to abort a running invocation. UNO currently does not offer a mechanism to do this generically, so that particular objects must provide dedicated methods for abortion. An example for this is the util/io/Acceptor implementation.

The overhead for automatic synchronization only affects inter-environment calls. The threading architecture of a particular application should be designed in a way, that closely connected objects happen to exist in the same Environment. Basically ensuring a low inter-environment call frequency, converting the potential advantage of self synchronized methods to the reverse.

Note: Scalability may be achieved by the introduction of Named Environments, actually allowing any number of Thread Unsafe Purpose Environments to exist simultanesously and to be entered by multiple threads concurrently.

Thread Affine

Thread Affine objects are rare. In OOo they are needed to encapsulate the Win32 thread affinity.

Thread Free

Thread Free objects may be used in any threading environment. They certainly need to respect particular environment related constraints.

Note: Unfortunately, no type safe specialized purpose references are yet available for any UNO language binding. So, this is planned. Currently, the only possible way to detect the purpose of a reference is at runtime, via the getCurrentEnvironment Runtime function.

Note: The UNO Runtime provides APIs for implementing any whished scenario. Including objects shared between different Purpose Environments or the instantiation of a Thread Safe component in a Thread Unsafe Environment. These mechanisms are in place to implement the Threading Framework itself, and to allow the programmer to gain full control, if needed.

Using Threads

You may create threads in your implementation. Depending on the selected threading architecture, these threads may are allowed to be visible from the outside or not. If your implementation is

Obviously, invisible threads can be used and created anytime anyway. Invisible threads do not harm Thread Transparency.

Implementation Types

Depending on the type of the planned implementation, which either is a

  • component, or a
  • library, or an
  • application,

care of the "purpose" characteristic of the involved objects needs to be taken.

Note: It is planned, to support the selection of an Implementation Environment (determining the implementations Purpose Environment) by a macro or an include. Some experiments have been done, so no final decisions have been made yet.

Components

C++ Examples

Libraries

Libraries providing functions dealing with UNO objects may be implemented as

  • free, meaning that the library can deal with any kind of objects, or as
  • specialized, meaning that the library can deal with one particular purpose type of objects only.

Free and specialized functions may be mixed, depending on the libraries purpose, this may or may not be good style.

The API of "free" libraries should reflect the "freeness" by using the EnvAwareReference in its definition.

The cppuhelper library dynamically checks the current environment, and returns matching references, e.g. guaranteeing that Thread Safe and Thread Unsafe objects do not get mixed.

Applications

Application code implemented other than in the Thread Safe environment needs to explicitly enter the wished Purpose Environment before calling any UNO aware library. UNO aware libraries may than provide matching objects, dependent on the implementation, again ensuring that objects with different "purposes" do not get mixed.

Note: The planned introduction of Implementation Environments is going to be usable by the application programmer as well. It basically removes the necessity to explicitly enter a particular Purpose Environment.

Personal tools