September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,34 @@
class OutOfInk(Exception.IOError){
const TEXT="Out of ink";
text=TEXT; // rename IOError to OutOfInk for this first/mother class
fcn init{ IOError.init(TEXT) } // this renames instances
}
class Printer{
var id, ink;
fcn init(_id,_ink){ id,ink=vm.arglist }
fcn print(line){
if(not ink) throw(OutOfInk);
println("%s: %s".fmt(id,line));
Atomic.sleep((0.0).random(0.01)); // don't let one thread dominate
ink-=1;
}
}
class RendezvousPrinter{ // the choke point between printers and tasks
var printers=Thread.List(); // a thread safe list
fcn init(_printers){ printers.extend(vm.arglist) }
fcn print(line){ // caller waits for print job to finish
var lines=Thread.List(); // fcn local [static] variable, the print queue
lines.write(line); // thread safe, stalls when full
// lines is racy - other threads are modifing it, length is suspect here
while(True){ // this thread can print that threads job
critical{ // creates a [global] mutex, automatically unlocks on exception
if(not printers) throw(OutOfInk); // No more printers to try
if(not lines) break; // only remove jobs in this serialized section
try{
printers[0].print(lines[0]); // can throw
lines.del(0); // successful print, remove job from queue
}catch(OutOfInk){ printers.del(0) } // Switch to the next printer
}
}
}
}

View file

@ -0,0 +1,20 @@
fcn printTask(taskNm,rendezvous,lines){
try{ foreach line in (vm.arglist[2,*]){ rendezvous.print(line); } }
catch{ println(taskNm," caught ",__exception); } // and thread quits trying to print
}
fcn humptyDumptyTask(rendezvous){ // a thread
printTask("humptyDumptyTask",rendezvous,
"Humpty Dumpty sat on a wall.",
"Humpty Dumpty had a great fall.",
"All the king's horses and all the king's men,",
"Couldn't put Humpty together again."
)
}
fcn motherGooseTask(rendezvous){ // a thread
printTask("motherGooseTask",rendezvous,
"Old Mother Goose,", "When she wanted to wander,",
"Would ride through the air,", "On a very fine gander.",
"Jack's mother came in,", "And caught the goose soon,",
"And mounting its back,", "Flew up to the moon."
)
}

View file

@ -0,0 +1,5 @@
rendezvous:=RendezvousPrinter(Printer("main",5), Printer("reserve",5));
humptyDumptyTask.launch(rendezvous);
motherGooseTask.launch(rendezvous);
Atomic.waitFor(fcn{ (not vm.numThreads) }); // wait for threads to finish