Docsity

Java Memory Model: Understanding Shared Memory and Data Races - Prof. John M. Mellor-Crumm, Study notes of Computer Science

Partial preview of the text

Download Java Memory Model: Understanding Shared Memory and Data Races - Prof. John M. Mellor-Crumm and more Study notes Computer Science in PDF only on Docsity!

John Mellor-Crummey

Department of Computer Science

Rice University

[email protected]

Java Memory Model

COMP 522 Lecture 12 13 October 2008

Why a Memory Model for Java?

  • Java supports threads that shared memory
  • Need memory model to define program semantics
  • Key issues
    • strong safety and security properties

Example: Double-Checked Locking class Foo { private Helper helper = null ; public Helper getHelper() { if (helper == null ) { synchronized ( this ) { if (helper == null ) { helper = new Helper(); } } } return helper; } ... }

  • Thread A sees null helper; acquires lock and begins initialization
  • Compiler might set helper before A finishes constructor
  • Thread B sees non-null helper and returns value

Fixing Double-Checked Locking? class Foo { private Helper helper = null; public Helper getHelper() { if (helper == null ) { Helper h; synchronized(this) { h = helper; if (h == null ) synchronized (this) { h = new Helper(); } // release inner synchronization lock helper = h; } } return helper; } ... } still broken: helper=h may be moved into inner synchronized block & executed before initialization is complete

Today’s Paper: Not The First JMM ... Original Memory Model for Java (JMM 1.0)

  • Java language specification [Gosling, Joy, Steele]
    • hard to interpret
    • poorly understood
    • had many problems ...
  • One feature: JMM 1.0 required “coherence”
    • informally, for each variable in isolation, uses and assigns to that variable must appear as if they acted directly on global memory in some order that respects the order within each thread - each variable in isolation is sequentially consistent

JMM 1.0 “Fatally Flawed” [Pugh, CPE00]

  • Imposed constraints that prohibited common compiler optimizations - e.g. coherence leads to “reads that kill”
  • Expensive to implement on existing hardware
  • Most JVMs violated constraints in the model
  • Conforming to the specification would impose significant performance penalties - enforcing “reads kill” in the compiler yielded programs 20-45% slower [Scales, 99] could see new value must new value too if q.x does

Why Not Sequential Consistency?

  • Easiest to understand
  • Programmer can write concurrent algorithms, but ...
    • requires great skill
    • however, algorithms that exploit SC are often incorrect
    • such programming should be discouraged!
  • SC restricts ordering pairs of memory ops in a thread
    • even if no data and control dependences
    • no compiler or processor reorderings should be visible
  • SC can be a serious obstacle to performance
    • e.g. even common subexpression elimination is illegal

Java Memory Model

  • Relaxed memory model
  • Adopts data race free model
    • correct programs are free of data races
    • such programs are guaranteed sequential consistency
  • What about programs with races?
    • must their behaviors be defined? yes!
  • • Why define behaviors for programs with races?Why define behaviors for programs with races?
  • leaving semantics unspecified allows violation of safety and security properties

Java Memory Model Benefits

  • Fully specified memory semantics
    • guarantees safety and security
  • Flexible enough to permit
    • performance (current and future)
    • programmer sanity

Terminology I

  • Conflicting accesses
    • two accesses to same variable, at least one is a write
  • Synchronization actions
    • lock/unlock, read/write of volatile variables
  • Synchronization order consistency
    • total order over all synchronization actions
      • must be consistent with program order
    • read of volatile must return value of last write in synch order

Happens-Before Model

  • Key question: What values can a read see?

Happens-Before Model

  • Key question: What values can a read see?
  • An execution → sync order (total order)

Happens-Before Model

  • Key question: What values can a read see?
  • An execution → sync order (total order)
  • Our interest: sync-with order (partial order)
    • synchronization actions induce sync-with edges
    • W( v ) syncs-with all subsequent R( v ) by any thread
    • partial because we don’t care about R(v)→R(v’)
    • unlock(l) syncs-with all subsequent lock(l) by any thread
    • many such orders
  • Happens-before partial order: transitive closure of
    • sync-with edges
    • program-order edges

Happens-Before Model

  • Key question: What values can a read see?
  • An execution → sync order (total order)
  • Our interest: sync-with order (partial order)
    • synchronization actions induce sync-with edges
    • W( v ) syncs-with all subsequent R( v ) by any thread
    • partial because we don’t care about R(v)→R(v’)
    • unlock(l) syncs-with all subsequent lock(l) by any thread
    • many such orders
  • Happens-before partial order: transitive closure of
    • sync-with edges
    • program-order edges
  • A well-formed execution must obey:
    • happens-before consistency: R( v ), v is ordinary
    • sync-order consistency: R( v ), v is volatile