Coro::Timer - timers and timeouts, independent of any event loop


Coro documentation Contained in the Coro distribution.

Index


Code Index:

NAME

Top

Coro::Timer - timers and timeouts, independent of any event loop

SYNOPSIS

Top

 # This package is mostly obsoleted by Coro::AnyEvent.

 use Coro::Timer qw(sleep timeout);
 # nothing exported by default

 sleep 10;

DESCRIPTION

Top

This package has been mostly obsoleted by Coro::AnyEvent, the only really useful function left in here is timeout.

$flag = timeout $seconds

This function will wake up the current coroutine after $seconds seconds and sets $flag to true (it is false initially). If $flag goes out of scope earlier then nothing happens.

This is used by Coro itself to implement the timed_down, timed_wait etc. primitives. It is used like this:

   sub timed_wait {
      my $timeout = Coro::Timer::timeout 60;

      while (condition false) {
         Coro::schedule; # wait until woken up or timeout
         return 0 if $timeout; # timed out
      }

      return 1; # condition satisfied
   }

AUTHOR

Top

 Marc Lehmann <schmorp@schmorp.de>
 http://home.schmorp.de/


Coro documentation Contained in the Coro distribution.
package Coro::Timer;

use common::sense;

use Carp ();
use base Exporter::;

use Coro ();
use Coro::AnyEvent ();

our $VERSION = 6.0;
our @EXPORT_OK = qw(timeout sleep);

# compatibility with older programs
*sleep = \&Coro::AnyEvent::sleep;

sub timeout($) {
   my $current = $Coro::current;
   my $timeout;

   bless [
      \$timeout,
      (AE::timer $_[0], 0, sub {
         $timeout = 1;
         $current->ready;
      }),
   ], "Coro::Timer::Timeout";
}

package Coro::Timer::Timeout;

sub bool { ${ $_[0][0] } }

use overload 'bool' => \&bool, '0+' => \&bool;

1;