This file is indexed.

/usr/share/doc/mlton/guide/MLtonFinalizable is in mlton-doc 20100608-5.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta name="robots" content="index,nofollow">



<title>MLtonFinalizable - MLton Standard ML Compiler (SML Compiler)</title>
<link rel="stylesheet" type="text/css" charset="iso-8859-1" media="all" href="common.css">
<link rel="stylesheet" type="text/css" charset="iso-8859-1" media="screen" href="screen.css">
<link rel="stylesheet" type="text/css" charset="iso-8859-1" media="print" href="print.css">


<link rel="Start" href="Home">


</head>

<body lang="en" dir="ltr">

<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-833377-1";
urchinTracker();
</script>
<table bgcolor = lightblue cellspacing = 0 style = "border: 0px;" width = 100%>
  <tr>
    <td style = "
		border: 0px;
		color: darkblue; 
		font-size: 150%;
		text-align: left;">
      <a class = mltona href="Home">MLton MLTONWIKIVERSION</a>
    <td style = "
		border: 0px;
		font-size: 150%;
		text-align: center;
		width: 50%;">
      MLtonFinalizable
    <td style = "
		border: 0px;
		text-align: right;">
      <table cellspacing = 0 style = "border: 0px">
        <tr style = "vertical-align: middle;">
      </table>
  <tr style = "background-color: white;">
    <td colspan = 3
	style = "
		border: 0px;
		font-size:70%;
		text-align: right;">
      <a href = "Home">Home</a>
      &nbsp;<a href = "TitleIndex">Index</a>
      &nbsp;
</table>
<div id="content" lang="en" dir="ltr">

<pre class=code>
<B><FONT COLOR="#0000FF">signature</FONT></B> MLTON_FINALIZABLE =
   <B><FONT COLOR="#0000FF">sig</FONT></B>
      <B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> 'a t

      </FONT></B><B><FONT COLOR="#A020F0">val</FONT></B> addFinalizer: 'a t * ('a -&gt; unit) -&gt; unit
      <B><FONT COLOR="#A020F0">val</FONT></B> finalizeBefore: 'a t * 'b t -&gt; unit
      <B><FONT COLOR="#A020F0">val</FONT></B> new: 'a -&gt; 'a t
      <B><FONT COLOR="#A020F0">val</FONT></B> touch: 'a t -&gt; unit
      <B><FONT COLOR="#A020F0">val</FONT></B> withValue: 'a t * ('a -&gt; 'b) -&gt; 'b
   <B><FONT COLOR="#0000FF">end</FONT></B>
</PRE>
<p>
 
</p>
<p>
A <em>finalizable</em> value is a container to which finalizers can be attached.  A container holds a value, which is reachable as long as the container itself is reachable.  A <em>finalizer</em> is a function that runs at some point after garbage collection determines that the container to which it is attached has become <a href="Reachability">unreachable</a>.  A finalizer is treated like a signal handler, in that it runs asynchronously in a separate thread, with signals blocked, and will not interrupt a critical section (see <a href="MLtonThread">MLtonThread</a>). 
</p>

    <ul>

    <li>
<p>
 <tt>addFinalizer&nbsp;(v,&nbsp;f)</tt>  <br>
adds <tt>f</tt> as a finalizer to <tt>v</tt>.  This means that sometime  after the last call to <tt>withValue</tt> on <tt>v</tt> completes and  <tt>v</tt> becomes unreachable, <tt>f</tt> will be called with the value of  <tt>v</tt>. 
</p>
</li>
    <li class="gap">
<p>
 <tt>finalizeBefore&nbsp;(v1,&nbsp;v2)</tt>  <br>
ensures that <tt>v1</tt> will be finalized before <tt>v2</tt>.  A cycle of  values <tt>v</tt> = <tt>v1</tt>, ..., <tt>vn</tt> = <tt>v</tt> with  <tt>finalizeBefore&nbsp;(vi,&nbsp;vi+1)</tt> will result in none of the <tt>vi</tt>  being finalized. 
</p>
</li>
    <li class="gap">
<p>
 <tt>new&nbsp;x</tt>  <br>
creates a new finalizable value, <tt>v</tt>, with value <tt>x</tt>.  The  finalizers of <tt>v</tt> will run sometime after the last call to  <tt>withValue</tt> on <tt>v</tt> when the garbage collector determines that  <tt>v</tt> is unreachable. 
</p>
</li>
    <li class="gap">
<p>
 <tt>touch&nbsp;v</tt>  <br>
ensures that <tt>v</tt>'s finalizers will not run before the call to  <tt>touch</tt>. 
</p>
</li>
    <li class="gap">
<p>
 <tt>withValue&nbsp;(v,&nbsp;f)</tt>  <br>
returns the result of applying <tt>f</tt> to the value of <tt>v</tt> and  ensures that <tt>v</tt>'s finalizers will not run before <tt>f</tt>  completes.  The call to <tt>f</tt> is a nontail call. 
</p>
</li>

    </ul>


<h2 id="head-0f01ed56a1e32a05e5ef96e4d779f34784af9a96">Example</h2>
<p>
Suppose that <tt>finalizable.sml</tt> contains the following. 
</p>

<pre class=code>
<B><FONT COLOR="#0000FF">signature</FONT></B> CLIST =
   <B><FONT COLOR="#0000FF">sig</FONT></B>
      <B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> t

      </FONT></B><B><FONT COLOR="#A020F0">val</FONT></B> cons: int * t -&gt; t
      <B><FONT COLOR="#A020F0">val</FONT></B> sing: int -&gt; t
      <B><FONT COLOR="#A020F0">val</FONT></B> sum: t -&gt; int
   <B><FONT COLOR="#0000FF">end</FONT></B>

<B><FONT COLOR="#0000FF">functor</FONT></B> CList (<B><FONT COLOR="#0000FF">structure</FONT></B> F: MLTON_FINALIZABLE
               <B><FONT COLOR="#0000FF">structure</FONT></B> Prim:
                  <B><FONT COLOR="#0000FF">sig</FONT></B>
                     <B><FONT COLOR="#A020F0">val</FONT></B> cons: int * Word32.word -&gt; Word32.word
                     <B><FONT COLOR="#A020F0">val</FONT></B> free: Word32.word -&gt; unit
                     <B><FONT COLOR="#A020F0">val</FONT></B> sing: int -&gt; Word32.word
                     <B><FONT COLOR="#A020F0">val</FONT></B> sum: Word32.word -&gt; int
                  <B><FONT COLOR="#0000FF">end</FONT></B>): CLIST =
   <B><FONT COLOR="#0000FF">struct</FONT></B>
      <B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> t </FONT></B>=<B><FONT COLOR="#228B22"> Word32.word F.t

      </FONT></B><B><FONT COLOR="#A020F0">fun</FONT></B> cons (n: int, l: t) =
         F.withValue
         (l, <B><FONT COLOR="#A020F0">fn</FONT></B> w' =&gt;
          <B><FONT COLOR="#A020F0">let</FONT></B>
             <B><FONT COLOR="#A020F0">val</FONT></B> c = F.new (Prim.cons (n, w'))
             <B><FONT COLOR="#A020F0">val</FONT></B> _ = F.addFinalizer (c, Prim.free)
             <B><FONT COLOR="#A020F0">val</FONT></B> _ = F.finalizeBefore (c, l)
          <B><FONT COLOR="#A020F0">in</FONT></B>
             c
          <B><FONT COLOR="#A020F0">end</FONT></B>)
      
      <B><FONT COLOR="#A020F0">fun</FONT></B> sing n =
         <B><FONT COLOR="#A020F0">let</FONT></B>
            <B><FONT COLOR="#A020F0">val</FONT></B> c = F.new (Prim.sing n)
            <B><FONT COLOR="#A020F0">val</FONT></B> _ = F.addFinalizer (c, Prim.free)
         <B><FONT COLOR="#A020F0">in</FONT></B>
            c
         <B><FONT COLOR="#A020F0">end</FONT></B>

      <B><FONT COLOR="#A020F0">fun</FONT></B> sum c = F.withValue (c, Prim.sum)
   <B><FONT COLOR="#0000FF">end</FONT></B>

<B><FONT COLOR="#0000FF">functor</FONT></B> Test (<B><FONT COLOR="#0000FF">structure</FONT></B> CList: CLIST
              <B><FONT COLOR="#0000FF">structure</FONT></B> MLton: <B><FONT COLOR="#0000FF">sig</FONT></B>
                                  <B><FONT COLOR="#0000FF">structure</FONT></B> GC:
                                     <B><FONT COLOR="#0000FF">sig</FONT></B>
                                        <B><FONT COLOR="#A020F0">val</FONT></B> collect: unit -&gt; unit
                                     <B><FONT COLOR="#0000FF">end</FONT></B>
                               <B><FONT COLOR="#0000FF">end</FONT></B>) =
   <B><FONT COLOR="#0000FF">struct</FONT></B>
      <B><FONT COLOR="#A020F0">fun</FONT></B> f n =
         <B><FONT COLOR="#A020F0">if</FONT></B> n = <B><FONT COLOR="#5F9EA0">1</FONT></B>
            <B><FONT COLOR="#A020F0">then</FONT></B> ()
         <B><FONT COLOR="#A020F0">else</FONT></B>
            <B><FONT COLOR="#A020F0">let</FONT></B>
               <B><FONT COLOR="#A020F0">val</FONT></B> a = Array.tabulate (n, <B><FONT COLOR="#A020F0">fn</FONT></B> i =&gt; i)
               <B><FONT COLOR="#A020F0">val</FONT></B> _ = Array.sub (a, <B><FONT COLOR="#5F9EA0">0</FONT></B>) + Array.sub (a, <B><FONT COLOR="#5F9EA0">1</FONT></B>)
            <B><FONT COLOR="#A020F0">in</FONT></B>
               f (n - <B><FONT COLOR="#5F9EA0">1</FONT></B>)
            <B><FONT COLOR="#A020F0">end</FONT></B>
            
      <B><FONT COLOR="#A020F0">val</FONT></B> l = CList.sing <B><FONT COLOR="#5F9EA0">2</FONT></B>
      <B><FONT COLOR="#A020F0">val</FONT></B> l = CList.cons (<B><FONT COLOR="#5F9EA0">2</FONT></B>,l)
      <B><FONT COLOR="#A020F0">val</FONT></B> l = CList.cons (<B><FONT COLOR="#5F9EA0">2</FONT></B>,l)
      <B><FONT COLOR="#A020F0">val</FONT></B> l = CList.cons (<B><FONT COLOR="#5F9EA0">2</FONT></B>,l)
      <B><FONT COLOR="#A020F0">val</FONT></B> l = CList.cons (<B><FONT COLOR="#5F9EA0">2</FONT></B>,l)
      <B><FONT COLOR="#A020F0">val</FONT></B> l = CList.cons (<B><FONT COLOR="#5F9EA0">2</FONT></B>,l)
      <B><FONT COLOR="#A020F0">val</FONT></B> l = CList.cons (<B><FONT COLOR="#5F9EA0">2</FONT></B>,l)
      <B><FONT COLOR="#A020F0">val</FONT></B> _ = MLton.GC.collect ()
      <B><FONT COLOR="#A020F0">val</FONT></B> _ = f <B><FONT COLOR="#5F9EA0">100</FONT></B>
      <B><FONT COLOR="#A020F0">val</FONT></B> _ = print (concat [<B><FONT COLOR="#BC8F8F">&quot;listSum(l) = &quot;</FONT></B>,
                             Int.toString (CList.sum l),
                             <B><FONT COLOR="#BC8F8F">&quot;\n&quot;</FONT></B>])
      <B><FONT COLOR="#A020F0">val</FONT></B> _ = MLton.GC.collect ()
      <B><FONT COLOR="#A020F0">val</FONT></B> _ = f <B><FONT COLOR="#5F9EA0">100</FONT></B>
   <B><FONT COLOR="#0000FF">end</FONT></B>

<B><FONT COLOR="#0000FF">structure</FONT></B> CList =
   CList (<B><FONT COLOR="#0000FF">structure</FONT></B> F = MLton.Finalizable
          <B><FONT COLOR="#0000FF">structure</FONT></B> Prim =
             <B><FONT COLOR="#0000FF">struct</FONT></B>
                <B><FONT COLOR="#A020F0">val</FONT></B> cons = _import <B><FONT COLOR="#BC8F8F">&quot;listCons&quot;</FONT></B>: int * Word32.word -&gt; Word32.word;
                <B><FONT COLOR="#A020F0">val</FONT></B> free = _import <B><FONT COLOR="#BC8F8F">&quot;listFree&quot;</FONT></B>: Word32.word -&gt; unit;
                <B><FONT COLOR="#A020F0">val</FONT></B> sing = _import <B><FONT COLOR="#BC8F8F">&quot;listSing&quot;</FONT></B>: int -&gt; Word32.word;
                <B><FONT COLOR="#A020F0">val</FONT></B> sum = _import <B><FONT COLOR="#BC8F8F">&quot;listSum&quot;</FONT></B>: Word32.word -&gt; int;
             <B><FONT COLOR="#0000FF">end</FONT></B>)

<B><FONT COLOR="#0000FF">structure</FONT></B> S = Test (<B><FONT COLOR="#0000FF">structure</FONT></B> CList = CList
                    <B><FONT COLOR="#0000FF">structure</FONT></B> MLton = MLton)
</PRE>
<p>
 
</p>
<p>
Suppose that <tt>cons.c</tt> contains the following. 
<pre>#include &lt;stdio.h&gt;

typedef unsigned int uint;

typedef struct Cons {
        struct Cons *next;
        int value;
} *Cons;

Cons listCons (int n, Cons c) {
        Cons res;

        res = (Cons) malloc (sizeof(*res));
        fprintf (stderr, "0x%08x = listCons (%d)\n", (uint)res, n);
        res-&gt;next = c;
        res-&gt;value = n;
        return res;
}

Cons listSing (int n) {
        Cons res;

        res = (Cons) malloc (sizeof(*res));
        fprintf (stderr, "0x%08x = listSing (%d)\n", (uint)res, n);
        res-&gt;next = NULL;
        res-&gt;value = n;
        return res;
}

void listFree (Cons p) {
        fprintf (stderr, "listFree (0x%08x)\n", (uint)p);
        free (p);
}

int listSum (Cons c) {
        int res;

        fprintf (stderr, "listSum\n");
        res = 0;
        for (; c != NULL; c = c-&gt;next)
                res += c-&gt;value;
        return res;
}
</pre>
</p>
<p>
We can compile these to create an executable with 
<pre>% mlton -default-ann 'allowFFI true' finalizable.sml cons.c
</pre>
</p>
<p>
Running this executable will create output like the following. 
<pre>% finalizable
0x08072890 = listSing (2)
0x080728a0 = listCons (2)
0x080728b0 = listCons (2)
0x080728c0 = listCons (2)
0x080728d0 = listCons (2)
0x080728e0 = listCons (2)
0x080728f0 = listCons (2)
listSum
listSum(l) = 14
listFree (0x080728f0)
listFree (0x080728e0)
listFree (0x080728d0)
listFree (0x080728c0)
listFree (0x080728b0)
listFree (0x080728a0)
listFree (0x08072890)
</pre>
</p>
<h2 id="head-1a7d780f4e551c514e4ae9523e76c5fe42784626">Synchronous Finalizers</h2>
<p>
Finalizers in MLton are asynchronous.  That is, they run at an unspecified time, interrupting the user program.  It is also possible, and sometimes useful, to have synchronous finalizers, where the user program explicitly decides when to run enabled finalizers.  We have considered this in MLton, and it seems possible, but there are some unresolved design issues.  See the thread at 
</p>

    <ul>

    <li>
<p>
 <a class="external" href="http://mlton.org/pipermail/mlton/2004-September/016570.html"><img src="moin-www.png" alt="[WWW]" height="11" width="11">http://mlton.org/pipermail/mlton/2004-September/016570.html</a> 
</p>
</li>

    </ul>


<h2 id="head-a4bc8bf5caf54b18cea9f58e83dd4acb488deb17">Also see</h2>

    <ul>

    <li>
<p>
 <a href = "References#Boehm03">Boehm03</a> 
</p>
</li>
</ul>

</div>



<p>
<hr>
Last edited on 2007-08-23 03:44:00 by <span title="c-71-57-91-146.hsd1.il.comcast.net"><a href="MatthewFluet">MatthewFluet</a></span>.
</body></html>