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.29 by root, Sat Aug 11 00:37:31 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
113# this coroutine is necessary because a coroutine
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};
122
124# we really need priorities... 123# we really need priorities...
125my @ready; # the ready queue. hehe, rather broken ;) 124my @ready; # the ready queue. hehe, rather broken ;)
126 125
127# static methods. not really. 126# static methods. not really.
128 127
148 147
149=cut 148=cut
150 149
151sub async(&@) { 150sub async(&@) {
152 my $pid = new Coro @_; 151 my $pid = new Coro @_;
152 $manager->ready; # this ensures that the stack is cloned from the manager
153 $pid->ready; 153 $pid->ready;
154 $pid; 154 $pid;
155} 155}
156 156
157=item schedule 157=item schedule
190Future versions of this function will allow result arguments. 190Future versions of this function will allow result arguments.
191 191
192=cut 192=cut
193 193
194sub terminate { 194sub terminate {
195 my $self = $current; 195 $current->cancel;
196 $self->{_results} = [@_]; 196 &schedule;
197 $current = shift @ready || $idle; 197 die; # NORETURN
198 Coro::State::transfer(delete $self->{_coro_state}, $current);
199 # cannot return
200 die;
201} 198}
202 199
203=back 200=back
204 201
205# dynamic methods 202# dynamic methods
238 235
239=cut 236=cut
240 237
241sub ready { 238sub ready {
242 push @ready, $_[0]; 239 push @ready, $_[0];
240}
241
242=item $process->cancel
243
244Like C<terminate>, but terminates the specified process instead.
245
246=cut
247
248sub cancel {
249 push @destroy, $_[0];
250 $manager->ready;
243} 251}
244 252
245=back 253=back
246 254
247=cut 255=cut
263 allow per-thread schedulers, but Coro::State does not yet allow this). 271 allow per-thread schedulers, but Coro::State does not yet allow this).
264 272
265=head1 SEE ALSO 273=head1 SEE ALSO
266 274
267L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, 275L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>,
268L<Coro::Signal>, L<Coro::State>, L<Coro::Event>. 276L<Coro::Signal>, L<Coro::State>, L<Coro::Event>, L<Coro::RWLock>,
277L<Coro::Handle>, L<Coro::Socket>.
269 278
270=head1 AUTHOR 279=head1 AUTHOR
271 280
272 Marc Lehmann <pcg@goof.com> 281 Marc Lehmann <pcg@goof.com>
273 http://www.goof.com/pcg/marc/ 282 http://www.goof.com/pcg/marc/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines