… | |
… | |
44 | |
44 | |
45 | sub timed_wait { |
45 | sub timed_wait { |
46 | my $timeout = Coro::Timer::timeout 60; |
46 | my $timeout = Coro::Timer::timeout 60; |
47 | |
47 | |
48 | while (condition false) { |
48 | while (condition false) { |
49 | schedule; # wait until woken up or timeout |
49 | Coro::schedule; # wait until woken up or timeout |
50 | return 0 if $timeout; # timed out |
50 | return 0 if $timeout; # timed out |
51 | } |
51 | } |
|
|
52 | |
52 | return 1; # condition satisfied |
53 | return 1; # condition satisfied |
53 | } |
54 | } |
54 | |
55 | |
55 | =cut |
56 | =cut |
56 | |
57 | |
… | |
… | |
82 | |
83 | |
83 | =cut |
84 | =cut |
84 | |
85 | |
85 | sub sleep { |
86 | sub sleep { |
86 | my $current = $Coro::current; |
87 | my $current = $Coro::current; |
|
|
88 | |
87 | my $timer = AnyEvent->timer (after => $_[0], cb => sub { $current->ready }); |
89 | my $timer = AnyEvent->timer (after => $_[0], cb => sub { |
88 | Coro::schedule; |
90 | $current->ready; |
|
|
91 | undef $current; |
|
|
92 | }); |
|
|
93 | |
|
|
94 | do { &Coro::schedule } while $current; |
89 | } |
95 | } |
90 | |
96 | |
91 | $Coro::idle = sub { |
97 | $Coro::idle = sub { |
92 | AnyEvent->one_event; |
98 | AnyEvent->one_event; |
93 | }; |
99 | }; |