about summary refs log blame commit diff stats
path: root/039wait.cc
blob: 89871022a2fe748119fc38e6fb7ad10dfcb94212 (plain) (tree)
1
2
3
4
5
6
7
8
9




                                                                             
                             

                             
                         















                                                                       
                            











                                                       
                                                                  


                                                                    
                                                                                                  
                                                                                                                                          





                                                     
                                               
                                                                                                                            
                                                 




                                                                                                                                                                     
                                                                                                    
                                               

                                                                                           

   
 
                                   


























                                                                       
                                                                  






                                                      


                                                                           
                                               


                                                    
                  
                                                 
                                                                       
                                                 

                                             



     
//: Routines can be put in a 'waiting' state, from which it will be ready to
//: run again when a specific memory location changes its value. This is mu's
//: basic technique for orchestrating the order in which different routines
//: operate.

:(scenario wait_for_location)
recipe f1 [
  1:integer <- copy 0:literal
  start-running f2:recipe
  wait-for-location 1:integer
  # now wait for f2 to run and modify location 1 before using its value
  2:integer <- copy 1:integer
]
recipe f2 [
  1:integer <- copy 34:literal
]
# if we got the synchronization wrong we'd be storing 0 in location 2
+mem: storing 34 in location 2

//: define the new state that all routines can be in

:(before "End routine States")
WAITING,
:(before "End routine Fields")
// only if state == WAITING
index_t waiting_on_location;
int old_value_of_wating_location;
:(before "End routine Constructor")
waiting_on_location = old_value_of_wating_location = 0;

//: primitive recipe to put routines in that state

:(before "End Primitive Recipe Declarations")
WAIT_FOR_LOCATION,
:(before "End Primitive Recipe Numbers")
Recipe_number["wait-for-location"] = WAIT_FOR_LOCATION;
:(before "End Primitive Recipe Implementations")
case WAIT_FOR_LOCATION: {
  reagent loc = canonize(current_instruction().ingredients.at(0));
  Current_routine->state = WAITING;
  Current_routine->waiting_on_location = loc.value;
  Current_routine->old_value_of_wating_location = Memory[loc.value];
  trace("run") << "waiting for location " << loc.value << " to change from " << Memory[loc.value];
//?   trace("schedule") << Current_routine->id << ": waiting for location " << loc.value << " to change from " << Memory[loc.value]; //? 1
  break;
}

//: scheduler tweak to get routines out of that state

:(before "End Scheduler State Transitions")
for (index_t i = 0; i < Routines.size(); ++i) {
//?   trace("schedule") << "wake up loop 1: routine " << Routines.at(i)->id << " has state " << Routines.at(i)->state; //? 1
  if (Routines.at(i)->state != WAITING) continue;
//?   trace("schedule") << "waiting on location: " << Routines.at(i)->waiting_on_location; //? 1
//?   if (Routines.at(i)->waiting_on_location) //? 1
//?     trace("schedule") << "checking routine " << Routines.at(i)->id << " waiting on location " //? 1
//?       << Routines.at(i)->waiting_on_location << ": " << Memory[Routines.at(i)->waiting_on_location] << " vs " << Routines[i]->old_value_of_wating_location; //? 1
  if (Routines.at(i)->waiting_on_location &&
      Memory[Routines.at(i)->waiting_on_location] != Routines.at(i)->old_value_of_wating_location) {
    trace("schedule") << "waking up routine\n";
    Routines.at(i)->state = RUNNING;
    Routines.at(i)->waiting_on_location = Routines.at(i)->old_value_of_wating_location = 0;
  }
}

//: also allow waiting on a routine

:(scenario wait_for_routine)
recipe f1 [
  1:integer <- copy 0:literal
  2:integer/routine <- start-running f2:recipe
  wait-for-routine 2:integer/routine
  # now wait for f2 to run and modify location 1 before using its value
  3:integer <- copy 1:integer
]
recipe f2 [
  1:integer <- copy 34:literal
]
# if we got the synchronization wrong we'd be storing 0 in location 3
+mem: storing 34 in location 3

:(before "End routine Fields")
// only if state == WAITING
index_t waiting_on_routine;
:(before "End routine Constructor")
waiting_on_routine = 0;

:(before "End Primitive Recipe Declarations")
WAIT_FOR_ROUTINE,
:(before "End Primitive Recipe Numbers")
Recipe_number["wait-for-routine"] = WAIT_FOR_ROUTINE;
:(before "End Primitive Recipe Implementations")
case WAIT_FOR_ROUTINE: {
  reagent loc = canonize(current_instruction().ingredients.at(0));
  Current_routine->state = WAITING;
  Current_routine->waiting_on_routine = loc.value;
  trace("run") << "waiting for routine " << loc.value;
  break;
}

:(before "End Scheduler State Transitions")
// Wake up any routines waiting for other routines to go to sleep.
// Important: this must come after the scheduler loop above giving routines
// waiting for locations to change a chance to wake up.
for (index_t i = 0; i < Routines.size(); ++i) {
  if (Routines.at(i)->state != WAITING) continue;
  if (!Routines.at(i)->waiting_on_routine) continue;
  index_t id = Routines.at(i)->waiting_on_routine;
  assert(id != i);
  for (index_t j = 0; j < Routines.size(); ++j) {
    if (Routines.at(j)->id == id && Routines.at(j)->state != WAITING) {
      trace("schedule") << "waking up routine\n";
      Routines.at(i)->state = RUNNING;
      Routines.at(i)->waiting_on_routine = 0;
    }
  }
}