16.3 Locks

Locks can be used to control access to shared data by several processes.

The two main symbols used in locking are the function make-lock , to create a lock, and the macro with-lock , to execute a body of code while holding the specified lock.

A lock has a name (a string) and several other components. The printed representation of a lock shows the name of the lock and whether it is currently locked. Additionally if the lock is locked it shows the name of the process holding the lock, and how many times that process has locked it. For example:

#<MP:LOCK "my-lock" Locked 2 times by "My Process" 2008CAD8>

The function lock-owner returns the process that locked a given lock.



mp:make-lock &key important-p &allow-other-keys

Creates a lock object. If important-p is t the lock is added to the list held in the global variable mp:*important-locks* . The function mp:free-important-locks frees all important locks associated with a given process (or all the important locks if called on nil ). Other keywords should be names of the lock components.



mp:process-lock lock &optional whostate timeout

Blocks the current process until the lock is claimed or timeout elapses if it has been specified. Returns t if lock was claimed, nil otherwise.



mp:process-unlock lock &optional errorp

Releases the lock. If errorp is non- nil it signals an error if the current process does not own the lock. The default value of errorp is t .



mp:with-lock (( lock &rest lock-args ) &body body

Executes the body with lock held. Arguments to pass on to mp:process-lock are specified using lock-args .

The following accessor is available for locks: lock-name .

LispWorks User Guide - 11 Mar 2008