Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Migrate cue_with_catch to cue(:&catch)

Also some left-behind schedule_* fixes.  Unfortunately, specifying a :catch()
currently blows up:

Unhandled exception: Method 'postcircumfix:<( )>' not found for invocant of class 'Scalar'
  in  (gen/jvm/BOOTSTRAP.nqp:1675)

Will look at this tomorrow, unless someone else beats me to this
  • Loading branch information...
commit f4853151ebc3fbd1a1ebc7c7e353c1eb552323a4 1 parent 0cfe87b
@lizmat lizmat authored
View
17 src/vm/jvm/core/CurrentThreadScheduler.pm
@@ -5,14 +5,27 @@ my class CurrentThreadScheduler does Scheduler {
$exception.throw
}
- method cue(&code, :$at, :$in, :$every ) {
+ method cue(&code, :$at, :$in, :$every, :&catch ) {
die "Cannot specify :at and :in at the same time"
if $at.defined and $in.defined;
my $delay = $at ?? $at - now !! $in;
sleep $delay if $delay;
- if $every {
+ if &catch {
+ if $every {
+ loop { { # extra block needed because of #120498
+ code();
+ sleep $every;
+ CATCH { default { catch($_) } };
+ } }
+ }
+ else {
+ code();
+ CATCH { default { catch($_) } };
+ }
+ }
+ elsif $every {
loop {
code();
sleep $every;
View
8 src/vm/jvm/core/Promise.pm
@@ -82,7 +82,7 @@ my class Promise {
method !schedule_thens() {
$!lock.lock();
while @!thens {
- $!scheduler.cue_with_catch(@!thens.shift, @!thens.shift)
+ $!scheduler.cue(@!thens.shift, :catch(@!thens.shift))
}
$!lock.unlock();
}
@@ -142,16 +142,16 @@ my class Promise {
method start(Promise:U: &code, :$scheduler = $*SCHEDULER) {
my $p = Promise.new(:$scheduler);
my $vow = $p.vow;
- $scheduler.cue_with_catch(
+ $scheduler.cue(
{ $vow.keep(code()) },
- -> $ex { $vow.break($ex) });
+ :catch(-> $ex { $vow.break($ex) }) );
$p
}
method sleep(Promise:U: $seconds, :$scheduler = $*SCHEDULER) {
my $p = Promise.new(:$scheduler);
my $vow = $p.vow;
- $scheduler.schedule_in({ $vow.keep(True) }, $seconds);
+ $scheduler.cue({ $vow.keep(True) }, :in($seconds));
$p
}
View
8 src/vm/jvm/core/Publish.pm
@@ -10,14 +10,14 @@ my class Publish {
method tap(|c) {
my $sub = self.Supply::tap(|c);
- $!scheduler.cue_with_catch(
+ $!scheduler.cue(
{
for @!values -> \val {
$sub.next().(val);
}
if $sub.last -> $l { $l() }
},
- -> $ex { if $sub.fail -> $t { $t($ex) } }
+ :catch(-> $ex { if $sub.fail -> $t { $t($ex) } })
);
$sub
}
@@ -35,12 +35,12 @@ my class Publish {
method tap(|c) {
my $sub = self.Supply::tap(|c);
- $!scheduler.schedule_every(
+ $!scheduler.cue(
{
state $i = 0;
$sub.next().($i++);
},
- $!interval, $!delay
+ :every($!interval), :in($!delay)
);
$sub
}
View
7 src/vm/jvm/core/Scheduler.pm
@@ -19,13 +19,6 @@ my role Scheduler {
}
method cue { ... }
-
- method cue_with_catch(&code, &catch) {
- self.cue({
- code();
- CATCH { default { catch($_) } }
- })
- }
method loads() { ... }
}
View
12 src/vm/jvm/core/ThreadPoolScheduler.pm
@@ -51,26 +51,30 @@ my class ThreadPoolScheduler does Scheduler {
if $!initial_threads > $!max_threads;
}
- method cue(&code, :$at, :$in, :$every ) {
+ method cue(&code, :$at, :$in, :$every, :&catch ) {
die "Cannot specify :at and :in at the same time"
if $at.defined and $in.defined;
my $delay = $at ?? $at - now !! $in // 0;
self!initialize unless $!started_any;
+ my &block = &catch
+ ?? -> { code(); CATCH { default { catch($_) } } }
+ !! -> { code() };
+
# need repeating
if $every {
$!timer.'method/scheduleAtFixedRate/(Ljava/util/TimerTask;JJ)V'(
nqp::jvmbootinterop().proxy(
- 'java.util.TimerTask', nqp::hash('run', -> { code() })),
+ 'java.util.TimerTask', nqp::hash('run', &block)),
($delay * 1000).Int,
($every * 1000).Int);
}
# only after waiting a bit
- elsif $delay {
+ elsif $delay or &catch {
$!timer.'method/schedule/(Ljava/util/TimerTask;J)V'(
nqp::jvmbootinterop().proxy(
- 'java.util.TimerTask', nqp::hash('run', -> { code() })),
+ 'java.util.TimerTask', nqp::hash('run', &block)),
($delay * 1000).Int);
}
Please sign in to comment.
Something went wrong with that request. Please try again.