| # Section 7.1.4: $LOCK |

###### abstract class $LOCK

#### Formal Definitions

This abstract class defines a state component which is a set of every class sub-typing from this class. Note that SAME has to be an instantiated class, not an abstract one.

types
SAME = object_type ;
$LOCK = set of object_type
state
locks : $LOCK
inv lock_types ==
forall obj in set loc_types & sub_type($LOCK,obj)

NOTE | See the important note about vdm state in the notes on vdm-sl usage in this specification.

This pervasive abstract class is the synchronisation absrtaction from which all forms of synchronising mechanism must sub-type.

#### acquirable

This feature provides a facility for a program using a class which sub-types from this abstraction to test if the thread applying it may acquire the lock concerned.

acquirable | (
| tid : $REFERENCE
| ) : BOOL

##### Formal Signature

acquirable(self : SAME, tid : $REFERENCE) res : BOOL

##### Pre-condition

The pre-condition is vsacuously true since both arguments are not optional vdm types.

##### Post-condition

This post-condition is vacuously true since the operation is total.

This predicate returns true if and only if the lock may be acquired by the calling thread.

NOTE | This call is not atomic with any subsequent call of acquire.

#### acquire

This feature enables an invoking thread to acquire the lock concerned. If necessary in order to do so the thread is suspended until the lock is free.

acquire | (
| tid : $REFERENCE
| )

##### Formal Signature

acquire(self : SAME, tid : $REFERENCE)

##### Pre-condition

The argument is an actual run-time thread identity. This cannot be enforced by the language since it is provided by the run-time environment.

##### Post-condition

The lock has been successfully acquired by the calling thread.

This routine attempts to acquire the lock (self), blocking if it is not available. When the routine returns the lock has always been acquired.

#### release

The action of releasing the lock makes it available for other threads.

release | (
| tid : $REFERENCE
| )

##### Formal Signature

release(self : SAME, tid : $REFERENCE)

##### Pre-condition

The argument is expected to be a run-time thread identity. This cannot be enforced by the language since it is externally provided at run-time.

##### Post-condition

The lock has been released for use by another thread.

This routine releases the lock self for use by other executing threads provided that the calling thread has previously acquired (and not yet released) the lock, otherwise no action is taken.

#### Lock Requests

The following three features work together to provide locking facilities for the situation where a thread can continue doing other actions while waiting for a lock.

#### request

Whether or not the thread executing this call acquires the lock as a result of requesting it, it continues execution without pause. Acquirable above may be used to test if the lock was actually acquired.

request | (
| tid : $REFERENCE
| )

##### Formal Signature

request(self : SAME, tid : $REFERENCE)

##### Pre-condition

The argument is expected to be a run-time thread identity. This cannot be enforced by the language since it is only available when executing.

##### Post-condition

Since the availability (or not) of this lock does not affect the post-execution state of the program, this post-condition is vacuously true.

This routine requests the lock (self). If the lock is available it is acquired. Execution continues whether or not the lock has been acquired.

#### cancel_request

If a lock request has been made and not satisfied, the application of this feature cancels the request. If the lock has been acquired then nothing is done, as is the case if no previous request has been made.

cancel_request | (
| tid : $REFERENCE
| )

##### Formal Signature

cancel_request(self : SAME, tid : $REFERENCE)

##### Pre-condition

The argument is expected to be a run-time thread identity. This cannot be enforced by the language.

##### Post-condition

This definition makes use of a fictitious thread type for explanatory purposes. If the lock is not acquirable then some other thread has acquired it - or may do so.

let others : set of thread = forall elem in set dom thread & elem <> tid in
not acquirable(self,tid)
or exists elem in set others & acquirable(self,elem)

If there is still an outstanding request for the lock then the request is cancelled. If either there is no request or the lock has been acquired then no action is taken.

#### wait_for

This feature is provided to let a thread which has requested, but not acquired, the lock and has nothing else to do, to wait for it to become available.

wait_for | (
| tid : $REFERENCE
| )

##### Formal Signature

wait_for(self : SAME, tid : $REFERENCE)

##### Pre-condition

The argument is expected to be a run-time thread identity. This cannot be enforced by the language.

##### Post-condition

Since the lock is always held by the calling thread on exit, this post-condition is vacuously true.

This routine blocks the calling thread until the lock self which had been requested is acquired. It differs from the acquire feature in that if the lock had not been requested then execution of the thread does not block but continues.

| Language Index |  | Library Index |  | Section 7 Index

Comments or enquiries should be made to Keith Hopper. Page last modified: Monday, 22 May 2000. |