Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Get sleep(), sleep-time() and sleep-till() up to spec
Since Duration and Instant were not known yet in control.pm, moved these subs
to Temporal.pm where they seemed to be more at home.
  • Loading branch information
lizmat committed Sep 11, 2013
1 parent 252008d commit 79c0f51
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 36 deletions.
42 changes: 42 additions & 0 deletions src/core/Temporal.pm
Expand Up @@ -623,6 +623,48 @@ multi infix:«>»(Date:D $a, Date:D $b) {

$PROCESS::TZ = get-local-timezone-offset();

sub sleep($seconds = $Inf --> Nil) {
if $seconds ~~ ($Inf|Whatever) {
nqp::sleep(1e16) while True;
}
elsif $seconds > 0 {
nqp::sleep($seconds.Num);
}
Nil;
}

sub sleep-timer ($seconds = $Inf --> Duration) {
if $seconds ~~ ($Inf|Whatever) {
nqp::sleep(1e16) while True;
Duration.new($Inf);
}
elsif $seconds <= 0 {
Duration.new(0);
}
else {
my $time1 = now;
nqp::sleep($seconds.Num);
Duration.new( ( $seconds - now - $time1 ) max 0 );
}
}

proto sub sleep-till (|) {*}
multi sub sleep-till (Instant $till --> Bool) {
my $seconds = $till - now;
return False if $seconds < 0;

1 while $seconds = sleep-timer($seconds);
True;
}
multi sub sleep-till (Inf $till --> Bool) { # when Inf works at this point
nqp::sleep(1e16) while True;
True; # at the end of times
}
multi sub sleep-till (Whatever $till --> Bool) {
nqp::sleep(1e16) while True;
True; # at the end of times
}

# =begin pod
#
# =head1 SEE ALSO
Expand Down
36 changes: 0 additions & 36 deletions src/core/control.pm
Expand Up @@ -212,42 +212,6 @@ my $Inf = nqp::p6box_n(nqp::inf());
my $NaN = nqp::p6box_n(nqp::nan());
# EM 20130627 attempt at using constants failed during optimizing phase


sub sleep($seconds = $Inf) { # fractional seconds also allowed
my $time1 = time;
if $seconds ~~ $Inf {
nqp::sleep(1e16) while True;
}
elsif $seconds < 0 {
fail "Cannot go {abs $seconds} seconds back in time";
}
else {
nqp::sleep($seconds.Num);
}
return time - $time1;
}

my %interval_wakeup; # needs to be hidden from GLOBAL:: somehow
sub interval($seconds ) { # fractional seconds also allowed

my $time = now.Num;
my $wakeup := %interval_wakeup{"thread_id"} //= $time; # XXX thread ID

# already past our morning call
if $time >= $wakeup {
$wakeup += $seconds;
0;
}

# still time to sleep
else {
my $slept = $wakeup - $time;
nqp::sleep($slept);
$wakeup += $seconds;
$slept;
}
}

sub QX($cmd) {
#?if parrot
my Mu $pio := nqp::open(nqp::unbox_s($cmd), 'rp');
Expand Down

0 comments on commit 79c0f51

Please sign in to comment.