Import Upstream version 20180207
[hcoop/debian/mlton.git] / doc / guide / src / MLtonWorld.adoc
CommitLineData
7f918cf1
CE
1MLtonWorld
2==========
3
4[source,sml]
5----
6signature MLTON_WORLD =
7 sig
8 datatype status = Clone | Original
9
10 val load: string -> 'a
11 val save: string -> status
12 val saveThread: string * Thread.Runnable.t -> unit
13 end
14----
15
16* `datatype status`
17+
18specifies whether a world is original or restarted (a clone).
19
20* `load f`
21+
22loads the saved computation from file `f`.
23
24* `save f`
25+
26saves the entire state of the computation to the file `f`. The
27computation can then be restarted at a later time using `World.load`
28or the `load-world` <:RunTimeOptions:runtime option>. The call to
29`save` in the original computation returns `Original` and the call in
30the restarted world returns `Clone`.
31
32* `saveThread (f, rt)`
33+
34saves the entire state of the computation to the file `f` that will
35resume with thread `rt` upon restart.
36
37
38== Notes ==
39
40<!Anchor(ASLR)>
41Executables that save and load worlds are incompatible with
42http://en.wikipedia.org/wiki/Address_space_layout_randomization[address space layout randomization (ASLR)]
43of the executable (though, not of shared libraries). The state of a
44computation includes addresses into the code and data segments of the
45executable (e.g., static runtime-system data, return addresses); such
46addresses are invalid when interpreted by the executable loaded at a
47different base address.
48
49Executables that save and load worlds should be compiled with an
50option to suppress the generation of position-independent executables.
51
52* <:RunningOnDarwin:Darwin 11 (Mac OS X Lion) and higher> : `-link-opt -fno-PIE`
53
54
55== Example ==
56
57Suppose that `save-world.sml` contains the following.
58[source,sml]
59----
60sys::[./bin/InclGitFile.py mlton master doc/examples/save-world/save-world.sml]
61----
62
63Then, if we compile `save-world.sml` and run it, the `Original`
64branch will execute, and a file named `world` will be created.
65----
66% mlton save-world.sml
67% ./save-world
68I am the original
69----
70
71We can then load `world` using the `load-world`
72<:RunTimeOptions:run time option>.
73----
74% ./save-world @MLton load-world world --
75I am the clone
76----