ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro.pm
Revision: 1.181
Committed: Fri May 9 22:04:37 2008 UTC (16 years ago) by root
Branch: MAIN
Changes since 1.180: +161 -78 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 =head1 NAME
2
3 Coro - coroutine process abstraction
4
5 =head1 SYNOPSIS
6
7 use Coro;
8
9 async {
10 # some asynchronous thread of execution
11 print "2\n";
12 cede; # yield back to main
13 print "4\n";
14 };
15 print "1\n";
16 cede; # yield to coroutine
17 print "3\n";
18 cede; # and again
19
20 # use locking
21 my $lock = new Coro::Semaphore;
22 my $locked;
23
24 $lock->down;
25 $locked = 1;
26 $lock->up;
27
28 =head1 DESCRIPTION
29
30 This module collection manages coroutines. Coroutines are similar to
31 threads but don't (in general) run in parallel at the same time even
32 on SMP machines. The specific flavor of coroutine used in this module
33 also guarantees you that it will not switch between coroutines unless
34 necessary, at easily-identified points in your program, so locking and
35 parallel access are rarely an issue, making coroutine programming much
36 safer and easier than threads programming.
37
38 Unlike a normal perl program, however, coroutines allow you to have
39 multiple running interpreters that share data, which is especially useful
40 to code pseudo-parallel processes, such as multiple HTTP-GET requests
41 running concurrently.
42
43 Coroutines are also useful because Perl has no support for threads (the so
44 called "threads" that perl offers are nothing more than the (bad) process
45 emulation coming from the Windows platform: On standard operating systems
46 they serve no purpose whatsoever, except by making your programs slow and
47 making them use a lot of memory. Best disable them when building perl, or
48 aks your software vendor/distributor to do it for you).
49
50 In this module, coroutines are defined as "callchain + lexical variables +
51 @_ + $_ + $@ + $/ + C stack), that is, a coroutine has its own callchain,
52 its own set of lexicals and its own set of perls most important global
53 variables (see L<Coro::State> for more configuration).
54
55 =cut
56
57 package Coro;
58
59 use strict;
60 no warnings "uninitialized";
61
62 use Coro::State;
63
64 use base qw(Coro::State Exporter);
65
66 our $idle; # idle handler
67 our $main; # main coroutine
68 our $current; # current coroutine
69
70 our $VERSION = 4.6;
71
72 our @EXPORT = qw(async async_pool cede schedule terminate current unblock_sub);
73 our %EXPORT_TAGS = (
74 prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)],
75 );
76 our @EXPORT_OK = (@{$EXPORT_TAGS{prio}}, qw(nready));
77
78 =over 4
79
80 =item $Coro::main
81
82 This variable stores the coroutine object that represents the main
83 program. While you cna C<ready> it and do most other things you can do to
84 coroutines, it is mainly useful to compare again C<$Coro::current>, to see
85 wether you are running in the main program or not.
86
87 =cut
88
89 $main = new Coro;
90
91 =item $Coro::current
92
93 The coroutine object representing the current coroutine (the last
94 coroutine that the Coro scheduler switched to). The initial value is
95 C<$main> (of course).
96
97 This variable is B<strictly> I<read-only>. You can take copies of the
98 value stored in it and use it as any other coroutine object, but you must
99 not otherwise modify the variable itself.
100
101 =cut
102
103 $main->{desc} = "[main::]";
104
105 # maybe some other module used Coro::Specific before...
106 $main->{_specific} = $current->{_specific}
107 if $current;
108
109 _set_current $main;
110
111 sub current() { $current } # [DEPRECATED]
112
113 =item $Coro::idle
114
115 This variable is mainly useful to integrate Coro into event loops. It is
116 usually better to rely on L<Coro::AnyEvent> or LC<Coro::EV>, as this is
117 pretty low-level functionality.
118
119 This variable stores a callback that is called whenever the scheduler
120 finds no ready coroutines to run. The default implementation prints
121 "FATAL: deadlock detected" and exits, because the program has no other way
122 to continue.
123
124 This hook is overwritten by modules such as C<Coro::Timer> and
125 C<Coro::AnyEvent> to wait on an external event that hopefully wake up a
126 coroutine so the scheduler can run it.
127
128 Note that the callback I<must not>, under any circumstances, block
129 the current coroutine. Normally, this is achieved by having an "idle
130 coroutine" that calls the event loop and then blocks again, and then
131 readying that coroutine in the idle handler.
132
133 See L<Coro::Event> or L<Coro::AnyEvent> for examples of using this
134 technique.
135
136 Please note that if your callback recursively invokes perl (e.g. for event
137 handlers), then it must be prepared to be called recursively itself.
138
139 =cut
140
141 $idle = sub {
142 require Carp;
143 Carp::croak ("FATAL: deadlock detected");
144 };
145
146 sub _cancel {
147 my ($self) = @_;
148
149 # free coroutine data and mark as destructed
150 $self->_destroy
151 or return;
152
153 # call all destruction callbacks
154 $_->(@{$self->{_status}})
155 for @{(delete $self->{_on_destroy}) || []};
156 }
157
158 # this coroutine is necessary because a coroutine
159 # cannot destroy itself.
160 my @destroy;
161 my $manager;
162
163 $manager = new Coro sub {
164 while () {
165 (shift @destroy)->_cancel
166 while @destroy;
167
168 &schedule;
169 }
170 };
171 $manager->desc ("[coro manager]");
172 $manager->prio (PRIO_MAX);
173
174 =back
175
176 =head2 SIMPLE COROUTINE CREATION
177
178 =over 4
179
180 =item async { ... } [@args...]
181
182 Create a new coroutine and return it's coroutine object (usually
183 unused). The coroutine will be put into the ready queue, so
184 it will start running automatically on the next scheduler run.
185
186 The first argument is a codeblock/closure that should be executed in the
187 coroutine. When it returns argument returns the coroutine is automatically
188 terminated.
189
190 The remaining arguments are passed as arguments to the closure.
191
192 See the C<Coro::State::new> constructor for info about the coroutine
193 environment in which coroutines are executed.
194
195 Calling C<exit> in a coroutine will do the same as calling exit outside
196 the coroutine. Likewise, when the coroutine dies, the program will exit,
197 just as it would in the main program.
198
199 If you do not want that, you can provide a default C<die> handler, or
200 simply avoid dieing (by use of C<eval>).
201
202 Example: Create a new coroutine that just prints its arguments.
203
204 async {
205 print "@_\n";
206 } 1,2,3,4;
207
208 =cut
209
210 sub async(&@) {
211 my $coro = new Coro @_;
212 $coro->ready;
213 $coro
214 }
215
216 =item async_pool { ... } [@args...]
217
218 Similar to C<async>, but uses a coroutine pool, so you should not call
219 terminate or join on it (although you are allowed to), and you get a
220 coroutine that might have executed other code already (which can be good
221 or bad :).
222
223 On the plus side, this function is faster than creating (and destroying)
224 a completely new coroutine, so if you need a lot of generic coroutines in
225 quick successsion, use C<async_pool>, not C<async>.
226
227 The code block is executed in an C<eval> context and a warning will be
228 issued in case of an exception instead of terminating the program, as
229 C<async> does. As the coroutine is being reused, stuff like C<on_destroy>
230 will not work in the expected way, unless you call terminate or cancel,
231 which somehow defeats the purpose of pooling (but is fine in the
232 exceptional case).
233
234 The priority will be reset to C<0> after each run, tracing will be
235 disabled, the description will be reset and the default output filehandle
236 gets restored, so you can change all these. Otherwise the coroutine will
237 be re-used "as-is": most notably if you change other per-coroutine global
238 stuff such as C<$/> you I<must needs> to revert that change, which is most
239 simply done by using local as in: C< local $/ >.
240
241 The pool size is limited to C<8> idle coroutines (this can be adjusted by
242 changing $Coro::POOL_SIZE), and there can be as many non-idle coros as
243 required.
244
245 If you are concerned about pooled coroutines growing a lot because a
246 single C<async_pool> used a lot of stackspace you can e.g. C<async_pool
247 { terminate }> once per second or so to slowly replenish the pool. In
248 addition to that, when the stacks used by a handler grows larger than 16kb
249 (adjustable via $Coro::POOL_RSS) it will also be destroyed.
250
251 =cut
252
253 our $POOL_SIZE = 8;
254 our $POOL_RSS = 16 * 1024;
255 our @async_pool;
256
257 sub pool_handler {
258 my $cb;
259
260 while () {
261 eval {
262 while () {
263 _pool_1 $cb;
264 &$cb;
265 _pool_2 $cb;
266 &schedule;
267 }
268 };
269
270 last if $@ eq "\3async_pool terminate\2\n";
271 warn $@ if $@;
272 }
273 }
274
275 sub async_pool(&@) {
276 # this is also inlined into the unlock_scheduler
277 my $coro = (pop @async_pool) || new Coro \&pool_handler;
278
279 $coro->{_invoke} = [@_];
280 $coro->ready;
281
282 $coro
283 }
284
285 =back
286
287 =head2 STATIC METHODS
288
289 Static methods are actually functions that operate on the current coroutine.
290
291 =over 4
292
293 =item schedule
294
295 Calls the scheduler. The scheduler will find the next coroutine that is
296 to be run from the ready queue and switches to it. The next coroutine
297 to be run is simply the one with the highest priority that is longest
298 in its ready queue. If there is no coroutine ready, it will clal the
299 C<$Coro::idle> hook.
300
301 Please note that the current coroutine will I<not> be put into the ready
302 queue, so calling this function usually means you will never be called
303 again unless something else (e.g. an event handler) calls C<< ->ready >>,
304 thus waking you up.
305
306 This makes C<schedule> I<the> generic method to use to block the current
307 coroutine and wait for events: first you remember the current coroutine in
308 a variable, then arrange for some callback of yours to call C<< ->ready
309 >> on that once some event happens, and last you call C<schedule> to put
310 yourself to sleep. Note that a lot of things can wake your coroutine up,
311 so you need to check wether the event indeed happened, e.g. by storing the
312 status in a variable.
313
314 The canonical way to wait on external events is this:
315
316 {
317 # remember current coroutine
318 my $current = $Coro::current;
319
320 # register a hypothetical event handler
321 on_event_invoke sub {
322 # wake up sleeping coroutine
323 $current->ready;
324 undef $current;
325 };
326
327 # call schedule until event occurred.
328 # in case we are woken up for other reasons
329 # (current still defined), loop.
330 Coro::schedule while $current;
331 }
332
333 =item cede
334
335 "Cede" to other coroutines. This function puts the current coroutine into
336 the ready queue and calls C<schedule>, which has the effect of giving
337 up the current "timeslice" to other coroutines of the same or higher
338 priority. Once your coroutine gets its turn again it will automatically be
339 resumed.
340
341 This function is often called C<yield> in other languages.
342
343 =item Coro::cede_notself
344
345 Works like cede, but is not exported by default and will cede to I<any>
346 coroutine, regardless of priority. This is useful sometimes to ensure
347 progress is made.
348
349 =item terminate [arg...]
350
351 Terminates the current coroutine with the given status values (see L<cancel>).
352
353 =item killall
354
355 Kills/terminates/cancels all coroutines except the currently running
356 one. This is useful after a fork, either in the child or the parent, as
357 usually only one of them should inherit the running coroutines.
358
359 Note that while this will try to free some of the main programs resources,
360 you cnanot free all of them, so if a coroutine that is not the main
361 program calls this function, there will be some one-time resource leak.
362
363 =cut
364
365 sub terminate {
366 $current->cancel (@_);
367 }
368
369 sub killall {
370 for (Coro::State::list) {
371 $_->cancel
372 if $_ != $current && UNIVERSAL::isa $_, "Coro";
373 }
374 }
375
376 =back
377
378 =head2 COROUTINE METHODS
379
380 These are the methods you can call on coroutine objects (or to create
381 them).
382
383 =over 4
384
385 =item new Coro \&sub [, @args...]
386
387 Create a new coroutine and return it. When the sub returns, the coroutine
388 automatically terminates as if C<terminate> with the returned values were
389 called. To make the coroutine run you must first put it into the ready
390 queue by calling the ready method.
391
392 See C<async> and C<Coro::State::new> for additional info about the
393 coroutine environment.
394
395 =cut
396
397 sub _run_coro {
398 terminate &{+shift};
399 }
400
401 sub new {
402 my $class = shift;
403
404 $class->SUPER::new (\&_run_coro, @_)
405 }
406
407 =item $success = $coroutine->ready
408
409 Put the given coroutine into the end of its ready queue (there is one
410 queue for each priority) and return true. If the coroutine is already in
411 the ready queue, do nothing and return false.
412
413 This ensures that the scheduler will resume this coroutine automatically
414 once all the coroutines of higher priority and all coroutines of the same
415 priority that were put into the ready queue earlier have been resumed.
416
417 =item $is_ready = $coroutine->is_ready
418
419 Return wether the coroutine is currently the ready queue or not,
420
421 =item $coroutine->cancel (arg...)
422
423 Terminates the given coroutine and makes it return the given arguments as
424 status (default: the empty list). Never returns if the coroutine is the
425 current coroutine.
426
427 =cut
428
429 sub cancel {
430 my $self = shift;
431 $self->{_status} = [@_];
432
433 if ($current == $self) {
434 push @destroy, $self;
435 $manager->ready;
436 &schedule while 1;
437 } else {
438 $self->_cancel;
439 }
440 }
441
442 =item $coroutine->join
443
444 Wait until the coroutine terminates and return any values given to the
445 C<terminate> or C<cancel> functions. C<join> can be called concurrently
446 from multiple coroutines, and all will be resumed and given the status
447 return once the C<$coroutine> terminates.
448
449 =cut
450
451 sub join {
452 my $self = shift;
453
454 unless ($self->{_status}) {
455 my $current = $current;
456
457 push @{$self->{_on_destroy}}, sub {
458 $current->ready;
459 undef $current;
460 };
461
462 &schedule while $current;
463 }
464
465 wantarray ? @{$self->{_status}} : $self->{_status}[0];
466 }
467
468 =item $coroutine->on_destroy (\&cb)
469
470 Registers a callback that is called when this coroutine gets destroyed,
471 but before it is joined. The callback gets passed the terminate arguments,
472 if any, and I<must not> die, under any circumstances.
473
474 =cut
475
476 sub on_destroy {
477 my ($self, $cb) = @_;
478
479 push @{ $self->{_on_destroy} }, $cb;
480 }
481
482 =item $oldprio = $coroutine->prio ($newprio)
483
484 Sets (or gets, if the argument is missing) the priority of the
485 coroutine. Higher priority coroutines get run before lower priority
486 coroutines. Priorities are small signed integers (currently -4 .. +3),
487 that you can refer to using PRIO_xxx constants (use the import tag :prio
488 to get then):
489
490 PRIO_MAX > PRIO_HIGH > PRIO_NORMAL > PRIO_LOW > PRIO_IDLE > PRIO_MIN
491 3 > 1 > 0 > -1 > -3 > -4
492
493 # set priority to HIGH
494 current->prio(PRIO_HIGH);
495
496 The idle coroutine ($Coro::idle) always has a lower priority than any
497 existing coroutine.
498
499 Changing the priority of the current coroutine will take effect immediately,
500 but changing the priority of coroutines in the ready queue (but not
501 running) will only take effect after the next schedule (of that
502 coroutine). This is a bug that will be fixed in some future version.
503
504 =item $newprio = $coroutine->nice ($change)
505
506 Similar to C<prio>, but subtract the given value from the priority (i.e.
507 higher values mean lower priority, just as in unix).
508
509 =item $olddesc = $coroutine->desc ($newdesc)
510
511 Sets (or gets in case the argument is missing) the description for this
512 coroutine. This is just a free-form string you can associate with a coroutine.
513
514 This method simply sets the C<< $coroutine->{desc} >> member to the given string. You
515 can modify this member directly if you wish.
516
517 =item $coroutine->throw ([$scalar])
518
519 If C<$throw> is specified and defined, it will be thrown as an exception
520 inside the coroutine at the next convinient point in time (usually after
521 it gains control at the next schedule/transfer/cede). Otherwise clears the
522 exception object.
523
524 The exception object will be thrown "as is" with the specified scalar in
525 C<$@>, i.e. if it is a string, no line number or newline will be appended
526 (unlike with C<die>).
527
528 This can be used as a softer means than C<cancel> to ask a coroutine to
529 end itself, although there is no guarentee that the exception will lead to
530 termination, and if the exception isn't caught it might well end the whole
531 program.
532
533 =cut
534
535 sub desc {
536 my $old = $_[0]{desc};
537 $_[0]{desc} = $_[1] if @_ > 1;
538 $old;
539 }
540
541 =back
542
543 =head2 GLOBAL FUNCTIONS
544
545 =over 4
546
547 =item Coro::nready
548
549 Returns the number of coroutines that are currently in the ready state,
550 i.e. that can be switched to by calling C<schedule> directory or
551 indirectly. The value C<0> means that the only runnable coroutine is the
552 currently running one, so C<cede> would have no effect, and C<schedule>
553 would cause a deadlock unless there is an idle handler that wakes up some
554 coroutines.
555
556 =item my $guard = Coro::guard { ... }
557
558 This creates and returns a guard object. Nothing happens until the object
559 gets destroyed, in which case the codeblock given as argument will be
560 executed. This is useful to free locks or other resources in case of a
561 runtime error or when the coroutine gets canceled, as in both cases the
562 guard block will be executed. The guard object supports only one method,
563 C<< ->cancel >>, which will keep the codeblock from being executed.
564
565 Example: set some flag and clear it again when the coroutine gets canceled
566 or the function returns:
567
568 sub do_something {
569 my $guard = Coro::guard { $busy = 0 };
570 $busy = 1;
571
572 # do something that requires $busy to be true
573 }
574
575 =cut
576
577 sub guard(&) {
578 bless \(my $cb = $_[0]), "Coro::guard"
579 }
580
581 sub Coro::guard::cancel {
582 ${$_[0]} = sub { };
583 }
584
585 sub Coro::guard::DESTROY {
586 ${$_[0]}->();
587 }
588
589
590 =item unblock_sub { ... }
591
592 This utility function takes a BLOCK or code reference and "unblocks" it,
593 returning a new coderef. Unblocking means that calling the new coderef
594 will return immediately without blocking, returning nothing, while the
595 original code ref will be called (with parameters) from within another
596 coroutine.
597
598 The reason this function exists is that many event libraries (such as the
599 venerable L<Event|Event> module) are not coroutine-safe (a weaker form
600 of thread-safety). This means you must not block within event callbacks,
601 otherwise you might suffer from crashes or worse. The only event library
602 currently known that is safe to use without C<unblock_sub> is L<EV>.
603
604 This function allows your callbacks to block by executing them in another
605 coroutine where it is safe to block. One example where blocking is handy
606 is when you use the L<Coro::AIO|Coro::AIO> functions to save results to
607 disk, for example.
608
609 In short: simply use C<unblock_sub { ... }> instead of C<sub { ... }> when
610 creating event callbacks that want to block.
611
612 If your handler does not plan to block (e.g. simply sends a message to
613 another coroutine, or puts some other coroutine into the ready queue),
614 there is no reason to use C<unblock_sub>.
615
616 =cut
617
618 our @unblock_queue;
619
620 # we create a special coro because we want to cede,
621 # to reduce pressure on the coro pool (because most callbacks
622 # return immediately and can be reused) and because we cannot cede
623 # inside an event callback.
624 our $unblock_scheduler = new Coro sub {
625 while () {
626 while (my $cb = pop @unblock_queue) {
627 # this is an inlined copy of async_pool
628 my $coro = (pop @async_pool) || new Coro \&pool_handler;
629
630 $coro->{_invoke} = $cb;
631 $coro->ready;
632 cede; # for short-lived callbacks, this reduces pressure on the coro pool
633 }
634 schedule; # sleep well
635 }
636 };
637 $unblock_scheduler->desc ("[unblock_sub scheduler]");
638
639 sub unblock_sub(&) {
640 my $cb = shift;
641
642 sub {
643 unshift @unblock_queue, [$cb, @_];
644 $unblock_scheduler->ready;
645 }
646 }
647
648 =back
649
650 =cut
651
652 1;
653
654 =head1 BUGS/LIMITATIONS
655
656 This module is not perl-pseudo-thread-safe. You should only ever use this
657 module from the same thread (this requirement might be removed in the
658 future to allow per-thread schedulers, but Coro::State does not yet allow
659 this). I recommend disabling thread support and using processes, as this
660 is much faster and uses less memory.
661
662 =head1 SEE ALSO
663
664 Event-Loop integration: L<Coro::AnyEvent>, L<Coro::EV>, L<Coro::Event>.
665
666 Debugging: L<Coro::Debug>.
667
668 Support/Utility: L<Coro::Specific>, L<Coro::Util>.
669
670 Locking/IPC: L<Coro::Signal>, L<Coro::Channel>, L<Coro::Semaphore>, L<Coro::SemaphoreSet>, L<Coro::RWLock>.
671
672 IO/Timers: L<Coro::Timer>, L<Coro::Handle>, L<Coro::Socket>, L<Coro::AIO>.
673
674 Compatibility: L<Coro::LWP>, L<Coro::BDB>, L<Coro::Storable>, L<Coro::Select>.
675
676 XS API: L<Coro::MakeMaker>.
677
678 Low level Configuration, Coroutine Environment: L<Coro::State>.
679
680 =head1 AUTHOR
681
682 Marc Lehmann <schmorp@schmorp.de>
683 http://home.schmorp.de/
684
685 =cut
686