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

Comparing Coro/Coro.pm (file contents):
Revision 1.20 by root, Sat Jul 21 18:21:45 2001 UTC vs.
Revision 1.26 by root, Fri Jul 27 02:51:33 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 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 32
33=cut 33=cut
34 34
35package Coro; 35package Coro;
36 36
37use Coro::State; 37use Coro::State;
38 38
39use base Exporter; 39use base Exporter;
40 40
41$VERSION = 0.10; 41$VERSION = 0.12;
42 42
43@EXPORT = qw(async yield schedule terminate current); 43@EXPORT = qw(async cede schedule terminate current);
44@EXPORT_OK = qw($current); 44@EXPORT_OK = qw($current);
45 45
46{ 46{
47 my @async; 47 my @async;
48 my $init;
48 49
49 # this way of handling attributes simply is NOT scalable ;() 50 # this way of handling attributes simply is NOT scalable ;()
50 sub import { 51 sub import {
51 Coro->export_to_level(1, @_); 52 Coro->export_to_level(1, @_);
52 my $old = *{(caller)[0]."::MODIFY_CODE_ATTRIBUTES"}{CODE}; 53 my $old = *{(caller)[0]."::MODIFY_CODE_ATTRIBUTES"}{CODE};
54 my ($package, $ref) = (shift, shift); 55 my ($package, $ref) = (shift, shift);
55 my @attrs; 56 my @attrs;
56 for (@_) { 57 for (@_) {
57 if ($_ eq "Coro") { 58 if ($_ eq "Coro") {
58 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 }
59 } else { 67 } else {
60 push @attrs, $_; 68 push @attrs, $_;
61 } 69 }
62 } 70 }
63 return $old ? $old->($package, $ref, @attrs) : @attrs; 71 return $old ? $old->($package, $ref, @attrs) : @attrs;
64 }; 72 };
65 } 73 }
66 74
67 sub INIT {
68 &async(pop @async) while @async;
69 }
70} 75}
71 76
72=item $main 77=item $main
73 78
74This coroutine represents the main program. 79This coroutine represents the main program.
103our $idle = new Coro sub { 108our $idle = new Coro sub {
104 print STDERR "FATAL: deadlock detected\n"; 109 print STDERR "FATAL: deadlock detected\n";
105 exit(51); 110 exit(51);
106}; 111};
107 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
108# we really need priorities... 123# we really need priorities...
109my @ready; # the ready queue. hehe, rather broken ;) 124my @ready; # the ready queue. hehe, rather broken ;)
110 125
111# static methods. not really. 126# static methods. not really.
112 127
132 147
133=cut 148=cut
134 149
135sub async(&@) { 150sub async(&@) {
136 my $pid = new Coro @_; 151 my $pid = new Coro @_;
152 $manager->ready; # this ensures that the stack is cloned from the manager
137 $pid->ready; 153 $pid->ready;
138 $pid; 154 $pid;
139} 155}
140 156
141=item schedule 157=item schedule
152 # should be done using priorities :( 168 # should be done using priorities :(
153 ($prev, $current) = ($current, shift @ready || $idle); 169 ($prev, $current) = ($current, shift @ready || $idle);
154 Coro::State::transfer($prev, $current); 170 Coro::State::transfer($prev, $current);
155} 171}
156 172
157=item yield 173=item cede
158 174
159Yield to other processes. This function puts the current process into the 175"Cede" to other processes. This function puts the current process into the
160ready 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.
161 178
162=cut 179=cut
163 180
164sub yield { 181sub cede {
165 $current->ready; 182 $current->ready;
166 &schedule; 183 &schedule;
167} 184}
168 185
169=item terminate 186=item terminate
173Future versions of this function will allow result arguments. 190Future versions of this function will allow result arguments.
174 191
175=cut 192=cut
176 193
177sub terminate { 194sub terminate {
178 $current->{_results} = [@_]; 195 push @destroy, $current;
196 $manager->ready;
179 &schedule; 197 &schedule;
198 # NORETURN
180} 199}
181 200
182=back 201=back
183 202
184# dynamic methods 203# dynamic methods
242 allow per-thread schedulers, but Coro::State does not yet allow this). 261 allow per-thread schedulers, but Coro::State does not yet allow this).
243 262
244=head1 SEE ALSO 263=head1 SEE ALSO
245 264
246L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, 265L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>,
247L<Coro::Signal>, L<Coro::State>, L<Coro::Event>. 266L<Coro::Signal>, L<Coro::State>, L<Coro::Event>, L<Coro::RWLock>,
267L<Coro::Handle>, L<Coro::Socket>.
248 268
249=head1 AUTHOR 269=head1 AUTHOR
250 270
251 Marc Lehmann <pcg@goof.com> 271 Marc Lehmann <pcg@goof.com>
252 http://www.goof.com/pcg/marc/ 272 http://www.goof.com/pcg/marc/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines