https://github.com/akkartik/mu/blob/master/074wait.cc
  1 //: Routines can be put in a 'waiting' state, from which it will be ready to
  2 //: run again when a specific memory location changes its value. This is Mu's
  3 //: basic technique for orchestrating the order in which different routines
  4 //: operate.
  5 
  6 :(scenario wait_for_location)
  7 def f1 [
  8   10:num <- copy 34
  9   start-running f2
 10   20:location <- copy 10/unsafe
 11   wait-for-reset-then-set 20:location
 12   # wait for f2 to run and reset location 1
 13   30:num <- copy 10:num
 14 ]
 15 def f2 [
 16   10:location <- copy 0/unsafe
 17 ]
 18 +schedule: f1
 19 +run: waiting for location 10 to reset
 20 +schedule: f2
 21 +schedule: waking up routine 1
 22 +schedule: f1
 23 +mem: storing 1 in location 30
 24 
 25 //: define the new state that all routines can be in
 26 
 27 :(before "End routine States")
 28 WAITING,
 29 :(before "End routine Fields")
 30 // only if state == WAITING
 31 int waiting_on_location;
 32 :(before "End routine Constructor")
 33 waiting_on_location = 0;
 34 
 35 :(before "End Mu Test Teardown")
 36 if (Passed && any_routines_waiting())
 37   raise << Current_scenario->name << ": deadlock!\n" << end();
 38 :(before "End Run Routine")
 39 if (any_routines_waiting()) {
 40   raise << "deadlock!\n" << end();
 41   dump_waiting_routines();
 42 }
 43 :(before "End Test Teardown")
 44 if (Passed && any_routines_with_error())
 45   raise << "some routines died with errors\n" << end();
 46 :(code)
 47 bool any_routines_waiting() {
 48   for (int i = 0;  i < SIZE(Routines);  ++i) {
 49     if (Routines.at(i)->state == WAITING)
 50       return true;
 51   }
 52   return false;
 53 }
 54 void dump_waiting_routines() {
 55   for (int i = 0;  i < SIZE(Routines);  ++i) {
 56     if (Routines.at(i)->state == WAITING)
 57       cerr << i << ": " << routine_label(Routines.at(i)) << '\n';
 58   }
 59 }
 60 
 61 :(scenario wait_for_location_can_deadlock)
 62 % Hide_errors = true;
 63 def main [
 64   10:num <- copy 1
 65   20:location <- copy 10/unsafe
 66   wait-for-reset-then-set 20:location
 67 ]
 68 +error: deadlock!
 69 
 70 //: Primitive recipe to put routines in that state.
 71 //: This primitive is also known elsewhere as compare-and-set (CAS). Used to
 72 //: build locks.
 73 
 74 :(before "End Primitive Recipe Declarations")
 75 WAIT_FOR_RESET_THEN_SET,
 76 :(before "End Primitive Recipe Numbers")
 77 put(Recipe_ordinal, "wait-for-reset-then-set", WAIT_FOR_RESET_THEN_SET);
 78 :(before "End Primitive Recipe Checks")
 79 case WAIT_FOR_RESET_THEN_SET: {
 80   if (SIZE(inst.ingredients) != 1) {
 81     raise << maybe(get(Recipe, r).name) << "'wait-for-reset-then-set' requires exactly one ingredient, but got '" << to_original_string(inst) << "'\n" << end();
 82     break;
 83   }
 84   if (!is_mu_location(inst.ingredients.at(0))) {
 85     raise << maybe(get(Recipe, r).name) << "'wait-for-reset-then-set' requires a location ingredient, but got '" << inst.ingredients.at(0).original_string << "'\n" << end();
 86   }
 87   break;
 88 }
 89 :(before "End Primitive Recipe Implementations")
 90 case WAIT_FOR_RESET_THEN_SET: {
 91   int loc = static_cast<int>(ingredients.at(0).at(0));
 92   trace(9998, "run") << "wait: *" << loc << " = " << get_or_insert(Memory, loc) << end();
 93   if (get_or_insert(Memory, loc) == 0) {
 94     trace(9998, "run") << "location " << loc << " is already 0; setting" << end();
 95     put(Memory, loc, 1);
 96     break;
 97   }
 98   trace(9998, "run") << "waiting for location " << loc << " to reset" << end();
 99   Current_routine->state = WAITING;
100   Current_routine->waiting_on_location = loc;
101   break;
102 }
103 
104 //: Counterpart to unlock a lock.
105 :(before "End Primitive Recipe Declarations")
106 RESET,
107 :(before "End Primitive Recipe Numbers")
108 put(Recipe_ordinal, "reset", RESET);
109 :(before "End Primitive Recipe Checks")
110 case RESET: {
111   if (SIZE(inst.ingredients) != 1) {
112     raise << maybe(get(Recipe, r).name) << "'reset' requires exactly one ingredient, but got '" << to_original_string(inst) << "'\n" << end();
113     break;
114   }
115   if (!is_mu_location(inst.ingredients.at(0))) {
116     raise << maybe(get(Recipe, r).name) << "'reset' requires a location ingredient, but got '" << inst.ingredients.at(0).original_string << "'\n" << end();
117   }
118   break;
119 }
120 :(before "End Primitive Recipe Implementations")
121 case RESET: {
122   int loc = static_cast<int>(ingredients.at(0).at(0));
123   put(Memory, loc, 0);
124   trace(9998, "run") << "reset: *" << loc << " = " << get_or_insert(Memory, loc) << end();
125   break;
126 }
127 
128 //: scheduler tweak to get routines out of that state
129 
130 :(before "End Scheduler State Transitions")
131 for (int i = 0;  i < SIZE(Routines);  ++i) {
132   if (Routines.at(i)->state != WAITING) continue;
133   int loc = Routines.at(i)->waiting_on_location;
134   if (loc && get_or_insert(Memory, loc) == 0) {
135     trace("schedule") << "waking up routine " << Routines.at(i)->id << end();
136     put(Memory, loc, 1);
137     Routines.at(i)->state = RUNNING;
138     Routines.at(i)->waiting_on_location = 0;
139   }
140 }
141 
142 //: Primitive to help compute locations to wait on.
143 //: Only supports elements immediately inside containers; no arrays or
144 //: containers within containers yet.
145 
146 :(scenario get_location)
147 def main [
148   12:num <- copy 34
149   13:num <- copy 35
150   15:location <- get-location 12:point, 1:offset
151 ]
152 +mem: storing 13 in location 15
153 
154 :(before "End Primitive Recipe Declarations")
155 GET_LOCATION,
156 :(before "End Primitive Recipe Numbers")
157 put(Recipe_ordinal, "get-location", GET_LOCATION);
158 :(before "End Primitive Recipe Checks")
159 case GET_LOCATION: {
160   if (SIZE(inst.ingredients) != 2) {
161     raise << maybe(get(Recipe, r).name) << "'get-location' expects exactly 2 ingredients in '" << to_original_string(inst) << "'\n" << end();
162     break;
163   }
164   reagent/*copy*/ base = inst.ingredients.at(0);
165   if (!canonize_type(base)) break;
166   if (!base.type) {
167     raise << maybe(get(Recipe, r).name) << "first ingredient of 'get-location' should be a container, but got '" << inst.ingredients.at(0).original_string << "'\n" << end();
168     break;
169   }
170   const type_tree* base_root_type = base.type->atom ? base.type : base.type->left;
171   if (!base_root_type->atom || base_root_type->value == 0 || !contains_key(Type, base_root_type->value) || get(Type, base_root_type->value).kind != CONTAINER) {
172     raise << maybe(get(Recipe, r).name) << "first ingredient of 'get-location' should be a container, but got '" << inst.ingredients.at(0).original_string << "'\n" << end();
173     break;
174   }
175   type_ordinal base_type = base.type->value;
176   const reagent& offset = inst.ingredients.at(1);
177   if (!is_literal(offset) || !is_mu_scalar(offset)) {
178     raise &lclass="p"><span id="L11" class="LineNr"> 11 </span><span class="subxComment"># To reduce the odds of such &quot;use after free&quot; errors, SubX programs tend to not</span>
<span id="L12" class="LineNr"> 12 </span><span class="subxComment"># reclaim and reuse dynamically allocated memory. (Running out of memory is far</span>
<span id="L13" class="LineNr"> 13 </span><span class="subxComment"># easier to debug.) Long-running programs that want to reuse memory are mostly</span>
<span id="L14" class="LineNr"> 14 </span><span class="subxComment"># on their own to be careful. However, they do get one bit of help: they can</span>
<span id="L15" class="LineNr"> 15 </span><span class="subxComment"># carve out chunks of memory and then allocate from them manually using this</span>
<span id="L16" class="LineNr"> 16 </span><span class="subxComment"># very same 'allocate' helper. They just need a new allocation descriptor for</span>
<span id="L17" class="LineNr"> 17 </span><span class="subxComment"># their book-keeping.</span>
<span id="L18" class="LineNr"> 18 </span>
<span id="L19" class="LineNr"> 19 </span>== data
<span id="L20" class="LineNr"> 20 </span>
<span id="L21" class="LineNr"> 21 </span><span class="subxComment"># The 'global' allocation descriptor. Pass this into 'allocate' to claim a</span>
<span id="L22" class="LineNr"> 22 </span><span class="subxComment"># hitherto unused bit of memory.</span>
<span id="L23" class="LineNr"> 23 </span><span class="SpecialChar">Heap</span>:
<span id="L24" class="LineNr"> 24 </span>    <span class="SpecialChar">Start-of-heap</span>/imm32  <span class="subxComment"># curr</span>
<span id="L25" class="LineNr"> 25 </span>    0x0b000000/imm32  <span class="subxComment"># limit; keep sync'd with DATA_SEGMENT + SEGMENT_ALIGNMENT</span>
<span id="L26" class="LineNr"> 26 </span>
<span id="L27" class="LineNr"> 27 </span>== code
<span id="L28" class="LineNr"> 28 </span><span class="subxComment">#   instruction                     effective address                                                   register    displacement    immediate</span>
<span id="L29" class="LineNr"> 29 </span><span class="subxS1Comment"># . op          subop               mod             rm32          base        index         scale       r32</span>
<span id="L30" class="LineNr"> 30 </span><span class="subxS1Comment"># . 1-3 bytes   3 bits              2 bits          3 bits        3 bits      3 bits        2 bits      2 bits      0/1/2/4 bytes   0/1/2/4 bytes</span>
<span id="L31" class="LineNr"> 31 </span>
<span id="L32" class="LineNr"> 32 </span><span class="subxComment"># Claim the next 'n' bytes of memory starting at ad-&gt;curr and update ad-&gt;curr.</span>
<span id="L33" class="LineNr"> 33 </span><span class="subxComment"># If there isn't enough memory before ad-&gt;limit, return 0 and leave 'ad' unmodified.</span>
<span id="L34" class="LineNr"> 34 </span><span class="subxFunction">allocate</span>:  <span class="subxComment"># ad : (address allocation-descriptor), n : int -&gt; address-or-null/EAX</span>
<span id="L35" class="LineNr"> 35 </span>    <span class="subxS1Comment"># . prolog</span>
<span id="L36" class="LineNr"> 36 </span>    55/push-EBP
<span id="L37" class="LineNr"> 37 </span>    89/copy                         3/mod/direct    5/rm32/EBP   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          4/r32/ESP  <span class="Normal"> . </span>             <span class="Normal"> . </span>                <span class="subxComment"># copy ESP to EBP</span>
<span id="L38" class="LineNr"> 38 </span>    <span class="subxS1Comment"># . save registers</span>
<span id="L39" class="LineNr"> 39 </span>    51/push-ECX
<span id="L40" class="LineNr"> 40 </span>    52/push-EDX
<span id="L41" class="LineNr"> 41 </span>    <span class="subxComment"># ECX = ad</span>
<span id="L42" class="LineNr"> 42 </span>    8b/copy                         1/mod/*+disp8   5/rm32/EBP   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          1/r32/ECX   8/disp8        <span class="Normal"> . </span>                <span class="subxComment"># copy *(EBP+8) to ECX</span>
<span id="L43" class="LineNr"> 43 </span>    <span class="subxComment"># save ad-&gt;curr</span>
<span id="L44" class="LineNr"> 44 </span>    8b/copy                         0/mod/indirect  1/rm32/ECX   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          0/r32/EAX  <span class="Normal"> . </span>             <span class="Normal"> . </span>                <span class="subxComment"># copy *ECX to EAX</span>
<span id="L45" class="LineNr"> 45 </span>    <span class="subxComment"># check if there's enough space</span>
<span id="L46" class="LineNr"> 46 </span>    <span class="subxS1Comment"># . EDX = ad-&gt;curr + n</span>
<span id="L47" class="LineNr"> 47 </span>    89/copy                         3/mod/direct    2/rm32/EDX   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          0/r32/EAX  <span class="Normal"> . </span>             <span class="Normal"> . </span>                <span class="subxComment"># copy EAX to EDX</span>
<span id="L48" class="LineNr"> 48 </span>    03/add                          1/mod/*+disp8   5/rm32/EBP   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          2/r32/EDX   0xc/disp8      <span class="Normal"> . </span>                <span class="subxComment"># add *(EBP+12) to EDX</span>
<span id="L49" class="LineNr"> 49 </span>    3b/compare                      1/mod/*+disp8   1/rm32/ECX   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          2/r32/EDX   4/disp8        <span class="Normal"> . </span>                <span class="subxComment"># compare EDX with *(ECX+4)</span>
<span id="L50" class="LineNr"> 50 </span>    7c/jump-if-lesser  $allocate:commit/disp8
<span id="L51" class="LineNr"> 51 </span>    <span class="subxComment"># return null if not</span>
<span id="L52" class="LineNr"> 52 </span>    b8/copy-to-EAX  0/imm32
<span id="L53" class="LineNr"> 53 </span>    eb/jump  $allocate:end/disp8
<span id="L54" class="LineNr"> 54 </span><span class="Constant">$allocate:commit</span>:
<span id="L55" class="LineNr"> 55 </span>    <span class="subxComment"># update ad-&gt;curr</span>
<span id="L56" class="LineNr"> 56 </span>    89/copy                         0/mod/indirect  1/rm32/ECX   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          2/r32/EDX  <span class="Normal"> . </span>             <span class="Normal"> . </span>                <span class="subxComment"># copy EDX to *ECX</span>
<span id="L57" class="LineNr"> 57 </span><span class="Constant">$allocate:end</span>:
<span id="L58" class="LineNr"> 58 </span>    <span class="subxS1Comment"># . restore registers</span>
<span id="L59" class="LineNr"> 59 </span>    5a/pop-to-EDX
<span id="L60" class="LineNr"> 60 </span>    59/pop-to-ECX
<span id="L61" class="LineNr"> 61 </span>    <span class="subxS1Comment"># . epilog</span>
<span id="L62" class="LineNr"> 62 </span>    89/copy                         3/mod/direct    4/rm32/ESP   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          5/r32/EBP  <span class="Normal"> . </span>             <span class="Normal"> . </span>                <span class="subxComment"># copy EBP to ESP</span>
<span id="L63" class="LineNr"> 63 </span>    5d/pop-to-EBP
<span id="L64" class="LineNr"> 64 </span>    c3/return
<span id="L65" class="LineNr"> 65 </span>
<span id="L66" class="LineNr"> 66 </span><span class="subxTest">test-allocate-success</span>:
<span id="L67" class="LineNr"> 67 </span>    <span class="subxS1Comment"># . prolog</span>
<span id="L68" class="LineNr"> 68 </span>    55/push-EBP
<span id="L69" class="LineNr"> 69 </span>    89/copy                         3/mod/direct    5/rm32/EBP   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          4/r32/ESP  <span class="Normal"> . </span>             <span class="Normal"> . </span>                <span class="subxComment"># copy ESP to EBP</span>
<span id="L70" class="LineNr"> 70 </span>    <span class="subxComment"># var ad/ECX : (address allocation-descriptor) = {11, 15}</span>
<span id="L71" class="LineNr"> 71 </span>    68/push  0xf/imm32/limit
<span id="L72" class="LineNr"> 72 </span>    68/push  0xb/imm32/curr
<span id="L73" class="LineNr"> 73 </span>    89/copy                         3/mod/direct    1/rm32/ECX   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          4/r32/ESP  <span class="Normal"> . </span>             <span class="Normal"> . </span>                <span class="subxComment"># copy ESP to ECX</span>
<span id="L74" class="LineNr"> 74 </span>    <span class="subxComment"># EAX = allocate(ad, 3)</span>
<span id="L75" class="LineNr"> 75 </span>    <span class="subxS2Comment"># . . push args</span>
<span id="L76" class="LineNr"> 76 </span>    68/push  3/imm32
<span id="L77" class="LineNr"> 77 </span>    51/push-ECX
<span id="L78" class="LineNr"> 78 </span>    <span class="subxS2Comment"># . . call</span>
<span id="L79" class="LineNr"> 79 </span>    e8/call  <a href='069allocate.subx.html#L34'>allocate</a>/disp32
<span id="L80" class="LineNr"> 80 </span>    <span class="subxS2Comment"># . . discard args</span>
<span id="L81" class="LineNr"> 81 </span>    81          0/subop/add         3/mod/direct    4/rm32/ESP   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>         <span class="Normal"> . </span>         <span class="Normal"> . </span>              8/imm32           <span class="subxComment"># add to ESP</span>
<span id="L82" class="LineNr"> 82 </span>    <span class="subxComment"># check-ints-equal(EAX, 11, msg)</span>
<span id="L83" class="LineNr"> 83 </span>    <span class="subxS2Comment"># . . push args</span>
<span id="L84" class="LineNr"> 84 </span>    68/push  <span class="Constant">&quot;F - <a href='069allocate.subx.html#L66'>test-allocate-success</a>: returns current pointer of allocation descriptor&quot;</span>/imm32
<span id="L85" class="LineNr"> 85 </span>    68/push  0xb/imm32
<span id="L86" class="LineNr"> 86 </span>    50/push-EAX
<span id="L87" class="LineNr"> 87 </span>    <span class="subxS2Comment"># . . call</span>
<span id="L88" class="LineNr"> 88 </span>    e8/call  <a href='051test.subx.html#L24'>check-ints-equal</a>/disp32
<span id="L89" class="LineNr"> 89 </span>    <span class="subxS2Comment"># . . discard args</span>
<span id="L90" class="LineNr"> 90 </span>    81          0/subop/add         3/mod/direct    4/rm32/ESP   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>         <span class="Normal"> . </span>         <span class="Normal"> . </span>              0xc/imm32         <span class="subxComment"># add to ESP</span>
<span id="L91" class="LineNr"> 91 </span>    <span class="subxComment"># check-ints-equal(ad-&gt;curr, 14, msg)</span>
<span id="L92" class="LineNr"> 92 </span>    <span class="subxS2Comment"># . . push args</span>
<span id="L93" class="LineNr"> 93 </span>    68/push  <span class="Constant">&quot;F - <a href='069allocate.subx.html#L66'>test-allocate-success</a>: updates allocation descriptor&quot;</span>/imm32
<span id="L94" class="LineNr"> 94 </span>    68/push  0xe/imm32
<span id="L95" class="LineNr"> 95 </span>    ff          6/subop/push        0/mod/indirect  1/rm32/ECX   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>         <span class="Normal"> . </span>         <span class="Normal"> . </span>             <span class="Normal"> . </span>                <span class="subxComment"># push *ECX</span>
<span id="L96" class="LineNr"> 96 </span>    <span class="subxS2Comment"># . . call</span>
<span id="L97" class="LineNr"> 97 </span>    e8/call  <a href='051test.subx.html#L24'>check-ints-equal</a>/disp32
<span id="L98" class="LineNr"> 98 </span>    <span class="subxS2Comment"># . . discard args</span>
<span id="L99" class="LineNr"> 99 </span>    81          0/subop/add         3/mod/direct    4/rm32/ESP   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>         <span class="Normal"> . </span>         <span class="Normal"> . </span>              0xc/imm32         <span class="subxComment"># add to ESP</span>
<span id="L100" class="LineNr">100 </span>    <span class