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

Comparing Coro/Coro.pm (file contents):
Revision 1.55 by pcg, Wed Nov 5 20:02:44 2003 UTC vs.
Revision 1.78 by root, Wed Nov 1 01:21:21 2006 UTC

30 30
31=cut 31=cut
32 32
33package Coro; 33package Coro;
34 34
35BEGIN { eval { require warnings } && warnings->unimport ("uninitialized") } 35use strict;
36no warnings "uninitialized";
36 37
37use Coro::State; 38use Coro::State;
38 39
39use vars qw($idle $main $current);
40
41use base Exporter; 40use base Exporter::;
42 41
43$VERSION = 0.8; 42our $idle; # idle coroutine
43our $main; # main coroutine
44our $current; # current coroutine
44 45
46our $VERSION = '2.1';
47
45@EXPORT = qw(async cede schedule terminate current); 48our @EXPORT = qw(async cede schedule terminate current);
46%EXPORT_TAGS = ( 49our %EXPORT_TAGS = (
47 prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], 50 prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)],
48); 51);
49@EXPORT_OK = @{$EXPORT_TAGS{prio}}; 52our @EXPORT_OK = @{$EXPORT_TAGS{prio}};
50 53
51{ 54{
52 my @async; 55 my @async;
53 my $init; 56 my $init;
54 57
55 # this way of handling attributes simply is NOT scalable ;() 58 # this way of handling attributes simply is NOT scalable ;()
56 sub import { 59 sub import {
60 no strict 'refs';
61
57 Coro->export_to_level(1, @_); 62 Coro->export_to_level(1, @_);
63
58 my $old = *{(caller)[0]."::MODIFY_CODE_ATTRIBUTES"}{CODE}; 64 my $old = *{(caller)[0]."::MODIFY_CODE_ATTRIBUTES"}{CODE};
59 *{(caller)[0]."::MODIFY_CODE_ATTRIBUTES"} = sub { 65 *{(caller)[0]."::MODIFY_CODE_ATTRIBUTES"} = sub {
60 my ($package, $ref) = (shift, shift); 66 my ($package, $ref) = (shift, shift);
61 my @attrs; 67 my @attrs;
62 for (@_) { 68 for (@_) {
120# this coroutine is necessary because a coroutine 126# this coroutine is necessary because a coroutine
121# cannot destroy itself. 127# cannot destroy itself.
122my @destroy; 128my @destroy;
123my $manager; 129my $manager;
124$manager = new Coro sub { 130$manager = new Coro sub {
125 while() { 131 while () {
126 # by overwriting the state object with the manager we destroy it 132 # by overwriting the state object with the manager we destroy it
127 # while still being able to schedule this coroutine (in case it has 133 # while still being able to schedule this coroutine (in case it has
128 # been readied multiple times. this is harmless since the manager 134 # been readied multiple times. this is harmless since the manager
129 # can be called as many times as neccessary and will always 135 # can be called as many times as neccessary and will always
130 # remove itself from the runqueue 136 # remove itself from the runqueue
131 while (@destroy) { 137 while (@destroy) {
132 my $coro = pop @destroy; 138 my $coro = pop @destroy;
133 $coro->{status} ||= []; 139 $coro->{status} ||= [];
134 $_->ready for @{delete $coro->{join} || []}; 140 $_->ready for @{delete $coro->{join} || []};
141
142 # the next line destroys the _coro_state, but keeps the
143 # process itself intact (we basically make it a zombie
144 # process that always runs the manager thread, so it's possible
145 # to transfer() to this process).
135 $coro->{_coro_state} = $manager->{_coro_state}; 146 $coro->{_coro_state} = $manager->{_coro_state};
136 } 147 }
137 &schedule; 148 &schedule;
138 } 149 }
139}; 150};
157 # create a new coroutine that just prints its arguments 168 # create a new coroutine that just prints its arguments
158 async { 169 async {
159 print "@_\n"; 170 print "@_\n";
160 } 1,2,3,4; 171 } 1,2,3,4;
161 172
162The coderef you submit MUST NOT be a closure that refers to variables
163in an outer scope. This does NOT work. Pass arguments into it instead.
164
165=cut 173=cut
166 174
167sub async(&@) { 175sub async(&@) {
168 my $pid = new Coro @_; 176 my $pid = new Coro @_;
169 $manager->ready; # this ensures that the stack is cloned from the manager 177 $manager->ready; # this ensures that the stack is cloned from the manager
187 195
188=cut 196=cut
189 197
190=item terminate [arg...] 198=item terminate [arg...]
191 199
192Terminates the current process. 200Terminates the current process with the given status values (see L<cancel>).
193
194Future versions of this function will allow result arguments.
195 201
196=cut 202=cut
197 203
198sub terminate { 204sub terminate {
199 $current->{status} = [@_];
200 $current->cancel; 205 $current->cancel (@_);
201 &schedule;
202 die; # NORETURN
203} 206}
204 207
205=back 208=back
206 209
207# dynamic methods 210# dynamic methods
236 239
237Put the given process into the ready queue. 240Put the given process into the ready queue.
238 241
239=cut 242=cut
240 243
241=item $process->cancel 244=item $process->cancel (arg...)
242 245
243Like C<terminate>, but terminates the specified process instead. 246Temrinates the given process and makes it return the given arguments as
247status (default: the empty list).
244 248
245=cut 249=cut
246 250
247sub cancel { 251sub cancel {
252 my $self = shift;
253 $self->{status} = [@_];
248 push @destroy, $_[0]; 254 push @destroy, $self;
249 $manager->ready; 255 $manager->ready;
250 &schedule if $current == $_[0]; 256 &schedule if $current == $self;
251} 257}
252 258
253=item $process->join 259=item $process->join
254 260
255Wait until the coroutine terminates and return any values given to the 261Wait until the coroutine terminates and return any values given to the
256C<terminate> function. C<join> can be called multiple times from multiple 262C<terminate> or C<cancel> functions. C<join> can be called multiple times
257processes. 263from multiple processes.
258 264
259=cut 265=cut
260 266
261sub join { 267sub join {
262 my $self = shift; 268 my $self = shift;
337 to allow per-thread schedulers, but Coro::State does not yet allow 343 to allow per-thread schedulers, but Coro::State does not yet allow
338 this). 344 this).
339 345
340=head1 SEE ALSO 346=head1 SEE ALSO
341 347
342L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, 348Support/Utility: L<Coro::Cont>, L<Coro::Specific>, L<Coro::State>, L<Coro::Util>.
343L<Coro::Signal>, L<Coro::State>, L<Coro::Timer>, L<Coro::Event>, 349
344L<Coro::L<Coro::RWLock>, Handle>, L<Coro::Socket>. 350Locking/IPC: L<Coro::Signal>, L<Coro::Channel>, L<Coro::Semaphore>, L<Coro::SemaphoreSet>, L<Coro::RWLock>.
351
352Event/IO: L<Coro::Timer>, L<Coro::Event>, L<Coro::Handle>, L<Coro::Socket>, L<Coro::Select>.
353
354Embedding: L<Coro:MakeMaker>
345 355
346=head1 AUTHOR 356=head1 AUTHOR
347 357
348 Marc Lehmann <pcg@goof.com> 358 Marc Lehmann <schmorp@schmorp.de>
349 http://www.goof.com/pcg/marc/ 359 http://home.schmorp.de/
350 360
351=cut 361=cut
352 362

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines