19.11.2014 Views

The Fortress Language Specification - CiteSeerX

The Fortress Language Specification - CiteSeerX

The Fortress Language Specification - CiteSeerX

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>The</strong> loop body reads a i and sum , then adds them and writes the result back to sum ; this will appear to occur<br />

atomically with respect to all other threads (including the other iterations of the loop body). Note in particular that the<br />

atomic expression will appear atomic with respect to reads and writes not in atomic expressions of a i and sum .<br />

13.24 Spawn Expressions<br />

Syntax:<br />

Flow ::= spawn Expr<br />

A thread can be created by a spawn expression. A spawn expression consists of the special reserved word spawn<br />

followed by an expression. A spawn expression spawns a thread which evaluates its subexpression in parallel with<br />

any succeeding evaluation. <strong>The</strong> value of a spawn expression is the spawned thread and the type of the expression is<br />

the Thread trait.<br />

<strong>The</strong> Thread trait has the following methods:<br />

• <strong>The</strong> val method returns the value computed by the subexpression of the spawn expression. If the thread has<br />

not yet completed execution, the invocation of val blocks until it has done so.<br />

• <strong>The</strong> wait method waits for a thread to complete, but does not return a value.<br />

• <strong>The</strong> ready method returns true if a thread has completed, and returns false otherwise.<br />

• <strong>The</strong> stop method attempts to terminate a thread as described in Section 32.6.<br />

In the absence of sufficient parallel resources, an attempt is made to run the subexpression of the spawn expression<br />

before continuing succeeding evaluation (so long as we have not specified a region for evaluation as described in<br />

Section 32.7, and we are not currently evaluating an atomic expression as described in Section 13.23). We can<br />

imagine that it is actually the rest of the evaluation after the parallel block which is spawned off in parallel. This is<br />

a subtle technical point, but makes the sequential execution of parallel code simpler to understand, and avoids subtle<br />

problems with the asymptotic stack usage of parallel code [19, 11].<br />

13.25 Throw Expressions<br />

Syntax:<br />

Flow ::= throw Expr<br />

A throw expression consists of the special reserved word throw followed by a subexpression. <strong>The</strong> subexpression<br />

must have the type Exception (see Chapter 14). A throw expression evaluates its subexpression to an exception<br />

value and throws the exception value; the expression completes abruptly and has BottomType.<br />

<strong>The</strong> type Exception has exactly two direct mutually exclusive subtypes, CheckedException and UncheckedException.<br />

Every CheckedException that is thrown must be caught or forbidden by an enclosing try expression (see Section<br />

13.26), or it must be declared in the throws clause of an enclosing functional declaration (see Section 12.1).<br />

Similarly, every CheckedException declared to be thrown in the static type of a functional called must be either<br />

caught or forbidden by an enclosing try expression, or declared in the throws clause of an enclosing functional<br />

declaration.<br />

108

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!