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.22 by root, Mon Jul 23 02:14:19 2001 UTC vs.
Revision 1.30 by root, Sat Aug 11 19:59:19 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.45;
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{
63 my @async; 47 my @async;
48 my $init;
64 49
65 # this way of handling attributes simply is NOT scalable ;() 50 # this way of handling attributes simply is NOT scalable ;()
66 sub import { 51 sub import {
67 Coro->export_to_level(1, @_); 52 Coro->export_to_level(1, @_);
68 my $old = *{(caller)[0]."::MODIFY_CODE_ATTRIBUTES"}{CODE}; 53 my $old = *{(caller)[0]."::MODIFY_CODE_ATTRIBUTES"}{CODE};
70 my ($package, $ref) = (shift, shift); 55 my ($package, $ref) = (shift, shift);
71 my @attrs; 56 my @attrs;
72 for (@_) { 57 for (@_) {
73 if ($_ eq "Coro") { 58 if ($_ eq "Coro") {
74 push @async, $ref; 59 push @async, $ref;
60 unless ($init++) {
61 eval q{
62 sub INIT {
63 &async(pop @async) while @async;
64 }
65 };
66 }
75 } else { 67 } else {
76 push @attrs, $_; 68 push @attrs, $_;
77 } 69 }
78 } 70 }
79 return $old ? $old->($package, $ref, @attrs) : @attrs; 71 return $old ? $old->($package, $ref, @attrs) : @attrs;
80 }; 72 };
81 } 73 }
82 74
83 sub INIT {
84 &async(pop @async) while @async;
85 }
86} 75}
87 76
88=item $main 77=item $main
89 78
90This coroutine represents the main program. 79This coroutine represents the main program.
119our $idle = new Coro sub { 108our $idle = new Coro sub {
120 print STDERR "FATAL: deadlock detected\n"; 109 print STDERR "FATAL: deadlock detected\n";
121 exit(51); 110 exit(51);
122}; 111};
123 112
124# we really need priorities... 113# this coroutine is necessary because a coroutine
125my @ready; # the ready queue. hehe, rather broken ;) 114# cannot destroy itself.
115my @destroy;
116my $manager = new Coro sub {
117 while() {
118 delete ((pop @destroy)->{_coro_state}) while @destroy;
119 &schedule;
120 }
121};
126 122
127# static methods. not really. 123# static methods. not really.
128 124
129=head2 STATIC METHODS 125=head2 STATIC METHODS
130 126
148 144
149=cut 145=cut
150 146
151sub async(&@) { 147sub async(&@) {
152 my $pid = new Coro @_; 148 my $pid = new Coro @_;
149 $manager->ready; # this ensures that the stack is cloned from the manager
153 $pid->ready; 150 $pid->ready;
154 $pid; 151 $pid;
155} 152}
156 153
157=item schedule 154=item schedule
160into the ready queue, so calling this function usually means you will 157into the ready queue, so calling this function usually means you will
161never be called again. 158never be called again.
162 159
163=cut 160=cut
164 161
165my $prev;
166
167sub schedule {
168 # should be done using priorities :(
169 ($prev, $current) = ($current, shift @ready || $idle);
170 Coro::State::transfer($prev, $current);
171}
172
173=item cede 162=item cede
174 163
175"Cede" to other processes. This function puts the current process into the 164"Cede" to other processes. This function puts the current process into the
176ready queue and calls C<schedule>, which has the effect of giving up the 165ready queue and calls C<schedule>, which has the effect of giving up the
177current "timeslice" to other coroutines of the same or higher priority. 166current "timeslice" to other coroutines of the same or higher priority.
178 167
179=cut 168=cut
180 169
181sub cede { 170=item terminate
171
172Terminates the current process.
173
174Future versions of this function will allow result arguments.
175
176=cut
177
178sub terminate {
182 $current->ready; 179 $current->cancel;
183 &schedule; 180 &schedule;
184} 181 die; # NORETURN
185
186=item terminate
187
188Terminates the current process.
189
190Future versions of this function will allow result arguments.
191
192=cut
193
194sub terminate {
195 my $self = $current;
196 $self->{_results} = [@_];
197 $current = shift @ready || $idle;
198 Coro::State::transfer(delete $self->{_coro_state}, $current);
199 # cannot return
200 die;
201} 182}
202 183
203=back 184=back
204 185
205# dynamic methods 186# dynamic methods
236 217
237Put the current process into the ready queue. 218Put the current process into the ready queue.
238 219
239=cut 220=cut
240 221
241sub ready { 222=item $process->cancel
223
224Like C<terminate>, but terminates the specified process instead.
225
226=cut
227
228sub cancel {
242 push @ready, $_[0]; 229 push @destroy, $_[0];
230 $manager->ready;
243} 231}
244 232
245=back 233=back
246 234
247=cut 235=cut
263 allow per-thread schedulers, but Coro::State does not yet allow this). 251 allow per-thread schedulers, but Coro::State does not yet allow this).
264 252
265=head1 SEE ALSO 253=head1 SEE ALSO
266 254
267L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, 255L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>,
268L<Coro::Signal>, L<Coro::State>, L<Coro::Event>. 256L<Coro::Signal>, L<Coro::State>, L<Coro::Event>, L<Coro::RWLock>,
257L<Coro::Handle>, L<Coro::Socket>.
269 258
270=head1 AUTHOR 259=head1 AUTHOR
271 260
272 Marc Lehmann <pcg@goof.com> 261 Marc Lehmann <pcg@goof.com>
273 http://www.goof.com/pcg/marc/ 262 http://www.goof.com/pcg/marc/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines