Changeset 20085 for docs/Perl6/Spec

Show
Ignore:
Timestamp:
03/08/08 00:17:16 (9 months ago)
Author:
buchetc
Message:

[t/spec] document coroutines

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • docs/Perl6/Spec/Concurrency.pod

    r20069 r20085  
    245245the use of named parameters is therefore advisable. 
    246246 
    247 =head2 Examples 
     247=head2 Coroutine attributes 
     248 
     249=over 
     250 
     251=item finished 
     252 
     253True on return() and false on midway yield() 
     254 
     255=back 
     256 
     257=head2 Coroutine methods 
     258 
     259=over 
     260 
     261=item start 
     262 
     263Set starting parameter list. 
     264 
     265=back 
     266 
     267=head2 Coroutine examples 
    248268 
    249269=over 
     
    254274  (1..4).map:{ dbl($_) } 
    255275  # should result in 2 2 6 4 
     276 
     277  # coro parameters 
     278  coro perm ( @x ) { 
     279     my @y = @x; # need a copy 
     280     while @x { @y.splice($_,1).yield; } 
     281  } 
    256282 
    257283=item Constant coro 
     
    274300=head1 Threads 
    275301 
    276 =head2 Thread methods and attributes 
    277  
    278 =over 
    279  
    280 =item Create 
     302=head2 Thread creation 
    281303 
    282304A thread will be created using the keyword C<async> followed by 
     
    288310   }; 
    289311 
     312 
     313=head2 Thread attributes 
     314 
     315=over 
     316 
    290317=item Self reflection 
     318 
     319TODO: how you can access thread attributes inside a thread 
    291320 
    292321   async { 
     
    294323   }; 
    295324 
    296 =item TODO 
    297  
    298  - numify to TIDs (as in pugs) 
    299  - stringify to something sensible (eg. "<Conc:tid=5>"); 
    300  - enumerable with Conc.list 
    301  - Conc.yield (if this is to live but deprecated, maybe call it sleep(0)?) 
    302  - sleep() always respects other threads, thank you very much 
    303  - standard methods: 
    304     - .join    # wait for invocant to finish (always item cxt) 
    305     - .die     # throw exception in the invocant thread 
    306     - .alarm   # set up alarms 
    307     - .alarms  # query existing alarms 
    308     - .suspend # pause a thread; fail if already paused 
    309     - .resume  # revive a thread; fail if already running 
    310     - .detach  # survives parent thread demise (promoted to process) 
    311                # process-local changes no longer affects parent 
    312                # tentatively, the control methods still applies to it 
    313                # including wait (which will always return undef) 
    314                # also needs to discard any atomicity context 
    315  - attributes: 
    316     - .started  # time 
    317     - .finished # time 
    318     - .waiting  # suspended (not diff from block on wakeup signal) 
    319                 # waiting on a handle, a condition, a lock, et cetera 
    320                 # otherwise returns false for running threads 
    321                 # if it's finished then it's undef(?) 
    322     - .current_continuation 
    323                 # the CC currently running in that thread 
    324  
    325  - "is throttled" trait 
     325=item started 
     326 
     327start time 
     328 
     329=item finished 
     330 
     331end time 
     332 
     333=item waiting 
     334 
     335suspended (not diff from block on wakeup signal) 
     336waiting on a handle, a condition, a lock, et cetera 
     337otherwise returns false for running threads 
     338if it's finished then it's undef(?) 
     339 
     340=item current_continuation 
     341 
     342the CC currently running in that thread 
     343 
     344=back 
     345 
     346=head2 Thread operators 
     347 
     348=over 
     349 
     350=item Stringify 
     351 
     352Stringify to something sensible (eg. "<Conc:tid=5>"); 
     353 
     354  my $thr = async { ... }; 
     355  say ~$thr; 
     356 
     357=item Numerify 
     358 
     359Numify to TIDs (as in pugs) 
     360 
     361  my $thr = async { ... }; 
     362  say +$thr; 
     363 
     364=item Enumerable 
     365 
     366TODO: Enumerable with Conc.list 
     367 
     368=back 
     369 
     370=head2 Thread methods 
     371 
     372=over 
     373 
     374=item yield 
     375 
     376TODO: Conc.yield (if this is to live but deprecated, maybe call it sleep(0)?) 
     377 
     378=item sleep 
     379 
     380sleep() always respects other threads, thank you very much 
     381 
     382=item join 
     383 
     384wait for invocant to finish (always item cxt) 
     385 
     386  my $thr = async { ... }; 
     387  $thr.join(); 
     388 
     389=item die 
     390 
     391throw exception in the invocant thread 
     392 
     393=item alarm 
     394 
     395set up alarms 
     396 
     397=item alarms 
     398 
     399query existing alarms 
     400 
     401=item suspend 
     402 
     403pause a thread; fail if already paused 
     404 
     405=item resume 
     406 
     407revive a thread; fail if already running 
     408 
     409=item detach 
     410 
     411survives parent thread demise (promoted to process) 
     412process-local changes no longer affects parent 
     413tentatively, the control methods still applies to it 
     414including wait (which will always return undef) 
     415also needs to discard any atomicity context 
     416 
     417=item "is throttled" trait 
     418 
     419TODO: 
    326420 
    327421    method throttled::trait_auxiliary:<is> ($limit=1, :$key=gensym()) { 
     
    500594- Coros are _like_ processes 
    501595 
    502   coro perm (@x) { 
    503      @x.splice(rand(@x),1).yield while @x; 
    504   } 
    505   my &p1 := &perm.start(1..10); 
    506   my &p2 := &perm.start(1..20); 
    507   p1(); p1(); 
    508   p2(); p2(); 
    509596 
    510597  coro foo ($x) { 
     
    520607    # return() means yielding and restart + no implicit falloff (I LIKE THIS) 
    521608 
    522   &foo.finished; # true on return() and false on midway yield() 
    523  
    524609  foo(4); # and that's all she wrote 
    525610 
     
    562647  %*ENV(123); 
    563648 
    564   &foo_continued := &foo.start(10); 
    565   &foo.start(20); 
    566  
    567   foo(10);    # returns 10 
    568  
    569   foo();      # be "insufficient param" error or just return 11? 
    570   foo(20);    # returns 21 
    571649 
    572650  # continuation coros