ALox  V. 2402 R. 0
Home ALox for C++ ALox for C# ALox for Java Download
Public Fields | Public Methods | Protected Fields | Package Access Methods | List of all members
ThreadLock Class Reference
Inheritance diagram for ThreadLock:
[legend]
Collaboration diagram for ThreadLock:
[legend]

Class Description


This class allows mutual exclusive access to resources shared by different threads. In other words, access to certain data that is accessed by different threads, can be protected if each thread (aka critical section code) uses the same ThreadLock to control the access to such data.

If an acquire() is not followed by a corresponding release(), other threads will not be able to gain control to this object and will wait endlessly (deadlock situation). To avoid this, it is a good practice to embed pairs of acquire()/release() calls into try/finally statements as follows:

try { tl.acquire(); ...critical section code .... } finally { tl.release() }

ThreadLock uses an internal counter to allow multiple calls to acquire() and to be freed only when a same amount of release() calls are performed. This behavior can be switched off by a constructor parameter. If switched off, each recursive acquire() call will not be counted and each call to release() will instantly free the mutex. This mode is not very recommended, the standard use is recursive mode.

Furthermore, ThreadLock allows to disable locking using setUnsafe(). The objective here is to gain execution speed, as thread synchronization causes relatively expensive system calls. Nevertheless, it is sometimes obvious that the same code may run in a thread safe mode one time and without thread locking the next time. Therefore, for performance critical code, it is quite useful to be able to control this behavior.

Caution: Use this class with great care. Deadlocks are not easy to detect and debug. Use this class only if standard synchronization of the Java language seems too limited and mechanisms like the class uses internally would need to be implemented.

Note
For information on debugging deadlocks with this class, see field createOwnerStackTrace.

Public Fields

int recursionWarningThreshold = 10
 
int waitWarningTimeLimitInMillis = 1000
 

Public Methods

 ThreadLock ()
 
 ThreadLock (LockMode lockMode)
 
 ThreadLock (LockMode lockMode, Safeness safeness)
 
void acquire ()
 
int dbgCountAcquirements (Thread thread)
 
LockMode getMode ()
 
Safeness getSafeness ()
 
void release ()
 
void setSafeness (Safeness safeness)
 
String toString ()
 
boolean willRelease ()
 

Protected Fields

int cntAcquirements
 
boolean createOwnerStackTrace = false
 
LockMode lockMode
 
Object mutex
 
Thread owner
 
Exception ownerException
 
Ticks waitTime = new Ticks()
 

Package Access Methods

void constructor (LockMode lockMode, Safeness safeness)
 

Constructor & Destructor Documentation

◆ ThreadLock() [1/3]


Create a ThreadLock that allows recursion.

◆ ThreadLock() [2/3]

ThreadLock ( LockMode  lockMode)

Create a ThreadLock that allows recursion. A warning will be given (ALIB Error) when the given recursion level is reached (and each multiple of it).

Parameters
lockMode(Optional) Flag if recursion support is on (the default). If not, nested locks are not counted.

◆ ThreadLock() [3/3]

ThreadLock ( LockMode  lockMode,
Safeness  safeness 
)

Create a ThreadLock that allows recursion. A warning will be given (ALIB Error) when the given recursion level is reached (and each multiple of it). In addition the lock can be initialized to be unsafe, which means the locking critical sections is disabled.

Parameters
lockMode(Optional) Flag if recursion support is on (the default). If not, nested locks are not counted.
safeness(Optional) Defaults to Safeness.Safe. See setSafeness for more information.

Member Function Documentation

◆ acquire()

void acquire ( )

Thread which invokes this method gets registered as the current owner of this object, until the same thread releases the ownership invoking release(). In the case that this object is already owned by another thread, the invoking thread is suspended until ownership can be gained. Multiple (nested) calls to this method are counted and the object is only released when the same number of release() calls have been made.

Reimplemented in Lox.

◆ constructor()

void constructor ( LockMode  lockMode,
Safeness  safeness 
)
package

Used by the Constructors to create an instance.

Parameters
lockMode(Optional) Flag if recursion support is on (the default). If not, nested locks are not counted.
safenessSee setSafeness for more information.

◆ dbgCountAcquirements()

int dbgCountAcquirements ( Thread  thread)

Returns the number of acquirements of this ThreadLock. The negative number (still providing the number of acquirements) is returned if the owning thread is not the same as the given one.

Note
This method is provided mainly for debugging and implementing debug assertions into the code. It is not considered a good practice to use this method for implementing software logic. In contrast the software should be designed in a way, that it is always clear who owns a ThreadLock or at least that acquiring a thread lock can be performed instead.
Parameters
threadThe thread to test current ownership of this. Defaults to the current (invoking) thread.
Returns
The number of (recursive) acquirements, negative if acquired by a different thread than provided.

◆ getMode()

LockMode getMode ( )

Query if this instance was set to work recursively.

Returns
A value of type com.aworx.lib.lang.LockMode "LockMode"

◆ getSafeness()

Safeness getSafeness ( )

Query if this instance was set to unsafe mode.

Returns
A value of type com.aworx.lib.lang.Safeness "Safeness"

◆ release()

void release ( )

Releases ownership of this object. If acquire() was called multiple times before, the same number of calls to this method have to be performed to release ownership.

Reimplemented in Lox.

◆ setSafeness()

void setSafeness ( Safeness  safeness)

If parameter is true, the whole locking system is disabled. The only objective here is to to gain execution speed, as thread synchronization causes relatively expensive system calls. Use this method only if you are 100% sure that your (otherwise) critical section are executed in a single threaded environment. And: "relative expensive" means: they are not really expensive. This is provided only for the rare case that your critical section is very, very frequently executed.

Parameters
safenessDetermines if this object should use a mutex (Safeness.Safe) or just do nothing (Safeness.Unsafe).

◆ toString()

String toString ( )

This is for debugging purposes. E.g. this enables the Eclipse IDE to display object descriptions in the debugger.

Returns
A human readable string representation of this object.

Reimplemented in Logger.

◆ willRelease()

boolean willRelease ( )

Returns true if the next invocation of release will release the lock. Returns false, if recursive acquirements have been performed.

Returns
true if locked exactly once.

Member Data Documentation

◆ cntAcquirements

int cntAcquirements
protected

Counter for the number of acquire() calls of the current thread.

◆ createOwnerStackTrace

boolean createOwnerStackTrace = false
protected

If set to true, whenever acquired, the stack trace of the acquirement is stored. Can be set to true for debugging deadlocks. Stack trace will be reported when waitWarningTimeLimitInMillis is exceeded.

◆ lockMode

LockMode lockMode
protected

Flag if recursion support is on. If not, nested locks are not counted.

◆ mutex

Object mutex
protected

The internal object to lock on.

◆ owner

Thread owner
protected

The current owner of the ThreadLock.

◆ ownerException

Exception ownerException
protected

Used for dumping stack trace of location of owner. For performance reasons, created only if public field createOwnerStackTrace was set to true.

◆ recursionWarningThreshold

int recursionWarningThreshold = 10

Limit of recursions. If limit is reached or a multiple of it, an error message is passed to ReportWriter. Defaults is 10.

◆ waitTime

Ticks waitTime = new Ticks()
protected

The internal object to measure the time

◆ waitWarningTimeLimitInMillis

int waitWarningTimeLimitInMillis = 1000

This is a threshold that causes acquire() to send a warning to ReportWriter if acquiring the access takes longer than the given number of milliseconds. To disable such messages, set this value to 0. Default is 1 second.


The documentation for this class was generated from the following file: