DYNAMIC-WIND before and after procedures are not protected from interruption
Taylor R Campbell <[email protected]> Thu, 24 Dec 2009 02:31:26 -0500
| Newsgroups | gmane.lisp.scheme.scheme48 |
|---|---|
| Message-ID | <[email protected]> |
Because DYNAMIC-WIND before and after procedures are not protected
from interruption, it is not possible, for example, to reliably
release a lock in Scheme48 when control exits an extent: in
(dynamic-wind
(let ((already? #f))
(lambda ()
(if already? (error "Reentry into locked extent prohibited."))
(set! already? #t)
(obtain-lock lock)))
(lambda () ...)
(lambda ()
(release-lock lock))),
if control is interrupted just before the call to RELEASE-LOCK, and
non-locally exited (e.g., by hitting ^C at the REPL and then typing
,reset), then the lock will not be released as intended. A user at
the REPL can work around this, but it requires manual intervention
(which works only because RELEASE-LOCK doesn't care which thread calls
it -- which, incidentally, masks some legitimate errors). Using
KILL-THREAD! rather than TERMINATE-THREAD! at the wrong time may still
make a mess of things, of course, but that is to be expected anyway.
In other Scheme systems such as MIT Scheme and PLT Scheme, their
respective notions of interrupts are deferred during DYNAMIC-WIND
before and after procedures. Precisely what should be deferred in
Scheme48 is not immediately clear -- perhaps all interrupts, or
perhaps just keyboard interrupts, or perhaps some class of interrupts
whose handlers are never supposed to perform any non-local exits.
(One may argue that it is not safe to release a lock on an arbitrary
exit out of the extent, and that the correct idiom is
(let ((done? #f))
(dynamic-wind
(let ((already? #f))
(lambda ()
(if already? (error "Reentry into locked extent prohibited."))
(set! already? #t)
(obtain-lock lock)))
(lambda () (begin0 ... (set! done? #t)))
(lambda ()
(if (not done?)
(error "Abnormal exit out of locked extent."))
(release-lock lock)))).
However, it may be safe to release the lock on any exit, if one is
careful to guarantee that every permanent effect in the ellipsis
stands alone and leaves the state of the world consistent. Either
idiom may be correct, depending on the circumstances.)