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

Comparing Coro/Coro.pm (file contents):
Revision 1.35 by root, Mon Sep 24 00:16:30 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.
32 30
33=cut 31=cut
34 32
35package Coro; 33package Coro;
36 34
35no warnings qw(uninitialized);
36
37use Coro::State; 37use Coro::State;
38 38
39use base Exporter; 39use base Exporter;
40 40
41$VERSION = 0.5; 41$VERSION = 0.52;
42 42
43@EXPORT = qw(async cede schedule terminate current); 43@EXPORT = qw(async cede schedule terminate current);
44%EXPORT_TAGS = ( 44%EXPORT_TAGS = (
45 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)],
46); 46);
114}; 114};
115 115
116# this coroutine is necessary because a coroutine 116# this coroutine is necessary because a coroutine
117# cannot destroy itself. 117# cannot destroy itself.
118my @destroy; 118my @destroy;
119my $manager;
119my $manager = new Coro sub { 120$manager = new Coro sub {
120 while() { 121 while() {
121 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 }
122 &schedule; 133 &schedule;
123 } 134 }
124}; 135};
125 136
126# static methods. not really. 137# static methods. not really.
168ready 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
169current "timeslice" to other coroutines of the same or higher priority. 180current "timeslice" to other coroutines of the same or higher priority.
170 181
171=cut 182=cut
172 183
173=item terminate 184=item terminate [arg...]
174 185
175Terminates the current process. 186Terminates the current process.
176 187
177Future versions of this function will allow result arguments. 188Future versions of this function will allow result arguments.
178 189
179=cut 190=cut
180 191
181sub terminate { 192sub terminate {
193 $current->{status} = [@_];
182 $current->cancel; 194 $current->cancel;
183 &schedule; 195 &schedule;
184 die; # NORETURN 196 die; # NORETURN
185} 197}
186 198
195=over 4 207=over 4
196 208
197=item new Coro \&sub [, @args...] 209=item new Coro \&sub [, @args...]
198 210
199Create 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
200automatically 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
201the ready queue by calling the ready method. 214by calling the ready method.
202
203The coderef you submit MUST NOT be a closure that refers to variables
204in an outer scope. This does NOT work. Pass arguments into it instead.
205 215
206=cut 216=cut
207 217
208sub _newcoro { 218sub _newcoro {
209 terminate &{+shift}; 219 terminate &{+shift};
216 }, $class; 226 }, $class;
217} 227}
218 228
219=item $process->ready 229=item $process->ready
220 230
221Put the current process into the ready queue. 231Put the given process into the ready queue.
222 232
223=cut 233=cut
224 234
225=item $process->cancel 235=item $process->cancel
226 236
232 push @destroy, $_[0]; 242 push @destroy, $_[0];
233 $manager->ready; 243 $manager->ready;
234 &schedule if $current == $_[0]; 244 &schedule if $current == $_[0];
235} 245}
236 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
237=item $oldprio = $process->prio($newprio) 264=item $oldprio = $process->prio($newprio)
238 265
239Sets 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
240lower priority processes. Priorities are smalled signed integer (currently 268processes. Priorities are smalled signed integer (currently -4 .. +3),
241-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
242tag :prio to get then): 270to get then):
243 271
244 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
245 3 > 1 > 0 > -1 > -3 > -4 273 3 > 1 > 0 > -1 > -3 > -4
246 274
247 # set priority to HIGH 275 # set priority to HIGH
272 300
273sub nice { 301sub nice {
274 $_[0]{prio} -= $_[1]; 302 $_[0]{prio} -= $_[1];
275} 303}
276 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
277=back 318=back
278 319
279=cut 320=cut
280 321
2811; 3221;
282 323
283=head1 BUGS/LIMITATIONS 324=head1 BUGS/LIMITATIONS
284 325
285 - 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.
286 very bad things might happen otherwise (usually segfaults). 327 very bad things might happen otherwise (usually segfaults).
287 - 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
288 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
289 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).
290 331
291=head1 SEE ALSO 332=head1 SEE ALSO
292 333

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines