summary refs log blame commit diff stats
path: root/doc/pydoc/ranger.gui.widgets.taskview.html
blob: 1c34e6816c8f29a9ced32cdc6bbb60ddb3666de7 (plain) (tree)




































































































































                                                                                                                                                                                                                                                                                                                          
                                                                                                          






                                                                                                                         
                                                                                                                                                                                                                                               








                                                                                                                                    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><title>Python: module ranger.gui.widgets.taskview</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head><body bgcolor="#f0f0f8">

<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="heading">
<tr bgcolor="#7799ee">
<td valign=bottom>&nbsp;<br>
<font color="#ffffff" face="helvetica, arial">&nbsp;<br><big><big><strong><a href="ranger.html"><font color="#ffffff">ranger</font></a>.<a href="ranger.gui.html"><font color="#ffffff">gui</font></a>.<a href="ranger.gui.widgets.html"><font color="#ffffff">widgets</font></a>.taskview</strong></big></big></font></td
><td align=right valign=bottom
><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:/home/hut/work/ranger/ranger/gui/widgets/taskview.py">/home/hut/work/ranger/ranger/gui/widgets/taskview.py</a></font></td></tr></table>
    <p><tt>The&nbsp;<a href="#TaskView">TaskView</a>&nbsp;allows&nbsp;you&nbsp;to&nbsp;modify&nbsp;what&nbsp;the&nbsp;loader&nbsp;is&nbsp;doing.</tt></p>
<p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#aa55cc">
<td colspan=3 valign=bottom>&nbsp;<br>
<font color="#ffffff" face="helvetica, arial"><big><strong>Modules</strong></big></font></td></tr>
    
<tr><td bgcolor="#aa55cc"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
<td width="100%"><table width="100%" summary="list"><tr><td width="25%" valign=top><a href="curses.html">curses</a><br>
</td><td width="25%" valign=top></td><td width="25%" valign=top></td><td width="25%" valign=top></td></tr></table></td></tr></table><p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#ee77aa">
<td colspan=3 valign=bottom>&nbsp;<br>
<font color="#ffffff" face="helvetica, arial"><big><strong>Classes</strong></big></font></td></tr>
    
<tr><td bgcolor="#ee77aa"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
<td width="100%"><dl>
<dt><font face="helvetica, arial"><a href="ranger.ext.accumulator.html#Accumulator">ranger.ext.accumulator.Accumulator</a>(<a href="builtins.html#object">builtins.object</a>)
</font></dt><dd>
<dl>
<dt><font face="helvetica, arial"><a href="ranger.gui.widgets.taskview.html#TaskView">TaskView</a>(<a href="ranger.gui.widgets.html#Widget">ranger.gui.widgets.Widget</a>, <a href="ranger.ext.accumulator.html#Accumulator">ranger.ext.accumulator.Accumulator</a>)
</font></dt></dl>
</dd>
<dt><font face="helvetica, arial"><a href="ranger.gui.widgets.html#Widget">ranger.gui.widgets.Widget</a>(<a href="ranger.gui.displayable.html#Displayable">ranger.gui.displayable.Displayable</a>)
</font></dt><dd>
<dl>
<dt><font face="helvetica, arial"><a href="ranger.gui.widgets.taskview.html#TaskView">TaskView</a>(<a href="ranger.gui.widgets.html#Widget">ranger.gui.widgets.Widget</a>, <a href="ranger.ext.accumulator.html#Accumulator">ranger.ext.accumulator.Accumulator</a>)
</font></dt></dl>
</dd>
</dl>
 <p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#ffc8d8">
<td colspan=3 valign=bottom>&nbsp;<br>
<font color="#000000" face="helvetica, arial"><a name="TaskView">class <strong>TaskView</strong></a>(<a href="ranger.gui.widgets.html#Widget">ranger.gui.widgets.Widget</a>, <a href="ranger.ext.accumulator.html#Accumulator">ranger.ext.accumulator.Accumulator</a>)</font></td></tr>
    
<tr><td bgcolor="#ffc8d8"><tt>&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
<td width="100%"><dl><dt>Method resolution order:</dt>
<dd><a href="ranger.gui.widgets.taskview.html#TaskView">TaskView</a></dd>
<dd><a href="ranger.gui.widgets.html#Widget">ranger.gui.widgets.Widget</a></dd>
<dd><a href="ranger.gui.displayable.html#Displayable">ranger.gui.displayable.Displayable</a></dd>
<dd><a href="ranger.shared.html#EnvironmentAware">ranger.shared.EnvironmentAware</a></dd>
<dd><a href="ranger.shared.html#FileManagerAware">ranger.shared.FileManagerAware</a></dd>
<dd><a href="ranger.shared.html#Awareness">ranger.shared.Awareness</a></dd>
<dd><a href="ranger.gui.curses_shortcuts.html#CursesShortcuts">ranger.gui.curses_shortcuts.CursesShortcuts</a></dd>
<dd><a href="ranger.shared.settings.html#SettingsAware">ranger.shared.settings.SettingsAware</a></dd>
<dd><a href="ranger.ext.accumulator.html#Accumulator">ranger.ext.accumulator.Accumulator</a></dd>
<dd><a href="builtins.html#object">builtins.object</a></dd>
</dl>
<hr>
Methods defined here:<br>
<dl><dt><a name="TaskView-__init__"><strong>__init__</strong></a>(self, win)</dt></dl>

<dl><dt><a name="TaskView-draw"><strong>draw</strong></a>(self)</dt></dl>

<dl><dt><a name="TaskView-get_list"><strong>get_list</strong></a>(self)</dt></dl>

<dl><dt><a name="TaskView-press"><strong>press</strong></a>(self, key)</dt></dl>

<dl><dt><a name="TaskView-task_move"><strong>task_move</strong></a>(self, absolute, i<font color="#909090">=None</font>)</dt></dl>

<dl><dt><a name="TaskView-task_remove"><strong>task_remove</strong></a>(self, i<font color="#909090">=None</font>)</dt></dl>

<hr>
Data and other attributes defined here:<br>
<dl><dt><strong>old_lst</strong> = None</dl>

<hr>
Methods inherited from <a href="ranger.gui.displayable.html#Displayable">ranger.gui.displayable.Displayable</a>:<br>
<dl><dt><a name="TaskView-__contains__"><strong>__contains__</strong></a>(self, item)</dt><dd><tt>Is&nbsp;item&nbsp;inside&nbsp;the&nbsp;boundaries?<br>
item&nbsp;can&nbsp;be&nbsp;an&nbsp;iterable&nbsp;like&nbsp;[y,&nbsp;x]&nbsp;or&nbsp;an&nbsp;object&nbsp;with&nbsp;x&nbsp;and&nbsp;y&nbsp;methods.</tt></dd></dl>

<dl><dt><a name="TaskView-__nonzero__"><strong>__nonzero__</strong></a>(self)</dt><dd><tt>Always&nbsp;True</tt></dd></dl>

<dl><dt><a name="TaskView-__str__"><strong>__str__</strong></a>(self)</dt></dl>

<dl><dt><a name="TaskView-click"><strong>click</strong></a>(self, event)</dt><dd><tt>Called&nbsp;when&nbsp;a&nbsp;mouse&nbsp;key&nbsp;is&nbsp;pressed&nbsp;and&nbsp;self.<strong>focused</strong>&nbsp;is&nbsp;True.<br>
Override&nbsp;this!</tt></dd></dl>

<dl><dt><a name="TaskView-contains_point"><strong>contains_point</strong></a>(self, y, x)</dt><dd><tt>Test&nbsp;whether&nbsp;the&nbsp;point&nbsp;(with&nbsp;absolute&nbsp;coordinates)&nbsp;lies<br>
within&nbsp;the&nbsp;boundaries&nbsp;of&nbsp;this&nbsp;object.</tt></dd></dl>

<dl><dt><a name="TaskView-destroy"><strong>destroy</strong></a>(self)</dt><dd><tt>Called&nbsp;when&nbsp;the&nbsp;object&nbsp;is&nbsp;destroyed.<br>
Override&nbsp;this!</tt></dd></dl>

<dl><dt><a name="TaskView-finalize"><strong>finalize</strong></a>(self)</dt><dd><tt>Called&nbsp;after&nbsp;every&nbsp;displayable&nbsp;is&nbsp;done&nbsp;drawing.<br>
Override&nbsp;this!</tt></dd></dl>

<dl><dt><a name="TaskView-poke"><strong>poke</strong></a>(self)</dt><dd><tt>Called&nbsp;before&nbsp;drawing,&nbsp;even&nbsp;if&nbsp;invisible</tt></dd></dl>

<dl><dt><a name="TaskView-resize"><strong>resize</strong></a>(self, y, x, hei<font color="#909090">=None</font>, wid<font color="#909090">=None</font>)</dt><dd><tt>Resize&nbsp;the&nbsp;widget</tt></dd></dl>

<hr>
Data and other attributes inherited from <a href="ranger.shared.html#EnvironmentAware">ranger.shared.EnvironmentAware</a>:<br>
<dl><dt><strong>env</strong> = None</dl>

<hr>
Data and other attributes inherited from <a href="ranger.shared.html#FileManagerAware">ranger.shared.FileManagerAware</a>:<br>
<dl><dt><strong>fm</strong> = None</dl>

<hr>
Data descriptors inherited from <a href="ranger.shared.html#Awareness">ranger.shared.Awareness</a>:<br>
<dl><dt><strong>__dict__</strong></dt>
<dd><tt>dictionary&nbsp;for&nbsp;instance&nbsp;variables&nbsp;(if&nbsp;defined)</tt></dd>
</dl>
<dl><dt><strong>__weakref__</strong></dt>
<dd><tt>list&nbsp;of&nbsp;weak&nbsp;references&nbsp;to&nbsp;the&nbsp;object&nbsp;(if&nbsp;defined)</tt></dd>
</dl>
<hr>
Methods inherited from <a href="ranger.gui.curses_shortcuts.html#CursesShortcuts">ranger.gui.curses_shortcuts.CursesShortcuts</a>:<br>
<dl><dt><a name="TaskView-addnstr"><strong>addnstr</strong></a>(self, *args)</dt></dl>

<dl><dt><a name="TaskView-addstr"><strong>addstr</strong></a>(self, *args)</dt></dl>

<dl><dt><a name="TaskView-color"><strong>color</strong></a>(self, keylist<font color="#909090">=None</font>, *keys)</dt><dd><tt>Change&nbsp;the&nbsp;colors&nbsp;from&nbsp;now&nbsp;on.</tt></dd></dl>

<dl><dt><a name="TaskView-color_at"><strong>color_at</strong></a>(self, y, x, wid, keylist<font color="#909090">=None</font>, *keys)</dt><dd><tt>Change&nbsp;the&nbsp;colors&nbsp;at&nbsp;the&nbsp;specified&nbsp;position</tt></dd></dl>

<dl><dt><a name="TaskView-color_reset"><strong>color_reset</strong></a>(self)</dt><dd><tt>Change&nbsp;the&nbsp;colors&nbsp;to&nbsp;the&nbsp;default&nbsp;colors</tt></dd></dl>

<hr>
Data and other attributes inherited from <a href="ranger.shared.settings.html#SettingsAware">ranger.shared.settings.SettingsAware</a>:<br>
<dl><dt><strong>settings</strong> = &lt;ranger.ext.openstruct.OpenStruct object at 0x7f6a9067fbd0&gt;</dl>

<hr>
Methods inherited from <a href="ranger.ext.accumulator.html#Accumulator">ranger.ext.accumulator.Accumulator</a>:<br>
<dl><dt><a name="TaskView-correct_pointer"><strong>correct_pointer</strong></a>(self)</dt></dl>

<dl><dt><a name="TaskView-get_height"><strong>get_height</strong></a>(self)</dt><dd><tt>OVERRIDE&nbsp;THIS</tt></dd></dl>

<dl><dt><a name="TaskView-move"><strong>move</strong></a>(self, relative<font color="#909090">=0</font>, absolute<font color="#909090">=None</font>, pages<font color="#909090">=None</font>, narg<font color="#909090">=None</font>)</dt></dl>

<dl><dt><a name="TaskView-move_to_obj"><strong>move_to_obj</strong></a>(self, arg, attr<font color="#909090">=None</font>)</dt></dl>

<dl><dt><a name="TaskView-pointer_is_synced"><strong>pointer_is_synced</strong></a>(self)</dt></dl>

<dl><dt><a name="TaskView-sync_index"><strong>sync_index</strong></a>(self, **kw)</dt></dl>

</td></tr></table></td></tr></table>
</body></html>
038scheduler.cc?h=main&id=16386f766ec1e347db8a19ebfd9cedaa9b281a5f'>^
b24eb476 ^

6c96a437 ^
05d17773 ^

82ceda30 ^


cfb142b9 ^
827898fc ^
82ceda30 ^

9d670bb5 ^
45ca3bb7 ^

9d670bb5 ^
134dad7c ^

795f5244 ^
166e3c0d ^
134dad7c ^
166e3c0d ^
2b250717 ^
e4630643 ^

7afe09fb ^
9dcbec39 ^
e4630643 ^

166e3c0d ^



b24eb476 ^
6c96a437 ^
134dad7c ^








a6cdf15c ^

795f5244 ^
166e3c0d ^



a6cdf15c ^

6c96a437 ^
00b808d9 ^
a6cdf15c ^


88e99894 ^


4a943d4e ^





















88e99894 ^





c442a5ad ^
88e99894 ^







6abdff27 ^
2ada05dc ^
6abdff27 ^
6abdff27 ^
2ada05dc ^
6abdff27 ^
6abdff27 ^


6c96a437 ^
6abdff27 ^





88e99894 ^
b24eb476 ^
88e99894 ^





795f5244 ^
166e3c0d ^
88e99894 ^
166e3c0d ^
2b250717 ^
88e99894 ^

7afe09fb ^
9dcbec39 ^
88e99894 ^

7afe09fb ^
9dcbec39 ^
88e99894 ^

166e3c0d ^



b24eb476 ^
6c96a437 ^
88e99894 ^






94fed202 ^
d8c6265d ^
ccba6718 ^
d8c6265d ^
ccba6718 ^


d8c6265d ^






2b250717 ^
d8c6265d ^











6c96a437 ^
d8c6265d ^
ccba6718 ^
d8c6265d ^






27d90d80 ^
4a943d4e ^














































9c184f02 ^


4a943d4e ^





















1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709

                                                                             
 

















                                   
 
                                         

                              



                                           
                                  









                                                                                                       

                                          




                          
                       





                                   
                       
                          
                              
                     
                          

                                          
                 
                       

                                               





                         
                                                              
                                
                           

                                              
                                                               
                          
                                  
                                     

                                         


                            
   
                    

 
                          
                                              
                                         
                   






                                                       
                                                 
                                                                                                                  
                                           
                                
                                       

             


   
                                



                                        
                       
                                     
                                                                               
                                          
                                                  



                      


                                                    
                                                 




                                                    
                                    
                       
                                 
                                        
                                         
                                                   
   
                    

 
                                                           
 
                                                                             
                                                             
                              
       
                       
                        
                     


                                   
                  
 


                                                                
                                                         


                                   
                                             
              
                                        
                                                    
                                       
                     
                                 
                                                                                                                                      

          
                                              
                                                                                                                                                                      

          



                                                
                                                              
                                                    
                         
                                                                                      
                                                                             
                                                                         
                                                                 
                                            
   
                                  
                     
                                            

        
 





























































                                                       
 

                                     












                                                                             




                                                                            
                                   
 



























































                                                             
 

                                               




















                                                                                                   
 
                                                  
                                                                               


                                       
                                                               
 













                                                                       
 
                                 
                                            

                                                                 
                                                      
                                

                                      


       
                                              
                                                                           





                                           

                                                              



















                                                                                     



                                             
                                                    
                                       
                     
                                    
                                                                                                                                                      

          
                                              
                                                                                                                                                                                                            

          



                                                

                                   
                                              

                                     


            
                     
                                   

        
 

                          
                                             

                                        
                                  
                                       
            
                                    
                                                                                                                                             

          
                                              
                                                                                                                                                                                                   

          



                                                
                                   
                                              








                                             

                                        
                                                      



                                       

                                                
                                              
                                                                                                                           


        


                                                             





















                                                                  





                                                      
                                                                                       







                                                  
                             
                                        
                                                       
                                
                                        
                                                                                   


                                
                                              





                                              
                              
          





                                             
                                              
                                       
                  
                                    
                                                                                                                                                   

          
                                              
                                                                                                                                                                                                         

          
                                              
                                                                                                                                                                                                      

          



                                                
                                   
                                              






                                                      
 
                              
                     
                                   


                                                                                             






                                                                      
                                                                                                                                                               











                                                                                                                                                                                                                     
                                              
                                   
                                                






                                   
 














































                                                                         


                                                                





















                                                        
//: Run a second routine concurrently using 'start-running', without any
//: guarantees on how the operations in each are interleaved with each other.

void test_scheduler() {
  run(
      "def f1 [\n"
      "  start-running f2\n"
         // wait for f2 to run
      "  {\n"
      "    jump-unless 1:num, -1\n"
      "  }\n"
      "]\n"
      "def f2 [\n"
      "  1:num <- copy 1\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "schedule: f1\n"
      "schedule: f2\n"
  );
}

//: first, add a deadline to run(routine)
:(before "End Globals")
int Scheduling_interval = 500;
:(before "End routine Fields")
int instructions_run_this_scheduling_slice;
:(before "End routine Constructor")
instructions_run_this_scheduling_slice = 0;
:(after "Running One Instruction")
 ++Current_routine->instructions_run_this_scheduling_slice;
:(replace{} "bool should_continue_running(const routine* current_routine)")
bool should_continue_running(const routine* current_routine) {
  assert(current_routine == Current_routine);  // argument passed in just to make caller readable above
  return Current_routine->state == RUNNING
      && Current_routine->instructions_run_this_scheduling_slice < Scheduling_interval;
}
:(after "stop_running_current_routine:")
// Reset instructions_run_this_scheduling_slice
Current_routine->instructions_run_this_scheduling_slice = 0;

//: now the rest of the scheduler is clean

:(before "struct routine")
enum routine_state {
  RUNNING,
  COMPLETED,
  // End routine States
};
:(before "End routine Fields")
enum routine_state state;
:(before "End routine Constructor")
state = RUNNING;

:(before "End Globals")
vector<routine*> Routines;
int Current_routine_index = 0;
:(before "End Reset")
Scheduling_interval = 500;
for (int i = 0;  i < SIZE(Routines);  ++i)
  delete Routines.at(i);
Routines.clear();
Current_routine = NULL;
:(replace{} "void run(const recipe_ordinal r)")
void run(const recipe_ordinal r) {
  run(new routine(r));
}

:(code)
void run(routine* rr) {
  Routines.push_back(rr);
  Current_routine_index = 0, Current_routine = Routines.at(0);
  while (!all_routines_done()) {
    skip_to_next_routine();
    assert(Current_routine);
    assert(Current_routine->state == RUNNING);
    trace(100, "schedule") << current_routine_label() << end();
    run_current_routine();
    // Scheduler State Transitions
    if (Current_routine->completed())
      Current_routine->state = COMPLETED;
    // End Scheduler State Transitions

    // Scheduler Cleanup
    // End Scheduler Cleanup
  }
  // End Run Routine
}

bool all_routines_done() {
  for (int i = 0;  i < SIZE(Routines);  ++i) {
    if (Routines.at(i)->state == RUNNING)
      return false;
  }
  return true;
}

// skip Current_routine_index past non-RUNNING routines
void skip_to_next_routine() {
  assert(!Routines.empty());
  assert(Current_routine_index < SIZE(Routines));
  for (int i = (Current_routine_index+1)%SIZE(Routines);  i != Current_routine_index;  i = (i+1)%SIZE(Routines)) {
    if (Routines.at(i)->state == RUNNING) {
      Current_routine_index = i;
      Current_routine = Routines.at(i);
      return;
    }
  }
}

string current_routine_label() {
  return routine_label(Current_routine);
}

string routine_label(routine* r) {
  ostringstream result;
  const call_stack& calls = r->calls;
  for (call_stack::const_iterator p = calls.begin();  p != calls.end();  ++p) {
    if (p != calls.begin()) result << '/';
    result << get(Recipe, p->running_recipe).name;
  }
  return result.str();
}

//: special case for the very first routine
:(replace{} "void run_main(int argc, char* argv[])")
void run_main(int argc, char* argv[]) {
  recipe_ordinal r = get(Recipe_ordinal, "main");
  assert(r);
  routine* main_routine = new routine(r);
  // pass in commandline args as ingredients to main
  // todo: test this
  Current_routine = main_routine;
  for (int i = 1;  i < argc;  ++i) {
    vector<double> arg;
    arg.push_back(/*alloc id*/0);
    arg.push_back(new_mu_text(argv[i]));
    assert(get(Memory, arg.back()) == 0);
    current_call().ingredient_atoms.push_back(arg);
  }
  run(main_routine);
}

//:: To schedule new routines to run, call 'start-running'.

//: 'start-running' will return a unique id for the routine that was created.
//: routine id is a number, but don't do any arithmetic on it
:(before "End routine Fields")
int id;
:(before "End Globals")
int Next_routine_id = 1;
:(before "End Reset")
Next_routine_id = 1;
:(before "End routine Constructor")
id = Next_routine_id;
++Next_routine_id;

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

:(before "End Primitive Recipe Declarations")
START_RUNNING,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "start-running", START_RUNNING);
:(before "End Primitive Recipe Checks")
case START_RUNNING: {
  if (inst.ingredients.empty()) {
    raise << maybe(get(Recipe, r).name) << "'start-running' requires at least one ingredient: the recipe to start running\n" << end();
    break;
  }
  if (!is_mu_recipe(inst.ingredients.at(0))) {
    raise << maybe(get(Recipe, r).name) << "first ingredient of 'start-running' should be a recipe, but got '" << to_string(inst.ingredients.at(0)) << "'\n" << end();
    break;
  }
  break;
}
:(before "End Primitive Recipe Implementations")
case START_RUNNING: {
  routine* new_routine = new routine(ingredients.at(0).at(0));
  new_routine->parent_index = Current_routine_index;
  // populate ingredients
  for (int i = /*skip callee*/1;  i < SIZE(current_instruction().ingredients);  ++i) {
    new_routine->calls.front().ingredient_atoms.push_back(ingredients.at(i));
    reagent/*copy*/ ingredient = current_instruction().ingredients.at(i);
    new_routine->calls.front().ingredients.push_back(ingredient);
    // End Populate start-running Ingredient
  }
  Routines.push_back(new_routine);
  products.resize(1);
  products.at(0).push_back(new_routine->id);
  break;
}

:(code)
void test_scheduler_runs_single_routine() {
  Scheduling_interval = 1;
  run(
      "def f1 [\n"
      "  1:num <- copy 0\n"
      "  2:num <- copy 0\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "schedule: f1\n"
      "run: {1: \"number\"} <- copy {0: \"literal\"}\n"
      "schedule: f1\n"
      "run: {2: \"number\"} <- copy {0: \"literal\"}\n"
  );
}

void test_scheduler_interleaves_routines() {
  Scheduling_interval = 1;
  run(
      "def f1 [\n"
      "  start-running f2\n"
      "  1:num <- copy 0\n"
      "  2:num <- copy 0\n"
      "]\n"
      "def f2 [\n"
      "  3:num <- copy 0\n"
      "  4:num <- copy 0\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "schedule: f1\n"
      "run: start-running {f2: \"recipe-literal\"}\n"
      "schedule: f2\n"
      "run: {3: \"number\"} <- copy {0: \"literal\"}\n"
      "schedule: f1\n"
      "run: {1: \"number\"} <- copy {0: \"literal\"}\n"
      "schedule: f2\n"
      "run: {4: \"number\"} <- copy {0: \"literal\"}\n"
      "schedule: f1\n"
      "run: {2: \"number\"} <- copy {0: \"literal\"}\n"
  );
}

void test_start_running_takes_ingredients() {
  run(
      "def f1 [\n"
      "  start-running f2, 3\n"
         // wait for f2 to run
      "  {\n"
      "    jump-unless 1:num, -1\n"
      "  }\n"
      "]\n"
      "def f2 [\n"
      "  1:num <- next-ingredient\n"
      "  2:num <- add 1:num, 1\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "mem: storing 4 in location 2\n"
  );
}

//: type-checking for 'start-running'

void test_start_running_checks_types() {
  Hide_errors = true;
  run(
      "def f1 [\n"
      "  start-running f2, 3\n"
      "]\n"
      "def f2 n:&:num [\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "error: f1: ingredient 0 has the wrong type at 'start-running f2, 3'\n"
  );
}

// 'start-running' only uses the ingredients of the callee, not its products
:(before "End is_indirect_call_with_ingredients Special-cases")
if (r == START_RUNNING) return true;

//: back to testing 'start-running'

:(code)
void test_start_running_returns_routine_id() {
  run(
      "def f1 [\n"
      "  1:num <- start-running f2\n"
      "]\n"
      "def f2 [\n"
      "  12:num <- copy 44\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "mem: storing 2 in location 1\n"
  );
}

//: this scenario requires some careful setup
void test_scheduler_skips_completed_routines() {
  recipe_ordinal f1 = load(
      "recipe f1 [\n"
      "  1:num <- copy 0\n"
      "]\n").front();
  recipe_ordinal f2 = load(
      "recipe f2 [\n"
      "  2:num <- copy 0\n"
      "]\n").front();
  Routines.push_back(new routine(f1));  // f1 meant to run
  Routines.push_back(new routine(f2));
  Routines.back()->state = COMPLETED;  // f2 not meant to run
  run(
      "def f3 [\n"
      "  3:num <- copy 0\n"
      "]\n"
  );
  // f1 and f3 can run in any order
  CHECK_TRACE_CONTENTS(
      "schedule: f1\n"
      "mem: storing 0 in location 1\n"
  );
  CHECK_TRACE_DOESNT_CONTAIN("schedule: f2");
  CHECK_TRACE_DOESNT_CONTAIN("mem: storing 0 in location 2");
  CHECK_TRACE_CONTENTS(
      "schedule: f3\n"
      "mem: storing 0 in location 3\n"
  );
}

void test_scheduler_starts_at_middle_of_routines() {
  Routines.push_back(new routine(COPY));
  Routines.back()->state = COMPLETED;
  run(
      "def f1 [\n"
      "  1:num <- copy 0\n"
      "  2:num <- copy 0\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "schedule: f1\n"
  );
  CHECK_TRACE_DOESNT_CONTAIN("run: idle");
}

//:: Errors in a routine cause it to terminate.

void test_scheduler_terminates_routines_after_errors() {
  Hide_errors = true;
  Scheduling_interval = 2;
  run(
      "def f1 [\n"
      "  start-running f2\n"
      "  1:num <- copy 0\n"
      "  2:num <- copy 0\n"
      "]\n"
      "def f2 [\n"
         // divide by 0 twice
      "  3:num <- divide-with-remainder 4, 0\n"
      "  4:num <- divide-with-remainder 4, 0\n"
      "]\n"
  );
  // f2 should stop after first divide by 0
  CHECK_TRACE_CONTENTS(
      "error: f2: divide by zero in '3:num <- divide-with-remainder 4, 0'\n"
  );
  CHECK_TRACE_DOESNT_CONTAIN("error: f2: divide by zero in '4:num <- divide-with-remainder 4, 0'");
}

:(after "operator<<(ostream& os, end /*unused*/)")
  if (Trace_stream && Trace_stream->curr_label == "error" && Current_routine) {
    Current_routine->state = COMPLETED;
  }

//:: Routines are marked completed when their parent completes.

:(code)
void test_scheduler_kills_orphans() {
  run(
      "def main [\n"
      "  start-running f1\n"
         // f1 never actually runs because its parent completes without
         // waiting for it
      "]\n"
      "def f1 [\n"
      "  1:num <- copy 0\n"
      "]\n"
  );
  CHECK_TRACE_DOESNT_CONTAIN("schedule: f1");
}

:(before "End Scheduler Cleanup")
for (int i = 0;  i < SIZE(Routines);  ++i) {
  if (Routines.at(i)->state == COMPLETED) continue;
  if (Routines.at(i)->parent_index < 0) continue;  // root thread
  // structured concurrency: http://250bpm.com/blog:71
  if (has_completed_parent(i)) {
    Routines.at(i)->state = COMPLETED;
  }
}

:(code)
bool has_completed_parent(int routine_index) {
  for (int j = routine_index;  j >= 0;  j = Routines.at(j)->parent_index) {
    if (Routines.at(j)->state == COMPLETED)
      return true;
  }
  return false;
}

//:: 'routine-state' can tell if a given routine id is running

void test_routine_state_test() {
  Scheduling_interval = 2;
  run(
      "def f1 [\n"
      "  1:num/child-id <- start-running f2\n"
      "  12:num <- copy 0\n"  // race condition since we don't care about location 12
         // thanks to Scheduling_interval, f2's one instruction runs in
         // between here and completes
      "  2:num/state <- routine-state 1:num/child-id\n"
      "]\n"
      "def f2 [\n"
      "  12:num <- copy 0\n"
         // trying to run a second instruction marks routine as completed
      "]\n"
  );
  // routine f2 should be in state COMPLETED
  CHECK_TRACE_CONTENTS(
      "mem: storing 1 in location 2\n"
  );
}

:(before "End Primitive Recipe Declarations")
ROUTINE_STATE,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "routine-state", ROUTINE_STATE);
:(before "End Primitive Recipe Checks")
case ROUTINE_STATE: {
  if (SIZE(inst.ingredients) != 1) {
    raise << maybe(get(Recipe, r).name) << "'routine-state' requires exactly one ingredient, but got '" << to_original_string(inst) << "'\n" << end();
    break;
  }
  if (!is_mu_number(inst.ingredients.at(0))) {
    raise << maybe(get(Recipe, r).name) << "first ingredient of 'routine-state' should be a routine id generated by 'start-running', but got '" << inst.ingredients.at(0).original_string << "'\n" << end();
    break;
  }
  break;
}
:(before "End Primitive Recipe Implementations")
case ROUTINE_STATE: {
  int id = ingredients.at(0).at(0);
  int result = -1;
  for (int i = 0;  i < SIZE(Routines);  ++i) {
    if (Routines.at(i)->id == id) {
      result = Routines.at(i)->state;
      break;
    }
  }
  products.resize(1);
  products.at(0).push_back(result);
  break;
}

//:: miscellaneous helpers

:(before "End Primitive Recipe Declarations")
STOP,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "stop", STOP);
:(before "End Primitive Recipe Checks")
case STOP: {
  if (SIZE(inst.ingredients) != 1) {
    raise << maybe(get(Recipe, r).name) << "'stop' requires exactly one ingredient, but got '" << to_original_string(inst) << "'\n" << end();
    break;
  }
  if (!is_mu_number(inst.ingredients.at(0))) {
    raise << maybe(get(Recipe, r).name) << "first ingredient of 'stop' should be a routine id generated by 'start-running', but got '" << inst.ingredients.at(0).original_string << "'\n" << end();
    break;
  }
  break;
}
:(before "End Primitive Recipe Implementations")
case STOP: {
  int id = ingredients.at(0).at(0);
  for (int i = 0;  i < SIZE(Routines);  ++i) {
    if (Routines.at(i)->id == id) {
      Routines.at(i)->state = COMPLETED;
      break;
    }
  }
  break;
}

:(before "End Primitive Recipe Declarations")
_DUMP_ROUTINES,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "$dump-routines", _DUMP_ROUTINES);
:(before "End Primitive Recipe Checks")
case _DUMP_ROUTINES: {
  break;
}
:(before "End Primitive Recipe Implementations")
case _DUMP_ROUTINES: {
  for (int i = 0;  i < SIZE(Routines);  ++i) {
    cerr << i << ": " << Routines.at(i)->id << ' ' << Routines.at(i)->state << ' ' << Routines.at(i)->parent_index << '\n';
  }
  break;
}

//: support for stopping routines after some number of cycles

:(code)
void test_routine_discontinues_past_limit() {
  Scheduling_interval = 2;
  run(
      "def f1 [\n"
      "  1:num/child-id <- start-running f2\n"
      "  limit-time 1:num/child-id, 10\n"
         // padding loop just to make sure f2 has time to complete
      "  2:num <- copy 20\n"
      "  2:num <- subtract 2:num, 1\n"
      "  jump-if 2:num, -2:offset\n"
      "]\n"
      "def f2 [\n"
      "  jump -1:offset\n"  // run forever
      "  $print [should never get here], 10/newline\n"
      "]\n"
  );
  // f2 terminates
  CHECK_TRACE_CONTENTS(
      "schedule: discontinuing routine 2\n"
  );
}

:(before "End routine States")
DISCONTINUED,
:(before "End Scheduler State Transitions")
if (Current_routine->limit >= 0) {
  if (Current_routine->limit <= Scheduling_interval) {
    trace(100, "schedule") << "discontinuing routine " << Current_routine->id << end();
    Current_routine->state = DISCONTINUED;
    Current_routine->limit = 0;
  }
  else {
    Current_routine->limit -= Scheduling_interval;
  }
}

:(before "End Test Teardown")
if (Passed && any_routines_with_error())
  raise << "some routines died with errors\n" << end();
:(before "End Mu Test Teardown")
if (Passed && any_routines_with_error())
  raise << Current_scenario->name << ": some routines died with errors\n" << end();

:(code)
bool any_routines_with_error() {
  for (int i = 0;  i < SIZE(Routines);  ++i) {
    if (Routines.at(i)->state == DISCONTINUED)
      return true;
  }
  return false;
}

:(before "End routine Fields")
int limit;
:(before "End routine Constructor")
limit = -1;  /* no limit */

:(before "End Primitive Recipe Declarations")
LIMIT_TIME,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "limit-time", LIMIT_TIME);
:(before "End Primitive Recipe Checks")
case LIMIT_TIME: {
  if (SIZE(inst.ingredients) != 2) {
    raise << maybe(get(Recipe, r).name) << "'limit-time' requires exactly two ingredient, but got '" << to_original_string(inst) << "'\n" << end();
    break;
  }
  if (!is_mu_number(inst.ingredients.at(0))) {
    raise << maybe(get(Recipe, r).name) << "first ingredient of 'limit-time' should be a routine id generated by 'start-running', but got '" << inst.ingredients.at(0).original_string << "'\n" << end();
    break;
  }
  if (!is_mu_number(inst.ingredients.at(1))) {
    raise << maybe(get(Recipe, r).name) << "second ingredient of 'limit-time' should be a number (of instructions to run for), but got '" << inst.ingredients.at(1).original_string << "'\n" << end();
    break;
  }
  break;
}
:(before "End Primitive Recipe Implementations")
case LIMIT_TIME: {
  int id = ingredients.at(0).at(0);
  for (int i = 0;  i < SIZE(Routines);  ++i) {
    if (Routines.at(i)->id == id) {
      Routines.at(i)->limit = ingredients.at(1).at(0);
      break;
    }
  }
  break;
}

:(before "End routine Fields")
int instructions_run;
:(before "End routine Constructor")
instructions_run = 0;
:(before "Reset instructions_run_this_scheduling_slice")
Current_routine->instructions_run += Current_routine->instructions_run_this_scheduling_slice;
:(before "End Primitive Recipe Declarations")
NUMBER_OF_INSTRUCTIONS,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "number-of-instructions", NUMBER_OF_INSTRUCTIONS);
:(before "End Primitive Recipe Checks")
case NUMBER_OF_INSTRUCTIONS: {
  if (SIZE(inst.ingredients) != 1) {
    raise << maybe(get(Recipe, r).name) << "'number-of-instructions' requires exactly one ingredient, but got '" << to_original_string(inst) << "'\n" << end();
    break;
  }
  if (!is_mu_number(inst.ingredients.at(0))) {
    raise << maybe(get(Recipe, r).name) << "first ingredient of 'number-of-instructions' should be a routine id generated by 'start-running', but got '" << inst.ingredients.at(0).original_string << "'\n" << end();
    break;
  }
  break;
}
:(before "End Primitive Recipe Implementations")
case NUMBER_OF_INSTRUCTIONS: {
  int id = ingredients.at(0).at(0);
  int result = -1;
  for (int i = 0;  i < SIZE(Routines);  ++i) {
    if (Routines.at(i)->id == id) {
      result = Routines.at(i)->instructions_run;
      break;
    }
  }
  products.resize(1);
  products.at(0).push_back(result);
  break;
}

:(code)
void test_number_of_instructions() {
  run(
      "def f1 [\n"
      "  10:num/child-id <- start-running f2\n"
      "  {\n"
      "    loop-unless 20:num\n"
      "  }\n"
      "  11:num <- number-of-instructions 10:num\n"
      "]\n"
      "def f2 [\n"
         // 2 instructions worth of work
      "  1:num <- copy 34\n"
      "  20:num <- copy 1\n"
      "]\n"
  );
  // f2 runs an extra instruction for the implicit 'return' added by the
  // fill_in_return_ingredients transform
  CHECK_TRACE_CONTENTS(
      "mem: storing 3 in location 11\n"
  );
}

void test_number_of_instructions_across_multiple_scheduling_intervals() {
  Scheduling_interval = 1;
  run(
      "def f1 [\n"
      "  10:num/child-id <- start-running f2\n"
      "  {\n"
      "    loop-unless 20:num\n"
      "  }\n"
      "  11:num <- number-of-instructions 10:num\n"
      "]\n"
      "def f2 [\n"
         // 4 instructions worth of work
      "  1:num <- copy 34\n"
      "  2:num <- copy 1\n"
      "  2:num <- copy 3\n"
      "  20:num <- copy 1\n"
      "]\n"
  );
  // f2 runs an extra instruction for the implicit 'return' added by the
  // fill_in_return_ingredients transform
  CHECK_TRACE_CONTENTS(
      "mem: storing 5 in location 11\n"
  );
}

//:: make sure that each routine gets a different alloc to start

void test_new_concurrent() {
  run(
      "def f1 [\n"
      "  start-running f2\n"
      "  1:&:num/raw <- new number:type\n"
         // wait for f2 to complete
      "  {\n"
      "    loop-unless 4:num/raw\n"
      "  }\n"
      "]\n"
      "def f2 [\n"
      "  2:&:num/raw <- new number:type\n"
         // hack: assumes scheduler implementation
      "  3:bool/raw <- equal 1:&:num/raw, 2:&:num/raw\n"
         // signal f2 complete
      "  4:num/raw <- copy 1\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "mem: storing 0 in location 3\n"
  );
}