ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro.pm
(Generate patch)

Comparing Coro/Coro.pm (file contents):
Revision 1.36 by root, Mon Sep 24 01:36:20 2001 UTC vs.
Revision 1.42 by root, Tue Nov 6 20:37:20 2001 UTC

19 cede; 19 cede;
20 20
21=head1 DESCRIPTION 21=head1 DESCRIPTION
22 22
23This module collection manages coroutines. Coroutines are similar to 23This module collection manages coroutines. Coroutines are similar to
24Threads but don't run in parallel. 24threads but don't run in parallel.
25
26This module is still experimental, see the BUGS section below.
27 25
28In this module, coroutines are defined as "callchain + lexical variables 26In this module, coroutines are defined as "callchain + lexical variables
29+ @_ + $_ + $@ + $^W + C stack), that is, a coroutine has it's own 27+ @_ + $_ + $@ + $^W + C stack), that is, a coroutine has it's own
30callchain, it's own set of lexicals and it's own set of perl's most 28callchain, it's own set of lexicals and it's own set of perl's most
31important global variables. 29important global variables.
38 36
39use Coro::State; 37use Coro::State;
40 38
41use base Exporter; 39use base Exporter;
42 40
43$VERSION = 0.5; 41$VERSION = 0.52;
44 42
45@EXPORT = qw(async cede schedule terminate current); 43@EXPORT = qw(async cede schedule terminate current);
46%EXPORT_TAGS = ( 44%EXPORT_TAGS = (
47 prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], 45 prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)],
48); 46);
116}; 114};
117 115
118# this coroutine is necessary because a coroutine 116# this coroutine is necessary because a coroutine
119# cannot destroy itself. 117# cannot destroy itself.
120my @destroy; 118my @destroy;
119my $manager;
121my $manager = new Coro sub { 120$manager = new Coro sub {
122 while() { 121 while() {
123 delete ((pop @destroy)->{_coro_state}) while @destroy; 122 # by overwriting the state object with the manager we destroy it
123 # while still being able to schedule this coroutine (in case it has
124 # been readied multiple times. this is harmless since the manager
125 # can be called as many times as neccessary and will always
126 # remove itself from the runqueue
127 while (@destroy) {
128 my $coro = pop @destroy;
129 $coro->{status} ||= [];
130 $_->ready for @{delete $coro->{join} || []};
131 $coro->{_coro_state} = $manager->{_coro_state};
132 }
124 &schedule; 133 &schedule;
125 } 134 }
126}; 135};
127 136
128# static methods. not really. 137# static methods. not really.
170ready queue and calls C<schedule>, which has the effect of giving up the 179ready queue and calls C<schedule>, which has the effect of giving up the
171current "timeslice" to other coroutines of the same or higher priority. 180current "timeslice" to other coroutines of the same or higher priority.
172 181
173=cut 182=cut
174 183
175=item terminate 184=item terminate [arg...]
176 185
177Terminates the current process. 186Terminates the current process.
178 187
179Future versions of this function will allow result arguments. 188Future versions of this function will allow result arguments.
180 189
181=cut 190=cut
182 191
183sub terminate { 192sub terminate {
193 $current->{status} = [@_];
184 $current->cancel; 194 $current->cancel;
185 &schedule; 195 &schedule;
186 die; # NORETURN 196 die; # NORETURN
187} 197}
188 198
197=over 4 207=over 4
198 208
199=item new Coro \&sub [, @args...] 209=item new Coro \&sub [, @args...]
200 210
201Create a new process and return it. When the sub returns the process 211Create a new process and return it. When the sub returns the process
202automatically terminates. To start the process you must first put it into 212automatically terminates as if C<terminate> with the returned values were
213called. To make the process run you must first put it into the ready queue
203the ready queue by calling the ready method. 214by calling the ready method.
204
205The coderef you submit MUST NOT be a closure that refers to variables
206in an outer scope. This does NOT work. Pass arguments into it instead.
207 215
208=cut 216=cut
209 217
210sub _newcoro { 218sub _newcoro {
211 terminate &{+shift}; 219 terminate &{+shift};
218 }, $class; 226 }, $class;
219} 227}
220 228
221=item $process->ready 229=item $process->ready
222 230
223Put the current process into the ready queue. 231Put the given process into the ready queue.
224 232
225=cut 233=cut
226 234
227=item $process->cancel 235=item $process->cancel
228 236
234 push @destroy, $_[0]; 242 push @destroy, $_[0];
235 $manager->ready; 243 $manager->ready;
236 &schedule if $current == $_[0]; 244 &schedule if $current == $_[0];
237} 245}
238 246
247=item $process->join
248
249Wait until the coroutine terminates and return any values given to the
250C<terminate> function. C<join> can be called multiple times from multiple
251processes.
252
253=cut
254
255sub join {
256 my $self = shift;
257 unless ($self->{status}) {
258 push @{$self->{join}}, $current;
259 &schedule;
260 }
261 wantarray ? @{$self->{status}} : $self->{status}[0];
262}
263
239=item $oldprio = $process->prio($newprio) 264=item $oldprio = $process->prio($newprio)
240 265
241Sets the priority of the process. Higher priority processes get run before 266Sets (or gets, if the argument is missing) the priority of the
267process. Higher priority processes get run before lower priority
242lower priority processes. Priorities are smalled signed integer (currently 268processes. Priorities are smalled signed integer (currently -4 .. +3),
243-4 .. +3), that you can refer to using PRIO_xxx constants (use the import 269that you can refer to using PRIO_xxx constants (use the import tag :prio
244tag :prio to get then): 270to get then):
245 271
246 PRIO_MAX > PRIO_HIGH > PRIO_NORMAL > PRIO_LOW > PRIO_IDLE > PRIO_MIN 272 PRIO_MAX > PRIO_HIGH > PRIO_NORMAL > PRIO_LOW > PRIO_IDLE > PRIO_MIN
247 3 > 1 > 0 > -1 > -3 > -4 273 3 > 1 > 0 > -1 > -3 > -4
248 274
249 # set priority to HIGH 275 # set priority to HIGH
274 300
275sub nice { 301sub nice {
276 $_[0]{prio} -= $_[1]; 302 $_[0]{prio} -= $_[1];
277} 303}
278 304
305=item $olddesc = $process->desc($newdesc)
306
307Sets (or gets in case the argument is missing) the description for this
308process. This is just a free-form string you can associate with a process.
309
310=cut
311
312sub desc {
313 my $old = $_[0]{desc};
314 $_[0]{desc} = $_[1] if @_ > 1;
315 $old;
316}
317
279=back 318=back
280 319
281=cut 320=cut
282 321
2831; 3221;
284 323
285=head1 BUGS/LIMITATIONS 324=head1 BUGS/LIMITATIONS
286 325
287 - you must make very sure that no coro is still active on global destruction. 326 - you must make very sure that no coro is still active on global destruction.
288 very bad things might happen otherwise (usually segfaults). 327 very bad things might happen otherwise (usually segfaults).
289 - this module is not thread-safe. You must only ever use this module from 328 - this module is not thread-safe. You should only ever use this module from
290 the same thread (this requirement might be loosened in the future to 329 the same thread (this requirement might be loosened in the future to
291 allow per-thread schedulers, but Coro::State does not yet allow this). 330 allow per-thread schedulers, but Coro::State does not yet allow this).
292 331
293=head1 SEE ALSO 332=head1 SEE ALSO
294 333

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines