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

Comparing Coro/Coro.pm (file contents):
Revision 1.25 by root, Wed Jul 25 21:12:57 2001 UTC vs.
Revision 1.30 by root, Sat Aug 11 19:59:19 2001 UTC

36 36
37use Coro::State; 37use Coro::State;
38 38
39use base Exporter; 39use base Exporter;
40 40
41$VERSION = 0.12; 41$VERSION = 0.45;
42 42
43@EXPORT = qw(async cede 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.
113 delete ((pop @destroy)->{_coro_state}) while @destroy; 118 delete ((pop @destroy)->{_coro_state}) while @destroy;
114 &schedule; 119 &schedule;
115 } 120 }
116}; 121};
117 122
118# we really need priorities...
119my @ready; # the ready queue. hehe, rather broken ;)
120
121# static methods. not really. 123# static methods. not really.
122 124
123=head2 STATIC METHODS 125=head2 STATIC METHODS
124 126
125Static methods are actually functions that operate on the current process only. 127Static methods are actually functions that operate on the current process only.
155into the ready queue, so calling this function usually means you will 157into the ready queue, so calling this function usually means you will
156never be called again. 158never be called again.
157 159
158=cut 160=cut
159 161
160my $prev;
161
162sub schedule {
163 # should be done using priorities :(
164 ($prev, $current) = ($current, shift @ready || $idle);
165 Coro::State::transfer($prev, $current);
166}
167
168=item cede 162=item cede
169 163
170"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
171ready 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
172current "timeslice" to other coroutines of the same or higher priority. 166current "timeslice" to other coroutines of the same or higher priority.
173 167
174=cut 168=cut
175 169
176sub 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 {
177 $current->ready; 179 $current->cancel;
178 &schedule; 180 &schedule;
179}
180
181=item terminate
182
183Terminates the current process.
184
185Future versions of this function will allow result arguments.
186
187=cut
188
189sub terminate {
190 push @destroy, $current;
191 $manager->ready;
192 &schedule;
193 # NORETURN 181 die; # NORETURN
194} 182}
195 183
196=back 184=back
197 185
198# dynamic methods 186# dynamic methods
229 217
230Put the current process into the ready queue. 218Put the current process into the ready queue.
231 219
232=cut 220=cut
233 221
234sub ready { 222=item $process->cancel
223
224Like C<terminate>, but terminates the specified process instead.
225
226=cut
227
228sub cancel {
235 push @ready, $_[0]; 229 push @destroy, $_[0];
230 $manager->ready;
236} 231}
237 232
238=back 233=back
239 234
240=cut 235=cut
257 252
258=head1 SEE ALSO 253=head1 SEE ALSO
259 254
260L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, 255L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>,
261L<Coro::Signal>, L<Coro::State>, L<Coro::Event>, L<Coro::RWLock>, 256L<Coro::Signal>, L<Coro::State>, L<Coro::Event>, L<Coro::RWLock>,
262L<Coro::L<Coro::Handle>, L<Coro::Socket>. 257L<Coro::Handle>, L<Coro::Socket>.
263 258
264=head1 AUTHOR 259=head1 AUTHOR
265 260
266 Marc Lehmann <pcg@goof.com> 261 Marc Lehmann <pcg@goof.com>
267 http://www.goof.com/pcg/marc/ 262 http://www.goof.com/pcg/marc/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines