1 |
root |
1.1 |
=head1 NAME |
2 |
|
|
|
3 |
|
|
Coro::Process - coroutine process abstraction |
4 |
|
|
|
5 |
|
|
=head1 SYNOPSIS |
6 |
|
|
|
7 |
|
|
use Coro::Process; |
8 |
|
|
|
9 |
|
|
async { |
10 |
|
|
# some asynchroneous thread of execution |
11 |
|
|
}; |
12 |
|
|
|
13 |
|
|
yield; |
14 |
|
|
|
15 |
|
|
=head1 DESCRIPTION |
16 |
|
|
|
17 |
|
|
=over 4 |
18 |
|
|
|
19 |
|
|
=cut |
20 |
|
|
|
21 |
|
|
package Coro::Process; |
22 |
|
|
|
23 |
|
|
use base Coro; |
24 |
|
|
use base Exporter; |
25 |
|
|
|
26 |
|
|
@EXPORT = qw(async yield schedule); |
27 |
|
|
|
28 |
|
|
my $idle = Coro::_newprocess { |
29 |
|
|
&yield while 1; |
30 |
|
|
}; |
31 |
|
|
|
32 |
|
|
# we really need priorities... |
33 |
|
|
my @ready = ($idle); # the ready queue. hehe ;) |
34 |
|
|
|
35 |
|
|
# static methods. not really. |
36 |
|
|
|
37 |
|
|
sub async(&) { |
38 |
|
|
new Coro::Process $_[0]; |
39 |
|
|
} |
40 |
|
|
|
41 |
|
|
sub schedule { |
42 |
|
|
shift(@ready)->resume; |
43 |
|
|
} |
44 |
|
|
|
45 |
|
|
sub yield { |
46 |
|
|
$Coro::current->ready; |
47 |
|
|
&schedule; |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
# dynamic methods |
51 |
|
|
|
52 |
|
|
sub new { |
53 |
|
|
my $class = shift; |
54 |
|
|
my $proc = shift; |
55 |
|
|
my $self = $class->SUPER::new(sub { &$proc; schedule }); |
56 |
|
|
push @ready, $self; |
57 |
|
|
$self; |
58 |
|
|
} |
59 |
|
|
|
60 |
|
|
# supplement the base class, this really is a bug! |
61 |
|
|
sub Coro::ready { |
62 |
|
|
push @ready, $_[0]; |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
1; |
66 |
|
|
|
67 |
|
|
=back |
68 |
|
|
|
69 |
|
|
=head1 AUTHOR |
70 |
|
|
|
71 |
|
|
Marc Lehmann <pcg@goof.com> |
72 |
|
|
http://www.goof.com/pcg/marc/ |
73 |
|
|
|
74 |
|
|
=cut |
75 |
|
|
|