45 lines
29 KiB
HTML
45 lines
29 KiB
HTML
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||
|
<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"/><meta name="viewport" content="width=device-width, initial-scale=0.8"/><title>16.3 Wills and Executors</title><link rel="stylesheet" type="text/css" href="../scribble.css" title="default"/><link rel="stylesheet" type="text/css" href="extras.css" title="default"/><link rel="stylesheet" type="text/css" href="../racket.css" title="default"/><link rel="stylesheet" type="text/css" href="../manual-style.css" title="default"/><link rel="stylesheet" type="text/css" href="../manual-racket.css" title="default"/><link rel="stylesheet" type="text/css" href="../manual-racket.css" title="default"/><link rel="stylesheet" type="text/css" href="../doc-site.css" title="default"/><script type="text/javascript" src="../scribble-common.js"></script><script type="text/javascript" src="../manual-racket.js"></script><script type="text/javascript" src="../manual-racket.js"></script><script type="text/javascript" src="../doc-site.js"></script><script type="text/javascript" src="../local-redirect/local-redirect.js"></script><script type="text/javascript" src="../local-redirect/local-user-redirect.js"></script><!--[if IE 6]><style type="text/css">.SIEHidden { overflow: hidden; }</style><![endif]--></head><body id="doc-racket-lang-org"><div class="tocset"><div class="tocview"><div class="tocviewlist tocviewlisttopspace"><div class="tocviewtitle"><table cellspacing="0" cellpadding="0"><tr><td style="width: 1em;"><a href="javascript:void(0);" title="Expand/Collapse" class="tocviewtoggle" onclick="TocviewToggle(this,"tocview_0");">►</a></td><td></td><td><a href="index.html" class="tocviewlink" data-pltdoc="x">The Racket Reference</a></td></tr></table></div><div class="tocviewsublisttop" style="display: none;" id="tocview_0"><table cellspacing="0" cellpadding="0"><tr><td align="right">1 </td><td><a href="model.html" class="tocviewlink" data-pltdoc="x">Language Model</a></td></tr><tr><td align="right">2 </td><td><a href="notation.html" class="tocviewlink" data-pltdoc="x">Notation for Documentation</a></td></tr><tr><td align="right">3 </td><td><a href="syntax.html" class="tocviewlink" data-pltdoc="x">Syntactic Forms</a></td></tr><tr><td align="right">4 </td><td><a href="data.html" class="tocviewlink" data-pltdoc="x">Datatypes</a></td></tr><tr><td align="right">5 </td><td><a href="structures.html" class="tocviewlink" data-pltdoc="x">Structures</a></td></tr><tr><td align="right">6 </td><td><a href="mzlib_class.html" class="tocviewlink" data-pltdoc="x">Classes and Objects</a></td></tr><tr><td align="right">7 </td><td><a href="mzlib_unit.html" class="tocviewlink" data-pltdoc="x">Units</a></td></tr><tr><td align="right">8 </td><td><a href="contracts.html" class="tocviewlink" data-pltdoc="x">Contracts</a></td></tr><tr><td align="right">9 </td><td><a href="match.html" class="tocviewlink" data-pltdoc="x">Pattern Matching</a></td></tr><tr><td align="right">10 </td><td><a href="control.html" class="tocviewlink" data-pltdoc="x">Control Flow</a></td></tr><tr><td align="right">11 </td><td><a href="concurrency.html" class="tocviewlink" data-pltdoc="x">Concurrency and Parallelism</a></td></tr><tr><td align="right">12 </td><td><a href="Macros.html" class="tocviewlink" data-pltdoc="x">Macros</a></td></tr><tr><td align="right">13 </td><td><a href="input-and-output.html" class="tocviewlink" data-pltdoc="x">Input and Output</a></td></tr><tr><td align="right">14 </td><td><a href="security.html" class="tocviewlink" data-pltdoc="x">Reflection and Security</a></td></tr><tr><td align="right">15 </td><td><a href="os.html" class="tocviewlink" data-pltdoc="x">Operating System</a></td></tr><tr><td align="right">16 </td><td><a href="memory.html" class="tocviewselflink" data-pltdoc="x">Memory Management</a></td></tr><tr><td align="right">17 </td><td><a href="unsafe.html" class="tocviewlink" data-pltdoc="x">Unsafe Operations</a></td></tr><tr><td align="right">18 </td><td><a href="running.html" clas
|
||
|
associated <a name="(tech._will)"></a><span style="font-style: italic">will</span> procedures
|
||
|
(a.k.a. <a name="(tech._finalizer)"></a><span style="font-style: italic">finalizers</span>). The <a href="willexecutor.html#%28tech._will%29" class="techoutside" data-pltdoc="x"><span class="techinside">will</span></a> procedure for each
|
||
|
value is ready to be executed when the value has been proven (by the
|
||
|
garbage collector) to be unreachable, except through weak references
|
||
|
(see <a href="weakbox.html" data-pltdoc="x">Weak Boxes</a>) or as the registrant for other will
|
||
|
executors. A <a href="willexecutor.html#%28tech._will%29" class="techoutside" data-pltdoc="x"><span class="techinside">will</span></a> is useful for triggering clean-up actions on
|
||
|
data associated with an unreachable value, such as closing a port
|
||
|
embedded in an object when the object is no longer used.</p><p>Calling the <span class="RktSym"><a href="willexecutor.html#%28def._%28%28quote._~23~25kernel%29._will-execute%29%29" class="RktValLink" data-pltdoc="x">will-execute</a></span> or <span class="RktSym"><a href="willexecutor.html#%28def._%28%28quote._~23~25kernel%29._will-try-execute%29%29" class="RktValLink" data-pltdoc="x">will-try-execute</a></span>
|
||
|
procedure executes a will that is ready in the specified will
|
||
|
executor. A will executor is also a <a href="sync.html#%28tech._synchronizable._event%29" class="techoutside" data-pltdoc="x"><span class="techinside">synchronizable event</span></a>, so <span class="RktSym"><a href="sync.html#%28def._%28%28quote._~23~25kernel%29._sync%29%29" class="RktValLink" data-pltdoc="x">sync</a></span>
|
||
|
or <span class="RktSym"><a href="sync.html#%28def._%28%28quote._~23~25kernel%29._sync%2Ftimeout%29%29" class="RktValLink" data-pltdoc="x">sync/timeout</a></span> can be used to detect when a will executor has
|
||
|
ready wills. Wills are not executed automatically, because certain
|
||
|
programs need control to avoid race conditions. However, a program can
|
||
|
create a thread whose sole job is to execute wills for a particular
|
||
|
executor.</p><p>If a value is registered with multiple wills (in one or multiple
|
||
|
executors), the wills are readied in the reverse order of
|
||
|
registration. Since readying a will procedure makes the value
|
||
|
reachable again, the will must be executed and the value must be
|
||
|
proven again unreachable through only weak references before another
|
||
|
of the wills is readied or executed. However, wills for distinct
|
||
|
unreachable values are readied at the same time, regardless of whether
|
||
|
the values are reachable from each other.</p><p>A will executor’s registrant is held non-weakly until after the
|
||
|
corresponding will procedure is executed. Thus, if the content value
|
||
|
of a weak box (see <a href="weakbox.html" data-pltdoc="x">Weak Boxes</a>) is registered with a will
|
||
|
executor, the weak box’s content is not changed to <span class="RktVal">#f</span> until
|
||
|
all wills have been executed for the value and the value has been
|
||
|
proven again reachable through only weak references.</p><p>A will executor can be used as a <a href="sync.html#%28tech._synchronizable._event%29" class="techoutside" data-pltdoc="x"><span class="techinside">synchronizable event</span></a> (see <a href="sync.html" data-pltdoc="x">Events</a>).
|
||
|
A will executor is <a href="sync.html#%28tech._ready._for._synchronization%29" class="techoutside" data-pltdoc="x"><span class="techinside">ready for synchronization</span></a> when
|
||
|
<span class="RktSym"><a href="willexecutor.html#%28def._%28%28quote._~23~25kernel%29._will-execute%29%29" class="RktValLink" data-pltdoc="x">will-execute</a></span> would not block; the <a href="sync.html#%28tech._synchronization._result%29" class="techoutside" data-pltdoc="x"><span class="techinside">synchronization result</span></a> of a will executor is the will executor itself.</p><p><div class="SIntrapara">These examples show how to run cleanup actions when
|
||
|
no synchronization is necessary. It simply runs the registered
|
||
|
executors as they become ready in another thread.
|
||
|
</div><div class="SIntrapara"><p><div class="SIntrapara">Examples:</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym"><a href="define.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29" class="RktStxLink" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktSym">an-executor</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="willexecutor.html#%28def._%28%28quote._~23~25kernel%29._make-will-executor%29%29" class="RktValLink" data-pltdoc="x">make-will-executor</a></span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym"><a href="void.html#%28def._%28%28quote._~23~25kernel%29._void%29%29" class="RktValLink" data-pltdoc="x">void</a></span></td></tr><tr><td><span class="hspace"> </span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="threads.html#%28def._%28%28quote._~23~25kernel%29._thread%29%29" class="RktValLink" data-pltdoc="x">thread</a></span></td></tr><tr><td><span class="hspace"> </span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="lambda.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._~ce~bb%29%29" class="RktStxLink" data-pltdoc="x">λ</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="let.html#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._let%29%29" class="RktStxLink" data-pltdoc="x">let</a></span><span class="hspace"> </span><span class="RktSym">loop</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="willexecutor.html#%28def._%28%28quote._~23~25kernel%29._will-execute%29%29" class="RktValLink" data-pltdoc="x">will-execute</a></span><span class="hspace"> </span><span class="RktSym">an-executor</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">loop</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym"><a href="define.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29" class="RktStxLink" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">executor-proc</span><span class="hspace"> </span><span class="RktSym">v</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="Writing.html#%28def._%28%28quote._~23~25kernel%29._printf%29%29" class="RktValLink" data-pltdoc="x">printf</a></span><span class="hspace"> </span><span class="RktVal">"a-box is now garbage\n"</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym"><a href="define.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29" class="RktStxLink" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktSym">a-box-to-track</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="bo
|
||
|
otherwise.</div></p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>procedure</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(def._((quote._~23~25kernel)._will-register))"></a><span title="Provided from: racket/base, racket | Package: base"><span class="RktSym"><a href="willexecutor.html#%28def._%28%28quote._~23~25kernel%29._will-register%29%29" class="RktValDef RktValLink" data-pltdoc="x">will-register</a></span></span><span class="hspace"> </span><span class="RktVar">executor</span><span class="hspace"> </span><span class="RktVar">v</span><span class="hspace"> </span><span class="RktVar">proc</span><span class="RktPn">)</span><span class="hspace"> </span>→<span class="hspace"> </span><span class="RktSym"><a href="void.html#%28def._%28%28quote._~23~25kernel%29._void~3f%29%29" class="RktValLink" data-pltdoc="x">void?</a></span></p></blockquote></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">executor</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktSym"><a href="willexecutor.html#%28def._%28%28quote._~23~25kernel%29._will-executor~3f%29%29" class="RktValLink" data-pltdoc="x">will-executor?</a></span></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">v</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktSym"><a href="data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fmisc..rkt%29._any%2Fc%29%29" class="RktValLink" data-pltdoc="x">any/c</a></span></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">proc</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fmisc..rkt%29._any%2Fc%29%29" class="RktValLink" data-pltdoc="x">any/c</a></span><span class="hspace"> </span><span class="RktPn">. </span><span class="RktSym"><a href="function-contracts.html#%28form._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._-~3e%29%29" class="RktStxLink" data-pltdoc="x"><span class="nobreak">-></span></a></span><span class="RktPn"> .</span><span class="hspace"> </span><span class="RktSym"><a href="data-structure-contracts.html#%28form._%28%28lib._racket%2Fcontract%2Fprivate%2Fmisc..rkt%29._any%29%29" class="RktStxLink" data-pltdoc="x">any</a></span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Registers the value <span class="RktVar">v</span> with the will procedure <span class="RktVar">proc</span>
|
||
|
in the will executor <span class="RktVar">executor</span>. When <span class="RktVar">v</span> is proven
|
||
|
unreachable, then the procedure <span class="RktVar">proc</span> is ready to be called
|
||
|
with <span class="RktVar">v</span> as its argument via <span class="RktSym"><a href="willexecutor.html#%28def._%28%28quote._~23~25kernel%29._will-execute%29%29" class="RktValLink" data-pltdoc="x">will-execute</a></span> or
|
||
|
<span class="RktSym"><a href="willexecutor.html#%28def._%28%28quote._~23~25kernel%29._will-try-execute%29%29" class="RktValLink" data-pltdoc="x">will-try-execute</a></span>. The <span class="RktVar">proc</span> argument is strongly
|
||
|
referenced until the will procedure is executed.</div></p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>procedure</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(def._((quote._~23~25kernel)._will-execute))"></a><span title="Provided from: racket/base, racket | Package: base"><span class="RktSym"><a href="willexecutor.html#%28def._%28%28quote._~23~25kernel%29._will-execute%29%29" class="RktValDef RktValLink" data-pltdoc="x">will-execute</a></span></span><span class="hspace"> </span><span class="RktVar">executor</span><span class="RktPn">)</span><span class="hspace"> </span>→<span class="hspace"> </span><span class="RktSym"><a href="data-structure-contracts.html#%28form._%28%28lib._racket%2Fcontract%2Fprivate%2Fmisc..rkt%29._any%29%29" class="RktStxLink" data-pltdoc="x">any</a></span></p></blockquote></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">executor</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktSym"><a href="willexecutor.html#%28def._%28%28quote._~23~25kernel%29._will-executor~3f%29%29" class="RktValLink" data-pltdoc="x">will-executor?</a></span></td></tr></table></blockquote></div><div class="SIntrapara">Invokes the will procedure for a single “unreachable” value
|
||
|
registered with the executor <span class="RktVar">executor</span>. The values returned
|
||
|
by the will procedure are the result of the <span class="RktSym"><a href="willexecutor.html#%28def._%28%28quote._~23~25kernel%29._will-execute%29%29" class="RktValLink" data-pltdoc="x">will-execute</a></span>
|
||
|
call. If no will is ready for immediate execution,
|
||
|
<span class="RktSym"><a href="willexecutor.html#%28def._%28%28quote._~23~25kernel%29._will-execute%29%29" class="RktValLink" data-pltdoc="x">will-execute</a></span> blocks until one is ready.</div></p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>procedure</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(def._((quote._~23~25kernel)._will-try-execute))"></a><span title="Provided from: racket/base, racket | Package: base"><span class="RktSym"><a href="willexecutor.html#%28def._%28%28quote._~23~25kernel%29._will-try-execute%29%29" class="RktValDef RktValLink" data-pltdoc="x">will-try-execute</a></span></span><span class="hspace"> </span><span class="RktVar">executor</span><span class="hspace"> </span><span class="RktOpt">[</span><span class="RktVar">v</span><span class="RktOpt">]</span><span class="RktPn">)</span><span class="hspace"> </span>→<span class="hspace"> </span><span class="RktSym"><a href="data-structure-contracts.html#%28form._%28%28lib._racket%2Fcontract%2Fprivate%2Fmisc..rkt%29._any%29%29" class="RktStxLink" data-pltdoc="x">any</a></span></p></blockquote></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">executor</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktSym"><a href="data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fmisc..rkt%29._any%2Fc%29%29" class="RktValLink" data-pltdoc="x">any/c</a></span></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">v</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktSym"><a href="data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fmisc..rkt%29._any%2Fc%29%29" class="RktValLink" data-pltdoc="x">any/c</a></span><span class="hspace"> </span>=<span class="hspace"> </span><span class="RktVal">#f</span></td></tr></table></blockquote></div><div class="SIntrapara">Like <span class="RktSym"><a href="willexecutor.html#%28def._%28%28quote._~23~25kernel%29._will-execute%29%29" class="RktValLink" data-pltdoc="x">will-execute</a></span> if a will is ready for immediate
|
||
|
execution. Otherwise, <span class="RktVar">v</span> is returned.</div></p><p class="SHistory">Changed in version 6.90.0.4 of package <span class="stt">base</span>: Added the <span class="RktVar">v</span> argument.</p><div class="navsetbottom"><span class="navleft"><form class="searchform"><input class="searchbox" id="searchbox" type="text" tabindex="1" placeholder="...search manuals..." title="Enter a search string to search the manuals" onkeypress="return DoSearchKey(event, this, "8.6", "../");"/></form> <a href="https://docs.racket-lang.org/index.html" title="up to the documentation top" data-pltdoc="x" onclick="return GotoPLTRoot("8.6");">top</a><span class="tocsettoggle"> <a href="javascript:void(0);" title="show/hide table of contents" onclick="TocsetToggle();">contents</a></span></span><span class="navright"> <a href="ephemerons.html" title="backward to "16.2 Ephemerons"" data-pltdoc="x">← prev</a> <a href="memory.html" title="up to "16 Memory Management"" data-pltdoc="x">up</a> <a href="garbagecollection.html" title="forward to "16.4 Garbage Collection"" data-pltdoc="x">next →</a></span> </div></div></div><div id="contextindicator"> </div></body></html>
|