Skip to content

Instantly share code, notes, and snippets.

@sam-mccall
Created April 26, 2012 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sam-mccall/2499473 to your computer and use it in GitHub Desktop.
Save sam-mccall/2499473 to your computer and use it in GitHub Desktop.
thread-ring benchmark (Dart)
#import("dart:isolate");
final THREAD_COUNT = 50;
final TOKEN_PASSES = 203;
final SETUP_DONE = "done";
main() {
var first = spawnFunction(isoMain);
first.send([1, first], port);
port.receive((message, reply) {
if (message == SETUP_DONE) {
first.send(0);
} else {
print(message);
port.close();
}
});
}
isoMain() {
var id;
var next;
var master;
setup(id_, first_, master_) {
print("Spawning isolate $id_");
id = id_;
master = master_;
if (id < THREAD_COUNT) {
next = spawnFunction(isoMain);
next.send([id + 1, first_], master_);
} else {
next = first_;
master_.send(SETUP_DONE);
}
};
receivedToken(token) {
print("Isolate $id received token");
if (token == TOKEN_PASSES) {
master.send(id);
} else {
next.send(token + 1);
}
}
port.receive((message, reply) {
if (id == null) {
setup(message[0], message[1], reply);
} else {
receivedToken(message);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment