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

Comparing Coro/Coro.pm (file contents):
Revision 1.39 by root, Tue Oct 9 00:39:08 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.51; 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);
124 # by overwriting the state object with the manager we destroy it 122 # by overwriting the state object with the manager we destroy it
125 # while still being able to schedule this coroutine (in case it has 123 # while still being able to schedule this coroutine (in case it has
126 # been readied multiple times. this is harmless since the manager 124 # been readied multiple times. this is harmless since the manager
127 # can be called as many times as neccessary and will always 125 # can be called as many times as neccessary and will always
128 # remove itself from the runqueue 126 # remove itself from the runqueue
127 while (@destroy) {
128 my $coro = pop @destroy;
129 $coro->{status} ||= [];
130 $_->ready for @{delete $coro->{join} || []};
129 (pop @destroy)->{_coro_state} = $manager->{_coro_state} while @destroy; 131 $coro->{_coro_state} = $manager->{_coro_state};
132 }
130 &schedule; 133 &schedule;
131 } 134 }
132}; 135};
133 136
134# static methods. not really. 137# static methods. not really.
176ready 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
177current "timeslice" to other coroutines of the same or higher priority. 180current "timeslice" to other coroutines of the same or higher priority.
178 181
179=cut 182=cut
180 183
181=item terminate 184=item terminate [arg...]
182 185
183Terminates the current process. 186Terminates the current process.
184 187
185Future versions of this function will allow result arguments. 188Future versions of this function will allow result arguments.
186 189
187=cut 190=cut
188 191
189sub terminate { 192sub terminate {
193 $current->{status} = [@_];
190 $current->cancel; 194 $current->cancel;
191 &schedule; 195 &schedule;
192 die; # NORETURN 196 die; # NORETURN
193} 197}
194 198
203=over 4 207=over 4
204 208
205=item new Coro \&sub [, @args...] 209=item new Coro \&sub [, @args...]
206 210
207Create 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
208automatically 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
209the ready queue by calling the ready method. 214by calling the ready method.
210
211The coderef you submit MUST NOT be a closure that refers to variables
212in an outer scope. This does NOT work. Pass arguments into it instead.
213 215
214=cut 216=cut
215 217
216sub _newcoro { 218sub _newcoro {
217 terminate &{+shift}; 219 terminate &{+shift};
240 push @destroy, $_[0]; 242 push @destroy, $_[0];
241 $manager->ready; 243 $manager->ready;
242 &schedule if $current == $_[0]; 244 &schedule if $current == $_[0];
243} 245}
244 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
245=item $oldprio = $process->prio($newprio) 264=item $oldprio = $process->prio($newprio)
246 265
247Sets 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
248lower priority processes. Priorities are smalled signed integer (currently 268processes. Priorities are smalled signed integer (currently -4 .. +3),
249-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
250tag :prio to get then): 270to get then):
251 271
252 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
253 3 > 1 > 0 > -1 > -3 > -4 273 3 > 1 > 0 > -1 > -3 > -4
254 274
255 # set priority to HIGH 275 # set priority to HIGH
280 300
281sub nice { 301sub nice {
282 $_[0]{prio} -= $_[1]; 302 $_[0]{prio} -= $_[1];
283} 303}
284 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
285=back 318=back
286 319
287=cut 320=cut
288 321
2891; 3221;
290 323
291=head1 BUGS/LIMITATIONS 324=head1 BUGS/LIMITATIONS
292 325
293 - 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.
294 very bad things might happen otherwise (usually segfaults). 327 very bad things might happen otherwise (usually segfaults).
295 - 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
296 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
297 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).
298 331
299=head1 SEE ALSO 332=head1 SEE ALSO
300 333

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines