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

Comparing Coro/Coro.pm (file contents):
Revision 1.246 by root, Mon Dec 15 00:30:40 2008 UTC vs.
Revision 1.247 by root, Mon Dec 15 02:07:11 2008 UTC

314 314
315=item terminate [arg...] 315=item terminate [arg...]
316 316
317Terminates the current coroutine with the given status values (see L<cancel>). 317Terminates the current coroutine with the given status values (see L<cancel>).
318 318
319=item Coro::on_enter BLOCK, Coro::on_leave BLOCK
320
321These function install enter and leave winders in the current scope. The
322enter block will be executed when on_enter is called and whenever the
323current coroutine is re-entered by the scheduler, while the leave block is
324executed whenever the current coroutine is blocked by the scheduler, and
325also when the containing scope is exited (by whatever means, be it exit,
326die, last etc.).
327
328I<Neither invoking the scheduler, nor exceptions, are allowed within those
329BLOCKs>. That means: do not even think about calling C<die> without an
330eval, and do not even think of entering the scheduler in any way.
331
332Since both BLOCKs are tied to the current scope, they will automatically
333be removed when the current scope exits.
334
335These functions implement the same concept as C<dynamic-wind> in scheme
336does, and are useful when you want to localise some resource to a specific
337coroutine.
338
339They slow down coroutine switching considerably for coroutines that use
340them (But coroutine switching is still reasonably fast if the handlers are
341fast).
342
343These functions are best understood by an example: The following function
344will change the current timezone to "Antarctica/South_Pole", which
345requires a call to C<tzset>, but by using C<on_enter> and C<on_leave>,
346which remember/change the current timezone and restore the previous
347value, respectively, the timezone is only changes for the coroutine that
348installed those handlers.
349
350 use POSIX qw(tzset);
351
352 async {
353 my $old_tz; # store outside TZ value here
354
355 Coro::on_enter {
356 $old_tz = $ENV{TZ}; # remember the old value
357
358 $ENV{TZ} = "Antarctica/South_Pole";
359 tzset; # enable new value
360 };
361
362 Coro::on_leave {
363 $ENV{TZ} = $old_tz;
364 tzset; # restore old value
365 };
366
367 # at this place, the timezone is Antarctica/South_Pole,
368 # without disturbing the TZ of any other coroutine.
369 };
370
371This can be used to localise about any resource (locale, uid, current
372working directory etc.) to a block, despite the existance of other
373coroutines.
374
319=item killall 375=item killall
320 376
321Kills/terminates/cancels all coroutines except the currently running 377Kills/terminates/cancels all coroutines except the currently running one.
322one. This can be useful after a fork, either in the child or the parent,
323as usually only one of them should inherit the running coroutines.
324 378
325Note that in the implementation, destructors run as normal, making this
326function not so useful after a fork. Future versions of this function
327might try to free resources without running any code.
328
329Note that while this will try to free some of the main programs resources, 379Note that while this will try to free some of the main interpreter
380resources if the calling coroutine isn't the main coroutine, but one
330you cannot free all of them, so if a coroutine that is not the main 381cannot free all of them, so if a coroutine that is not the main coroutine
331program calls this function, there will be some one-time resource leak. 382calls this function, there will be some one-time resource leak.
332 383
333=cut 384=cut
334 385
335sub killall { 386sub killall {
336 for (Coro::State::list) { 387 for (Coro::State::list) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines