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.
The function lock-name returns the name of a lock.
The function process-lock blocks the current process until a given lock is claimed or a timeout passes, and process-unlock releases the lock.
The macro with-lock executes code with a lock held, and releases the lock on exit, as if by process-lock and process-unlock.
If you need to avoid blocking on a lock that is held by some other thread, then use with-lock with timeout 0, like this:
(unless (mp:with-lock (lock nil 0)
The macros with-sharing-lock and with-exclusive-lock can be used with sharing locks.
The keyword argument recursivep to make-lock, when true, allows the lock to be locked recursively. recursivep is true by default. If recursivep is false then trying to lock again causes an error. This is useful for debugging code where the lock is not expected to be claimed recursively.
The keyword argument sharing to make-lock, when true, creates an "sharing" lock object, which supports sharing and exclusive locking. A sharing lock is handled by different functions and methods. See with-exclusive-lock, with-sharing-lock, process-exclusive-lock, process-exclusive-unlock, process-sharing-lock and process-sharing-unlock.
In compiled code process-lock, process-exclusive-lock and process-sharing-lock are guaranteed to return if they locked their argument. In other words there will not be any throw between the time they locked the lock and the time they return. That means that in compiled code the next form will at least start executing, and if it is an
unwind-protect the cleanup forms will at least start executing. (If the code is evaluated, this is not guaranteed.) "Locking" here also means incrementing the count of a lock that is already held by the current thread.
LispWorks User Guide and Reference Manual - 13 Feb 2015