5.2.5 Locks

5.2.5.1 Avoiding deadlocks

When processes are acquiring and releasing locks, you must make certain that they do not become deadlocked. A deadlock occurs when two or more processes are waiting for locks held by each other. When a deadlock occurs, the waiting processes wait forever and appear to hang. A deadlock can also occur when a lock is acquired while scheduling is inhibited.

The Multitasking Facility signals an error whenever a process attempts to acquire a lock while scheduling is inhibited or while interrupts are deferred. To prevent such potential deadlock errors, processes should not acquire locks while scheduling is either explicitly inhibited, by a call to the macrowith-scheduling-inhibited, or implicitly inhibited. The following guidelines should help you avoid deadlocks:

A process can safely acquire a lock in a protected context simply by acquiring the lock before inhibiting scheduling, as shown in the following example:

;;; Incorrect way to make sure windows do not move around between
;;; locating the window and drawing on it.
(with-scheduling-inhibited
   (let ((window (find-topmost-window)))
     (draw-on-window window)))

;;; Correct way; the code inside WITH-SCHEDULING-INHIBITED will ;;; always be able to acquire the lock. (with-window-system-lock (with-scheduling-inhibited (let ((window (find-topmost-window))) (draw-on-window window))))

To prevent processes from waiting forever for a lock, use the:action keyword argument towith-process-lock or the action argument toprocess-lock to specify the action a process should take if a requested lock is already held by another process.


The Advanced User's Guide - 9 SEP 1996

Generated with Harlequin WebMaker