[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Chicken-users] Threads and dynamic-wind still problematic
From: |
Tobia Conforto |
Subject: |
Re: [Chicken-users] Threads and dynamic-wind still problematic |
Date: |
Wed, 11 Mar 2009 00:44:21 +0100 |
John Cowan wrote:
Tobia Conforto scripsit:
I wonder how other compilers do it. For example, I find Java's
(and Python's) try/finally syntax quite useful. I've always
thought dynamic-wind was Scheme's equivalent construct, but it
would appear I was mistaken.
Here's the relevant writeup from Taylor Campbell's (Riastradh's)
blag, slightly edited.
Thank you, very instructive.
(UNWIND-PROTECT form protection-form ...), a special form, evaluates
the first form and returns its value, but before returning the value
also executes the protection forms. Furthermore, if a throw in the
primary form transfers control to outside the whole UNWIND-PROTECT
form, this, too, will execute the protection forms.
This is a restricted definition of what unwind-protect does in CL. It
doesn't just execute the protection forms before a normal return and
an exception, but also before return-from, go, and other kinds of non-
local exits that in Scheme would be coded with call/cc.
But if the definition above (executing the finalizers before normal
returns and exceptions only) is all that matters, it's not hard to
write:
(define (unwind-protect thunk finalizer)
(let* ((original-e-h (current-exception-handler))
(result (with-exception-handler
(lambda (exn)
(with-exception-handler original-e-h finalizer)
(original-e-h exn))
thunk)))
(finalizer)
result))
Or if you care for multiple values:
(define (unwind-protect thunk finalizer)
(let ((original-e-h (current-exception-handler)))
(call-with-values
(lambda ()
(with-exception-handler
(lambda (exn)
(with-exception-handler original-e-h finalizer)
(original-e-h exn))
thunk))
(lambda vals
(finalizer)
(apply values vals)))))
I believe this is much more of a Chicken version of try/finally than
dynamic-wind is.
Tobia