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

Comparing Coro/README (file contents):
Revision 1.21 by root, Mon Dec 15 20:52:04 2008 UTC vs.
Revision 1.24 by root, Tue Jun 23 23:40:06 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
303 333
304 This ensures that the scheduler will resume this coro automatically 334 This ensures that the scheduler will resume this coro automatically
305 once all the coro of higher priority and all coro of the same 335 once all the coro of higher priority and all coro of the same
306 priority that were put into the ready queue earlier have been 336 priority that were put into the ready queue earlier have been
307 resumed. 337 resumed.
338
339 $coro->suspend
340 Suspends the specified coro. A suspended coro works just like any
341 other coro, except that the scheduler will not select a suspended
342 coro for execution.
343
344 Suspending a coro can be useful when you want to keep the coro from
345 running, but you don't want to destroy it, or when you want to
346 temporarily freeze a coro (e.g. for debugging) to resume it later.
347
348 A scenario for the former would be to suspend all (other) coros
349 after a fork and keep them alive, so their destructors aren't
350 called, but new coros can be created.
351
352 $coro->resume
353 If the specified coro was suspended, it will be resumed. Note that
354 when the coro was in the ready queue when it was suspended, it might
355 have been unreadied by the scheduler, so an activation might have
356 been lost.
357
358 To avoid this, it is best to put a suspended coro into the ready
359 queue unconditionally, as every synchronisation mechanism must
360 protect itself against spurious wakeups, and the one in the Coro
361 family certainly do that.
308 362
309 $is_ready = $coro->is_ready 363 $is_ready = $coro->is_ready
310 Returns true iff the Coro object is in the ready queue. Unless the 364 Returns true iff the Coro object is in the ready queue. Unless the
311 Coro object gets destroyed, it will eventually be scheduled by the 365 Coro object gets destroyed, it will eventually be scheduled by the
312 scheduler. 366 scheduler.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines