1 | NAME |
1 | NAME |
2 | Coro - the only real threads in perl |
2 | Coro - the only real threads in perl |
3 | |
3 | |
4 | SYNOPSIS |
4 | SYNOPSIS |
5 | use Coro; |
5 | use Coro; |
6 | |
6 | |
7 | async { |
7 | async { |
8 | # some asynchronous thread of execution |
8 | # some asynchronous thread of execution |
9 | print "2\n"; |
9 | print "2\n"; |
10 | cede; # yield back to main |
10 | cede; # yield back to main |
11 | print "4\n"; |
11 | print "4\n"; |
12 | }; |
12 | }; |
13 | print "1\n"; |
13 | print "1\n"; |
14 | cede; # yield to coro |
14 | cede; # yield to coro |
15 | print "3\n"; |
15 | print "3\n"; |
16 | cede; # and again |
16 | cede; # and again |
17 | |
17 | |
18 | # use locking |
18 | # use locking |
19 | use Coro::Semaphore; |
19 | use Coro::Semaphore; |
20 | my $lock = new Coro::Semaphore; |
20 | my $lock = new Coro::Semaphore; |
21 | my $locked; |
21 | my $locked; |
22 | |
22 | |
23 | $lock->down; |
23 | $lock->down; |
24 | $locked = 1; |
24 | $locked = 1; |
25 | $lock->up; |
25 | $lock->up; |
26 | |
26 | |
27 | DESCRIPTION |
27 | DESCRIPTION |
28 | For a tutorial-style introduction, please read the Coro::Intro manpage. |
28 | For a tutorial-style introduction, please read the Coro::Intro manpage. |
… | |
… | |
238 | |
238 | |
239 | These functions implement the same concept as "dynamic-wind" in |
239 | These functions implement the same concept as "dynamic-wind" in |
240 | scheme does, and are useful when you want to localise some resource |
240 | scheme does, and are useful when you want to localise some resource |
241 | to a specific coro. |
241 | to a specific coro. |
242 | |
242 | |
243 | They slow down coro switching considerably for coros that use them |
243 | They slow down thread switching considerably for coros that use them |
|
|
244 | (about 40% for a BLOCK with a single assignment, so thread switching |
244 | (But coro switching is still reasonably fast if the handlers are |
245 | is still reasonably fast if the handlers are fast). |
245 | fast). |
|
|
246 | |
246 | |
247 | These functions are best understood by an example: The following |
247 | These functions are best understood by an example: The following |
248 | function will change the current timezone to |
248 | function will change the current timezone to |
249 | "Antarctica/South_Pole", which requires a call to "tzset", but by |
249 | "Antarctica/South_Pole", which requires a call to "tzset", but by |
250 | using "on_enter" and "on_leave", which remember/change the current |
250 | using "on_enter" and "on_leave", which remember/change the current |
… | |
… | |
273 | }; |
273 | }; |
274 | |
274 | |
275 | This can be used to localise about any resource (locale, uid, |
275 | This can be used to localise about any resource (locale, uid, |
276 | current working directory etc.) to a block, despite the existance of |
276 | current working directory etc.) to a block, despite the existance of |
277 | other coros. |
277 | other coros. |
|
|
278 | |
|
|
279 | Another interesting example implements time-sliced multitasking |
|
|
280 | using interval timers (this could obviously be optimised, but does |
|
|
281 | the job): |
|
|
282 | |
|
|
283 | # "timeslice" the given block |
|
|
284 | sub timeslice(&) { |
|
|
285 | use Time::HiRes (); |
|
|
286 | |
|
|
287 | Coro::on_enter { |
|
|
288 | # on entering the thread, we set an VTALRM handler to cede |
|
|
289 | $SIG{VTALRM} = sub { cede }; |
|
|
290 | # and then start the interval timer |
|
|
291 | Time::HiRes::setitimer &Time::HiRes::ITIMER_VIRTUAL, 0.01, 0.01; |
|
|
292 | }; |
|
|
293 | Coro::on_leave { |
|
|
294 | # on leaving the thread, we stop the interval timer again |
|
|
295 | Time::HiRes::setitimer &Time::HiRes::ITIMER_VIRTUAL, 0, 0; |
|
|
296 | }; |
|
|
297 | |
|
|
298 | &{+shift}; |
|
|
299 | } |
|
|
300 | |
|
|
301 | # use like this: |
|
|
302 | timeslice { |
|
|
303 | # The following is an endless loop that would normally |
|
|
304 | # monopolise the process. Since it runs in a timesliced |
|
|
305 | # environment, it will regularly cede to other threads. |
|
|
306 | while () { } |
|
|
307 | }; |
278 | |
308 | |
279 | killall |
309 | killall |
280 | Kills/terminates/cancels all coros except the currently running one. |
310 | Kills/terminates/cancels all coros except the currently running one. |
281 | |
311 | |
282 | Note that while this will try to free some of the main interpreter |
312 | Note that while this will try to free some of the main interpreter |