Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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...
commit 79c0f515837fcc974b58f878ff0818a23206280c 1 parent 252008d
@lizmat lizmat authored
Showing with 42 additions and 36 deletions.
  1. +42 −0 src/core/Temporal.pm
  2. +0 −36 src/core/control.pm
View
42 src/core/Temporal.pm
@@ -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
View
36 src/core/control.pm
@@ -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');
Please sign in to comment.
Something went wrong with that request. Please try again.