Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Minor edits after a read through.

...

    T-save                             ObjectMonitor              T-deflate
    ------------------------------- -- +-----------------------+  ------------------------------------------
    save_om_ptr() {   | owner=DEFLATER_MARKER |  deflate_monitor_using_JT() {
    atomic inc ref_count    | ref_count=-max_jint+1 |  cmpxchg(DEFLATER_MARKER, &owner, NULL)
   1> if (owner == DEFLATER_MARKER && +-----------------------+  :
    ref_count <= 0) {                 ||              prev = cmpxchg(-max_jint, &ref_count, 0)
        restore obj header             \/          1> if (prev == 0 &&
      atomic dec ref_count           +-----------------------+        owner == DEFLATER_MARKER) {
     2> return false to force retry    | owner=DEFLATER_MARKER | restore obj header
    }                                | ref_count=-max_jint |   2> finish the deflation
+-----------------------+ }

...

    T-save                             ObjectMonitor              T-deflate
    --------------------------------- +-----------------------+  ------------------------------------------
    save_om_ptr() { | owner=DEFLATER_MARKER |  deflate_monitor_using_JT() {
    atomic inc ref_count    | ref_count=1          |  cmpxchg(DEFLATER_MARKER, &owner, NULL)
   1> if (owner == DEFLATER_MARKER && +-----------------------+  :
    ref_count <= 0) {              ||                prev = cmpxchg(-max_jint, &ref_count, 0)
    } else {        \/         1> if (prev == 0 &&
    save om_ptr in the          +-----------------------+         owner == DEFLATER_MARKER) {
ObjectMonitorHandle | owner=NULL | } else {
2> return true | ref_count=1          | cmpxchg(NULL, &owner, DEFLATER_MARKER)
+-----------------------+ 2> return

...

    T-enter                                       ObjectMonitor                T-deflate
    -------------------------------------------- +-------------------------+  ------------------------------------------
    ObjectMonitor::enter() { | owner=DEFLATER_MARKER |  deflate_monitor_using_JT() {
    <owner is contended>   | ref_count=1            |  cmpxchg(DEFLATER_MARKER, &owner, NULL)
   1> EnterI() {   +-------------------------+ 1> :
  if (owner == DEFLATER_MARKER && || 2> : <thread_stalls>
      cmpxchg(Self, &owner,                    \/ :
    DEFLATER_MARKER) +-------------------------+ :
== DEFLATER_MARKER) { | owner=Self/T-enter | :
// EnterI is done | ref_count=0 | : <thread_resumes>
return +-------------------------+ prev = cmpxchg(-max_jint, &ref_count, 0)
} || if (prev == 0 &&
} // enter() is done \/ 3> owner == DEFLATER_MARKER) {
~OMH: atomic dec ref_count +-------------------------+ } else {
2> : <does app work> | owner=Self/T-enter|NULL | cmpxchg(NULL, &owner, DEFLATER_MARKER)
3> : | ref_count=-max_jint | atomic add max_jint to ref_count
exit() monitor +-------------------------+ 4> bailout on deflation
4> owner = NULL || }
\/
+-------------------------+
| owner=Self/T-enter|NULL |
| ref_count=0 |
+-------------------------+
    • T-deflate has executed cmpxchg() and set owner to DEFLATE_MARKER.
    • T-enter has called ObjectMonitor::enter() with "ref_count == 1", noticed that the owner is contended and is about to call ObjectMonitor::EnterI().
    • The first ObjectMonitor box is showing the fields at this point and the "1>" markers are showing where each thread is at for that ObjectMonitor box.
    • T-deflate stalls after setting the owner field to DEFLATER_MARKER.
    • T-enter calls EnterI() to do the contended enter work.:
      • EnterI() observes owner == DEFLATER_MARKER and uses cmpxchg() to set the owner field to Self/T-enter.
      • T-enter owns the monitor, returns from EnterI(), and returns from enter().
      • The ObjectMonitorHandle destructor decrements the ref_count.
    • T-enter is now ready to do work that requires the monitor to be owned.
    • The second ObjectMonitor box is showing the fields at this point and the "2>" markers are showing where each thread is at for that ObjectMonitor box.
    • T-enter is doing app work (but it also could have finished and exited the monitor).
    • T-deflate resumes, sets calls cmpxchg() to set the ref_count field to -max_jint, and passes the first part of the bailout expression because "prev == 0".
    • The third ObjectMonitor box is showing the fields at this point and the "3>" markers are showing where each thread is at for that ObjectMonitor box.
    • T-deflate performs the A-B-A check which observes that "owner != DEFLATE_MARKER" and bails out on deflation:
      • Depending on when T-deflate resumes after the stall, it will see "owner == T-enter" or "owner == NULL".
      • Both of those values will cause deflation to bailout so we have to conditionally undo work:
        • restore the owner field to NULL if it is still DEFLATER_MARKER (it's not DEFLATER_MARKER)
        • undo setting ref_count to -max_jint by atomically adding max_jint to ref_count which will restore ref_count to its proper value.
      • If the T-enter thread has managed to enter but not exit the monitor during the T-deflate stall, then our owner field A-B-A transition is:
        • NULL → DEFLATE_MARKER → Self/T-enter

      • so we really have A1-B-A2, but the A-B-A principal still holds.

      • If the T-enter thread has managed to enter and exit the monitor during the T-deflate stall, then our owner field A-B-A transition is:

        • NULL → DEFLATE_MARKER → Self/T-enter  → NULL

      • so we really have A-B1-B2-A, but the A-B-A principal still holds.

    • T-enter finished doing app work and is about to exit the monitor (or it has already exited the monitor).

    • The fourth ObjectMonitor box is showing the fields at this point and the "4>" markers are showing where each thread is at for that ObjectMonitor box.

...

    T-save                                       object            T-deflate
    -------------------------------------------  +-------------+   -------------------------------------------
    install_displaced_markword_in_object() {   | mark=om_ptr |  install_displaced_markword_in_object() {
     dmw = header()                    +-------------+  dmw = header()
if (!dmw->is_marked() && if (!dmw->is_marked() &&
         dmw->hash() == 0) {                                           dmw->hash() == 0) {
       create marked_dmw                                             create marked_dmw
       dmw = cmpxchg(marked_dmw, &header, dmw)                       dmw = cmpxchg(marked_dmw, &header, dmw)
     }                                                             }
     // dmw == marked_dmw here                                     // dmw == original dmw here
     if (dmw->is_marked())                                         if (dmw->is_marked())
      unmark dmw                                                    unmark dmw
    obj = object()                                                obj = object()
    obj->cas_set_mark(dmw, this)                                  obj->cas_set_mark(dmw, this)

...

    • T-deflate made it past the cmpxchg() of ref_count before T-hash incremented it.
    • T-deflate set the ref_count field to -max_jint and is about to make the last of the protocol checks.
    • The first ObjectMonitor box is showing the fields at this point and the "1>" markers are showing where each thread is at for that ObjectMonitor box.
    • T-deflate sees "prev == 0 && owner == DEFLATER_MARKER" so it knows that it has won the race.
    • T-deflate restores obj header (not shown).
    • T-hash increments the ref_count.
    • T-hash observes "owner == DEFLATER_MARKER && ref_count <= 0" so it restores obj header (not shown) and decrements ref_count.
    • The second ObjectMonitor box is showing the fields at this point and the "2>" markers are showing where each thread is at for that ObjectMonitor box.
    • T-deflate finishes the deflation work.
    • T-hash returns false to cause a retry and when T-hash retries:
      • it observes the restored object header (done by T-hash or T-deflate):
        • if the object's header does not have a hash, then generate a hash and merge it with the object's header.
        • Otherwise, extract the hash from the object's header and return it.

...