From 9542bb112419d575190a72baf7f964c3e32df223 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Sat, 25 Jul 2015 22:15:51 -0700 Subject: 1853 --- html/038scheduler.cc.html | 161 +++++++++++++++++++++++----------------------- 1 file changed, 80 insertions(+), 81 deletions(-) (limited to 'html/038scheduler.cc.html') diff --git a/html/038scheduler.cc.html b/html/038scheduler.cc.html index 89bd5f74..56f3c3c4 100644 --- a/html/038scheduler.cc.html +++ b/html/038scheduler.cc.html @@ -13,16 +13,17 @@ pre { white-space: pre-wrap; font-family: monospace; color: #eeeeee; background-color: #080808; } body { font-family: monospace; color: #eeeeee; background-color: #080808; } * { font-size: 1.05em; } -.traceContains { color: #008000; } -.cSpecial { color: #008000; } -.Constant { color: #00a0a0; } -.SalientComment { color: #00ffff; } .traceAbsent { color: #c00000; } +.SalientComment { color: #00ffff; } +.Constant { color: #00a0a0; } +.CommentedCode { color: #6c6c6c; } +.cSpecial { color: #008000; } +.Normal { color: #eeeeee; background-color: #080808; padding-bottom: 1px; } .Comment { color: #9090ff; } .Delimiter { color: #a04060; } .Special { color: #ff6060; } -.CommentedCode { color: #6c6c6c; } .Identifier { color: #804000; } +.traceContains { color: #008000; } --> @@ -54,65 +55,57 @@ recipe f2 [ //: first, add a deadline to run(routine) //: these changes are ugly and brittle; just close your nose and get through the next few lines :(replace "void run_current_routine()") -void run_current_routine(long long int time_slice) +void run_current_routine(long long int time_slice) :(replace "while (!Current_routine->completed())" following "void run_current_routine(long long int time_slice)") -long long int ninstrs = 0; -while (Current_routine->state == RUNNING && ninstrs < time_slice) +long long int ninstrs = 0; +while (Current_routine->state == RUNNING && ninstrs < time_slice) :(after "Running One Instruction") ninstrs++; //: now the rest of the scheduler is clean :(before "struct routine") -enum routine_state { +enum routine_state { RUNNING, COMPLETED, // End routine States }; :(before "End routine Fields") -enum routine_state state; +enum routine_state state; :(before "End routine Constructor") state = RUNNING; :(before "End Globals") vector<routine*> Routines; -long long int Current_routine_index = 0; -long long int Scheduling_interval = 500; +long long int Current_routine_index = 0; +long long int Scheduling_interval = 500; :(before "End Setup") Scheduling_interval = 500; Routines.clear(); :(replace{} "void run(recipe_ordinal r)") -void run(recipe_ordinal r) { -//? cerr << "AAA 4\n"; //? 1 - Routines.push_back(new routine(r)); -//? cerr << "AAA " << Routines.size() << " routines\n"; //? 1 +void run(recipe_ordinal r) { + Routines.push_back(new routine(r)); Current_routine_index = 0, Current_routine = Routines.at(0); - while (!all_routines_done()) { + while (!all_routines_done()) { skip_to_next_routine(); -//? cout << "scheduler: " << Current_routine_index << '\n'; //? 1 assert(Current_routine); assert(Current_routine->state == RUNNING); - trace("schedule") << current_routine_label(); -//? trace("schedule") << Current_routine_index << ": " << current_recipe_name(); //? 1 -//? trace("schedule") << Current_routine->id << " " << current_recipe_name(); //? 1 + trace("schedule") << current_routine_label() << end(); run_current_routine(Scheduling_interval); // Scheduler State Transitions -//? cerr << "AAA completed? " << Current_routine->completed() << '\n'; //? 1 - if (Current_routine->completed()) + if (Current_routine->completed()) Current_routine->state = COMPLETED; // End Scheduler State Transitions // Scheduler Cleanup // End Scheduler Cleanup } -//? cout << "done with run\n"; //? 1 } :(code) -bool all_routines_done() { - for (long long int i = 0; i < SIZE(Routines); ++i) { -//? cout << "routine " << i << ' ' << Routines.at(i)->state << '\n'; //? 1 - if (Routines.at(i)->state == RUNNING) { +bool all_routines_done() { + for (long long int i = 0; i < SIZE(Routines); ++i) { + if (Routines.at(i)->state == RUNNING) { return false; } } @@ -120,33 +113,31 @@ bool all_routines_done() } // skip Current_routine_index past non-RUNNING routines -void skip_to_next_routine() { +void skip_to_next_routine() { assert(!Routines.empty()); assert(Current_routine_index < SIZE(Routines)); - for (long long int i = (Current_routine_index+1)%SIZE(Routines); i != Current_routine_index; i = (i+1)%SIZE(Routines)) { - if (Routines.at(i)->state == RUNNING) { -//? cout << "switching to " << i << '\n'; //? 1 + for (long long 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; } } -//? cout << "all done\n"; //? 1 } string current_routine_label() { ostringstream result; call_stack calls = Current_routine->calls; - for (call_stack::iterator p = calls.begin(); p != calls.end(); ++p) { - if (p != calls.begin()) result << '/'; + for (call_stack::iterator p = calls.begin(); p != calls.end(); ++p) { + if (p != calls.begin()) result << '/'; result << Recipe[p->running_recipe].name; } return result.str(); } :(before "End Teardown") -for (long long int i = 0; i < SIZE(Routines); ++i) - delete Routines.at(i); +for (long long int i = 0; i < SIZE(Routines); ++i) + delete Routines.at(i); Routines.clear(); //:: To schedule new routines to run, call 'start-running'. @@ -154,9 +145,9 @@ Routines.clear(); //: '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") -long long int id; +long long int id; :(before "End Globals") -long long int Next_routine_id = 1; +long long int Next_routine_id = 1; :(before "End Setup") Next_routine_id = 1; :(before "End routine Constructor") @@ -166,7 +157,7 @@ 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. -long long int parent_index; // only < 0 if there's no parent_index +long long int parent_index; // only < 0 if there's no parent_index :(before "End routine Constructor") parent_index = -1; @@ -175,12 +166,11 @@ START_RUNNING, :(before "End Primitive Recipe Numbers") Recipe_ordinal["start-running"] = START_RUNNING; :(before "End Primitive Recipe Implementations") -case START_RUNNING: { - routine* new_routine = new routine(ingredients.at(0).at(0)); -//? cerr << new_routine->id << " -> " << Current_routine->id << '\n'; //? 1 +case START_RUNNING: { + routine* new_routine = new routine(ingredients.at(0).at(0)); new_routine->parent_index = Current_routine_index; // populate ingredients - for (long long int i = 1; i < SIZE(current_instruction().ingredients); ++i) + for (long long int i = 1; i < SIZE(current_instruction().ingredients); ++i) new_routine->calls.front().ingredient_atoms.push_back(ingredients.at(i)); Routines.push_back(new_routine); products.resize(1); @@ -288,30 +278,18 @@ recipe f1 [ -schedule: f1 :(before "End Scheduler Cleanup") -//? trace("schedule") << "Before cleanup"; //? 1 -//? for (long long int i = 0; i < SIZE(Routines); ++i) { //? 1 -//? trace("schedule") << i << ": " << Routines.at(i)->id << ' ' << Routines.at(i)->state << ' ' << Routines.at(i)->parent_index << ' ' << Routines.at(i)->state; //? 1 -//? } //? 1 -for (long long int i = 0; i < SIZE(Routines); ++i) { - if (Routines.at(i)->state == COMPLETED) continue; - if (Routines.at(i)->parent_index < 0) continue; // root thread -//? trace("schedule") << "AAA " << i; //? 1 - if (has_completed_parent(i)) { -//? trace("schedule") << "BBB " << i; //? 1 +for (long long int i = 0; i < SIZE(Routines); ++i) { + if (Routines.at(i)->state == COMPLETED) continue; + if (Routines.at(i)->parent_index < 0) continue; // root thread + if (has_completed_parent(i)) { Routines.at(i)->state = COMPLETED; } } -//? trace("schedule") << "After cleanup"; //? 1 -//? for (long long int i = 0; i < SIZE(Routines); ++i) { //? 1 -//? trace("schedule") << i << ": " << Routines.at(i)->id << ' ' << Routines.at(i)->state << ' ' << Routines.at(i)->parent_index << ' ' << Routines.at(i)->state; //? 1 -//? } //? 1 :(code) -bool has_completed_parent(long long int routine_index) { -//? trace("schedule") << "CCC " << routine_index << '\n'; //? 2 - for (long long int j = routine_index; j >= 0; j = Routines.at(j)->parent_index) { -//? trace("schedule") << "DDD " << j << '\n'; //? 2 - if (Routines.at(j)->state == COMPLETED) +bool has_completed_parent(long long int routine_index) { + for (long long int j = routine_index; j >= 0; j = Routines.at(j)->parent_index) { + if (Routines.at(j)->state == COMPLETED) return true; } return false; @@ -339,12 +317,19 @@ ROUTINE_STATE, :(before "End Primitive Recipe Numbers") Recipe_ordinal["routine-state"] = ROUTINE_STATE; :(before "End Primitive Recipe Implementations") -case ROUTINE_STATE: { - assert(scalar(ingredients.at(0))); - long long int id = ingredients.at(0).at(0); - long long int result = -1; - for (long long int i = 0; i < SIZE(Routines); ++i) { - if (Routines.at(i)->id == id) { +case ROUTINE_STATE: { + if (SIZE(ingredients) != 1) { + raise << current_recipe_name() << ": 'routine-state' requires exactly one ingredient, but got " << current_instruction().to_string() << '\n' << end(); + break; + } + if (!scalar(ingredients.at(0))) { + raise << current_recipe_name() << ": first ingredient of 'routine-state' should be a routine id generated by 'start-running', but got " << current_instruction().ingredients.at(0).original_string << '\n' << end(); + break; + } + long long int id = ingredients.at(0).at(0); + long long int result = -1; + for (long long int i = 0; i < SIZE(Routines); ++i) { + if (Routines.at(i)->id == id) { result = Routines.at(i)->state; break; } @@ -361,11 +346,18 @@ RESTART, :(before "End Primitive Recipe Numbers") Recipe_ordinal["restart"] = RESTART; :(before "End Primitive Recipe Implementations") -case RESTART: { - assert(scalar(ingredients.at(0))); - long long int id = ingredients.at(0).at(0); - for (long long int i = 0; i < SIZE(Routines); ++i) { - if (Routines.at(i)->id == id) { +case RESTART: { + if (SIZE(ingredients) != 1) { + raise << current_recipe_name() << ": 'restart' requires exactly one ingredient, but got " << current_instruction().to_string() << '\n' << end(); + break; + } + if (!scalar(ingredients.at(0))) { + raise << current_recipe_name() << ": first ingredient of 'restart' should be a routine id generated by 'start-running', but got " << current_instruction().ingredients.at(0).original_string << '\n' << end(); + break; + } + long long int id = ingredients.at(0).at(0); + for (long long int i = 0; i < SIZE(Routines); ++i) { + if (Routines.at(i)->id == id) { Routines.at(i)->state = RUNNING; break; } @@ -378,11 +370,18 @@ STOP, :(before "End Primitive Recipe Numbers") Recipe_ordinal["stop"] = STOP; :(before "End Primitive Recipe Implementations") -case STOP: { - assert(scalar(ingredients.at(0))); - long long int id = ingredients.at(0).at(0); - for (long long int i = 0; i < SIZE(Routines); ++i) { - if (Routines.at(i)->id == id) { +case STOP: { + if (SIZE(ingredients) != 1) { + raise << current_recipe_name() << ": 'stop' requires exactly one ingredient, but got " << current_instruction().to_string() << '\n' << end(); + break; + } + if (!scalar(ingredients.at(0))) { + raise << current_recipe_name() << ": first ingredient of 'stop' should be a routine id generated by 'start-running', but got " << current_instruction().ingredients.at(0).original_string << '\n' << end(); + break; + } + long long int id = ingredients.at(0).at(0); + for (long long int i = 0; i < SIZE(Routines); ++i) { + if (Routines.at(i)->id == id) { Routines.at(i)->state = COMPLETED; break; } @@ -395,8 +394,8 @@ _DUMP_ROUTINES, :(before "End Primitive Recipe Numbers") Recipe_ordinal["$dump-routines"] = _DUMP_ROUTINES; :(before "End Primitive Recipe Implementations") -case _DUMP_ROUTINES: { - for (long long int i = 0; i < SIZE(Routines); ++i) { +case _DUMP_ROUTINES: { + for (long long int i = 0; i < SIZE(Routines); ++i) { cerr << i << ": " << Routines.at(i)->id << ' ' << Routines.at(i)->state << ' ' << Routines.at(i)->parent_index << '\n'; } break; -- cgit 1.4.1-2-gfad0