1
0
Fork 0
cl-sites/HyperSpec-7-0/HyperSpec/Body/s_unwind.htm
2024-04-01 10:24:07 +02:00

167 lines
9.7 KiB
HTML

<!-- Common Lisp HyperSpec (TM), version 7.0 generated by Kent M. Pitman on Mon, 11-Apr-2005 2:31am EDT -->
<HTML>
<HEAD>
<TITLE>CLHS: Special Operator UNWIND-PROTECT</TITLE>
<LINK HREF="../Data/clhs.css" REL="stylesheet" TYPE="text/css" />
<META HTTP-EQUIV="Author" CONTENT="Kent M. Pitman">
<META HTTP-EQUIV="Organization" CONTENT="LispWorks Ltd.">
<LINK REL=TOP HREF="../Front/index.htm">
<LINK REL=COPYRIGHT HREF="../Front/Help.htm#Legal">
<LINK REL=DISCLAIMER HREF="../Front/Help.htm#Disclaimer">
<LINK REL=PREV HREF="s_throw.htm">
<LINK REL=UP HREF="c_data_a.htm">
<LINK REL=NEXT HREF="v_nil.htm">
</HEAD>
<BODY>
<H1><A REV=MADE HREF="http://www.lispworks.com/"><IMG WIDTH=80 HEIGHT=65 ALT="[LISPWORKS]" SRC="../Graphics/LWSmall.gif" ALIGN=Bottom></A><A REL=TOP HREF="../Front/index.htm"><IMG WIDTH=237 HEIGHT=65 ALT="[Common Lisp HyperSpec (TM)]" SRC="../Graphics/CLHS_Sm.gif" ALIGN=Bottom></A> <A REL=PREV HREF="s_throw.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="c_data_a.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="v_nil.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
<HR>
<A NAME="unwind-protect"><I>Special Operator</I> <B>UNWIND-PROTECT</B></A> <P>
<P><B>Syntax:</B><P>
<P>
<B>unwind-protect</B> <I><I>protected-form</I> <I>cleanup-form</I><B>*</B></I> =&gt; <I><I>result</I><B>*</B></I><P>
<P>
<P><B>Arguments and Values:</B><P>
<P>
<I>protected-form</I>---a <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A>. <P>
<I>cleanup-form</I>---a <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A>. <P>
<I>results</I>---the <A REL=DEFINITION HREF="26_glo_v.htm#value"><I>values</I></A> of the <I>protected-form</I>. <P>
<P><B>Description:</B><P>
<A REL=DEFINITION HREF="#unwind-protect"><B>unwind-protect</B></A> evaluates <I>protected-form</I> and guarantees that <I>cleanup-forms</I> are executed before <A REL=DEFINITION HREF="#unwind-protect"><B>unwind-protect</B></A> exits, whether it terminates normally or is aborted by a control transfer of some kind. <A REL=DEFINITION HREF="#unwind-protect"><B>unwind-protect</B></A> is intended to be used to make sure that certain side effects take place after the evaluation of <I>protected-form</I>. <P>
If a <A REL=DEFINITION HREF="26_glo_n.htm#non-local_exit"><I>non-local exit</I></A> occurs during execution of <I>cleanup-forms</I>, no special action is taken. The <I>cleanup-forms</I> of <A REL=DEFINITION HREF="#unwind-protect"><B>unwind-protect</B></A> are not protected by that <A REL=DEFINITION HREF="#unwind-protect"><B>unwind-protect</B></A>. <P>
<A REL=DEFINITION HREF="#unwind-protect"><B>unwind-protect</B></A> protects against all attempts to exit from <I>protected-form</I>, including <A REL=DEFINITION HREF="s_go.htm#go"><B>go</B></A>, <A REL=DEFINITION HREF="m_hand_1.htm#handler-case"><B>handler-case</B></A>, <A REL=DEFINITION HREF="m_ignore.htm#ignore-errors"><B>ignore-errors</B></A>, <A REL=DEFINITION HREF="m_rst_ca.htm#restart-case"><B>restart-case</B></A>, <A REL=DEFINITION HREF="s_ret_fr.htm#return-from"><B>return-from</B></A>, <A REL=DEFINITION HREF="s_throw.htm#throw"><B>throw</B></A>, and <A REL=DEFINITION HREF="m_w_smp_.htm#with-simple-restart"><B>with-simple-restart</B></A>. <P>
Undoing of <A REL=DEFINITION HREF="26_glo_h.htm#handler"><I>handler</I></A> and <A REL=DEFINITION HREF="26_glo_r.htm#restart"><I>restart</I></A> <A REL=DEFINITION HREF="26_glo_b.htm#binding"><I>bindings</I></A> during an exit happens in parallel with the undoing of the bindings of <A REL=DEFINITION HREF="26_glo_d.htm#dynamic_variable"><I>dynamic variables</I></A> and <A REL=DEFINITION HREF="s_catch.htm#catch"><B>catch</B></A> tags, in the reverse order in which they were established. The effect of this is that <I>cleanup-form</I> sees the same <A REL=DEFINITION HREF="26_glo_h.htm#handler"><I>handler</I></A> and <A REL=DEFINITION HREF="26_glo_r.htm#restart"><I>restart</I></A> <A REL=DEFINITION HREF="26_glo_b.htm#binding"><I>bindings</I></A>, as well as <A REL=DEFINITION HREF="26_glo_d.htm#dynamic_variable"><I>dynamic variable</I></A> <A REL=DEFINITION HREF="26_glo_b.htm#binding"><I>bindings</I></A> and <A REL=DEFINITION HREF="s_catch.htm#catch"><B>catch</B></A> tags, as were visible when the <A REL=DEFINITION HREF="#unwind-protect"><B>unwind-protect</B></A> was entered. <P>
<P><B>Examples:</B><P>
<PRE>
(tagbody
(let ((x 3))
(unwind-protect
(if (numberp x) (go out))
(print x)))
out
...)
</PRE>
</TT> When <A REL=DEFINITION HREF="s_go.htm#go"><B>go</B></A> is executed, the call to <A REL=DEFINITION HREF="f_wr_pr.htm#print"><B>print</B></A> is executed first, and then the transfer of control to the tag <TT>out</TT> is completed. <P>
<PRE>
(defun dummy-function (x)
(setq state 'running)
(unless (numberp x) (throw 'abort 'not-a-number))
(setq state (1+ x))) =&gt; DUMMY-FUNCTION
(catch 'abort (dummy-function 1)) =&gt; 2
state =&gt; 2
(catch 'abort (dummy-function 'trash)) =&gt; NOT-A-NUMBER
state =&gt; RUNNING
(catch 'abort (unwind-protect (dummy-function 'trash)
(setq state 'aborted))) =&gt; NOT-A-NUMBER
state =&gt; ABORTED
</PRE>
</TT> <P>
The following code is not correct: <P>
<PRE>
(unwind-protect
(progn (incf *access-count*)
(perform-access))
(decf *access-count*))
</PRE>
</TT> If an exit occurs before completion of <A REL=DEFINITION HREF="m_incf_.htm#incf"><B>incf</B></A>, the <A REL=DEFINITION HREF="m_incf_.htm#decf"><B>decf</B></A> <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A> is executed anyway, resulting in an incorrect value for <TT>*access-count*</TT>. The correct way to code this is as follows: <P>
<PRE>
(let ((old-count *access-count*))
(unwind-protect
(progn (incf *access-count*)
(perform-access))
(setq *access-count* old-count)))
</PRE>
</TT> <P>
<P>
<PRE>
;;; The following returns 2.
(block nil
(unwind-protect (return 1)
(return 2)))
;;; The following has undefined consequences.
(block a
(block b
(unwind-protect (return-from a 1)
(return-from b 2))))
;;; The following returns 2.
(catch nil
(unwind-protect (throw nil 1)
(throw nil 2)))
;;; The following has undefined consequences because the catch of B is
;;; passed over by the first THROW, hence portable programs must assume
;;; its dynamic extent is terminated. The binding of the catch tag is not
;;; yet disestablished and therefore it is the target of the second throw.
(catch 'a
(catch 'b
(unwind-protect (throw 'a 1)
(throw 'b 2))))
;;; The following prints &quot;The inner catch returns :SECOND-THROW&quot;
;;; and then returns :OUTER-CATCH.
(catch 'foo
(format t &quot;The inner catch returns ~s.~%&quot;
(catch 'foo
(unwind-protect (throw 'foo :first-throw)
(throw 'foo :second-throw))))
:outer-catch)
;;; The following returns 10. The inner CATCH of A is passed over, but
;;; because that CATCH is disestablished before the THROW to A is executed,
;;; it isn't seen.
(catch 'a
(catch 'b
(unwind-protect (1+ (catch 'a (throw 'b 1)))
(throw 'a 10))))
;;; The following has undefined consequences because the extent of
;;; the (CATCH 'BAR ...) exit ends when the (THROW 'FOO ...)
;;; commences.
(catch 'foo
(catch 'bar
(unwind-protect (throw 'foo 3)
(throw 'bar 4)
(print 'xxx))))
;;; The following returns 4; XXX is not printed.
;;; The (THROW 'FOO ...) has no effect on the scope of the BAR
;;; catch tag or the extent of the (CATCH 'BAR ...) exit.
(catch 'bar
(catch 'foo
(unwind-protect (throw 'foo 3)
(throw 'bar 4)
(print 'xxx))))
;;; The following prints 5.
(block nil
(let ((x 5))
(declare (special x))
(unwind-protect (return)
(print x))))
</PRE>
</TT> <P>
<P><B>Affected By:</B> None.
<P>
<P><B>Exceptional Situations:</B> None.
<P>
<P><B>See Also:</B><P>
<P>
<A REL=DEFINITION HREF="s_catch.htm#catch"><B>catch</B></A>, <A REL=DEFINITION HREF="s_go.htm#go"><B>go</B></A>, <A REL=DEFINITION HREF="m_hand_1.htm#handler-case"><B>handler-case</B></A>, <A REL=DEFINITION HREF="m_rst_ca.htm#restart-case"><B>restart-case</B></A>, <A REL=DEFINITION HREF="m_return.htm#return"><B>return</B></A>, <A REL=DEFINITION HREF="s_ret_fr.htm#return-from"><B>return-from</B></A>, <A REL=DEFINITION HREF="s_throw.htm#throw"><B>throw</B></A>, <A REL=CHILD HREF="03_a.htm">Section 3.1 (Evaluation)</A> <P>
<P><B>Notes:</B> None.
<P>
<P><HR>The following <A REL=META HREF="../Front/X3J13Iss.htm">X3J13 cleanup issues</A>, <I>not part of the specification</I>, apply to this section:<P><UL><LI> <A REL=CHILD HREF="../Issues/iss152.htm">EXIT-EXTENT:MINIMAL</A><LI> <A REL=CHILD HREF="../Issues/iss151.htm">EXIT-EXTENT-AND-CONDITION-SYSTEM:LIKE-DYNAMIC-BINDINGS</A><P></UL><HR>
<A REL=NAVIGATOR HREF="../Front/StartPts.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Starting Points]" SRC="../Graphics/StartPts.gif" ALIGN=Bottom></A><A REL=TOC HREF="../Front/Contents.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Contents]" SRC="../Graphics/Contents.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Master.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Index]" SRC="../Graphics/Index.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Symbol.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Symbols]" SRC="../Graphics/Symbols.gif" ALIGN=Bottom></A><A REL=GLOSSARY HREF="../Body/26_a.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Glossary]" SRC="../Graphics/Glossary.gif" ALIGN=Bottom></A><A HREF="../Front/X3J13Iss.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Issues]" SRC="../Graphics/Issues.gif" ALIGN=Bottom></A><BR>
<A REL=COPYRIGHT HREF="../Front/Help.htm#Legal"><I>Copyright 1996-2005, LispWorks Ltd. All rights reserved.</I></A><P>
</BODY>
</HTML>