about summary refs log tree commit diff stats
path: root/test/tc_keyapi.py
Commit message (Expand)AuthorAgeFilesLines
* Fixed unittestshut2010-04-181-0/+1
* added copyright info to config files and test caseshut2010-03-081-0/+15
* tc_keyapi: updated testcasehut2010-02-251-1/+1
* fixed unit testshut2010-01-201-2/+2
* random cleanups and fixeshut2010-01-071-0/+1
* test case for keyapi.Wrapperhut2009-12-271-0/+24
href='#n64'>64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
<!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>Mu - 038scheduler.cc</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: #eeeeee; background-color: #080808; }
body { font-family: monospace; color: #eeeeee; background-color: #080808; }
* { font-size: 1em; }
.traceContains { color: #008000; }
.cSpecial { color: #008000; }
.Constant { color: #008080; }
.SalientComment { color: #00ffff; }
.traceAbsent { color: #c00000; }
.Comment { color: #8080ff; }
.Delimiter { color: #c000c0; }
.Special { color: #ff6060; }
.CommentedCode { color: #6c6c6c; }
.Identifier { color: #008080; }
-->
</style>

<script type='text/javascript'>
<!--

-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment">//: Run a second routine concurrently using 'start-running', without any</span>
<span class="Comment">//: guarantees on how the operations in each are interleaved with each other.</span>

<span class="Delimiter">:(scenario scheduler)</span>
recipe f1 [
  start-running f2:recipe
  <span class="Comment"># wait for f2 to run</span>
  <span class="Delimiter">{</span>
    jump-unless <span class="Constant">1</span>:number<span class="Delimiter">,</span> -<span class="Constant">1</span>:literal
  <span class="Delimiter">}</span>
]
recipe f2 [
  <span class="Constant">1</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">1</span>:literal
]
<span class="traceContains">+schedule: f1</span>
<span class="traceContains">+schedule: f2</span>

<span class="Comment">//: first, add a deadline to run(routine)</span>
<span class="Comment">//: these changes are ugly and brittle; just close your nose and get through the next few lines</span>
<span class="Delimiter">:(replace &quot;void run_current_routine()&quot;)</span>
void run_current_routine<span class="Delimiter">(</span>long long int time_slice<span class="Delimiter">)</span>
<span class="Delimiter">:(replace &quot;while (!Current_routine-&gt;completed())&quot; following &quot;void run_current_routine(long long int time_slice)&quot;)</span>
long long int ninstrs = <span class="Constant">0</span><span class="Delimiter">;</span>
while <span class="Delimiter">(</span>Current_routine<span class="Delimiter">-&gt;</span>state == RUNNING &amp;&amp; ninstrs &lt; time_slice<span class="Delimiter">)</span>
<span class="Delimiter">:(after &quot;Running One Instruction&quot;)</span>
ninstrs++<span class="Delimiter">;</span>

<span class="Comment">//: now the rest of the scheduler is clean</span>

<span class="Delimiter">:(before &quot;struct routine&quot;)</span>
enum routine_state <span class="Delimiter">{</span>
  RUNNING<span class="Delimiter">,</span>
  COMPLETED<span class="Delimiter">,</span>
  <span class="Comment">// End routine States</span>
<span class="Delimiter">};</span>
<span class="Delimiter">:(before &quot;End routine Fields&quot;)</span>
enum routine_state state<span class="Delimiter">;</span>
<span class="Delimiter">:(before &quot;End routine Constructor&quot;)</span>
state = RUNNING<span class="Delimiter">;</span>

<span class="Delimiter">:(before &quot;End Globals&quot;)</span>
vector&lt;routine*&gt; Routines<span class="Delimiter">;</span>
long long int Current_routine_index = <span class="Constant">0</span><span class="Delimiter">;</span>
long long int Scheduling_interval = <span class="Constant">500</span><span class="Delimiter">;</span>
<span class="Delimiter">:(before &quot;End Setup&quot;)</span>
Scheduling_interval = <span class="Constant">500</span><span class="Delimiter">;</span>
<span class="Delimiter">:(replace{} &quot;void run(recipe_number r)&quot;)</span>
void run<span class="Delimiter">(</span>recipe_number r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="CommentedCode">//?   cerr &lt;&lt; &quot;AAA 4\n&quot;; //? 1</span>
  Routines<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>new routine<span class="Delimiter">(</span>r<span class="Delimiter">));</span>
<span class="CommentedCode">//?   cerr &lt;&lt; &quot;AAA &quot; &lt;&lt; Routines.size() &lt;&lt; &quot; routines\n&quot;; //? 1</span>
  Current_routine_index = <span class="Constant">0</span><span class="Delimiter">,</span> Current_routine = Routines<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span>
  while <span class="Delimiter">(</span>!all_routines_done<span class="Delimiter">())</span> <span class="Delimiter">{</span>
    skip_to_next_routine<span class="Delimiter">();</span>
<span class="CommentedCode">//?     cout &lt;&lt; &quot;scheduler: &quot; &lt;&lt; Current_routine_index &lt;&lt; '\n'; //? 1</span>
    assert<span class="Delimiter">(</span>Current_routine<span class="Delimiter">);</span>
    assert<span class="Delimiter">(</span>Current_routine<span class="Delimiter">-&gt;</span>state == RUNNING<span class="Delimiter">);</span>
    trace<span class="Delimiter">(</span><span class="Constant">&quot;schedule&quot;</span><span class="Delimiter">)</span> &lt;&lt; current_routine_label<span class="Delimiter">();</span>
<span class="CommentedCode">//?     trace(&quot;schedule&quot;) &lt;&lt; Current_routine_index &lt;&lt; &quot;: &quot; &lt;&lt; current_recipe_name(); //? 1</span>
<span class="CommentedCode">//?     trace(&quot;schedule&quot;) &lt;&lt; Current_routine-&gt;id &lt;&lt; &quot; &quot; &lt;&lt; current_recipe_name(); //? 1</span>
    run_current_routine<span class="Delimiter">(</span>Scheduling_interval<span class="Delimiter">);</span>
    <span class="Comment">// Scheduler State Transitions</span>
<span class="CommentedCode">//?     cerr &lt;&lt; &quot;AAA completed? &quot; &lt;&lt; Current_routine-&gt;completed() &lt;&lt; '\n'; //? 1</span>
    if <span class="Delimiter">(</span>Current_routine<span class="Delimiter">-&gt;</span>completed<span class="Delimiter">())</span>
      Current_routine<span class="Delimiter">-&gt;</span>state = COMPLETED<span class="Delimiter">;</span>
    <span class="Comment">// End Scheduler State Transitions</span>

    <span class="Comment">// Scheduler Cleanup</span>
    <span class="Comment">// End Scheduler Cleanup</span>
  <span class="Delimiter">}</span>
<span class="CommentedCode">//?   cout &lt;&lt; &quot;done with run\n&quot;; //? 1</span>
<span class="Delimiter">}</span>

<span class="Delimiter">:(code)</span>
bool all_routines_done<span class="Delimiter">()</span> <span class="Delimiter">{</span>
  for <span class="Delimiter">(</span>long long int i = <span class="Constant">0</span><span class="Delimiter">;</span> i &lt; SIZE<span class="Delimiter">(</span>Routines<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="CommentedCode">//?     cout &lt;&lt; &quot;routine &quot; &lt;&lt; i &lt;&lt; ' ' &lt;&lt; Routines.at(i)-&gt;state &lt;&lt; '\n'; //? 1</span>
    if <span class="Delimiter">(</span>Routines<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">)-&gt;</span>state == RUNNING<span class="Delimiter">)</span> <span class="Delimiter">{</span>
      <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
    <span class="Delimiter">}</span>
  <span class="Delimiter">}</span>
  <span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Comment">// skip Current_routine_index past non-RUNNING routines</span>
void skip_to_next_routine<span class="Delimiter">()</span> <span class="Delimiter">{</span>
  assert<span class="Delimiter">(</span>!Routines<span class="Delimiter">.</span>empty<span class="Delimiter">());</span>
  assert<span class="Delimiter">(</span>Current_routine_index &lt; SIZE<span class="Delimiter">(</span>Routines<span class="Delimiter">));</span>
  for <span class="Delimiter">(</span>long long int i = <span class="Delimiter">(</span>Current_routine_index+<span class="Constant">1</span><span class="Delimiter">)</span>%SIZE<span class="Delimiter">(</span>Routines<span class="Delimiter">);</span>  i != Current_routine_index<span class="Delimiter">;</span>  i = <span class="Delimiter">(</span>i+<span class="Constant">1</span><span class="Delimiter">)</span>%SIZE<span class="Delimiter">(</span>Routines<span class="Delimiter">))</span> <span class="Delimiter">{</span>
    if <span class="Delimiter">(</span>Routines<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">)-&gt;</span>state == RUNNING<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="CommentedCode">//?       cout &lt;&lt; &quot;switching to &quot; &lt;&lt; i &lt;&lt; '\n'; //? 1</span>
      Current_routine_index = i<span class="Delimiter">;</span>
      Current_routine = Routines<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
      <span class="Identifier">return</span><span class="Delimiter">;</span>
    <span class="Delimiter">}</span>
  <span class="Delimiter">}</span>
<span class="CommentedCode">//?   cout &lt;&lt; &quot;all done\n&quot;; //? 1</span>
<span class="Delimiter">}</span>

string current_routine_label<span class="Delimiter">()</span> <span class="Delimiter">{</span>
  ostringstream result<span class="Delimiter">;</span>
  call_stack calls = Current_routine<span class="Delimiter">-&gt;</span>calls<span class="Delimiter">;</span>
  for <span class="Delimiter">(</span>call_stack::iterator p = calls<span class="Delimiter">.</span>begin<span class="Delimiter">();</span> p != calls<span class="Delimiter">.</span>end<span class="Delimiter">();</span> ++p<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    if <span class="Delimiter">(</span>p != calls<span class="Delimiter">.</span>begin<span class="Delimiter">())</span> result &lt;&lt; <span class="Constant">'/'</span><span class="Delimiter">;</span>
    result &lt;&lt; Recipe[p<span class="Delimiter">-&gt;</span>running_recipe]<span class="Delimiter">.</span>name<span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
  <span class="Identifier">return</span> result<span class="Delimiter">.</span>str<span class="Delimiter">();</span>
<span class="Delimiter">}</span>

<span class="Delimiter">:(before &quot;End Teardown&quot;)</span>
for <span class="Delimiter">(</span>long long int i = <span class="Constant">0</span><span class="Delimiter">;</span> i &lt; SIZE<span class="Delimiter">(</span>Routines<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span>
  delete Routines<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
Routines<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>

<span class="SalientComment">//:: To schedule new routines to run, call 'start-running'.</span>

<span class="Comment">//: 'start-running' will return a unique id for the routine that was created.</span>
<span class="Comment">//: routine id is a number, but don't do any arithmetic on it</span>
<span class="Delimiter">:(before &quot;End routine Fields&quot;)</span>
long long int id<span class="Delimiter">;</span>
<span class="Delimiter">:(before &quot;End Globals&quot;)</span>
long long int Next_routine_id = <span class="Constant">1</span><span class="Delimiter">;</span>
<span class="Delimiter">:(before &quot;End Setup&quot;)</span>
Next_routine_id = <span class="Constant">1</span><span class="Delimiter">;</span>
<span class="Delimiter">:(before &quot;End routine Constructor&quot;)</span>
id = Next_routine_id<span class="Delimiter">;</span>
Next_routine_id++<span class="Delimiter">;</span>

<span class="Comment">//: it needs a new type: 'recipe'</span>
<span class="Delimiter">:(before &quot;End Mu Types Initialization&quot;)</span>
Type_number[<span class="Constant">&quot;recipe&quot;</span>] = <span class="Constant">0</span><span class="Delimiter">;</span>

<span class="Comment">//: routines save the routine that spawned them</span>
<span class="Delimiter">:(before &quot;End routine Fields&quot;)</span>
<span class="Comment">// todo: really should be routine_id, but that's less efficient.</span>
long long int parent_index<span class="Delimiter">;</span>  <span class="Comment">// only &lt; 0 if there's no parent_index</span>
<span class="Delimiter">:(before &quot;End routine Constructor&quot;)</span>
parent_index = -<span class="Constant">1</span><span class="Delimiter">;</span>

<span class="Delimiter">:(before &quot;End Primitive Recipe Declarations&quot;)</span>
START_RUNNING<span class="Delimiter">,</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Numbers&quot;)</span>
Recipe_number[<span class="Constant">&quot;start-running&quot;</span>] = START_RUNNING<span class="Delimiter">;</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Implementations&quot;)</span>
case START_RUNNING: <span class="Delimiter">{</span>
  assert<span class="Delimiter">(</span>isa_literal<span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)));</span>
  assert<span class="Delimiter">(</span>!current_instruction<span class="Delimiter">().</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>initialized<span class="Delimiter">);</span>
  routine* new_routine = new routine<span class="Delimiter">(</span>Recipe_number[current_instruction<span class="Delimiter">().</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>name]<span class="Delimiter">);</span>
<span class="CommentedCode">//?   cerr &lt;&lt; new_routine-&gt;id &lt;&lt; &quot; -&gt; &quot; &lt;&lt; Current_routine-&gt;id &lt;&lt; '\n'; //? 1</span>
  new_routine<span class="Delimiter">-&gt;</span>parent_index = Current_routine_index<span class="Delimiter">;</span>
  <span class="Comment">// populate ingredients</span>
  for <span class="Delimiter">(</span>long long int i = <span class="Constant">1</span><span class="Delimiter">;</span> i &lt; SIZE<span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>ingredients<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span>
    new_routine<span class="Delimiter">-&gt;</span>calls<span class="Delimiter">.</span>front<span class="Delimiter">().</span>ingredient_atoms<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">));</span>
  Routines<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>new_routine<span class="Delimiter">);</span>
  products<span class="Delimiter">.</span>resize<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">);</span>
  products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>push_back<span class="Delimiter">(</span>new_routine<span class="Delimiter">-&gt;</span>id<span class="Delimiter">);</span>
  <span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Delimiter">:(scenario scheduler_runs_single_routine)</span>
<span class="Special">% Scheduling_interval = 1;</span>
recipe f1 [
  <span class="Constant">1</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>:literal
  <span class="Constant">2</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>:literal
]
<span class="traceContains">+schedule: f1</span>
<span class="traceContains">+run: 1:number &lt;- copy 0:literal</span>
<span class="traceContains">+schedule: f1</span>
<span class="traceContains">+run: 2:number &lt;- copy 0:literal</span>

<span class="Delimiter">:(scenario scheduler_interleaves_routines)</span>
<span class="Special">% Scheduling_interval = 1;</span>
recipe f1 [
  start-running f2:recipe
  <span class="Constant">1</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>:literal
  <span class="Constant">2</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>:literal
]
recipe f2 [
  <span class="Constant">3</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>:literal
  <span class="Constant">4</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>:literal
]
<span class="traceContains">+schedule: f1</span>
<span class="traceContains">+run: start-running f2:recipe</span>
<span class="traceContains">+schedule: f2</span>
<span class="traceContains">+run: 3:number &lt;- copy 0:literal</span>
<span class="traceContains">+schedule: f1</span>
<span class="traceContains">+run: 1:number &lt;- copy 0:literal</span>
<span class="traceContains">+schedule: f2</span>
<span class="traceContains">+run: 4:number &lt;- copy 0:literal</span>
<span class="traceContains">+schedule: f1</span>
<span class="traceContains">+run: 2:number &lt;- copy 0:literal</span>

<span class="Delimiter">:(scenario start_running_takes_args)</span>
recipe f1 [
  start-running f2:recipe<span class="Delimiter">,</span> <span class="Constant">3</span>:literal
  <span class="Comment"># wait for f2 to run</span>
  <span class="Delimiter">{</span>
    jump-unless <span class="Constant">1</span>:number<span class="Delimiter">,</span> -<span class="Constant">1</span>:literal
  <span class="Delimiter">}</span>
]
recipe f2 [
  <span class="Constant">1</span>:number<span class="Special"> &lt;- </span>next-ingredient
  <span class="Constant">2</span>:number<span class="Special"> &lt;- </span>add <span class="Constant">1</span>:number<span class="Delimiter">,</span> <span class="Constant">1</span>:literal
]
<span class="traceContains">+mem: storing 4 in location 2</span>

<span class="Delimiter">:(scenario start_running_returns_routine_id)</span>
recipe f1 [
  <span class="Constant">1</span>:number<span class="Special"> &lt;- </span>start-running f2:recipe
]
recipe f2 [
  <span class="Constant">12</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">44</span>:literal
]
<span class="traceContains">+mem: storing 2 in location 1</span>

<span class="Comment">//: this scenario will require some careful setup in escaped C++</span>
<span class="Comment">//: (straining our tangle capabilities to near-breaking point)</span>
<span class="Delimiter">:(scenario scheduler_skips_completed_routines)</span>
<span class="Special">% recipe_number f1 = load(&quot;recipe f1 [\n1:number &lt;- copy 0:literal\n]&quot;).front();</span>
<span class="Special">% recipe_number f2 = load(&quot;recipe f2 [\n2:number &lt;- copy 0:literal\n]&quot;).front();</span>
<span class="Special">% Routines.push_back(new routine(f1));  // f1 meant to run</span>
<span class="Special">% Routines.push_back(new routine(f2));</span>
<span class="Special">% Routines.back()-&gt;state = COMPLETED;  // f2 not meant to run</span>
<span class="CommentedCode">#? % Trace_stream-&gt;dump_layer = &quot;all&quot;;</span>
<span class="Comment"># must have at least one routine without escaping</span>
recipe f3 [
  <span class="Constant">3</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>:literal
]
<span class="Comment"># by interleaving '+' lines with '-' lines, we allow f1 and f3 to run in any order</span>
<span class="traceContains">+schedule: f1</span>
<span class="traceContains">+mem: storing 0 in location 1</span>
<span class="traceAbsent">-schedule: f2</span>
<span class="traceAbsent">-mem: storing 0 in location 2</span>
<span class="traceContains">+schedule: f3</span>
<span class="traceContains">+mem: storing 0 in location 3</span>

<span class="Delimiter">:(scenario scheduler_starts_at_middle_of_routines)</span>
<span class="Special">% Routines.push_back(new routine(COPY));</span>
<span class="Special">% Routines.back()-&gt;state = COMPLETED;</span>
recipe f1 [
  <span class="Constant">1</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>:literal
  <span class="Constant">2</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>:literal
]
<span class="traceContains">+schedule: f1</span>
<span class="traceAbsent">-run: idle</span>

<span class="SalientComment">//:: Routines are marked completed when their parent completes.</span>

<span class="Delimiter">:(scenario scheduler_kills_orphans)</span>
recipe main [
  start-running f1:recipe
  <span class="Comment"># f1 never actually runs because its parent completes without waiting for it</span>
]
recipe f1 [
  <span class="Constant">1</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>:literal
]
<span class="traceAbsent">-schedule: f1</span>

<span class="Delimiter">:(before &quot;End Scheduler Cleanup&quot;)</span>
<span class="CommentedCode">//? trace(&quot;schedule&quot;) &lt;&lt; &quot;Before cleanup&quot;; //? 1</span>
<span class="CommentedCode">//? for (long long int i = 0; i &lt; SIZE(Routines); ++i) { //? 1</span>
<span class="CommentedCode">//?   trace(&quot;schedule&quot;) &lt;&lt; i &lt;&lt; &quot;: &quot; &lt;&lt; Routines.at(i)-&gt;id &lt;&lt; ' ' &lt;&lt; Routines.at(i)-&gt;state &lt;&lt; ' ' &lt;&lt; Routines.at(i)-&gt;parent_index &lt;&lt; ' ' &lt;&lt; Routines.at(i)-&gt;state; //? 1</span>
<span class="CommentedCode">//? } //? 1</span>
for <span class="Delimiter">(</span>long long int i = <span class="Constant">0</span><span class="Delimiter">;</span> i &lt; SIZE<span class="Delimiter">(</span>Routines<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  if <span class="Delimiter">(</span>Routines<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">)-&gt;</span>state == COMPLETED<span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
  if <span class="Delimiter">(</span>Routines<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">)-&gt;</span>parent_index &lt; <span class="Constant">0</span><span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>  <span class="Comment">// root thread</span>
<span class="CommentedCode">//?   trace(&quot;schedule&quot;) &lt;&lt; &quot;AAA &quot; &lt;&lt; i; //? 1</span>
  if <span class="Delimiter">(</span>has_completed_parent<span class="Delimiter">(</span>i<span class="Delimiter">))</span> <span class="Delimiter">{</span>
<span class="CommentedCode">//?     trace(&quot;schedule&quot;) &lt;&lt; &quot;BBB &quot; &lt;&lt; i; //? 1</span>
    Routines<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">)-&gt;</span>state = COMPLETED<span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="CommentedCode">//? trace(&quot;schedule&quot;) &lt;&lt; &quot;After cleanup&quot;; //? 1</span>
<span class="CommentedCode">//? for (long long int i = 0; i &lt; SIZE(Routines); ++i) { //? 1</span>
<span class="CommentedCode">//?   trace(&quot;schedule&quot;) &lt;&lt; i &lt;&lt; &quot;: &quot; &lt;&lt; Routines.at(i)-&gt;id &lt;&lt; ' ' &lt;&lt; Routines.at(i)-&gt;state &lt;&lt; ' ' &lt;&lt; Routines.at(i)-&gt;parent_index &lt;&lt; ' ' &lt;&lt; Routines.at(i)-&gt;state; //? 1</span>
<span class="CommentedCode">//? } //? 1</span>

<span class="Delimiter">:(code)</span>
bool has_completed_parent<span class="Delimiter">(</span>long long int routine_index<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="CommentedCode">//?   trace(&quot;schedule&quot;) &lt;&lt; &quot;CCC &quot; &lt;&lt; routine_index &lt;&lt; '\n'; //? 2</span>
  for <span class="Delimiter">(</span>long long int j = routine_index<span class="Delimiter">;</span> j &gt;= <span class="Constant">0</span><span class="Delimiter">;</span> j = Routines<span class="Delimiter">.</span>at<span class="Delimiter">(</span>j<span class="Delimiter">)-&gt;</span>parent_index<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="CommentedCode">//?     trace(&quot;schedule&quot;) &lt;&lt; &quot;DDD &quot; &lt;&lt; j &lt;&lt; '\n'; //? 2</span>
    if <span class="Delimiter">(</span>Routines<span class="Delimiter">.</span>at<span class="Delimiter">(</span>j<span class="Delimiter">)-&gt;</span>state == COMPLETED<span class="Delimiter">)</span>
      <span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
  <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="SalientComment">//:: 'routine-state' can tell if a given routine id is running</span>

<span class="Delimiter">:(scenario routine_state_test)</span>
<span class="Special">% Scheduling_interval = 2;</span>
recipe f1 [
  <span class="Constant">1</span>:number/child-id<span class="Special"> &lt;- </span>start-running f2:recipe
  <span class="Constant">12</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>:literal  <span class="Comment"># race condition since we don't care about location 12</span>
  <span class="Comment"># thanks to Scheduling_interval, f2's one instruction runs in between here and completes</span>
  <span class="Constant">2</span>:number/state<span class="Special"> &lt;- </span>routine-state <span class="Constant">1</span>:number/child-id
]
recipe f2 [
  <span class="Constant">12</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>:literal
  <span class="Comment"># trying to run a second instruction marks routine as completed</span>
]
<span class="Comment"># recipe f2 should be in state COMPLETED</span>
<span class="traceContains">+mem: storing 1 in location 2</span>

<span class="Delimiter">:(before &quot;End Primitive Recipe Declarations&quot;)</span>
ROUTINE_STATE<span class="Delimiter">,</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Numbers&quot;)</span>
Recipe_number[<span class="Constant">&quot;routine-state&quot;</span>] = ROUTINE_STATE<span class="Delimiter">;</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Implementations&quot;)</span>
case ROUTINE_STATE: <span class="Delimiter">{</span>
  assert<span class="Delimiter">(</span>scalar<span class="Delimiter">(</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)));</span>
  long long int id = ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span>
  long long int result = -<span class="Constant">1</span><span class="Delimiter">;</span>
  for <span class="Delimiter">(</span>long long int i = <span class="Constant">0</span><span class="Delimiter">;</span> i &lt; SIZE<span class="Delimiter">(</span>Routines<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    if <span class="Delimiter">(</span>Routines<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">)-&gt;</span>id == id<span class="Delimiter">)</span> <span class="Delimiter">{</span>
      result = Routines<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">)-&gt;</span>state<span class="Delimiter">;</span>
      <span class="Identifier">break</span><span class="Delimiter">;</span>
    <span class="Delimiter">}</span>
  <span class="Delimiter">}</span>
  products<span class="Delimiter">.</span>resize<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">);</span>
  products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>push_back<span class="Delimiter">(</span>result<span class="Delimiter">);</span>
  <span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="SalientComment">//:: miscellaneous helpers</span>

<span class="Delimiter">:(before &quot;End Primitive Recipe Declarations&quot;)</span>
RESTART<span class="Delimiter">,</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Numbers&quot;)</span>
Recipe_number[<span class="Constant">&quot;restart&quot;</span>] = RESTART<span class="Delimiter">;</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Implementations&quot;)</span>
case RESTART: <span class="Delimiter">{</span>
  assert<span class="Delimiter">(</span>scalar<span class="Delimiter">(</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)));</span>
  long long int id = ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span>
  for <span class="Delimiter">(</span>long long int i = <span class="Constant">0</span><span class="Delimiter">;</span> i &lt; SIZE<span class="Delimiter">(</span>Routines<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    if <span class="Delimiter">(</span>Routines<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">)-&gt;</span>id == id<span class="Delimiter">)</span> <span class="Delimiter">{</span>
      Routines<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">)-&gt;</span>state = RUNNING<span class="Delimiter">;</span>
      <span class="Identifier">break</span><span class="Delimiter">;</span>
    <span class="Delimiter">}</span>
  <span class="Delimiter">}</span>
  <span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Delimiter">:(before &quot;End Primitive Recipe Declarations&quot;)</span>
STOP<span class="Delimiter">,</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Numbers&quot;)</span>
Recipe_number[<span class="Constant">&quot;stop&quot;</span>] = STOP<span class="Delimiter">;</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Implementations&quot;)</span>
case STOP: <span class="Delimiter">{</span>
  assert<span class="Delimiter">(</span>scalar<span class="Delimiter">(</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)));</span>
  long long int id = ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span>
  for <span class="Delimiter">(</span>long long int i = <span class="Constant">0</span><span class="Delimiter">;</span> i &lt; SIZE<span class="Delimiter">(</span>Routines<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    if <span class="Delimiter">(</span>Routines<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">)-&gt;</span>id == id<span class="Delimiter">)</span> <span class="Delimiter">{</span>
      Routines<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">)-&gt;</span>state = COMPLETED<span class="Delimiter">;</span>
      <span class="Identifier">break</span><span class="Delimiter">;</span>
    <span class="Delimiter">}</span>
  <span class="Delimiter">}</span>
  <span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Delimiter">:(before &quot;End Primitive Recipe Declarations&quot;)</span>
_DUMP_ROUTINES<span class="Delimiter">,</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Numbers&quot;)</span>
Recipe_number[<span class="Constant">&quot;$dump-routines&quot;</span>] = _DUMP_ROUTINES<span class="Delimiter">;</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Implementations&quot;)</span>
case _DUMP_ROUTINES: <span class="Delimiter">{</span>
  for <span class="Delimiter">(</span>long long int i = <span class="Constant">0</span><span class="Delimiter">;</span> i &lt; SIZE<span class="Delimiter">(</span>Routines<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    cerr &lt;&lt; i &lt;&lt; <span class="Constant">&quot;: &quot;</span> &lt;&lt; Routines<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">)-&gt;</span>id &lt;&lt; <span class="Constant">' '</span> &lt;&lt; Routines<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">)-&gt;</span>state &lt;&lt; <span class="Constant">' '</span> &lt;&lt; Routines<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">)-&gt;</span>parent_index &lt;&lt; <span class="cSpecial">'\n'</span><span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
  <span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->