Created
April 26, 2012 13:15
-
-
Save sam-mccall/2499473 to your computer and use it in GitHub Desktop.
thread-ring benchmark (Dart)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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