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

Comparing Coro/README (file contents):
Revision 1.22 by root, Mon Mar 16 22:22:12 2009 UTC vs.
Revision 1.25 by root, Tue Jun 30 08:28:55 2009 UTC

1NAME 1NAME
2 Coro - the only real threads in perl 2 Coro - the only real threads in perl
3 3
4SYNOPSIS 4SYNOPSIS
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
27DESCRIPTION 27DESCRIPTION
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
251 timezone and restore the previous value, respectively, the timezone 251 timezone and restore the previous value, respectively, the timezone
252 is only changes for the coro that installed those handlers. 252 is only changed for the coro that installed those handlers.
253 253
254 use POSIX qw(tzset); 254 use POSIX qw(tzset);
255 255
256 async { 256 async {
257 my $old_tz; # store outside TZ value here 257 my $old_tz; # store outside TZ value here
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
489 Wait for the specified rouse callback (or the last one that was 519 Wait for the specified rouse callback (or the last one that was
490 created in this coro). 520 created in this coro).
491 521
492 As soon as the callback is invoked (or when the callback was invoked 522 As soon as the callback is invoked (or when the callback was invoked
493 before "rouse_wait"), it will return the arguments originally passed 523 before "rouse_wait"), it will return the arguments originally passed
494 to the rouse callback. 524 to the rouse callback. In scalar context, that means you get the
525 *last* argument, just as if "rouse_wait" had a "return ($a1, $a2,
526 $a3...)" statement at the end.
495 527
496 See the section HOW TO WAIT FOR A CALLBACK for an actual usage 528 See the section HOW TO WAIT FOR A CALLBACK for an actual usage
497 example. 529 example.
498 530
499HOW TO WAIT FOR A CALLBACK 531HOW TO WAIT FOR A CALLBACK

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines