19.11.2014 Views

The Fortress Language Specification - CiteSeerX

The Fortress Language Specification - CiteSeerX

The Fortress Language Specification - CiteSeerX

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

(discussed in Section 13.13) to a surrounding label (the label is considered to be out of scope), but may fail to catch an<br />

exception and thus complete abruptly. When this happens, the exception is deferred. Any invocation of the val method<br />

on the spawned thread throws the deferred exception. If the spawned thread object is discarded, the exception will be<br />

silently ignored. In particular, if the result of a spawn expression is dropped, it is impossible to detect completion of<br />

the spawned thread or to recover from any deferred exceptions.<br />

<strong>The</strong> wait method waits until the thread is complete and then returns the void value. <strong>The</strong> ready method does not wait,<br />

but instead returns a boolean value indicating whether the thread is complete. Invocations of these methods do not<br />

cause deferred exceptions to be thrown.<br />

We say a spawned thread has been observed to complete after invoking the val or wait methods, or when an invocation<br />

of the ready method returns true .<br />

<strong>The</strong>re are three ways in which a thread can be suspended. First, a thread that begins evaluating an implicitly parallel<br />

construct is suspended until the thread group has completed. Second, a thread that invokes val or wait is suspended<br />

until the spawned thread is complete. Finally, invoking the abort function from within an atomic expression may<br />

cause a thread to suspend; see Section 32.3.<br />

Threads in a <strong>Fortress</strong> program can perform operations simultaneously on shared objects. In order to synchronize data<br />

accesses, <strong>Fortress</strong> provides atomic expressions (see Section 13.23). Chapter 21 describes the memory model which<br />

is obeyed by <strong>Fortress</strong> programs.<br />

Different implicit threads (even different iterations of the same loop body) may execute in very different amounts<br />

of time. A naively parallelized loop will cause processors to idle until every iteration finishes. <strong>The</strong> simplest way to<br />

mitigate this delay is to expose substantially more parallel work than the number of underlying processors available to<br />

run them. Load balancing can move the resulting (smaller) units of work onto idle processors to balance load.<br />

<strong>The</strong> ratio between available work and number of threads is called parallel slack [3, 4]. With support for very<br />

lightweight threading and load balancing, slack in hundreds or thousands (or more) proves beneficial; very slack<br />

computations easily adapt to differences in the number of available processors. Slack is a desirable property, and the<br />

<strong>Fortress</strong> programmer should endeavor to expose parallelism where possible.<br />

4.5 Environments<br />

An environment maps names to values or locations. Environments are immutable, and two environments that map<br />

exactly the same names to the same values or locations are identical.<br />

A program starts executing with an empty environment. Environments are extended with new mappings by variable,<br />

function, and object declarations, and functional calls (including calls to object constructors). After initializing all<br />

top-level variables and singleton objects as described in Section 22.6, the top-level environment for each component is<br />

constructed.<br />

<strong>The</strong> environment of a value is determined by how it is constructed. For all but object expressions, function expressions<br />

and local function declarations, the environment of the constructed value is the top-level environment of the component<br />

in which the expression or declaration occurs. For object and function expressions and local function declarations, the<br />

environment of the constructed value is the lexical environment in which the expression or declaration was evaluated.<br />

We carefully distinguish a spawned thread from its associated spawned thread object. In particular, note that the<br />

execution environment of a spawned thread, in which the body expression is evaluated, is distinct from the environment<br />

of the associated thread object, in which calls to the thread methods are evaluated.<br />

39

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

Saved successfully!

Ooh no, something went wrong!