Difference between revisions of "Uno/Spec/Threading-Model"

From Apache OpenOffice Wiki
< Uno‎ | Spec
Jump to: navigation, search
(Moved from udk.openoffice.org)
 
(reworked:)
Line 2: Line 2:
 
version: {{REVISIONID}} <br>
 
version: {{REVISIONID}} <br>
 
state:  draft          <br>
 
state:  draft          <br>
−
created: 12/12/2006     <br>
+
created: 12/12/2005     <br>
−
changed: 12/12/2006    <br>
+
changed: 03/06/2006    <br>
 
type:    specification  <br>
 
type:    specification  <br>
  
Line 9: Line 9:
 
== UNO Threading Model ==
 
== UNO Threading Model ==
  
 +
; Feature
 +
: A UNO Runtime provides support for implementing ''thread safe'', ''thread unsafe'' or ''thread affine'' code.
  
−
; Feature: Abstract UNO Threading Model  
+
: The UNO Threading Model allows code to leverage external locking, respectively to leverage thread specificy. Code can either be specified to be
−
: UNO Runtimes can provide support for simplifying implementing thread safe or thread affine code. Code can be differentiated into
+
:* thread safe, or to be
−
: * client code
+
:* thread unsafe, or to be
−
: * component / object code
+
:* thread affine.
  
−
: The UNO Threading Model allows code to leverage external locking, respectively to leverage thread specificy.
+
;; : Thread Safe Code
 +
:: Thread safe code takes care of all threading issues, including locking critical sections etc. Code doing blocking I/O needs to be thread safe, so that system calls can be interrupted. Thread safe code may also be used to allow high concurrency, while avoiding the runtime overhead of external locking.
  
−
: For that to work, code has to decide to use one of three threading models:
+
;; : Thread Unsafe Code
−
: * thread safe
+
:: Thread unsafe code must not care of any threading issues, except thread affinity. Thread unsafe code must be implemented [[Thread Transparent]]. On the desgined level of granularity methods are guaranteed to not be called concurrently. The calling thread may vary over time.
−
: * thread unsafe
+
: The Unsafe Threading Model should become the standard model of implementation. Ensuring all code to be in this Model and utilizing only one thread should give optimal performance without any threading overhead.
−
: * thread affine
 
  
−
: The Free Threading Model
+
;; : The Thread Affine Code
−
: # code needs to take care of any threading issues, e.g. as locking critical sections etc.
+
:: Thread affine code must not care of any threading issues, ''including'' thread affinity. Thread affine code must be implemented [[Thread Transparent]]. On the designed level of granularity methods are guaranteed to not be called concurrelty. Thread calling thread ''does not'' vary over time.
 +
: Declaring code to be thread affine is e.g. very usable for programming Windows thread affine APIs as OLE or WIN32.
  
−
: This model should be leveraged for code doing blocking system calls, while providing an API for interruption. This model may also be used if a high degree of concurrency is needed, while one wants to eliminate any kind of apartment overhead.
 
  
−
: The Thread-Unsafe Model
+
; Rationale:
−
: # code does not need to take care of any locking issues
 
−
: # code running in the Unsafe Thread Model is guaranteed to only be invoked once paralell per runtime.
 
−
: # code needs to take care of any thread affinity, means that code may be called by different threads alternately.
 
  
−
: This should become the standard model of implementation. Ensuring all code to be in this Model and utilizing only one thread should give optimal performance without any threading overhead.
+
; API
 +
: '''Extended Environment Types:''' The different threading models are implemented as standard UNO environments (CPP: uno/environment.h). Environment specifications (e.g. "gcc3", "java") are extended by "purpose" specifiers (e.g. ":mutex", ":thread").  
  
−
: The Affine Thread Model
+
: The predefined purposes to objects according to their threading type are
−
: * Is the as in the Unsafe Thread Model, while deleting -3-
+
:* <code>":mutex" </code> - for thread unsafe code,
−
: * Code in the Affine Thread Model can rely on being called by the same thread always.
+
:* <code>":thread"</code> - for thread affine code, and
 +
:* <code>""      </code> - for thread safe code, to ensure compatibility with previous UNO versions.
  
−
: The Affine Thread Model is e.g. very usable for programming Windows thread affine APIs, e.g. OLE or WIN32.
+
: '''Getting the Current Environment:''' The current purpose environment does not only vary with the implementation language (e.g. Java or G++), but also with the codes declared threading type. Therefor an API for accessing the ''current'' environment is provided.
  
−
: The programming counterpart for a threading model is the so called apartment.
+
; Dependencies: None.
  
−
: The different Threading Models are to be implemented via UNO environments being backed by apartments. A UNO Threading Model supporting UNO Runtime needs to provide:
+
; Compatibility Issues: None.
−
: * threading model specific environment access (e.g. uno:mutex or java:thread)
 
−
: * an API for accessing the current apartment (the threading model)
 
−
: * an API for accessing the default apartments (mutex, thread, free)
 
−
 
 
−
: The apartments need to provide APIs for:
 
−
: * getting the name of an apartment
 
−
 
 
−
 
 
−
: Apartments Access Characteristics
 
−
 
 
−
: The different threading model specific apartments have different access characteristics, derived from the backing apartments:
 
−
 
 
−
: Free
 
−
: * Has no (threading) restrictions.
 
−
 
 
−
: Mutex Apartment
 
−
: * Can only be entered by one thread at a time.
 
−
: * Keepy being entered, even if the thread calls into another apartment.
 
−
: * Can be re-entered by a thread.
 
−
 
 
−
: Thread Apartment
 
−
: * Inherits the Mutex apartment attributes.
 
−
: * Has an associated thread.
 
−
: * All encapsulated objects only become called by the associated thread.
 
−
: * Blocks the entering thread and switches to the associated thread, passes along all parameters
 
−
: * Relieves the entering thread when the associated thread has executed the desired method. Passes back all results.
 
−
: * Switches back to the entering thread, in case an outside object should be called, passes along all parameters.
 
−
: * Switches back to the associated thread, when comming back from an outside call, passes back all results.
 

Revision as of 15:38, 6 March 2006

author: Kr
version: 5807
state: draft
created: 12/12/2005
changed: 03/06/2006
type: specification


UNO Threading Model

Feature
A UNO Runtime provides support for implementing thread safe, thread unsafe or thread affine code.
The UNO Threading Model allows code to leverage external locking, respectively to leverage thread specificy. Code can either be specified to be
  • thread safe, or to be
  • thread unsafe, or to be
  • thread affine.
Thread Safe Code
Thread safe code takes care of all threading issues, including locking critical sections etc. Code doing blocking I/O needs to be thread safe, so that system calls can be interrupted. Thread safe code may also be used to allow high concurrency, while avoiding the runtime overhead of external locking.
Thread Unsafe Code
Thread unsafe code must not care of any threading issues, except thread affinity. Thread unsafe code must be implemented Thread Transparent. On the desgined level of granularity methods are guaranteed to not be called concurrently. The calling thread may vary over time.
The Unsafe Threading Model should become the standard model of implementation. Ensuring all code to be in this Model and utilizing only one thread should give optimal performance without any threading overhead.
The Thread Affine Code
Thread affine code must not care of any threading issues, including thread affinity. Thread affine code must be implemented Thread Transparent. On the designed level of granularity methods are guaranteed to not be called concurrelty. Thread calling thread does not vary over time.
Declaring code to be thread affine is e.g. very usable for programming Windows thread affine APIs as OLE or WIN32.


Rationale
API
Extended Environment Types: The different threading models are implemented as standard UNO environments (CPP: uno/environment.h). Environment specifications (e.g. "gcc3", "java") are extended by "purpose" specifiers (e.g. ":mutex", ":thread").
The predefined purposes to objects according to their threading type are
  • ":mutex" - for thread unsafe code,
  • ":thread" - for thread affine code, and
  • "" - for thread safe code, to ensure compatibility with previous UNO versions.
Getting the Current Environment: The current purpose environment does not only vary with the implementation language (e.g. Java or G++), but also with the codes declared threading type. Therefor an API for accessing the current environment is provided.
Dependencies
None.
Compatibility Issues
None.
Personal tools