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.25 by root, Wed Jul 25 21:12:57 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
53use Coro::State; 37use Coro::State;
54 38
55use base Exporter; 39use base Exporter;
56 40
57$VERSION = 0.10; 41$VERSION = 0.12;
58 42
59@EXPORT = qw(async cede schedule terminate current); 43@EXPORT = qw(async cede schedule terminate current);
60@EXPORT_OK = qw($current); 44@EXPORT_OK = qw($current);
61 45
62{ 46{
119our $idle = new Coro sub { 103our $idle = new Coro sub {
120 print STDERR "FATAL: deadlock detected\n"; 104 print STDERR "FATAL: deadlock detected\n";
121 exit(51); 105 exit(51);
122}; 106};
123 107
108# this coroutine is necessary because a coroutine
109# cannot destroy itself.
110my @destroy;
111my $manager = new Coro sub {
112 while() {
113 delete ((pop @destroy)->{_coro_state}) while @destroy;
114 &schedule;
115 }
116};
117
124# we really need priorities... 118# we really need priorities...
125my @ready; # the ready queue. hehe, rather broken ;) 119my @ready; # the ready queue. hehe, rather broken ;)
126 120
127# static methods. not really. 121# static methods. not really.
128 122
148 142
149=cut 143=cut
150 144
151sub async(&@) { 145sub async(&@) {
152 my $pid = new Coro @_; 146 my $pid = new Coro @_;
147 $manager->ready; # this ensures that the stack is cloned from the manager
153 $pid->ready; 148 $pid->ready;
154 $pid; 149 $pid;
155} 150}
156 151
157=item schedule 152=item schedule
190Future versions of this function will allow result arguments. 185Future versions of this function will allow result arguments.
191 186
192=cut 187=cut
193 188
194sub terminate { 189sub terminate {
195 my $self = $current; 190 push @destroy, $current;
196 $self->{_results} = [@_]; 191 $manager->ready;
197 $current = shift @ready || $idle; 192 &schedule;
198 Coro::State::transfer(delete $self->{_coro_state}, $current); 193 # NORETURN
199 # cannot return
200 die;
201} 194}
202 195
203=back 196=back
204 197
205# dynamic methods 198# dynamic methods
263 allow per-thread schedulers, but Coro::State does not yet allow this). 256 allow per-thread schedulers, but Coro::State does not yet allow this).
264 257
265=head1 SEE ALSO 258=head1 SEE ALSO
266 259
267L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, 260L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>,
268L<Coro::Signal>, L<Coro::State>, L<Coro::Event>. 261L<Coro::Signal>, L<Coro::State>, L<Coro::Event>, L<Coro::RWLock>,
262L<Coro::L<Coro::Handle>, L<Coro::Socket>.
269 263
270=head1 AUTHOR 264=head1 AUTHOR
271 265
272 Marc Lehmann <pcg@goof.com> 266 Marc Lehmann <pcg@goof.com>
273 http://www.goof.com/pcg/marc/ 267 http://www.goof.com/pcg/marc/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines