Re: RFC 185 (v3) Thread Programming Model

2000-09-27 Thread James Mastros

On Wed, Sep 27, 2000 at 05:29:22AM -, Perl6 RFC Librarian wrote:
   $ok = try $scalar;
   $ok = try @array
   $ok = try %hash;
   $ok = try sub;
I'd like to see a more specific name for these.  'try' is too useful a word
for core to gobble it up for everything (IMHO).  attempt_lock?  Or simply
put these in a module and not export try by default.  (Also, try might get
used for exceptions -- or would at least confuse people who expect it to be.)

Also, it'd be nice if an implicit join was done if a Thread was used in a
stringifying or numifying context.  That way, you could treat it as a
function call that ran until it was acatualy needed, as in $factorial27 =
async{factorial(27)}, and somewhere later in your program, did a computation
involving it.  At that point, $factorial27 would be forced to take a value
(to whit, 27!), and would no longer be a Thread.  OTOH, this is broken wrt
things not returning numbers or strings (IE lists or refs), and makes
numifying/stringifying a mutator, which is probably a Bad Thing.

However, it seems amazingly useful.  Hm, I think it could be done easily
enough as a wrapper class.

Sorry I didn't get these in on v2; I don't follow the perl6-language* lists
as well as I probably should.  Too much trafic, to little time.

-=- James Mastros

-- 
-BEGIN GEEK CODE BLOCK-
Version: 3.1
GUCS d--- s-:- a20 C++ UL+++@ P L++@ E-() N o? K? w@ M-- !V
PS++ PE Y+ PGP(-) t++@ 5+ X+++ R+ tv+ b+++ DI+ D+ G e++ h! r- y?
--END GEEK CODE BLOCK--



RFC 185 (v3) Thread Programming Model

2000-09-26 Thread Perl6 RFC Librarian

This and other RFCs are available on the web at
  http://dev.perl.org/rfc/

=head1 TITLE

Thread Programming Model

=head1 VERSION

  Maintainer: Steven McDougall [EMAIL PROTECTED]
  Date: 31 Aug 2000
  Last Modified: 26 Sep 2000
  Mailing List: [EMAIL PROTECTED]
  Number: 185
  Version: 3
  Status: Frozen

=head1 ABSTRACT

This RFC describes the programming interface to Perl6 threads. 
It documents the function calls, operators, classes, methods, or
whatever else the language provides for programming with threads.

=head1 CHANGES

=head2 v3

Frozen

=head2 v2

=over 4

=item * 

Added SYNOPSIS, and wrote a proper ABSTRACT

=item *

Detailed Casync 

=item *

Detailed sharing of lexicals between threads

=item *

Traded Mutexes back for Clock, Ctry, and Cunlock

=item *

Pushed CSemaphore, CEvent, and CTimer down into CThread::

=item *

Specified readable, writable and failure to return Events

=item *

Reworked the wait functions

=item *

Added CQueue

=back


=head1 FREEZE

There was little, if any, further discussion after version 2.


=head1 SYNOPSIS

  use Thread;
  
  $sub = sub { ... };  
  
  $thread  = new Thread \func   , @args;
  $thread  = new Thread  $sub, @args;
  $thread  = new Thread   sub { ... }, @args;
async { ... };
  $result  = join $thread;
   
  $thread  = this Thread;
  @threads = all  Thread;
  
  $thread1 == $thread2 and ...
  Thread::yield();

  critical { ... };   # one thread at a time in this block
  
  lock $scalar;
  lock @array
  lock %hash;
  lock sub;
  
  $ok = try $scalar;
  $ok = try @array
  $ok = try %hash;
  $ok = try sub;
  
  unlock $scalar;
  unlock @array
  unlock %hash;
  unlock sub;
  
  $event = auto   Thread::Event; 
  $event = manual Thread::Event;
   set$event;
   reset  $event;
   wait   $event;
  
   $semaphore = new Thread::Semaphore $initial;
  $ok= $semaphore-up($n);
   $semaphore-down;
  $count = $semaphore-count;
  
  $timer = Thread::Timer-delay($seconds);
  $timer = Thread::Timer-alarm($time);
  $timer-wait;
  
  $event = $fh-readable
  $event = $fh-writable
  $event = $fh-failure
  
  $ok = wait_all(@references);
  $i  = wait_any(@references);
  
   $queue = new Thread::Queue
   $queue-enqueue($a);
  $a = $queue-dequeue;
  $empty = $queue-empty;


=head1 DESCRIPTION

=head2 Thread

=over 4

=item I$thread = Cnew CThread \Ifunc, I@args

Executes Ifunc(I@args) in a separate thread. The return value is
a reference to the CThread object that manages the thread.

The subroutine executes in its enclosing lexical context. This means
that lexical variables declared in that context may be shared between
threads. See RFC 178 for examples.


=item I$thread = Cnew CThread I$sub, I@args

=item I$thread = Cnew CThread Csub { ... }, I@args

Executes an anonymous subroutine in a separate thread, passing it
I@args. The return value is a reference to the CThread object that
manages the thread.

The subroutine is a closure. References to variables in its lexical
context are bound when the Csub operator executes. See RFC 178 for
examples.


=item Casync BLOCK

Executes BLOCK in a separate thread. Syntactically, Casync BLOCK
works like Cdo BLOCK. Casync creates a CThread object to manage
the thread, but it does not return a reference to it. If you want the
CThread object, use one of the Cnew CThread forms shown above.

The BLOCK executes in its enclosing lexical context. This means that
lexical variables declared in that context may be shared between
threads.


=item I$thread = Cthis CThread

Returns a reference to the CThread object that manages the current
thread.


=item I@threads = Call CThread

Returns a list of references to all existing CThread objects in the
program. This includes CThread objects created for Casync blocks.


=item I$result = Cjoin I$thread

=item I@result = Cjoin I$thread

Blocks until I$thread terminates. May be called repeatedly,
by any number of threads.

Returns the last expression evaluated in I$thread. This expression
is evaluated in list context inside the thread.

If Cjoin is called in list context, it returns the entire list; if
Cjoin is called in scalar context, it returns the first element of
the list.


=item I$thread1 == I$thread2

Evaluates to true iff I$thread1 and I$thread2 reference the same
CThread object.


=item CThread::yield()

Gives the interpreter an opportunity to switch to another thread. The
interpreter is not obligated to take this opportunity, and the calling
thread may regain control after an arbitrarily short period of time.

=back


=head2 Critical section

Ccritical is a new keyword. Syntactically, it works like Cdo. 

  critical { ... }; 

The interpreter guarantees that only one thread at a time can execute
a Ccritical block.


=head2 Lock

=over 4

=item Clock I$scalar

=item Clock I@array

=item Clock I%hash

=item Clock Isub

Applies a lock to a variable.

If there are no locks applied to