<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="generator" content="AsciiDoc 8.6.10">
<title>MLtonWorld</title>
<link rel="stylesheet" href="./asciidoc.css" type="text/css">
<link rel="stylesheet" href="./pygments.css" type="text/css">


<script type="text/javascript" src="./asciidoc.js"></script>
<script type="text/javascript">
/*<![CDATA[*/
asciidoc.install();
/*]]>*/
</script>
<link rel="stylesheet" href="./mlton.css" type="text/css">
</head>
<body class="article">
<div id="banner">
<div id="banner-home">
<a href="./Home">MLton 20201002</a>
</div>
</div>
<div id="header">
<h1>MLtonWorld</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="k">signature</span><span class="w"> </span><span class="n">MLTON_WORLD</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="k">sig</span><span class="w"></span>
<span class="w">      </span><span class="k">datatype</span><span class="w"> </span><span class="n">status</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">Clone</span><span class="w"> </span><span class="p">|</span><span class="w"> </span><span class="n">Original</span><span class="w"></span>

<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">load</span><span class="p">:</span><span class="w"> </span><span class="n">string</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">save</span><span class="p">:</span><span class="w"> </span><span class="n">string</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">status</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">saveThread</span><span class="p">:</span><span class="w"> </span><span class="n">string</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">Thread</span><span class="p">.</span><span class="n">Runnable</span><span class="p">.</span><span class="n">t</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">unit</span><span class="w"></span>
<span class="w">   </span><span class="k">end</span><span class="w"></span>
</pre></div></div></div>
<div class="ulist"><ul>
<li>
<p>
<span class="monospaced">datatype status</span>
</p>
<div class="paragraph"><p>specifies whether a world is original or restarted (a clone).</p></div>
</li>
<li>
<p>
<span class="monospaced">load f</span>
</p>
<div class="paragraph"><p>loads the saved computation from file <span class="monospaced">f</span>.</p></div>
</li>
<li>
<p>
<span class="monospaced">save f</span>
</p>
<div class="paragraph"><p>saves the entire state of the computation to the file <span class="monospaced">f</span>.  The
computation can then be restarted at a later time using <span class="monospaced">World.load</span>
or the <span class="monospaced">load-world</span> <a href="RunTimeOptions">runtime option</a>.  The call to
<span class="monospaced">save</span> in the original computation returns <span class="monospaced">Original</span> and the call in
the restarted world returns <span class="monospaced">Clone</span>.</p></div>
</li>
<li>
<p>
<span class="monospaced">saveThread (f, rt)</span>
</p>
<div class="paragraph"><p>saves the entire state of the computation to the file <span class="monospaced">f</span> that will
resume with thread <span class="monospaced">rt</span> upon restart.</p></div>
</li>
</ul></div>
</div>
</div>
<div class="sect1">
<h2 id="_notes">Notes</h2>
<div class="sectionbody">
<div class="paragraph"><p><a id="ASLR"></a>
Executables that save and load worlds are incompatible with
<a href="http://en.wikipedia.org/wiki/Address_space_layout_randomization">address space layout randomization (ASLR)</a>
of the executable (though, not of shared libraries).  The state of a
computation includes addresses into the code and data segments of the
executable (e.g., static runtime-system data, return addresses); such
addresses are invalid when interpreted by the executable loaded at a
different base address.</p></div>
<div class="paragraph"><p>Executables that save and load worlds should be compiled with an
option to suppress the generation of position-independent executables.</p></div>
<div class="ulist"><ul>
<li>
<p>
<a href="RunningOnDarwin">Darwin 11 (Mac OS X Lion) and higher</a> : <span class="monospaced">-link-opt -fno-PIE</span>
</p>
</li>
</ul></div>
</div>
</div>
<div class="sect1">
<h2 id="_example">Example</h2>
<div class="sectionbody">
<div class="paragraph"><p>Suppose that <span class="monospaced">save-world.sml</span> contains the following.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="k">open</span><span class="w"> </span><span class="n">MLton</span><span class="p">.</span><span class="n">World</span><span class="w"></span>

<span class="k">val</span><span class="w"> </span><span class="p">_</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="k">case</span><span class="w"> </span><span class="n">save</span><span class="w"> </span><span class="s">&quot;world&quot;</span><span class="w"> </span><span class="k">of</span><span class="w"></span>
<span class="w">      </span><span class="n">Original</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">print</span><span class="w"> </span><span class="s">&quot;I am the original</span><span class="se">\n</span><span class="s">&quot;</span><span class="w"></span>
<span class="w">    </span><span class="p">|</span><span class="w"> </span><span class="n">Clone</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">print</span><span class="w"> </span><span class="s">&quot;I am the clone</span><span class="se">\n</span><span class="s">&quot;</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Then, if we compile <span class="monospaced">save-world.sml</span> and run it, the <span class="monospaced">Original</span>
branch will execute, and a file named <span class="monospaced">world</span> will be created.</p></div>
<div class="listingblock">
<div class="content monospaced">
<pre>% mlton save-world.sml
% ./save-world
I am the original</pre>
</div></div>
<div class="paragraph"><p>We can then load <span class="monospaced">world</span> using the <span class="monospaced">load-world</span>
<a href="RunTimeOptions">run time option</a>.</p></div>
<div class="listingblock">
<div class="content monospaced">
<pre>% ./save-world @MLton load-world world --
I am the clone</pre>
</div></div>
</div>
</div>
</div>
<div id="footnotes"><hr></div>
<div id="footer">
<div id="footer-text">
</div>
<div id="footer-badges">
</div>
</div>
</body>
</html>
