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

Comparing Coro/Coro.pm (file contents):
Revision 1.30 by root, Sat Aug 11 19:59:19 2001 UTC vs.
Revision 1.40 by root, Sun Oct 28 17:00:05 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.45; 43$VERSION = 0.51;
42 44
43@EXPORT = qw(async cede schedule terminate current); 45@EXPORT = qw(async cede schedule terminate current);
44@EXPORT_OK = qw($current); 46%EXPORT_TAGS = (
47 prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)],
48);
49@EXPORT_OK = @{$EXPORT_TAGS{prio}};
45 50
46{ 51{
47 my @async; 52 my @async;
48 my $init; 53 my $init;
49 54
111}; 116};
112 117
113# this coroutine is necessary because a coroutine 118# this coroutine is necessary because a coroutine
114# cannot destroy itself. 119# cannot destroy itself.
115my @destroy; 120my @destroy;
121my $manager;
116my $manager = new Coro sub { 122$manager = new Coro sub {
117 while() { 123 while() {
118 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 }
119 &schedule; 135 &schedule;
120 } 136 }
121}; 137};
122 138
123# static methods. not really. 139# static methods. not really.
165ready 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
166current "timeslice" to other coroutines of the same or higher priority. 182current "timeslice" to other coroutines of the same or higher priority.
167 183
168=cut 184=cut
169 185
170=item terminate 186=item terminate [arg...]
171 187
172Terminates the current process. 188Terminates the current process.
173 189
174Future versions of this function will allow result arguments. 190Future versions of this function will allow result arguments.
175 191
176=cut 192=cut
177 193
178sub terminate { 194sub terminate {
195 $current->{status} = [@_];
179 $current->cancel; 196 $current->cancel;
180 &schedule; 197 &schedule;
181 die; # NORETURN 198 die; # NORETURN
182} 199}
183 200
192=over 4 209=over 4
193 210
194=item new Coro \&sub [, @args...] 211=item new Coro \&sub [, @args...]
195 212
196Create 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
197automatically terminates. To start the process you must first put it into 214automatically terminates as if C<terminate> with the returned values were
198the ready queue by calling the ready method. 215called. To start the process you must first put it into the ready queue by
216calling the ready method.
199 217
200The coderef you submit MUST NOT be a closure that refers to variables 218The coderef you submit MUST NOT be a closure that refers to variables
201in an outer scope. This does NOT work. Pass arguments into it instead. 219in an outer scope. This does NOT work. Pass arguments into it instead.
202 220
203=cut 221=cut
213 }, $class; 231 }, $class;
214} 232}
215 233
216=item $process->ready 234=item $process->ready
217 235
218Put the current process into the ready queue. 236Put the given process into the ready queue.
219 237
220=cut 238=cut
221 239
222=item $process->cancel 240=item $process->cancel
223 241
226=cut 244=cut
227 245
228sub cancel { 246sub cancel {
229 push @destroy, $_[0]; 247 push @destroy, $_[0];
230 $manager->ready; 248 $manager->ready;
249 &schedule if $current == $_[0];
250}
251
252=item $process->join
253
254Wait until the coroutine terminates and return any values given to the
255C<terminate> function. C<join> can be called multiple times from multiple
256processes.
257
258=cut
259
260sub join {
261 my $self = shift;
262 unless ($self->{status}) {
263 push @{$self->{join}}, $current;
264 &schedule;
265 }
266 wantarray ? @{$self->{status}} : $self->{status}[0];
267}
268
269=item $oldprio = $process->prio($newprio)
270
271Sets the priority of the process. Higher priority processes get run before
272lower priority processes. Priorities are smalled signed integer (currently
273-4 .. +3), that you can refer to using PRIO_xxx constants (use the import
274tag :prio to get then):
275
276 PRIO_MAX > PRIO_HIGH > PRIO_NORMAL > PRIO_LOW > PRIO_IDLE > PRIO_MIN
277 3 > 1 > 0 > -1 > -3 > -4
278
279 # set priority to HIGH
280 current->prio(PRIO_HIGH);
281
282The idle coroutine ($Coro::idle) always has a lower priority than any
283existing coroutine.
284
285Changing the priority of the current process will take effect immediately,
286but changing the priority of processes in the ready queue (but not
287running) will only take effect after the next schedule (of that
288process). This is a bug that will be fixed in some future version.
289
290=cut
291
292sub prio {
293 my $old = $_[0]{prio};
294 $_[0]{prio} = $_[1] if @_ > 1;
295 $old;
296}
297
298=item $newprio = $process->nice($change)
299
300Similar to C<prio>, but subtract the given value from the priority (i.e.
301higher values mean lower priority, just as in unix).
302
303=cut
304
305sub nice {
306 $_[0]{prio} -= $_[1];
231} 307}
232 308
233=back 309=back
234 310
235=cut 311=cut
236 312
2371; 3131;
238 314
239=head1 BUGS/LIMITATIONS 315=head1 BUGS/LIMITATIONS
240 316
241 - could be faster, especially when the core would introduce special 317 - you must make very sure that no coro is still active on global destruction.
242 support for coroutines (like it does for threads). 318 very bad things might happen otherwise (usually segfaults).
243 - there is still a memleak on coroutine termination that I could not
244 identify. Could be as small as a single SV.
245 - this module is not well-tested.
246 - if variables or arguments "disappear" (become undef) or become
247 corrupted please contact the author so he cen iron out the
248 remaining bugs.
249 - this module is not thread-safe. You must only ever use this module from 319 - this module is not thread-safe. You must only ever use this module from
250 the same thread (this requirement might be loosened in the future to 320 the same thread (this requirement might be loosened in the future to
251 allow per-thread schedulers, but Coro::State does not yet allow this). 321 allow per-thread schedulers, but Coro::State does not yet allow this).
252 322
253=head1 SEE ALSO 323=head1 SEE ALSO

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines