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.19 by root, Sat Jul 21 03:44:06 2001 UTC vs.
Revision 1.22 by root, Mon Jul 23 02:14:19 2001 UTC

14 14
15 sub some_func : Coro { 15 sub some_func : Coro {
16 # some more async code 16 # some more async code
17 } 17 }
18 18
19 yield; 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 25
26This module is still experimental, see the BUGS section below. 26This module is still experimental, see the BUGS section below.
27 27
28In this module, coroutines are defined as "callchain + lexical variables
29+ @_ + $_ + $@ + $^W), that is, a coroutine has it's own callchain, it's
30own set of lexicals and it's own set of perl's most important global
31variables.
32
33WARNING: When using this module, make sure that, at program end, no
34coroutines are still running OR just call exit before falling off the
35end. The reason for this is that some coroutine of yours might have called
36into a C function, and falling off the end of main:: results in returning
37to that C function instead if to the main C interpreter.
38
39WARNING: Unless you really know what you are doing, do NOT do context
40switches inside callbacks from the XS level. The reason for this is
41similar to the reason above: A callback calls a perl function, this
42perl function does a context switch, some other callback is called, the
43original function returns from it - to what? To the wrong XS function,
44with totally different return values. Unfortunately, this includes
45callbacks done by perl itself (tie'd variables!).
46
47The only workaround for this is to do coroutines on C level.
48
28=cut 49=cut
29 50
30package Coro; 51package Coro;
31 52
32use Coro::State; 53use Coro::State;
33 54
34use base Exporter; 55use base Exporter;
35 56
36$VERSION = 0.09; 57$VERSION = 0.10;
37 58
38@EXPORT = qw(async yield schedule terminate current); 59@EXPORT = qw(async cede schedule terminate current);
39@EXPORT_OK = qw($current); 60@EXPORT_OK = qw($current);
40 61
41{ 62{
42 use subs 'async';
43
44 my @async; 63 my @async;
45 64
46 # this way of handling attributes simply is NOT scalable ;() 65 # this way of handling attributes simply is NOT scalable ;()
47 sub import { 66 sub import {
48 Coro->export_to_level(1, @_); 67 Coro->export_to_level(1, @_);
60 return $old ? $old->($package, $ref, @attrs) : @attrs; 79 return $old ? $old->($package, $ref, @attrs) : @attrs;
61 }; 80 };
62 } 81 }
63 82
64 sub INIT { 83 sub INIT {
65 async pop @async while @async; 84 &async(pop @async) while @async;
66 } 85 }
67} 86}
68 87
69=item $main 88=item $main
70 89
149 # should be done using priorities :( 168 # should be done using priorities :(
150 ($prev, $current) = ($current, shift @ready || $idle); 169 ($prev, $current) = ($current, shift @ready || $idle);
151 Coro::State::transfer($prev, $current); 170 Coro::State::transfer($prev, $current);
152} 171}
153 172
154=item yield 173=item cede
155 174
156Yield to other processes. This function puts the current process into the 175"Cede" to other processes. This function puts the current process into the
157ready queue and calls C<schedule>. 176ready queue and calls C<schedule>, which has the effect of giving up the
177current "timeslice" to other coroutines of the same or higher priority.
158 178
159=cut 179=cut
160 180
161sub yield { 181sub cede {
162 $current->ready; 182 $current->ready;
163 &schedule; 183 &schedule;
164} 184}
165 185
166=item terminate 186=item terminate
170Future versions of this function will allow result arguments. 190Future versions of this function will allow result arguments.
171 191
172=cut 192=cut
173 193
174sub terminate { 194sub terminate {
195 my $self = $current;
175 $current->{_results} = [@_]; 196 $self->{_results} = [@_];
176 &schedule; 197 $current = shift @ready || $idle;
198 Coro::State::transfer(delete $self->{_coro_state}, $current);
199 # cannot return
200 die;
177} 201}
178 202
179=back 203=back
180 204
181# dynamic methods 205# dynamic methods
234 - if variables or arguments "disappear" (become undef) or become 258 - if variables or arguments "disappear" (become undef) or become
235 corrupted please contact the author so he cen iron out the 259 corrupted please contact the author so he cen iron out the
236 remaining bugs. 260 remaining bugs.
237 - this module is not thread-safe. You must only ever use this module from 261 - this module is not thread-safe. You must only ever use this module from
238 the same thread (this requirement might be loosened in the future to 262 the same thread (this requirement might be loosened in the future to
239 allow per-thread schedulers, but Coro::Satte does not yet allow this). 263 allow per-thread schedulers, but Coro::State does not yet allow this).
240 264
241=head1 SEE ALSO 265=head1 SEE ALSO
242 266
243L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, 267L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>,
244L<Coro::Signal>, L<Coro::State>, L<Coro::Event>. 268L<Coro::Signal>, L<Coro::State>, L<Coro::Event>.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines