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

Comparing Coro/Coro.pm (file contents):
Revision 1.57 by pcg, Sun Nov 30 22:49:25 2003 UTC vs.
Revision 1.82 by root, Fri Nov 24 13:40:36 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.9"; 42our $idle; # idle coroutine
43our $main; # main coroutine
44our $current; # current coroutine
44 45
46our $VERSION = '2.5';
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 (@_) {
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}->_clone_state_from ($manager->{_coro_state});
136 } 147 }
137 &schedule; 148 &schedule;
138 } 149 }
139}; 150};
140 151
151=item async { ... } [@args...] 162=item async { ... } [@args...]
152 163
153Create a new asynchronous process and return it's process object 164Create a new asynchronous process and return it's process object
154(usually unused). When the sub returns the new process is automatically 165(usually unused). When the sub returns the new process is automatically
155terminated. 166terminated.
167
168When the coroutine dies, the program will exit, just as in the main
169program.
156 170
157 # create a new coroutine that just prints its arguments 171 # create a new coroutine that just prints its arguments
158 async { 172 async {
159 print "@_\n"; 173 print "@_\n";
160 } 1,2,3,4; 174 } 1,2,3,4;
184 198
185=cut 199=cut
186 200
187=item terminate [arg...] 201=item terminate [arg...]
188 202
189Terminates the current process. 203Terminates the current process with the given status values (see L<cancel>).
190
191Future versions of this function will allow result arguments.
192 204
193=cut 205=cut
194 206
195sub terminate { 207sub terminate {
196 $current->{status} = [@_];
197 $current->cancel; 208 $current->cancel (@_);
198 &schedule;
199 die; # NORETURN
200} 209}
201 210
202=back 211=back
203 212
204# dynamic methods 213# dynamic methods
223} 232}
224 233
225sub new { 234sub new {
226 my $class = shift; 235 my $class = shift;
227 bless { 236 bless {
228 _coro_state => (new Coro::State $_[0] && \&_newcoro, @_), 237 _coro_state => (new Coro::State \&_newcoro, @_),
229 }, $class; 238 }, $class;
230} 239}
231 240
232=item $process->ready 241=item $process->ready
233 242
234Put the given process into the ready queue. 243Put the given process into the ready queue.
235 244
236=cut 245=cut
237 246
238=item $process->cancel 247=item $process->cancel (arg...)
239 248
240Like C<terminate>, but terminates the specified process instead. 249Terminates the given process and makes it return the given arguments as
250status (default: the empty list).
241 251
242=cut 252=cut
243 253
244sub cancel { 254sub cancel {
255 my $self = shift;
256 $self->{status} = [@_];
245 push @destroy, $_[0]; 257 push @destroy, $self;
246 $manager->ready; 258 $manager->ready;
247 &schedule if $current == $_[0]; 259 &schedule if $current == $self;
248} 260}
249 261
250=item $process->join 262=item $process->join
251 263
252Wait until the coroutine terminates and return any values given to the 264Wait until the coroutine terminates and return any values given to the
253C<terminate> function. C<join> can be called multiple times from multiple 265C<terminate> or C<cancel> functions. C<join> can be called multiple times
254processes. 266from multiple processes.
255 267
256=cut 268=cut
257 269
258sub join { 270sub join {
259 my $self = shift; 271 my $self = shift;
262 &schedule; 274 &schedule;
263 } 275 }
264 wantarray ? @{$self->{status}} : $self->{status}[0]; 276 wantarray ? @{$self->{status}} : $self->{status}[0];
265} 277}
266 278
267=item $oldprio = $process->prio($newprio) 279=item $oldprio = $process->prio ($newprio)
268 280
269Sets (or gets, if the argument is missing) the priority of the 281Sets (or gets, if the argument is missing) the priority of the
270process. Higher priority processes get run before lower priority 282process. Higher priority processes get run before lower priority
271processes. Priorities are small signed integers (currently -4 .. +3), 283processes. Priorities are small signed integers (currently -4 .. +3),
272that you can refer to using PRIO_xxx constants (use the import tag :prio 284that you can refer to using PRIO_xxx constants (use the import tag :prio
287process). This is a bug that will be fixed in some future version. 299process). This is a bug that will be fixed in some future version.
288 300
289=cut 301=cut
290 302
291sub prio { 303sub prio {
292 my $old = $_[0]{prio}; 304 shift->{_coro_state}->prio (@_)
293 $_[0]{prio} = $_[1] if @_ > 1;
294 $old;
295} 305}
296 306
297=item $newprio = $process->nice($change) 307=item $newprio = $process->nice ($change)
298 308
299Similar to C<prio>, but subtract the given value from the priority (i.e. 309Similar to C<prio>, but subtract the given value from the priority (i.e.
300higher values mean lower priority, just as in unix). 310higher values mean lower priority, just as in unix).
301 311
302=cut 312=cut
303 313
304sub nice { 314sub nice {
305 $_[0]{prio} -= $_[1]; 315 shift->{_coro_state}->nice (@_)
306} 316}
307 317
308=item $olddesc = $process->desc($newdesc) 318=item $olddesc = $process->desc ($newdesc)
309 319
310Sets (or gets in case the argument is missing) the description for this 320Sets (or gets in case the argument is missing) the description for this
311process. This is just a free-form string you can associate with a process. 321process. This is just a free-form string you can associate with a process.
312 322
313=cut 323=cut
334 to allow per-thread schedulers, but Coro::State does not yet allow 344 to allow per-thread schedulers, but Coro::State does not yet allow
335 this). 345 this).
336 346
337=head1 SEE ALSO 347=head1 SEE ALSO
338 348
339L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, 349Support/Utility: L<Coro::Cont>, L<Coro::Specific>, L<Coro::State>, L<Coro::Util>.
340L<Coro::Signal>, L<Coro::State>, L<Coro::Timer>, L<Coro::Event>, 350
341L<Coro::L<Coro::RWLock>, Handle>, L<Coro::Socket>. 351Locking/IPC: L<Coro::Signal>, L<Coro::Channel>, L<Coro::Semaphore>, L<Coro::SemaphoreSet>, L<Coro::RWLock>.
352
353Event/IO: L<Coro::Timer>, L<Coro::Event>, L<Coro::Handle>, L<Coro::Socket>, L<Coro::Select>.
354
355Embedding: L<Coro:MakeMaker>
342 356
343=head1 AUTHOR 357=head1 AUTHOR
344 358
345 Marc Lehmann <pcg@goof.com> 359 Marc Lehmann <schmorp@schmorp.de>
346 http://www.goof.com/pcg/marc/ 360 http://home.schmorp.de/
347 361
348=cut 362=cut
349 363

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines