1 |
=head1 NAME |
2 |
|
3 |
Coro::Process - coroutine process abstraction |
4 |
|
5 |
=head1 SYNOPSIS |
6 |
|
7 |
use Coro::Process; |
8 |
|
9 |
async { |
10 |
# some asynchronous thread of execution |
11 |
}; |
12 |
|
13 |
# alternatively create an async process like this: |
14 |
|
15 |
sub some_func : Coro { |
16 |
# some more async code |
17 |
} |
18 |
|
19 |
yield; |
20 |
|
21 |
=head1 DESCRIPTION |
22 |
|
23 |
=cut |
24 |
|
25 |
package Coro::Process; |
26 |
|
27 |
use base Coro; |
28 |
use base Exporter; |
29 |
|
30 |
$VERSION = 0.01; |
31 |
|
32 |
@EXPORT = qw(async yield schedule); |
33 |
@EXPORT_OK = qw($current); |
34 |
|
35 |
{ |
36 |
use subs 'async'; |
37 |
|
38 |
my @async; |
39 |
|
40 |
sub import { |
41 |
Coro::Process->export_to_level(1, @_); |
42 |
my $old = *{(caller)[0]."::MODIFY_CODE_ATTRIBUTES"}{CODE}; |
43 |
*{(caller)[0]."::MODIFY_CODE_ATTRIBUTES"} = sub { |
44 |
my ($package, $ref) = (shift, shift); |
45 |
my @attrs; |
46 |
for (@_) { |
47 |
if ($_ eq "Coro") { |
48 |
push @async, $ref; |
49 |
} else { |
50 |
push @attrs, @_; |
51 |
} |
52 |
} |
53 |
return $old ? $old->($package, $name, @attrs) : @attrs; |
54 |
}; |
55 |
} |
56 |
|
57 |
sub INIT { |
58 |
async pop @async while @async; |
59 |
} |
60 |
} |
61 |
|
62 |
my $idle = new Coro::Process sub { |
63 |
&yield while 1; |
64 |
}; |
65 |
|
66 |
=item $main |
67 |
|
68 |
This coroutine represents the main program. |
69 |
|
70 |
=cut |
71 |
|
72 |
$main = new Coro::Process; |
73 |
|
74 |
=item $current |
75 |
|
76 |
The current coroutine (the last coroutine switched to). The initial value is C<$main> (of course). |
77 |
|
78 |
=cut |
79 |
|
80 |
$current = $main; |
81 |
|
82 |
# we really need priorities... |
83 |
my @ready = (); # the ready queue. hehe, rather broken ;) |
84 |
|
85 |
# static methods. not really. |
86 |
|
87 |
=head2 STATIC METHODS |
88 |
|
89 |
Static methods are actually functions that operate on the current process only. |
90 |
|
91 |
=over 4 |
92 |
|
93 |
=item async { ... }; |
94 |
|
95 |
Create a new asynchronous process and return it's process object |
96 |
(usually unused). When the sub returns the new process is automatically |
97 |
terminated. |
98 |
|
99 |
=cut |
100 |
|
101 |
sub async(&) { |
102 |
new Coro::Process $_[0]; |
103 |
} |
104 |
|
105 |
=item schedule |
106 |
|
107 |
Calls the scheduler. Please note that the current process will not be put |
108 |
into the ready queue, so calling this function usually means you will |
109 |
never be called again. |
110 |
|
111 |
=cut |
112 |
|
113 |
my $prev; |
114 |
|
115 |
sub schedule { |
116 |
($prev, $current) = ($current, shift @ready); |
117 |
Coro::transfer($prev, $current); |
118 |
} |
119 |
|
120 |
=item yield |
121 |
|
122 |
Yield to other processes. This function puts the current process into the |
123 |
ready queue and calls C<schedule>. |
124 |
|
125 |
=cut |
126 |
|
127 |
sub yield { |
128 |
$current->ready; |
129 |
&schedule; |
130 |
} |
131 |
|
132 |
=item terminate |
133 |
|
134 |
Terminates the current process. |
135 |
|
136 |
=cut |
137 |
|
138 |
sub terminate { |
139 |
&schedule; |
140 |
} |
141 |
|
142 |
=back |
143 |
|
144 |
# dynamic methods |
145 |
|
146 |
=head2 PROCESS METHODS |
147 |
|
148 |
These are the methods you can call on process objects. |
149 |
|
150 |
=over 4 |
151 |
|
152 |
=item new Coro::Process \⊂ |
153 |
|
154 |
Create a new process, put it into the ready queue and return it. When the |
155 |
sub returns the process automatically terminates. |
156 |
|
157 |
=cut |
158 |
|
159 |
sub new { |
160 |
my $class = shift; |
161 |
my $proc = shift; |
162 |
my $self = $class->SUPER::new($proc ? sub { &$proc; &terminate } : $proc); |
163 |
$self->ready; |
164 |
$self; |
165 |
} |
166 |
|
167 |
=item $process->ready |
168 |
|
169 |
Put the current process into the ready queue. |
170 |
|
171 |
=cut |
172 |
|
173 |
sub ready { |
174 |
push @ready, $_[0]; |
175 |
} |
176 |
|
177 |
=back |
178 |
|
179 |
=cut |
180 |
|
181 |
1; |
182 |
|
183 |
=head1 AUTHOR |
184 |
|
185 |
Marc Lehmann <pcg@goof.com> |
186 |
http://www.goof.com/pcg/marc/ |
187 |
|
188 |
=cut |
189 |
|