… | |
… | |
66 | package Coro; |
66 | package Coro; |
67 | |
67 | |
68 | use strict qw(vars subs); |
68 | use strict qw(vars subs); |
69 | no warnings "uninitialized"; |
69 | no warnings "uninitialized"; |
70 | |
70 | |
|
|
71 | use Guard (); |
|
|
72 | |
71 | use Coro::State; |
73 | use Coro::State; |
72 | |
74 | |
73 | use base qw(Coro::State Exporter); |
75 | use base qw(Coro::State Exporter); |
74 | |
76 | |
75 | our $idle; # idle handler |
77 | our $idle; # idle handler |
… | |
… | |
312 | |
314 | |
313 | =item terminate [arg...] |
315 | =item terminate [arg...] |
314 | |
316 | |
315 | Terminates the current coroutine with the given status values (see L<cancel>). |
317 | Terminates the current coroutine with the given status values (see L<cancel>). |
316 | |
318 | |
|
|
319 | =item Coro::on_enter BLOCK, Coro::on_leave BLOCK |
|
|
320 | |
|
|
321 | These function install enter and leave winders in the current scope. The |
|
|
322 | enter block will be executed when on_enter is called and whenever the |
|
|
323 | current coroutine is re-entered by the scheduler, while the leave block is |
|
|
324 | executed whenever the current coroutine is blocked by the scheduler, and |
|
|
325 | also when the containing scope is exited (by whatever means, be it exit, |
|
|
326 | die, last etc.). |
|
|
327 | |
|
|
328 | I<Neither invoking the scheduler, nor exceptions, are allowed within those |
|
|
329 | BLOCKs>. That means: do not even think about calling C<die> without an |
|
|
330 | eval, and do not even think of entering the scheduler in any way. |
|
|
331 | |
|
|
332 | Since both BLOCKs are tied to the current scope, they will automatically |
|
|
333 | be removed when the current scope exits. |
|
|
334 | |
|
|
335 | These functions implement the same concept as C<dynamic-wind> in scheme |
|
|
336 | does, and are useful when you want to localise some resource to a specific |
|
|
337 | coroutine. |
|
|
338 | |
|
|
339 | They slow down coroutine switching considerably for coroutines that use |
|
|
340 | them (But coroutine switching is still reasonably fast if the handlers are |
|
|
341 | fast). |
|
|
342 | |
|
|
343 | These functions are best understood by an example: The following function |
|
|
344 | will change the current timezone to "Antarctica/South_Pole", which |
|
|
345 | requires a call to C<tzset>, but by using C<on_enter> and C<on_leave>, |
|
|
346 | which remember/change the current timezone and restore the previous |
|
|
347 | value, respectively, the timezone is only changes for the coroutine that |
|
|
348 | installed 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 | |
|
|
371 | This can be used to localise about any resource (locale, uid, current |
|
|
372 | working directory etc.) to a block, despite the existance of other |
|
|
373 | coroutines. |
|
|
374 | |
317 | =item killall |
375 | =item killall |
318 | |
376 | |
319 | Kills/terminates/cancels all coroutines except the currently running |
377 | Kills/terminates/cancels all coroutines except the currently running one. |
320 | one. This can be useful after a fork, either in the child or the parent, |
|
|
321 | as usually only one of them should inherit the running coroutines. |
|
|
322 | |
378 | |
323 | Note that in the implementation, destructors run as normal, making this |
|
|
324 | function not so useful after a fork. Future versions of this function |
|
|
325 | might try to free resources without running any code. |
|
|
326 | |
|
|
327 | Note that while this will try to free some of the main programs resources, |
379 | Note that while this will try to free some of the main interpreter |
|
|
380 | resources if the calling coroutine isn't the main coroutine, but one |
328 | you cannot free all of them, so if a coroutine that is not the main |
381 | cannot free all of them, so if a coroutine that is not the main coroutine |
329 | program calls this function, there will be some one-time resource leak. |
382 | calls this function, there will be some one-time resource leak. |
330 | |
383 | |
331 | =cut |
384 | =cut |
332 | |
385 | |
333 | sub killall { |
386 | sub killall { |
334 | for (Coro::State::list) { |
387 | for (Coro::State::list) { |