LispWorks User Guide and Reference Manual > 15 Multiprocessing > 15.7 Locks

NextPrevUpTopContentsIndex

15.7.2 Guarantees and limitations when locking and unlocking

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.

However these functions may throw before locking. For example, in the following code process-lock may throw without locking, for example because something interrupts the process by process-interrupt:

(unwind-protect
 (progn (mp:process-lock lock)
   (whatever))
 (mp:process-unlock lock))

If this call to process-lock does throw without locking, then process-unlock will be called on a lock that is not locked.

The correct code that guarantees (when compiled) that process-unlock is called on exit only when process-lock did lock is:

(mp:process-lock lock)
 
(unwind-protect
    (whatever)
  (mp:process-unlock lock))

Conversely, process-unlock, process-exclusive-unlock and process-sharing-unlock guarantee to successfully unlock the lock, but are not guaranteed to return.

For example, the following code may fail to call another-cleanup :

(mp:process-lock lock)
 
(unwind-protect
    (whatever)
  (mp:process-unlock lock)
  (another-cleanup))

If another-cleanup is essential to execute in all throws, it needs its own unwind-protect :

(mp:process-lock lock)
 
(unwind-protect
    (whatever)
  (unwind-protect
      (mp:process-unlock lock)
    (another-cleanup)))

Note: the guarantees described in this section are relevant only in compiled code.


LispWorks User Guide and Reference Manual - 22 Dec 2009

NextPrevUpTopContentsIndex