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

Comparing Coro/Coro.pm (file contents):
Revision 1.22 by root, Mon Jul 23 02:14:19 2001 UTC vs.
Revision 1.23 by root, Mon Jul 23 04:23:32 2001 UTC

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 28In this module, coroutines are defined as "callchain + lexical variables
29+ @_ + $_ + $@ + $^W), that is, a coroutine has it's own callchain, it's 29+ @_ + $_ + $@ + $^W + C stack), that is, a coroutine has it's own
30own set of lexicals and it's own set of perl's most important global 30callchain, it's own set of lexicals and it's own set of perl's most
31variables. 31important global variables.
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 32
49=cut 33=cut
50 34
51package Coro; 35package Coro;
52 36
189 173
190Future versions of this function will allow result arguments. 174Future versions of this function will allow result arguments.
191 175
192=cut 176=cut
193 177
178# this coroutine is necessary because a coroutine
179# cannot destroy itself.
180my @destroy;
181my $terminate = new Coro sub {
182 while() {
183 delete ((pop @destroy)->{_coro_state}) while @destroy;
184 &schedule;
185 }
186};
187
194sub terminate { 188sub terminate {
195 my $self = $current; 189 push @destroy, $current;
196 $self->{_results} = [@_]; 190 $terminate->ready;
197 $current = shift @ready || $idle; 191 &schedule;
198 Coro::State::transfer(delete $self->{_coro_state}, $current); 192 # NORETURN
199 # cannot return
200 die;
201} 193}
202 194
203=back 195=back
204 196
205# dynamic methods 197# dynamic methods

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines