| 1 |
= MLtonFinalizable |
| 2 |
|
| 3 |
[source,sml] |
| 4 |
---- |
| 5 |
signature MLTON_FINALIZABLE = |
| 6 |
sig |
| 7 |
type 'a t |
| 8 |
|
| 9 |
val addFinalizer: 'a t * ('a -> unit) -> unit |
| 10 |
val finalizeBefore: 'a t * 'b t -> unit |
| 11 |
val new: 'a -> 'a t |
| 12 |
val touch: 'a t -> unit |
| 13 |
val withValue: 'a t * ('a -> 'b) -> 'b |
| 14 |
end |
| 15 |
---- |
| 16 |
|
| 17 |
A _finalizable_ value is a container to which finalizers can be |
| 18 |
attached. A container holds a value, which is reachable as long as |
| 19 |
the container itself is reachable. A _finalizer_ is a function that |
| 20 |
runs at some point after garbage collection determines that the |
| 21 |
container to which it is attached has become |
| 22 |
<<Reachability#,unreachable>>. A finalizer is treated like a signal |
| 23 |
handler, in that it runs asynchronously in a separate thread, with |
| 24 |
signals blocked, and will not interrupt a critical section (see |
| 25 |
<<MLtonThread#>>). |
| 26 |
|
| 27 |
* `addFinalizer (v, f)` |
| 28 |
+ |
| 29 |
adds `f` as a finalizer to `v`. This means that sometime after the |
| 30 |
last call to `withValue` on `v` completes and `v` becomes unreachable, |
| 31 |
`f` will be called with the value of `v`. |
| 32 |
|
| 33 |
* `finalizeBefore (v1, v2)` |
| 34 |
+ |
| 35 |
ensures that `v1` will be finalized before `v2`. A cycle of values |
| 36 |
`v` = `v1`, ..., `vn` = `v` with `finalizeBefore (vi, vi+1)` will |
| 37 |
result in none of the `vi` being finalized. |
| 38 |
|
| 39 |
* `new x` |
| 40 |
+ |
| 41 |
creates a new finalizable value, `v`, with value `x`. The finalizers |
| 42 |
of `v` will run sometime after the last call to `withValue` on `v` |
| 43 |
when the garbage collector determines that `v` is unreachable. |
| 44 |
|
| 45 |
* `touch v` |
| 46 |
+ |
| 47 |
ensures that `v`’s finalizers will not run before the call to `touch`. |
| 48 |
|
| 49 |
* `withValue (v, f)` |
| 50 |
+ |
| 51 |
returns the result of applying `f` to the value of `v` and ensures |
| 52 |
that `v`’s finalizers will not run before `f` completes. The call to |
| 53 |
`f` is a nontail call. |
| 54 |
|
| 55 |
|
| 56 |
== Example |
| 57 |
|
| 58 |
Suppose that `finalizable.sml` contains the following: |
| 59 |
[source,sml] |
| 60 |
---- |
| 61 |
include::https://raw.github.com/MLton/mlton/master/doc/examples/finalizable/finalizable.sml[indent=0] |
| 62 |
---- |
| 63 |
|
| 64 |
Suppose that `cons.c` contains the following. |
| 65 |
[source,c] |
| 66 |
---- |
| 67 |
include::https://raw.github.com/MLton/mlton/master/doc/examples/finalizable/cons.c[indent=0] |
| 68 |
---- |
| 69 |
|
| 70 |
We can compile these to create an executable with |
| 71 |
---- |
| 72 |
% mlton -default-ann 'allowFFI true' finalizable.sml cons.c |
| 73 |
---- |
| 74 |
|
| 75 |
Running this executable will create output like the following. |
| 76 |
---- |
| 77 |
% finalizable |
| 78 |
0x08072890 = listSing (2) |
| 79 |
0x080728a0 = listCons (2) |
| 80 |
0x080728b0 = listCons (2) |
| 81 |
0x080728c0 = listCons (2) |
| 82 |
0x080728d0 = listCons (2) |
| 83 |
0x080728e0 = listCons (2) |
| 84 |
0x080728f0 = listCons (2) |
| 85 |
listSum |
| 86 |
listSum(l) = 14 |
| 87 |
listFree (0x080728f0) |
| 88 |
listFree (0x080728e0) |
| 89 |
listFree (0x080728d0) |
| 90 |
listFree (0x080728c0) |
| 91 |
listFree (0x080728b0) |
| 92 |
listFree (0x080728a0) |
| 93 |
listFree (0x08072890) |
| 94 |
---- |
| 95 |
|
| 96 |
|
| 97 |
== Synchronous Finalizers |
| 98 |
|
| 99 |
Finalizers in MLton are asynchronous. That is, they run at an |
| 100 |
unspecified time, interrupting the user program. It is also possible, |
| 101 |
and sometimes useful, to have synchronous finalizers, where the user |
| 102 |
program explicitly decides when to run enabled finalizers. We have |
| 103 |
considered this in MLton, and it seems possible, but there are some |
| 104 |
unresolved design issues. See the thread at |
| 105 |
|
| 106 |
* http://www.mlton.org/pipermail/mlton/2004-September/016570.html |
| 107 |
|
| 108 |
== Also see |
| 109 |
|
| 110 |
* <<References#Boehm03,Boehm03>> |