Permalink
Browse files

Hopefully final Concurrency up-to-specness

.schedule       -> .cue
.schedule_in    -> .cue( :in )
.schedule_every -> .cue( :every )
                -> .cue( :at )

Alas, there seems to be a MMD bug preventing this from working:

Ambiguous call to 'cue'; these signatures all match:
:(ThreadPoolScheduler : &code, Mu *%_)
:(ThreadPoolScheduler : &code, Mu *%_)

Leaving this to jnthn++ to figure out why
  • Loading branch information...
1 parent 6da29b2 commit d2cc29879c4d9009b03f91dab35d142e936e145e @lizmat lizmat committed Nov 8, 2013
Showing with 43 additions and 31 deletions.
  1. +18 −11 src/vm/jvm/core/CurrentThreadScheduler.pm
  2. +8 −5 src/vm/jvm/core/Scheduler.pm
  3. +17 −15 src/vm/jvm/core/ThreadPoolScheduler.pm
View
29 src/vm/jvm/core/CurrentThreadScheduler.pm
@@ -1,27 +1,34 @@
# Scheduler that always does things immediately, on the current thread.
+
my class CurrentThreadScheduler does Scheduler {
method handle_uncaught($exception) {
$exception.throw
}
- method schedule(&code) {
+ proto method cue(|) { * }
+ multi method cue(&code) {
code()
}
-
- method schedule_in(&code, $delay) {
- sleep $delay;
+ multi method cue(&code, :$in!) {
+ sleep $in;
code();
}
-
- method schedule_every(&code, $interval, $delay = 0) {
- sleep $delay;
+ multi method cue (&code, :$every!, :$in ) {
+ sleep $in if $in.defined;
loop {
code();
- sleep $interval;
+ sleep $every;
}
}
-
- method outstanding() {
- 0
+ multi method cue (&code, :$at!, :$every, :$in ) {
+ die "Cannot specify :at and :in at the same time" if $in.defined;
+ sleep $at - now;
+ loop {
+ code();
+ last unless $every.defined;
+ sleep $every;
+ }
}
+
+ method outstanding() { 0 }
}
View
13 src/vm/jvm/core/Scheduler.pm
@@ -1,6 +1,7 @@
# Schedulers do this role. It mostly serves as an interface for the things
# that schedulers must do, as well as a way to factor out some common "sugar"
# and infrastructure.
+
my role Scheduler {
has &.uncaught_handler is rw;
@@ -17,16 +18,18 @@ my role Scheduler {
}
}
- method schedule(&code) { ... }
+ proto method cue(|) { * }
+ multi method cue(&code) { ... }
+ multi method cue(&code, :$in!) { ... }
+ multi method cue(&code, :$every!, :$in = 0) { ... }
+ multi method cue(&code, :$at!, :$every, :$in) { ... }
- method schedule_with_catch(&code, &catch) {
- self.schedule({
+ method cue_with_catch(&code, &catch) {
+ self.cue({
code();
CATCH { default { catch($_) } }
})
}
- method schedule_in(&code, $delay) { ... }
- method schedule_every(&code, $interval, $delay = 0) { ... }
method outstanding() { ... }
}
View
32 src/vm/jvm/core/ThreadPoolScheduler.pm
@@ -51,37 +51,39 @@ my class ThreadPoolScheduler does Scheduler {
if $!initial_threads > $!max_threads;
}
- method schedule(&code) {
+ proto method cue(|) { * }
+ multi method cue(&code) {
self!initialize unless $!started_any;
my $outstanding = $!outstanding.incrementAndGet();
self!maybe_new_thread()
if !$!started_any || $outstanding > 1;
$!queue.add(nqp::jvmbootinterop().sixmodelToJavaObject(&code));
}
+ multi method cue(&code, :$in!) {
+ self!cue_in(&code, $in);
+ }
+ multi method cue(&code, :$every!, :$in = 0) {
+ self!cue_in(&code, $in, $every);
+ }
+ multi method cue(&code, :$at!, :$every, :$in) {
+ die "Cannot specify :at and :in at the same time" if $in.defined;
+ self!cue_in(&code, $at - now, $every);
+ }
- method schedule_in(&code, $delay) {
- self!initialize() unless $!started_any;
- $!timer.'method/schedule/(Ljava/util/TimerTask;J)V'(
- nqp::jvmbootinterop().proxy(
- 'java.util.TimerTask',
- nqp::hash('run', -> { code() })),
- ($delay * 1000).Int);
+ method outstanding() {
+ $!outstanding.get()
}
- method schedule_every(&code, $interval, $delay = 0) {
+ method !cue_in(&code, $in, $every?) {
self!initialize() unless $!started_any;
$!timer.'method/scheduleAtFixedRate/(Ljava/util/TimerTask;JJ)V'(
nqp::jvmbootinterop().proxy(
'java.util.TimerTask',
nqp::hash('run', -> { code() })),
- ($delay * 1000).Int,
- ($interval * 1000).Int);
+ ($in * 1000).Int,
+ ( $every.defined ?? ($every * 1000).Int !! () ) );
}
- method outstanding() {
- $!outstanding.get()
- }
-
method !initialize() {
# Things we will use from the JVM.
my $interop := nqp::jvmbootinterop();

0 comments on commit d2cc298

Please sign in to comment.