mu/074wait.cc

665 lines
22 KiB
C++
Raw Normal View History

//: Routines can be put in a 'waiting' state, from which it will be ready to
2016-10-22 23:56:07 +00:00
//: 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.
5001 - drop the :(scenario) DSL I've been saying for a while[1][2][3] that adding extra abstractions makes things harder for newcomers, and adding new notations doubly so. And then I notice this DSL in my own backyard. Makes me feel like a hypocrite. [1] https://news.ycombinator.com/item?id=13565743#13570092 [2] https://lobste.rs/s/to8wpr/configuration_files_are_canary_warning [3] https://lobste.rs/s/mdmcdi/little_languages_by_jon_bentley_1986#c_3miuf2 The implementation of the DSL was also highly hacky: a) It was happening in the tangle/ tool, but was utterly unrelated to tangling layers. b) There were several persnickety constraints on the different kinds of lines and the specific order they were expected in. I kept finding bugs where the translator would silently do the wrong thing. Or the error messages sucked, and readers may be stuck looking at the generated code to figure out what happened. Fixing error messages would require a lot more code, which is one of my arguments against DSLs in the first place: they may be easy to implement, but they're hard to design to go with the grain of the underlying platform. They require lots of iteration. Is that effort worth prioritizing in this project? On the other hand, the DSL did make at least some readers' life easier, the ones who weren't immediately put off by having to learn a strange syntax. There were fewer quotes to parse, fewer backslash escapes. Anyway, since there are also people who dislike having to put up with strange syntaxes, we'll call that consideration a wash and tear this DSL out. --- This commit was sheer drudgery. Hopefully it won't need to be redone with a new DSL because I grow sick of backslashes.
2019-03-13 01:56:55 +00:00
void test_wait_for_location() {
run(
"def f1 [\n"
" 10:num <- copy 34\n"
" start-running f2\n"
" 20:location <- copy 10/unsafe\n"
" wait-for-reset-then-set 20:location\n"
// wait for f2 to run and reset location 1
" 30:num <- copy 10:num\n"
"]\n"
"def f2 [\n"
" 10:location <- copy 0/unsafe\n"
"]\n"
);
CHECK_TRACE_CONTENTS(
"schedule: f1\n"
"run: waiting for location 10 to reset\n"
"schedule: f2\n"
"schedule: waking up routine 1\n"
"schedule: f1\n"
"mem: storing 1 in location 30\n"
);
}
//: define the new state that all routines can be in
:(before "End routine States")
WAITING,
:(before "End routine Fields")
// only if state == WAITING
int waiting_on_location;
:(before "End routine Constructor")
waiting_on_location = 0;
2016-04-23 06:23:51 +00:00
:(before "End Mu Test Teardown")
if (Passed && any_routines_waiting())
2016-04-23 06:23:51 +00:00
raise << Current_scenario->name << ": deadlock!\n" << end();
:(before "End Run Routine")
if (any_routines_waiting()) {
raise << "deadlock!\n" << end();
dump_waiting_routines();
}
2016-04-23 06:23:51 +00:00
:(before "End Test Teardown")
if (Passed && any_routines_with_error())
2016-04-23 06:23:51 +00:00
raise << "some routines died with errors\n" << end();
:(code)
bool any_routines_waiting() {
2016-10-20 05:10:35 +00:00
for (int i = 0; i < SIZE(Routines); ++i) {
2016-04-23 06:23:51 +00:00
if (Routines.at(i)->state == WAITING)
return true;
}
return false;
}
void dump_waiting_routines() {
2016-10-20 05:10:35 +00:00
for (int i = 0; i < SIZE(Routines); ++i) {
if (Routines.at(i)->state == WAITING)
cerr << i << ": " << routine_label(Routines.at(i)) << '\n';
}
}
2016-04-23 06:23:51 +00:00
5001 - drop the :(scenario) DSL I've been saying for a while[1][2][3] that adding extra abstractions makes things harder for newcomers, and adding new notations doubly so. And then I notice this DSL in my own backyard. Makes me feel like a hypocrite. [1] https://news.ycombinator.com/item?id=13565743#13570092 [2] https://lobste.rs/s/to8wpr/configuration_files_are_canary_warning [3] https://lobste.rs/s/mdmcdi/little_languages_by_jon_bentley_1986#c_3miuf2 The implementation of the DSL was also highly hacky: a) It was happening in the tangle/ tool, but was utterly unrelated to tangling layers. b) There were several persnickety constraints on the different kinds of lines and the specific order they were expected in. I kept finding bugs where the translator would silently do the wrong thing. Or the error messages sucked, and readers may be stuck looking at the generated code to figure out what happened. Fixing error messages would require a lot more code, which is one of my arguments against DSLs in the first place: they may be easy to implement, but they're hard to design to go with the grain of the underlying platform. They require lots of iteration. Is that effort worth prioritizing in this project? On the other hand, the DSL did make at least some readers' life easier, the ones who weren't immediately put off by having to learn a strange syntax. There were fewer quotes to parse, fewer backslash escapes. Anyway, since there are also people who dislike having to put up with strange syntaxes, we'll call that consideration a wash and tear this DSL out. --- This commit was sheer drudgery. Hopefully it won't need to be redone with a new DSL because I grow sick of backslashes.
2019-03-13 01:56:55 +00:00
void test_wait_for_location_can_deadlock() {
Hide_errors = true;
run(
"def main [\n"
" 10:num <- copy 1\n"
" 20:location <- copy 10/unsafe\n"
" wait-for-reset-then-set 20:location\n"
"]\n"
);
CHECK_TRACE_CONTENTS(
"error: deadlock!\n"
);
}
2016-10-18 05:36:29 +00:00
//: Primitive recipe to put routines in that state.
//: This primitive is also known elsewhere as compare-and-set (CAS). Used to
//: build locks.
:(before "End Primitive Recipe Declarations")
WAIT_FOR_RESET_THEN_SET,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "wait-for-reset-then-set", WAIT_FOR_RESET_THEN_SET);
2015-10-02 00:30:14 +00:00
:(before "End Primitive Recipe Checks")
case WAIT_FOR_RESET_THEN_SET: {
if (SIZE(inst.ingredients) != 1) {
2017-05-26 23:43:18 +00:00
raise << maybe(get(Recipe, r).name) << "'wait-for-reset-then-set' requires exactly one ingredient, but got '" << to_original_string(inst) << "'\n" << end();
break;
}
if (!is_mu_location(inst.ingredients.at(0))) {
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();
}
2015-10-02 00:30:14 +00:00
break;
}
:(before "End Primitive Recipe Implementations")
case WAIT_FOR_RESET_THEN_SET: {
int loc = static_cast<int>(ingredients.at(0).at(0));
trace(Callstack_depth+1, "run") << "wait: *" << loc << " = " << get_or_insert(Memory, loc) << end();
if (get_or_insert(Memory, loc) == 0) {
trace(Callstack_depth+1, "run") << "location " << loc << " is already 0; setting" << end();
put(Memory, loc, 1);
break;
}
trace(Callstack_depth+1, "run") << "waiting for location " << loc << " to reset" << end();
Current_routine->state = WAITING;
Current_routine->waiting_on_location = loc;
break;
}
//: Counterpart to unlock a lock.
:(before "End Primitive Recipe Declarations")
RESET,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "reset", RESET);
:(before "End Primitive Recipe Checks")
case RESET: {
if (SIZE(inst.ingredients) != 1) {
2017-05-26 23:43:18 +00:00
raise << maybe(get(Recipe, r).name) << "'reset' requires exactly one ingredient, but got '" << to_original_string(inst) << "'\n" << end();
break;
}
if (!is_mu_location(inst.ingredients.at(0))) {
raise << maybe(get(Recipe, r).name) << "'reset' requires a location ingredient, but got '" << inst.ingredients.at(0).original_string << "'\n" << end();
}
break;
}
:(before "End Primitive Recipe Implementations")
case RESET: {
int loc = static_cast<int>(ingredients.at(0).at(0));
put(Memory, loc, 0);
trace(Callstack_depth+1, "run") << "reset: *" << loc << " = " << get_or_insert(Memory, loc) << end();
break;
}
//: scheduler tweak to get routines out of that state
:(before "End Scheduler State Transitions")
2016-10-20 05:10:35 +00:00
for (int i = 0; i < SIZE(Routines); ++i) {
if (Routines.at(i)->state != WAITING) continue;
int loc = Routines.at(i)->waiting_on_location;
if (loc && get_or_insert(Memory, loc) == 0) {
trace(100, "schedule") << "waking up routine " << Routines.at(i)->id << end();
put(Memory, loc, 1);
Routines.at(i)->state = RUNNING;
Routines.at(i)->waiting_on_location = 0;
}
}
2015-05-06 01:30:53 +00:00
//: Primitive to help compute locations to wait on.
//: Only supports elements immediately inside containers; no arrays or
//: containers within containers yet.
5001 - drop the :(scenario) DSL I've been saying for a while[1][2][3] that adding extra abstractions makes things harder for newcomers, and adding new notations doubly so. And then I notice this DSL in my own backyard. Makes me feel like a hypocrite. [1] https://news.ycombinator.com/item?id=13565743#13570092 [2] https://lobste.rs/s/to8wpr/configuration_files_are_canary_warning [3] https://lobste.rs/s/mdmcdi/little_languages_by_jon_bentley_1986#c_3miuf2 The implementation of the DSL was also highly hacky: a) It was happening in the tangle/ tool, but was utterly unrelated to tangling layers. b) There were several persnickety constraints on the different kinds of lines and the specific order they were expected in. I kept finding bugs where the translator would silently do the wrong thing. Or the error messages sucked, and readers may be stuck looking at the generated code to figure out what happened. Fixing error messages would require a lot more code, which is one of my arguments against DSLs in the first place: they may be easy to implement, but they're hard to design to go with the grain of the underlying platform. They require lots of iteration. Is that effort worth prioritizing in this project? On the other hand, the DSL did make at least some readers' life easier, the ones who weren't immediately put off by having to learn a strange syntax. There were fewer quotes to parse, fewer backslash escapes. Anyway, since there are also people who dislike having to put up with strange syntaxes, we'll call that consideration a wash and tear this DSL out. --- This commit was sheer drudgery. Hopefully it won't need to be redone with a new DSL because I grow sick of backslashes.
2019-03-13 01:56:55 +00:00
:(code)
void test_get_location() {
run(
"def main [\n"
" 12:num <- copy 34\n"
" 13:num <- copy 35\n"
" 15:location <- get-location 12:point, 1:offset\n"
"]\n"
);
CHECK_TRACE_CONTENTS(
"mem: storing 13 in location 15\n"
);
}
:(before "End Primitive Recipe Declarations")
GET_LOCATION,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "get-location", GET_LOCATION);
:(before "End Primitive Recipe Checks")
case GET_LOCATION: {
if (SIZE(inst.ingredients) != 2) {
2017-05-26 23:43:18 +00:00
raise << maybe(get(Recipe, r).name) << "'get-location' expects exactly 2 ingredients in '" << to_original_string(inst) << "'\n" << end();
break;
}
2016-05-06 07:46:39 +00:00
reagent/*copy*/ base = inst.ingredients.at(0);
if (!canonize_type(base)) break;
3309 Rip out everything to fix one failing unit test (commit 3290; type abbreviations). This commit does several things at once that I couldn't come up with a clean way to unpack: A. It moves to a new representation for type trees without changing the actual definition of the `type_tree` struct. B. It adds unit tests for our type metadata precomputation, so that errors there show up early and in a simpler setting rather than dying when we try to load Mu code. C. It fixes a bug, guarding against infinite loops when precomputing metadata for recursive shape-shifting containers. To do this it uses a dumb way of comparing type_trees, comparing their string representations instead. That is likely incredibly inefficient. Perhaps due to C, this commit has made Mu incredibly slow. Running all tests for the core and the edit/ app now takes 6.5 minutes rather than 3.5 minutes. == more notes and details I've been struggling for the past week now to back out of a bad design decision, a premature optimization from the early days: storing atoms directly in the 'value' slot of a cons cell rather than creating a special 'atom' cons cell and storing it on the 'left' slot. In other words, if a cons cell looks like this: o / | \ left val right ..then the type_tree (a b c) used to look like this (before this commit): o | \ a o | \ b o | \ c null ..rather than like this 'classic' approach to s-expressions which never mixes val and right (which is what we now have): o / \ o o | / \ a o o | / \ b o null | c The old approach made several operations more complicated, most recently the act of replacing a (possibly atom/leaf) sub-tree with another. That was the final straw that got me to realize the contortions I was going through to save a few type_tree nodes (cons cells). Switching to the new approach was hard partly because I've been using the old approach for so long and type_tree manipulations had pervaded everything. Another issue I ran into was the realization that my layers were not cleanly separated. Key parts of early layers (precomputing type metadata) existed purely for far later ones (shape-shifting types). Layers I got repeatedly stuck at: 1. the transform for precomputing type sizes (layer 30) 2. type-checks on merge instructions (layer 31) 3. the transform for precomputing address offsets in types (layer 36) 4. replace operations in supporting shape-shifting recipes (layer 55) After much thrashing I finally noticed that it wasn't the entirety of these layers that was giving me trouble, but just the type metadata precomputation, which had bugs that weren't manifesting until 30 layers later. Or, worse, when loading .mu files before any tests had had a chance to run. A common failure mode was running into types at run time that I hadn't precomputed metadata for at transform time. Digging into these bugs got me to realize that what I had before wasn't really very good, but a half-assed heuristic approach that did a whole lot of extra work precomputing metadata for utterly meaningless types like `((address number) 3)` which just happened to be part of a larger type like `(array (address number) 3)`. So, I redid it all. I switched the representation of types (because the old representation made unit tests difficult to retrofit) and added unit tests to the metadata precomputation. I also made layer 30 only do the minimal metadata precomputation it needs for the concepts introduced until then. In the process, I also made the precomputation more correct than before, and added hooks in the right place so that I could augment the logic when I introduced shape-shifting containers. == lessons learned There's several levels of hygiene when it comes to layers: 1. Every layer introduces precisely what it needs and in the simplest way possible. If I was building an app until just that layer, nothing would seem over-engineered. 2. Some layers are fore-shadowing features in future layers. Sometimes this is ok. For example, layer 10 foreshadows containers and arrays and so on without actually supporting them. That is a net win because it lets me lay out the core of Mu's data structures out in one place. But if the fore-shadowing gets too complex things get nasty. Not least because it can be hard to write unit tests for features before you provide the plumbing to visualize and manipulate them. 3. A layer is introducing features that are tested only in later layers. 4. A layer is introducing features with tests that are invalidated in later layers. (This I knew from early on to be an obviously horrendous idea.) Summary: avoid Level 2 (foreshadowing layers) as much as possible. Tolerate it indefinitely for small things where the code stays simple over time, but become strict again when things start to get more complex. Level 3 is mostly a net lose, but sometimes it can be expedient (a real case of the usually grossly over-applied term "technical debt"), and it's better than the conventional baseline of no layers and no scenarios. Just clean it up as soon as possible. Definitely avoid layer 4 at any time. == minor lessons Avoid unit tests for trivial things, write scenarios in context as much as possible. But within those margins unit tests are fine. Just introduce them before any scenarios (commit 3297). Reorganizing layers can be easy. Just merge layers for starters! Punt on resplitting them in some new way until you've gotten them to work. This is the wisdom of Refactoring: small steps. What made it hard was not wanting to merge *everything* between layer 30 and 55. The eventual insight was realizing I just need to move those two full-strength transforms and nothing else.
2016-09-10 01:32:52 +00:00
if (!base.type) {
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();
break;
}
const type_tree* base_root_type = base.type->atom ? base.type : base.type->left;
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) {
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();
break;
}
type_ordinal base_type = base.type->value;
2016-05-06 07:46:39 +00:00
const reagent& offset = inst.ingredients.at(1);
if (!is_literal(offset) || !is_mu_scalar(offset)) {
raise << maybe(get(Recipe, r).name) << "second ingredient of 'get-location' should have type 'offset', but got '" << inst.ingredients.at(1).original_string << "'\n" << end();
break;
}
int offset_value = 0;
//: later layers will permit non-integer offsets
if (is_integer(offset.name)) {
offset_value = to_integer(offset.name);
if (offset_value < 0 || offset_value >= SIZE(get(Type, base_type).elements)) {
raise << maybe(get(Recipe, r).name) << "invalid offset " << offset_value << " for '" << get(Type, base_type).name << "'\n" << end();
break;
}
}
else {
offset_value = offset.value;
}
if (inst.products.empty()) break;
if (!is_mu_location(inst.products.at(0))) {
raise << maybe(get(Recipe, r).name) << "'get-location " << base.original_string << ", " << offset.original_string << "' should write to type location but '" << inst.products.at(0).name << "' has type '" << names_to_string_without_quotes(inst.products.at(0).type) << "'\n" << end();
break;
}
break;
}
:(before "End Primitive Recipe Implementations")
case GET_LOCATION: {
2016-05-06 07:46:39 +00:00
reagent/*copy*/ base = current_instruction().ingredients.at(0);
canonize(base);
int base_address = base.value;
if (base_address == 0) {
2017-05-26 23:43:18 +00:00
raise << maybe(current_recipe_name()) << "tried to access location 0 in '" << to_original_string(current_instruction()) << "'\n" << end();
break;
}
const type_tree* base_type = get_base_type(base.type);
int offset = ingredients.at(1).at(0);
if (offset < 0 || offset >= SIZE(get(Type, base_type->value).elements)) break; // copied from Check above
int result = base_address;
2016-10-20 05:10:35 +00:00
for (int i = 0; i < offset; ++i)
2016-04-30 17:09:38 +00:00
result += size_of(element_type(base.type, i));
trace(Callstack_depth+1, "run") << "address to copy is " << result << end();
products.resize(1);
products.at(0).push_back(result);
break;
}
:(code)
2016-05-06 07:46:39 +00:00
bool is_mu_location(reagent/*copy*/ x) {
if (!canonize_type(x)) return false;
if (!x.type) return false;
2016-11-08 12:18:50 +00:00
if (!x.type->atom) return false;
return x.type->value == get(Type_ordinal, "location");
}
5001 - drop the :(scenario) DSL I've been saying for a while[1][2][3] that adding extra abstractions makes things harder for newcomers, and adding new notations doubly so. And then I notice this DSL in my own backyard. Makes me feel like a hypocrite. [1] https://news.ycombinator.com/item?id=13565743#13570092 [2] https://lobste.rs/s/to8wpr/configuration_files_are_canary_warning [3] https://lobste.rs/s/mdmcdi/little_languages_by_jon_bentley_1986#c_3miuf2 The implementation of the DSL was also highly hacky: a) It was happening in the tangle/ tool, but was utterly unrelated to tangling layers. b) There were several persnickety constraints on the different kinds of lines and the specific order they were expected in. I kept finding bugs where the translator would silently do the wrong thing. Or the error messages sucked, and readers may be stuck looking at the generated code to figure out what happened. Fixing error messages would require a lot more code, which is one of my arguments against DSLs in the first place: they may be easy to implement, but they're hard to design to go with the grain of the underlying platform. They require lots of iteration. Is that effort worth prioritizing in this project? On the other hand, the DSL did make at least some readers' life easier, the ones who weren't immediately put off by having to learn a strange syntax. There were fewer quotes to parse, fewer backslash escapes. Anyway, since there are also people who dislike having to put up with strange syntaxes, we'll call that consideration a wash and tear this DSL out. --- This commit was sheer drudgery. Hopefully it won't need to be redone with a new DSL because I grow sick of backslashes.
2019-03-13 01:56:55 +00:00
void test_get_location_out_of_bounds() {
Hide_errors = true;
run(
"def main [\n"
" 12:num <- copy 34\n"
" 13:num <- copy 35\n"
" 14:num <- copy 36\n"
" get-location 12:point-number/raw, 2:offset\n" // point-number occupies 3 locations but has only 2 fields; out of bounds
"]\n"
);
CHECK_TRACE_CONTENTS(
"error: main: invalid offset 2 for 'point-number'\n"
);
}
void test_get_location_out_of_bounds_2() {
Hide_errors = true;
run(
"def main [\n"
" 12:num <- copy 34\n"
" 13:num <- copy 35\n"
" 14:num <- copy 36\n"
" get-location 12:point-number/raw, -1:offset\n"
"]\n"
);
CHECK_TRACE_CONTENTS(
"error: main: invalid offset -1 for 'point-number'\n"
);
}
void test_get_location_product_type_mismatch() {
Hide_errors = true;
run(
"container boolbool [\n"
" x:bool\n"
" y:bool\n"
"]\n"
"def main [\n"
" 12:bool <- copy 1\n"
" 13:bool <- copy 0\n"
" 15:bool <- get-location 12:boolbool, 1:offset\n"
"]\n"
);
CHECK_TRACE_CONTENTS(
"error: main: 'get-location 12:boolbool, 1:offset' should write to type location but '15' has type 'boolean'\n"
);
}
void test_get_location_indirect() {
// 'get-location' can read from container address
run(
"def main [\n"
" 1:num/alloc-id, 2:num <- copy 0, 10\n"
" 10:num/alloc-id, 11:num/x, 12:num/y <- copy 0, 34, 35\n"
" 20:location <- get-location 1:&:point/lookup, 0:offset\n"
"]\n"
);
CHECK_TRACE_CONTENTS(
"mem: storing 11 in location 20\n"
);
}
void test_get_location_indirect_2() {
run(
"def main [\n"
" 1:num/alloc-id, 2:num <- copy 0, 10\n"
" 10:num/alloc-id, 11:num/x, 12:num/y <- copy 0, 34, 35\n"
" 4:num/alloc-id, 5:num <- copy 0, 20\n"
" 4:&:location/lookup <- get-location 1:&:point/lookup, 0:offset\n"
"]\n"
);
CHECK_TRACE_CONTENTS(
"mem: storing 11 in location 21\n"
);
}
//: allow waiting on a routine to complete
5001 - drop the :(scenario) DSL I've been saying for a while[1][2][3] that adding extra abstractions makes things harder for newcomers, and adding new notations doubly so. And then I notice this DSL in my own backyard. Makes me feel like a hypocrite. [1] https://news.ycombinator.com/item?id=13565743#13570092 [2] https://lobste.rs/s/to8wpr/configuration_files_are_canary_warning [3] https://lobste.rs/s/mdmcdi/little_languages_by_jon_bentley_1986#c_3miuf2 The implementation of the DSL was also highly hacky: a) It was happening in the tangle/ tool, but was utterly unrelated to tangling layers. b) There were several persnickety constraints on the different kinds of lines and the specific order they were expected in. I kept finding bugs where the translator would silently do the wrong thing. Or the error messages sucked, and readers may be stuck looking at the generated code to figure out what happened. Fixing error messages would require a lot more code, which is one of my arguments against DSLs in the first place: they may be easy to implement, but they're hard to design to go with the grain of the underlying platform. They require lots of iteration. Is that effort worth prioritizing in this project? On the other hand, the DSL did make at least some readers' life easier, the ones who weren't immediately put off by having to learn a strange syntax. There were fewer quotes to parse, fewer backslash escapes. Anyway, since there are also people who dislike having to put up with strange syntaxes, we'll call that consideration a wash and tear this DSL out. --- This commit was sheer drudgery. Hopefully it won't need to be redone with a new DSL because I grow sick of backslashes.
2019-03-13 01:56:55 +00:00
void test_wait_for_routine() {
run(
"def f1 [\n"
// add a few routines to run
" 1:num/routine <- start-running f2\n"
" 2:num/routine <- start-running f3\n"
" wait-for-routine 1:num/routine\n"
// now wait for f2 to *complete* and modify location 13 before using its value
" 20:num <- copy 13:num\n"
"]\n"
"def f2 [\n"
" 10:num <- copy 0\n" // just padding
" switch\n" // simulate a block; routine f1 shouldn't restart at this point
" 13:num <- copy 34\n"
"]\n"
"def f3 [\n"
// padding routine just to help simulate the block in f2 using 'switch'
" 11:num <- copy 0\n"
" 12:num <- copy 0\n"
"]\n"
);
CHECK_TRACE_CONTENTS(
"schedule: f1\n"
"run: waiting for routine 2\n"
"schedule: f2\n"
"schedule: f3\n"
"schedule: f2\n"
"schedule: waking up routine 1\n"
"schedule: f1\n"
// if we got the synchronization wrong we'd be storing 0 in location 20
"mem: storing 34 in location 20\n"
);
}
:(before "End routine Fields")
// only if state == WAITING
int waiting_on_routine;
:(before "End routine Constructor")
waiting_on_routine = 0;
:(before "End Primitive Recipe Declarations")
WAIT_FOR_ROUTINE,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "wait-for-routine", WAIT_FOR_ROUTINE);
:(before "End Primitive Recipe Checks")
case WAIT_FOR_ROUTINE: {
if (SIZE(inst.ingredients) != 1) {
2017-05-26 23:43:18 +00:00
raise << maybe(get(Recipe, r).name) << "'wait-for-routine' 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 'wait-for-routine' 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 WAIT_FOR_ROUTINE: {
if (ingredients.at(0).at(0) == Current_routine->id) {
2017-05-26 23:43:18 +00:00
raise << maybe(current_recipe_name()) << "routine can't wait for itself! '" << to_original_string(current_instruction()) << "'\n" << end();
break;
}
Current_routine->state = WAITING;
Current_routine->waiting_on_routine = ingredients.at(0).at(0);
trace(Callstack_depth+1, "run") << "waiting for routine " << ingredients.at(0).at(0) << end();
break;
}
:(before "End Scheduler State Transitions")
// Wake up any routines waiting for other routines to complete.
// Important: this must come after the scheduler loop above giving routines
// waiting for locations to change a chance to wake up.
2016-10-20 05:10:35 +00:00
for (int i = 0; i < SIZE(Routines); ++i) {
if (Routines.at(i)->state != WAITING) continue;
routine* waiter = Routines.at(i);
if (!waiter->waiting_on_routine) continue;
int id = waiter->waiting_on_routine;
assert(id != waiter->id); // routine can't wait on itself
2016-10-20 05:10:35 +00:00
for (int j = 0; j < SIZE(Routines); ++j) {
const routine* waitee = Routines.at(j);
if (waitee->id == id && waitee->state != RUNNING && waitee->state != WAITING) {
// routine is COMPLETED or DISCONTINUED
trace(100, "schedule") << "waking up routine " << waiter->id << end();
waiter->state = RUNNING;
waiter->waiting_on_routine = 0;
}
}
}
//: yield voluntarily to let some other routine run
:(before "End Primitive Recipe Declarations")
SWITCH,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "switch", SWITCH);
:(before "End Primitive Recipe Checks")
case SWITCH: {
break;
}
:(before "End Primitive Recipe Implementations")
case SWITCH: {
++current_step_index();
goto stop_running_current_routine;
}
5001 - drop the :(scenario) DSL I've been saying for a while[1][2][3] that adding extra abstractions makes things harder for newcomers, and adding new notations doubly so. And then I notice this DSL in my own backyard. Makes me feel like a hypocrite. [1] https://news.ycombinator.com/item?id=13565743#13570092 [2] https://lobste.rs/s/to8wpr/configuration_files_are_canary_warning [3] https://lobste.rs/s/mdmcdi/little_languages_by_jon_bentley_1986#c_3miuf2 The implementation of the DSL was also highly hacky: a) It was happening in the tangle/ tool, but was utterly unrelated to tangling layers. b) There were several persnickety constraints on the different kinds of lines and the specific order they were expected in. I kept finding bugs where the translator would silently do the wrong thing. Or the error messages sucked, and readers may be stuck looking at the generated code to figure out what happened. Fixing error messages would require a lot more code, which is one of my arguments against DSLs in the first place: they may be easy to implement, but they're hard to design to go with the grain of the underlying platform. They require lots of iteration. Is that effort worth prioritizing in this project? On the other hand, the DSL did make at least some readers' life easier, the ones who weren't immediately put off by having to learn a strange syntax. There were fewer quotes to parse, fewer backslash escapes. Anyway, since there are also people who dislike having to put up with strange syntaxes, we'll call that consideration a wash and tear this DSL out. --- This commit was sheer drudgery. Hopefully it won't need to be redone with a new DSL because I grow sick of backslashes.
2019-03-13 01:56:55 +00:00
:(code)
void test_switch_preempts_current_routine() {
run(
"def f1 [\n"
" start-running f2\n"
" 1:num <- copy 34\n"
" switch\n"
" 3:num <- copy 36\n"
"]\n"
"def f2 [\n"
" 2:num <- copy 35\n"
"]\n"
);
CHECK_TRACE_CONTENTS(
"mem: storing 34 in location 1\n"
// context switch
"mem: storing 35 in location 2\n"
// back to original thread
"mem: storing 36 in location 3\n"
);
}
//:: helpers for manipulating routines in tests
//:
//: Managing arbitrary scenarios requires the ability to:
//: a) check if a routine is blocked
2017-09-01 08:50:10 +00:00
//: b) restart a blocked routine ('restart')
//:
//: A routine is blocked either if it's waiting or if it explicitly signals
//: that it's blocked (even as it periodically wakes up and polls for some
//: event).
//:
//: Signalling blockedness might well be a huge hack. But Mu doesn't have Unix
//: signals to avoid polling with, because signals are also pretty hacky.
:(before "End routine Fields")
bool blocked;
:(before "End routine Constructor")
blocked = false;
:(before "End Primitive Recipe Declarations")
CURRENT_ROUTINE_IS_BLOCKED,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "current-routine-is-blocked", CURRENT_ROUTINE_IS_BLOCKED);
:(before "End Primitive Recipe Checks")
case CURRENT_ROUTINE_IS_BLOCKED: {
if (!inst.ingredients.empty()) {
2017-05-26 23:43:18 +00:00
raise << maybe(get(Recipe, r).name) << "'current-routine-is-blocked' should have no ingredients, but got '" << to_original_string(inst) << "'\n" << end();
break;
}
break;
}
:(before "End Primitive Recipe Implementations")
case CURRENT_ROUTINE_IS_BLOCKED: {
Current_routine->blocked = true;
break;
}
:(before "End Primitive Recipe Declarations")
CURRENT_ROUTINE_IS_UNBLOCKED,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "current-routine-is-unblocked", CURRENT_ROUTINE_IS_UNBLOCKED);
:(before "End Primitive Recipe Checks")
case CURRENT_ROUTINE_IS_UNBLOCKED: {
if (!inst.ingredients.empty()) {
2017-05-26 23:43:18 +00:00
raise << maybe(get(Recipe, r).name) << "'current-routine-is-unblocked' should have no ingredients, but got '" << to_original_string(inst) << "'\n" << end();
break;
}
break;
}
:(before "End Primitive Recipe Implementations")
case CURRENT_ROUTINE_IS_UNBLOCKED: {
Current_routine->blocked = false;
break;
}
//: also allow waiting on a routine to block
//: (just for tests; use wait_for_routine above wherever possible)
2015-05-06 01:30:53 +00:00
5001 - drop the :(scenario) DSL I've been saying for a while[1][2][3] that adding extra abstractions makes things harder for newcomers, and adding new notations doubly so. And then I notice this DSL in my own backyard. Makes me feel like a hypocrite. [1] https://news.ycombinator.com/item?id=13565743#13570092 [2] https://lobste.rs/s/to8wpr/configuration_files_are_canary_warning [3] https://lobste.rs/s/mdmcdi/little_languages_by_jon_bentley_1986#c_3miuf2 The implementation of the DSL was also highly hacky: a) It was happening in the tangle/ tool, but was utterly unrelated to tangling layers. b) There were several persnickety constraints on the different kinds of lines and the specific order they were expected in. I kept finding bugs where the translator would silently do the wrong thing. Or the error messages sucked, and readers may be stuck looking at the generated code to figure out what happened. Fixing error messages would require a lot more code, which is one of my arguments against DSLs in the first place: they may be easy to implement, but they're hard to design to go with the grain of the underlying platform. They require lots of iteration. Is that effort worth prioritizing in this project? On the other hand, the DSL did make at least some readers' life easier, the ones who weren't immediately put off by having to learn a strange syntax. There were fewer quotes to parse, fewer backslash escapes. Anyway, since there are also people who dislike having to put up with strange syntaxes, we'll call that consideration a wash and tear this DSL out. --- This commit was sheer drudgery. Hopefully it won't need to be redone with a new DSL because I grow sick of backslashes.
2019-03-13 01:56:55 +00:00
:(code)
void test_wait_for_routine_to_block() {
run(
"def f1 [\n"
" 1:num/routine <- start-running f2\n"
" wait-for-routine-to-block 1:num/routine\n"
// now wait for f2 to run and modify location 10 before using its value
" 11:num <- copy 10:num\n"
"]\n"
"def f2 [\n"
" 10:num <- copy 34\n"
"]\n"
);
CHECK_TRACE_CONTENTS(
"schedule: f1\n"
"run: waiting for routine 2 to block\n"
"schedule: f2\n"
"schedule: waking up routine 1 because routine 2 is blocked\n"
"schedule: f1\n"
// if we got the synchronization wrong we'd be storing 0 in location 11
"mem: storing 34 in location 11\n"
);
}
2015-05-06 01:30:53 +00:00
:(before "End routine Fields")
// only if state == WAITING
int waiting_on_routine_to_block;
2015-05-06 01:30:53 +00:00
:(before "End routine Constructor")
waiting_on_routine_to_block = 0;
2015-05-06 01:30:53 +00:00
:(before "End Primitive Recipe Declarations")
WAIT_FOR_ROUTINE_TO_BLOCK,
2015-05-06 01:30:53 +00:00
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "wait-for-routine-to-block", WAIT_FOR_ROUTINE_TO_BLOCK);
2015-10-02 00:30:14 +00:00
:(before "End Primitive Recipe Checks")
case WAIT_FOR_ROUTINE_TO_BLOCK: {
2015-10-02 00:30:14 +00:00
if (SIZE(inst.ingredients) != 1) {
2017-05-26 23:43:18 +00:00
raise << maybe(get(Recipe, r).name) << "'wait-for-routine-to-block' 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 'wait-for-routine-to-block' should be a routine id generated by 'start-running', but got '" << inst.ingredients.at(0).original_string << "'\n" << end();
break;
}
2015-10-02 00:30:14 +00:00
break;
}
:(before "End Primitive Recipe Implementations")
case WAIT_FOR_ROUTINE_TO_BLOCK: {
if (ingredients.at(0).at(0) == Current_routine->id) {
2017-05-26 23:43:18 +00:00
raise << maybe(current_recipe_name()) << "routine can't wait for itself! '" << to_original_string(current_instruction()) << "'\n" << end();
break;
}
2015-05-06 01:30:53 +00:00
Current_routine->state = WAITING;
Current_routine->waiting_on_routine_to_block = ingredients.at(0).at(0);
trace(Callstack_depth+1, "run") << "waiting for routine " << ingredients.at(0).at(0) << " to block" << end();
2015-05-06 01:30:53 +00:00
break;
}
:(before "End Scheduler State Transitions")
// Wake up any routines waiting for other routines to stop running.
2016-10-20 05:10:35 +00:00
for (int i = 0; i < SIZE(Routines); ++i) {
if (Routines.at(i)->state != WAITING) continue;
routine* waiter = Routines.at(i);
if (!waiter->waiting_on_routine_to_block) continue;
int id = waiter->waiting_on_routine_to_block;
assert(id != waiter->id); // routine can't wait on itself
2016-10-20 05:10:35 +00:00
for (int j = 0; j < SIZE(Routines); ++j) {
const routine* waitee = Routines.at(j);
if (waitee->id != id) continue;
if (waitee->state != RUNNING || waitee->blocked) {
trace(100, "schedule") << "waking up routine " << waiter->id << " because routine " << waitee->id << " is blocked" << end();
waiter->state = RUNNING;
waiter->waiting_on_routine_to_block = 0;
2015-05-06 01:30:53 +00:00
}
}
}
//: helper for restarting blocking routines in tests
2016-08-16 18:31:05 +00:00
:(before "End Primitive Recipe Declarations")
RESTART,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "restart", RESTART);
:(before "End Primitive Recipe Checks")
case RESTART: {
if (SIZE(inst.ingredients) != 1) {
2017-05-26 23:43:18 +00:00
raise << maybe(get(Recipe, r).name) << "'restart' requires exactly one ingredient, but got '" << to_original_string(inst) << "'\n" << end();
2016-08-16 18:31:05 +00:00
break;
}
if (!is_mu_number(inst.ingredients.at(0))) {
raise << maybe(get(Recipe, r).name) << "first ingredient of 'restart' 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 RESTART: {
int id = ingredients.at(0).at(0);
2016-10-20 05:10:35 +00:00
for (int i = 0; i < SIZE(Routines); ++i) {
2016-08-16 18:31:05 +00:00
if (Routines.at(i)->id == id) {
if (Routines.at(i)->state == WAITING)
Routines.at(i)->state = RUNNING;
Routines.at(i)->blocked = false;
2016-08-16 18:31:05 +00:00
break;
}
}
break;
}
5001 - drop the :(scenario) DSL I've been saying for a while[1][2][3] that adding extra abstractions makes things harder for newcomers, and adding new notations doubly so. And then I notice this DSL in my own backyard. Makes me feel like a hypocrite. [1] https://news.ycombinator.com/item?id=13565743#13570092 [2] https://lobste.rs/s/to8wpr/configuration_files_are_canary_warning [3] https://lobste.rs/s/mdmcdi/little_languages_by_jon_bentley_1986#c_3miuf2 The implementation of the DSL was also highly hacky: a) It was happening in the tangle/ tool, but was utterly unrelated to tangling layers. b) There were several persnickety constraints on the different kinds of lines and the specific order they were expected in. I kept finding bugs where the translator would silently do the wrong thing. Or the error messages sucked, and readers may be stuck looking at the generated code to figure out what happened. Fixing error messages would require a lot more code, which is one of my arguments against DSLs in the first place: they may be easy to implement, but they're hard to design to go with the grain of the underlying platform. They require lots of iteration. Is that effort worth prioritizing in this project? On the other hand, the DSL did make at least some readers' life easier, the ones who weren't immediately put off by having to learn a strange syntax. There were fewer quotes to parse, fewer backslash escapes. Anyway, since there are also people who dislike having to put up with strange syntaxes, we'll call that consideration a wash and tear this DSL out. --- This commit was sheer drudgery. Hopefully it won't need to be redone with a new DSL because I grow sick of backslashes.
2019-03-13 01:56:55 +00:00
:(code)
void test_cannot_restart_completed_routine() {
Scheduling_interval = 1;
run(
"def main [\n"
" local-scope\n"
" r:num/routine-id <- start-running f\n"
" x:num <- copy 0\n" // wait for f to be scheduled
// r is COMPLETED by this point
" restart r\n" // should have no effect
" x:num <- copy 0\n" // give f time to be scheduled (though it shouldn't be)
"]\n"
"def f [\n"
" 1:num/raw <- copy 1\n"
"]\n"
);
// shouldn't crash
}
void test_restart_blocked_routine() {
Scheduling_interval = 1;
run(
"def main [\n"
" local-scope\n"
" r:num/routine-id <- start-running f\n"
" wait-for-routine-to-block r\n" // get past the block in f below
" restart r\n"
" wait-for-routine-to-block r\n" // should run f to completion
"]\n"
// function with one block
"def f [\n"
" current-routine-is-blocked\n"
// 8 instructions of padding, many more than 'main' above
" 1:num <- add 1:num, 1\n"
" 1:num <- add 1:num, 1\n"
" 1:num <- add 1:num, 1\n"
" 1:num <- add 1:num, 1\n"
" 1:num <- add 1:num, 1\n"
" 1:num <- add 1:num, 1\n"
" 1:num <- add 1:num, 1\n"
" 1:num <- add 1:num, 1\n"
" 1:num <- add 1:num, 1\n"
"]\n"
);
// make sure all of f ran
CHECK_TRACE_CONTENTS(
"mem: storing 8 in location 1\n"
);
}