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.

y evaluating the two elements of the tuple) will always run in parallel; it is possible for the first element of the tuple<br />

to continually abort without ever running the second element of the tuple:<br />

r : Z64 := 0<br />

(a, b) = (atomic if r = 1 then 17 else abort() end,<br />

do r := 1; r end)(∗ INCORRECT! ∗)<br />

<strong>Fortress</strong> also includes a tryatomic expression, which attempts to run its body expression atomically. If it succeeds,<br />

the result is returned; if it aborts due to a call to abort , the AtomicAborted exception is thrown; if it aborts due to<br />

conflict (as described in Section 13.23), the AtomicConflict exception is thrown. <strong>The</strong>se exceptions both implement<br />

the AtomicFailed trait, which is an instance of CheckedException. Conceptually atomic can be defined in terms<br />

of tryatomic as follows:<br />

label AtomicBlock<br />

while true do<br />

try<br />

result = tryatomic body<br />

exit AtomicBlock with result<br />

catch e<br />

AtomicFailed ⇒ ()(∗ continue execution ∗)<br />

end<br />

end<br />

throw UnreachableCode(∗ inserted for type correctness ∗)<br />

end AtomicBlock<br />

Unlike the above definition, an implementation may choose to suspend a thread running an atomic expression which<br />

invokes abort , re-starting it at a later time when it may be possible to make further progress. <strong>The</strong> above definition<br />

restarts the body of the atomic expression immediately without suspending.<br />

32.4 Shared and Local Data<br />

Every object in a <strong>Fortress</strong> program is considered to be either shared or local (collectively referred to as the sharedness<br />

of the object). A local object must be transitively reachable (through zero or more object references) from the variables<br />

of at most one running thread. A local object may be accessed more cheaply than a shared object, particularly in the<br />

case of atomic reads and writes. Sharedness is ordinarily managed implicitly by the <strong>Fortress</strong> implementation. Control<br />

over sharedness is intended to be a performance optimization; however, methods such as isShared and localize can<br />

affect program semantics, and must be used with care.<br />

<strong>The</strong> sharedness of an object should be contrasted with its region. <strong>The</strong> region of an object describes where that object<br />

is located on the machine. <strong>The</strong> sharedness of an object describes whether the object is visible to one thread or to many.<br />

A local object need not actually reside in a region near the thread to which it is visible (though ordinarily it will).<br />

<strong>The</strong> following rules govern sharedness:<br />

• Reference objects are initially local when they are constructed.<br />

• <strong>The</strong> sharedness of an object may change as the program executes.<br />

• If an object is currently transitively reachable from more than one running thread, it must be shared.<br />

• When a reference to a local object is stored into a field of a shared object, the local object must be published:<br />

Its sharedness is changed to shared, and all of the data to which it refers is also published.<br />

• <strong>The</strong> value of a local variable referenced by a thread must be published before that thread may be run in parallel<br />

with the thread which created it. Values assigned to the variable while the threads run in parallel must also be<br />

published.<br />

217

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

Saved successfully!

Ooh no, something went wrong!