Permalink
Browse files

Add a CurrentThreadScheduler.

Always schedules things on the current thread. Useful for forcing
synchrony on things that default to asynchrony.
  • Loading branch information...
1 parent 7409806 commit 15a4103275e9dd5acc97fe0cfab934f695be5d93 @jnthn jnthn committed Oct 31, 2013
Showing with 28 additions and 0 deletions.
  1. +28 −0 src/vm/jvm/core/Threading.pm
View
28 src/vm/jvm/core/Threading.pm
@@ -234,6 +234,34 @@ my class ThreadPoolScheduler does Scheduler {
# This thread pool scheduler will be the default one.
$PROCESS::SCHEDULER = ThreadPoolScheduler.new();
+# Scheduler that always does things immediately, on the current thread.
+my class CurrentThreadScheduler does Scheduler {
+ method handle_uncaught($exception) {
+ $exception.throw
+ }
+
+ method schedule(&code) {
+ code()
+ }
+
+ method schedule_in(&code, $delay) {
+ sleep $delay;
+ code();
+ }
+
+ method schedule_every(&code, $interval, $delay = 0) {
+ sleep $delay;
+ loop {
+ code();
+ sleep $interval;
+ }
+ }
+
+ method outstanding() {
+ 0
+ }
+}
+
# A promise is a synchronization mechanism for a piece of work that will
# produce a single result (keeping the promise) or fail (breaking the
# promise).

0 comments on commit 15a4103

Please sign in to comment.