about summary refs log tree commit diff stats
path: root/html/039wait.cc.html
diff options
context:
space:
mode:
Diffstat (limited to 'html/039wait.cc.html')
-rw-r--r--html/039wait.cc.html146
1 files changed, 146 insertions, 0 deletions
diff --git a/html/039wait.cc.html b/html/039wait.cc.html
new file mode 100644
index 00000000..2009d50f
--- /dev/null
+++ b/html/039wait.cc.html
@@ -0,0 +1,146 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
+<html>
+<head>
+<meta http-equiv="content-type" content="text/html; charset=UTF-8">
+<title>~/Desktop/s/mu/039wait.cc.html</title>
+<meta name="Generator" content="Vim/7.4">
+<meta name="plugin-version" content="vim7.4_v1">
+<meta name="syntax" content="cpp">
+<meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">
+<meta name="colorscheme" content="minimal">
+<style type="text/css">
+<!--
+pre { white-space: pre-wrap; font-family: monospace; color: #d0d0d0; background-color: #000000; }
+body { font-family: monospace; color: #d0d0d0; background-color: #000000; }
+* { font-size: 1em; }
+.cSpecial { color: #008000; }
+.Constant { color: #008080; }
+.Comment { color: #8080ff; }
+.Delimiter { color: #c000c0; }
+.Special { color: #ff6060; }
+.Identifier { color: #008080; }
+.traceContains { color: #008000; }
+-->
+</style>
+
+<script type='text/javascript'>
+<!--
+
+-->
+</script>
+</head>
+<body>
+<pre id='vimCodeElement'>
+<span class="Comment">//: Routines can be put in a 'waiting' state, from which it will be ready to</span>
+<span class="Comment">//: run again when a specific memory location changes its value. This is mu's</span>
+<span class="Comment">//: basic technique for orchestrating the order in which different routines</span>
+<span class="Comment">//: operate.</span>
+
+<span class="Delimiter">:(scenario wait_for_location)</span>
+recipe f1 [
+  <span class="Constant">1</span>:integer<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>:literal
+  start-running f2:recipe
+  wait-for-location <span class="Constant">1</span>:integer
+  <span class="Comment"># now wait for f2 to run and modify location 1 before using its value</span>
+  <span class="Constant">2</span>:integer<span class="Special"> &lt;- </span>copy <span class="Constant">1</span>:integer
+]
+recipe f2 [
+  <span class="Constant">1</span>:integer<span class="Special"> &lt;- </span>copy <span class="Constant">34</span>:literal
+]
+<span class="Comment"># if we got the synchronization wrong we'd be storing 0 in location 2</span>
+<span class="traceContains">+mem: storing 34 in location 2</span>
+
+<span class="Comment">//: define the new state that all routines can be in</span>
+
+<span class="Delimiter">:(before &quot;End routine States&quot;)</span>
+WAITING<span class="Delimiter">,</span>
+<span class="Delimiter">:(before &quot;End routine Fields&quot;)</span>
+<span class="Comment">// only if state == WAITING</span>
+index_t waiting_on_location<span class="Delimiter">;</span>
+int old_value_of_wating_location<span class="Delimiter">;</span>
+<span class="Delimiter">:(before &quot;End routine Constructor&quot;)</span>
+waiting_on_location = old_value_of_wating_location = <span class="Constant">0</span><span class="Delimiter">;</span>
+
+<span class="Comment">//: primitive recipe to put routines in that state</span>
+
+<span class="Delimiter">:(before &quot;End Primitive Recipe Declarations&quot;)</span>
+WAIT_FOR_LOCATION<span class="Delimiter">,</span>
+<span class="Delimiter">:(before &quot;End Primitive Recipe Numbers&quot;)</span>
+Recipe_number[<span class="Constant">&quot;wait-for-location&quot;</span>] = WAIT_FOR_LOCATION<span class="Delimiter">;</span>
+<span class="Delimiter">:(before &quot;End Primitive Recipe Implementations&quot;)</span>
+case WAIT_FOR_LOCATION: <span class="Delimiter">{</span>
+  reagent loc = canonize<span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>ingredients[<span class="Constant">0</span>]<span class="Delimiter">);</span>
+  Current_routine<span class="Delimiter">-&gt;</span>state = WAITING<span class="Delimiter">;</span>
+  Current_routine<span class="Delimiter">-&gt;</span>waiting_on_location = loc<span class="Delimiter">.</span>value<span class="Delimiter">;</span>
+  Current_routine<span class="Delimiter">-&gt;</span>old_value_of_wating_location = Memory[loc<span class="Delimiter">.</span>value]<span class="Delimiter">;</span>
+  trace<span class="Delimiter">(</span><span class="Constant">&quot;run&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;waiting for location &quot;</span> &lt;&lt; loc<span class="Delimiter">.</span>value &lt;&lt; <span class="Constant">&quot; to change from &quot;</span> &lt;&lt; Memory[loc<span class="Delimiter">.</span>value]<span class="Delimiter">;</span>
+  <span class="Identifier">break</span><span class="Delimiter">;</span>
+<span class="Delimiter">}</span>
+
+<span class="Comment">//: scheduler tweak to get routines out of that state</span>
+
+<span class="Delimiter">:(before &quot;End Scheduler State Transitions&quot;)</span>
+for <span class="Delimiter">(</span>index_t i = <span class="Constant">0</span><span class="Delimiter">;</span> i &lt; Routines<span class="Delimiter">.</span>size<span class="Delimiter">();</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
+  if <span class="Delimiter">(</span>Routines[i]<span class="Delimiter">-&gt;</span>state != WAITING<span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
+  if <span class="Delimiter">(</span>Memory[Routines[i]<span class="Delimiter">-&gt;</span>waiting_on_location] &amp;&amp;
+      Memory[Routines[i]<span class="Delimiter">-&gt;</span>waiting_on_location] != Routines[i]<span class="Delimiter">-&gt;</span>old_value_of_wating_location<span class="Delimiter">)</span> <span class="Delimiter">{</span>
+    trace<span class="Delimiter">(</span><span class="Constant">&quot;schedule&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;waking up routine</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span><span class="Delimiter">;</span>
+    Routines[i]<span class="Delimiter">-&gt;</span>state = RUNNING<span class="Delimiter">;</span>
+    Routines[i]<span class="Delimiter">-&gt;</span>waiting_on_location = Routines[i]<span class="Delimiter">-&gt;</span>old_value_of_wating_location = <span class="Constant">0</span><span class="Delimiter">;</span>
+  <span class="Delimiter">}</span>
+<span class="Delimiter">}</span>
+
+<span class="Comment">//: also allow waiting on a routine</span>
+
+<span class="Delimiter">:(scenario wait_for_routine)</span>
+recipe f1 [
+  <span class="Constant">1</span>:integer<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>:literal
+  <span class="Constant">2</span>:integer/routine<span class="Special"> &lt;- </span>start-running f2:recipe
+  wait-for-routine <span class="Constant">2</span>:integer/routine
+  <span class="Comment"># now wait for f2 to run and modify location 1 before using its value</span>
+  <span class="Constant">3</span>:integer<span class="Special"> &lt;- </span>copy <span class="Constant">1</span>:integer
+]
+recipe f2 [
+  <span class="Constant">1</span>:integer<span class="Special"> &lt;- </span>copy <span class="Constant">34</span>:literal
+]
+<span class="Comment"># if we got the synchronization wrong we'd be storing 0 in location 3</span>
+<span class="traceContains">+mem: storing 34 in location 3</span>
+
+<span class="Delimiter">:(before &quot;End routine Fields&quot;)</span>
+<span class="Comment">// only if state == WAITING</span>
+index_t waiting_on_routine<span class="Delimiter">;</span>
+<span class="Delimiter">:(before &quot;End routine Constructor&quot;)</span>
+waiting_on_routine = <span class="Constant">0</span><span class="Delimiter">;</span>
+
+<span class="Delimiter">:(before &quot;End Primitive Recipe Declarations&quot;)</span>
+WAIT_FOR_ROUTINE<span class="Delimiter">,</span>
+<span class="Delimiter">:(before &quot;End Primitive Recipe Numbers&quot;)</span>
+Recipe_number[<span class="Constant">&quot;wait-for-routine&quot;</span>] = WAIT_FOR_ROUTINE<span class="Delimiter">;</span>
+<span class="Delimiter">:(before &quot;End Primitive Recipe Implementations&quot;)</span>
+case WAIT_FOR_ROUTINE: <span class="Delimiter">{</span>
+  reagent loc = canonize<span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>ingredients[<span class="Constant">0</span>]<span class="Delimiter">);</span>
+  Current_routine<span class="Delimiter">-&gt;</span>state = WAITING<span class="Delimiter">;</span>
+  Current_routine<span class="Delimiter">-&gt;</span>waiting_on_routine = loc<span class="Delimiter">.</span>value<span class="Delimiter">;</span>
+  trace<span class="Delimiter">(</span><span class="Constant">&quot;run&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;waiting for routine &quot;</span> &lt;&lt; loc<span class="Delimiter">.</span>value<span class="Delimiter">;</span>
+  <span class="Identifier">break</span><span class="Delimiter">;</span>
+<span class="Delimiter">}</span>
+
+<span class="Delimiter">:(before &quot;End Scheduler State Transitions&quot;)</span>
+for <span class="Delimiter">(</span>index_t i = <span class="Constant">0</span><span class="Delimiter">;</span> i &lt; Routines<span class="Delimiter">.</span>size<span class="Delimiter">();</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
+  if <span class="Delimiter">(</span>Routines[i]<span class="Delimiter">-&gt;</span>state != WAITING<span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
+  if <span class="Delimiter">(</span>!Routines[i]<span class="Delimiter">-&gt;</span>waiting_on_routine<span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
+  index_t id = Routines[i]<span class="Delimiter">-&gt;</span>waiting_on_routine<span class="Delimiter">;</span>
+  assert<span class="Delimiter">(</span>id != i<span class="Delimiter">);</span>
+  for <span class="Delimiter">(</span>index_t j = <span class="Constant">0</span><span class="Delimiter">;</span> j &lt; Routines<span class="Delimiter">.</span>size<span class="Delimiter">();</span> ++j<span class="Delimiter">)</span> <span class="Delimiter">{</span>
+    if <span class="Delimiter">(</span>Routines[j]<span class="Delimiter">-&gt;</span>id == id &amp;&amp; Routines[j]<span class="Delimiter">-&gt;</span>state != WAITING<span class="Delimiter">)</span> <span class="Delimiter">{</span>
+      trace<span class="Delimiter">(</span><span class="Constant">&quot;schedule&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;waking up routine</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span><span class="Delimiter">;</span>
+      Routines[i]<span class="Delimiter">-&gt;</span>state = RUNNING<span class="Delimiter">;</span>
+      Routines[i]<span class="Delimiter">-&gt;</span>waiting_on_routine = <span class="Constant">0</span><span class="Delimiter">;</span>
+    <span class="Delimiter">}</span>
+  <span class="Delimiter">}</span>
+<span class="Delimiter">}</span>
+
+</pre>
+</body>
+</html>
+<!-- vim: set foldmethod=manual : -->