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