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

Comparing cvsroot/Coro/Coro.pm (file contents):
Revision 1.32 by root, Sun Sep 2 01:03:53 2001 UTC vs.
Revision 1.41 by root, Tue Nov 6 20:34:09 2001 UTC

32 32
33=cut 33=cut
34 34
35package Coro; 35package Coro;
36 36
37no warnings qw(uninitialized);
38
37use Coro::State; 39use Coro::State;
38 40
39use base Exporter; 41use base Exporter;
40 42
41$VERSION = 0.49; 43$VERSION = 0.52;
42 44
43@EXPORT = qw(async cede schedule terminate current); 45@EXPORT = qw(async cede schedule terminate current);
44%EXPORT_TAGS = ( 46%EXPORT_TAGS = (
45 prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], 47 prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)],
46); 48);
114}; 116};
115 117
116# this coroutine is necessary because a coroutine 118# this coroutine is necessary because a coroutine
117# cannot destroy itself. 119# cannot destroy itself.
118my @destroy; 120my @destroy;
121my $manager;
119my $manager = new Coro sub { 122$manager = new Coro sub {
120 while() { 123 while() {
121 delete ((pop @destroy)->{_coro_state}) while @destroy; 124 # by overwriting the state object with the manager we destroy it
125 # while still being able to schedule this coroutine (in case it has
126 # been readied multiple times. this is harmless since the manager
127 # can be called as many times as neccessary and will always
128 # remove itself from the runqueue
129 while (@destroy) {
130 my $coro = pop @destroy;
131 $coro->{status} ||= [];
132 $_->ready for @{delete $coro->{join} || []};
133 $coro->{_coro_state} = $manager->{_coro_state};
134 }
122 &schedule; 135 &schedule;
123 } 136 }
124}; 137};
125 138
126# static methods. not really. 139# static methods. not really.
168ready queue and calls C<schedule>, which has the effect of giving up the 181ready queue and calls C<schedule>, which has the effect of giving up the
169current "timeslice" to other coroutines of the same or higher priority. 182current "timeslice" to other coroutines of the same or higher priority.
170 183
171=cut 184=cut
172 185
173=item terminate 186=item terminate [arg...]
174 187
175Terminates the current process. 188Terminates the current process.
176 189
177Future versions of this function will allow result arguments. 190Future versions of this function will allow result arguments.
178 191
179=cut 192=cut
180 193
181sub terminate { 194sub terminate {
195 $current->{status} = [@_];
182 $current->cancel; 196 $current->cancel;
183 &schedule; 197 &schedule;
184 die; # NORETURN 198 die; # NORETURN
185} 199}
186 200
195=over 4 209=over 4
196 210
197=item new Coro \&sub [, @args...] 211=item new Coro \&sub [, @args...]
198 212
199Create a new process and return it. When the sub returns the process 213Create a new process and return it. When the sub returns the process
200automatically terminates. To start the process you must first put it into 214automatically terminates as if C<terminate> with the returned values were
215called. To make the process run you must first put it into the ready queue
201the ready queue by calling the ready method. 216by 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 217
206=cut 218=cut
207 219
208sub _newcoro { 220sub _newcoro {
209 terminate &{+shift}; 221 terminate &{+shift};
216 }, $class; 228 }, $class;
217} 229}
218 230
219=item $process->ready 231=item $process->ready
220 232
221Put the current process into the ready queue. 233Put the given process into the ready queue.
222 234
223=cut 235=cut
224 236
225=item $process->cancel 237=item $process->cancel
226 238
229=cut 241=cut
230 242
231sub cancel { 243sub cancel {
232 push @destroy, $_[0]; 244 push @destroy, $_[0];
233 $manager->ready; 245 $manager->ready;
246 &schedule if $current == $_[0];
247}
248
249=item $process->join
250
251Wait until the coroutine terminates and return any values given to the
252C<terminate> function. C<join> can be called multiple times from multiple
253processes.
254
255=cut
256
257sub join {
258 my $self = shift;
259 unless ($self->{status}) {
260 push @{$self->{join}}, $current;
261 &schedule;
262 }
263 wantarray ? @{$self->{status}} : $self->{status}[0];
234} 264}
235 265
236=item $oldprio = $process->prio($newprio) 266=item $oldprio = $process->prio($newprio)
237 267
238Sets the priority of the process. Higher priority processes get run before 268Sets (or gets, if the argument is missing) the priority of the
269process. Higher priority processes get run before lower priority
239lower priority processes. Priorities are smalled signed integer (currently 270processes. Priorities are smalled signed integer (currently -4 .. +3),
240-4 .. +3), that you can refer to using PRIO_xxx constants (use the import 271that you can refer to using PRIO_xxx constants (use the import tag :prio
241tag :prio to get then): 272to get then):
242 273
243 PRIO_MAX > PRIO_HIGH > PRIO_NORMAL > PRIO_LOW > PRIO_IDLE > PRIO_MIN 274 PRIO_MAX > PRIO_HIGH > PRIO_NORMAL > PRIO_LOW > PRIO_IDLE > PRIO_MIN
244 3 > 1 > 0 > -1 > -3 > -4 275 3 > 1 > 0 > -1 > -3 > -4
245 276
246 # set priority to HIGH 277 # set priority to HIGH
271 302
272sub nice { 303sub nice {
273 $_[0]{prio} -= $_[1]; 304 $_[0]{prio} -= $_[1];
274} 305}
275 306
307=item $olddesc = $process->desc($newdesc)
308
309Sets (or gets in case the argument is missing) the description for this
310process. This is just a free-form string you can associate with a process.
311
312=cut
313
314sub desc {
315 my $old = $_[0]{desc};
316 $_[0]{desc} = $_[1] if @_ > 1;
317 $old;
318}
319
276=back 320=back
277 321
278=cut 322=cut
279 323
2801; 3241;
281 325
282=head1 BUGS/LIMITATIONS 326=head1 BUGS/LIMITATIONS
283 327
284 - could be faster, especially when the core would introduce special 328 - you must make very sure that no coro is still active on global destruction.
285 support for coroutines (like it does for threads). 329 very bad things might happen otherwise (usually segfaults).
286 - there is still a memleak on coroutine termination that I could not
287 identify. Could be as small as a single SV.
288 - this module is not well-tested.
289 - if variables or arguments "disappear" (become undef) or become
290 corrupted please contact the author so he cen iron out the
291 remaining bugs.
292 - this module is not thread-safe. You must only ever use this module from 330 - this module is not thread-safe. You must only ever use this module from
293 the same thread (this requirement might be loosened in the future to 331 the same thread (this requirement might be loosened in the future to
294 allow per-thread schedulers, but Coro::State does not yet allow this). 332 allow per-thread schedulers, but Coro::State does not yet allow this).
295 333
296=head1 SEE ALSO 334=head1 SEE ALSO

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines