Skip to content
Browse files

Implement *Scheduler.cue( :times )

  • Loading branch information...
1 parent 05c2fef commit a664d48018d753f3a09b4e02dd05c4f63b9d6462 @lizmat lizmat committed Dec 8, 2013
Showing with 18 additions and 14 deletions.
  1. +4 −2 src/vm/jvm/core/CurrentThreadScheduler.pm
  2. +14 −12 src/vm/jvm/core/ThreadPoolScheduler.pm
View
6 src/vm/jvm/core/CurrentThreadScheduler.pm
@@ -5,9 +5,11 @@ my class CurrentThreadScheduler does Scheduler {
$exception.throw
}
- method cue(&code, :$at, :$in, :$every, :&catch is copy ) {
+ method cue(&code, :$at, :$in, :$every, :$times = 1, :&catch is copy ) {
die "Cannot specify :at and :in at the same time"
if $at.defined and $in.defined;
+ die "Cannot specify :every and :times at the same time"
+ if $every.defined and $times > 1;
die "Cannot specify :every in {self.HOW.name(self)}"
if $every;
@@ -16,7 +18,7 @@ my class CurrentThreadScheduler does Scheduler {
&catch //=
self.uncaught_handler // -> $ex { self.handle_uncaught($ex) };
- code();
+ code() for 1 .. $times;
CATCH { default { catch($_) } };
}
View
26 src/vm/jvm/core/ThreadPoolScheduler.pm
@@ -52,9 +52,11 @@ my class ThreadPoolScheduler does Scheduler {
if $!initial_threads > $!max_threads;
}
- method cue(&code, :$at, :$in, :$every, :&catch ) {
+ method cue(&code, :$at, :$in, :$every, :$times = 1, :&catch ) {
die "Cannot specify :at and :in at the same time"
if $at.defined and $in.defined;
+ die "Cannot specify :every and :times at the same time"
+ if $every.defined and $times > 1;
my $delay = $at ?? $at - now !! $in // 0;
self!initialize unless $!started_any;
@@ -72,17 +74,17 @@ my class ThreadPoolScheduler does Scheduler {
($every * 1000).Int);
}
- # only after waiting a bit
- elsif $delay {
- $!timer.'method/schedule/(Ljava/util/TimerTask;J)V'(
- nqp::jvmbootinterop().proxy(
- 'java.util.TimerTask',
- nqp::hash( 'run', &catch
- ?? -> { code(); CATCH { default { catch($_) } } }
- !! -> { code() }
- )
- ),
- ($delay * 1000).Int);
+ # only after waiting a bit or more than once
+ elsif $delay or $times > 1 {
+ my $todo :=nqp::hash( 'run', &catch
+ ?? -> { code(); CATCH { default { catch($_) } } }
+ !! -> { code() } );
+ for 1 .. $times {
+ $!timer.'method/schedule/(Ljava/util/TimerTask;J)V'(
+ nqp::jvmbootinterop().proxy('java.util.TimerTask', $todo),
+ ($delay * 1000).Int);
+ $delay = 0;
+ }
}
# just cue the code

0 comments on commit a664d48

Please sign in to comment.
Something went wrong with that request. Please try again.