This draft document is a paste together from various sources. The bulk of it is simply the old S17-concurrency.pod, which dealt only with concurrency. Signals were added from S16-io, but haven't been merged with the conflicting S17 signals doco. An event loop section has been added here because a) Larry mentioned the idea, and b) Moritz suggested that http://www.seas.upenn.edu/~lipeng/homepage/unify.html be our model for concurrency, and in that model, an event loop underlies the threads.

An event loop underlies everything in this document. POSIX signals can interact with this, and concurrency is built on top of it. Naturally, IPC (inter-process communication) is documented here too (XXX or should be :) ).

class Proc::Signals::Signal {
has $exception; # This specifies what exception will be raised when this signal is received
has $interrupt; # See siginterrupt(3)
has $blocked; # Is this signal blocked? cf. sigprocmask
}

The @*SIGQUEUE array contains a queue of the signals that are blocked and queued.

The standard POSIX signals simply raise control exceptions that are handled as normal through the control signal handler, and caught by CONTROL blocks, as specified in S04.

To declare your main program catches INT signals, put a CONTROL block anywhere in the toplevel to handle exceptions like this:

Term Default action is to terminate the process.
Ign Default action is to ignore the signal ($signal.exception is undefined by default)
Core Default action is to terminate the process and dump core (see core(5)).
Stop Default action is to stop the process.
Cont Default action is to continue the process if it is currently stopped.

Some actions do the Resumeable role. An exception listed in the table below that does the Resumeable role is marked with a * in the R column.

XXX I'm unsure how the actions in the table above can be made to make sense. The Ign actions are already dealt with because %SIG{CHLD}.exception already defaults to undefined. The Term action will probably be self-solving (ie. will terminate the process). The others I'm just plain unsure about. XXX

If you request a repeated alarm using the repeated named argument, it will attempt to fire off the alarm that many times. However, the alarm will be suppressed when inside a CATCH block that's already handling the exception raised by same alarm.

To repeat 0 times is to not fire off any alarms at all. To repeat +Inf times is to repeat over and over again.

You can arrange a callback (like JavaScript's setTimeOut) in alarm, which will then be invoked with the then-current code as caller.

If you set up such a callback to another Conc object, what happens is just like when you called .die on behalf of that object -- namely, the callback closure, along with anything it referenced, is shared to the target Conc context.

Unlike in Perl 5's ithreads where you cannot share anything after the fact, this allows passing shared objects in an ad-hoc fashion across concurrent parts of the program. Under the default (multiplexing) concurrency model, this is basically a no-op.

This is a draft document. After being some time under the surface of Perl 6 development this is a attempt to document working concurrency issues, list the remaining todos and mark the probably obsolete and redundant points.

Concurrency can take many forms in Perl 6. With varying degrees of explicitness and control capabilities. This document attempts to describe what these capabilities are and in which form they can be accessed in Perl 6.

Concurrency comes in many shapes and forms. Most Perl users are used to the concept of a "process" or a "thread" (usually depending on the OS they work on). Some systems even are familiar with very lightweight threads called "fibers".

When discussing issues about concurrency with different people, it soon becomes apparent that everybody has his own set of "understandings" about what each word means, which doesn't make it any easier to describe Perl 6 concurrency.

It seemed the most natural to use the word "thread" to describe a process which has its own context, but also shares context with 0 or more concurrently running processes. Depending on your OS, or even specific version of your OS, this could still be a single "process" from the OS's point of view. Or it could contain an OS process for each thread. Or any mixture of these two implementations.

In this document we try to be agnostic about this: all we know in Perl 6 are "threads", which have their own context and share context with other concurrently running "threads". Whether they be process, threads or fibres at the OS level should not matter at the Perl 6 level.

And for sake of consistency, an unthreaded "normal" program is considered to be also running in a single thread.

In the past, there have been two models for concurrent processes in Perl. In general, these are referred to as "5.005 threads" (perldoc perlothrtut) and "ithreads" (perldoc perlthrtut).

The main difference between these two models from a programmer's point of view, is that variables in "5.005 threads" are shared by default. Whereas in the "ithreads" model, only variables that have been indicated to be "shared", are actually shared between threads. All other variable values are actually copies of the variable's value in the "parent" thread.

With regards to variables, the concurrency model of Perl 6 is closer to the "5.005 threads" model than it is to the "ithreads" model. In fact, all variables "visible" to a particular scope in Perl 6 will be accessible and modifiable (if allowed to do so) from all of the concurrent processes that start from that scope. In that sense, one could consider the "ithreads" model as a historical diversion: the Perl 6 concurrency picks up where the "5.005 threads" path left off.

(EM: maybe point out that the "ithreads" behaviour can be simulated with some kind of copy-on-write magic to be automagically added to all variable access inside a thread, except for those with an explicit "is shared" attribute?)

Differently from any current concurrent process implementation in Perl, there are no user accessible locks. Instead, the concept of Software Transactional Memory is used. This is in concept similar to the use of

BEGIN TRANSACTION
... do your uninterruptible actions
COMMIT

in the database world. More interestingly, this also includes the concept of rollback:

The defer function basically restores the state of the thread at the last checkpoint and will wait there until an external event allows it to potentially run that atomic contend section of code again without having to defer again.

If there are no external events possible that could restart execution, an exception will be raised.

The last checkpoint is either the outermost contend boundary, or the most immediate caller constructed with maybe.

Because Perl 6 must be able to revert its state to the state it had at the checkpoint, it is not allowed to perform any non-revertible actions. These would include reading / writing from file handles that do not support seek (such as sockets). Attempting to do so will cause a fatal error to occur.

This will probably need to be expanded to all objects: any object that has some interface with data "outside" of the knowledge of the language (e.g. an interface with an external XML library) would also need to provide some method for freezing a state, and restoring to a previously frozen state.

If you're not interested in revertability, but are interested in uninterruptability, you could use the "is critical" trait.

sub tricky is critical {
# code accessing external info, not to be interrupted
}
if ($update) {
also is critical;
# code accessing external info, not to be interrupted
}

A Code block marked "is critical" can not be interrupted in any way. But since it is able to access non-revertible data structures (such as non-seekable file handles), it cannot do a defer as it would be impossible to restore the state to the beginning of the Code block.

Both "atomic" as well as "critical" propagate down the call chain. This means that any subroutine that in itself is not "atomic" or "critical" becomes uninterruptible if called inside a code block that is marked as "atomic" or "critical".

Atomic Code blocks called inside the call chain of a "critical" code block do not pose a problem, as they are more restrictive.

Any code that attempts to perform any non-revertible action (e.g. reading from a socket) will cause a fatal error when called inside the call chain of an Atomic Code block.

survives parent thread demise (promoted to process) process-local changes no longer affects parent tentatively, the control methods still applies to it including wait (which will always return Nil) also needs to discard any atomicity context