< prev index next >

src/hotspot/share/runtime/objectMonitor.cpp

Print this page




 232 //   the LockByte is 0 iff the monitor is owned.  _owner is simply an auxiliary
 233 //   variable, like _recursions, in the scheme.  The threads or Events that form
 234 //   the list would have to be aligned in 256-byte addresses.  A thread would
 235 //   try to acquire the lock or enqueue itself with CAS, but exiting threads
 236 //   could use a 1-0 protocol and simply STB to set the LockByte to 0.
 237 //   Note that is is *not* word-tearing, but it does presume that full-word
 238 //   CAS operations are coherent with intermix with STB operations.  That's true
 239 //   on most common processors.
 240 //
 241 // * See also http://blogs.sun.com/dave
 242 
 243 
 244 // -----------------------------------------------------------------------------
 245 // Enter support
 246 
 247 void ObjectMonitor::enter(TRAPS) {
 248   // The following code is ordered to check the most common cases first
 249   // and to reduce RTS->RTO cache line upgrades on SPARC and IA32 processors.
 250   Thread * const Self = THREAD;
 251 
 252   void * cur = Atomic::cmpxchg_ptr (Self, &_owner, NULL);
 253   if (cur == NULL) {
 254     // Either ASSERT _recursions == 0 or explicitly set _recursions = 0.
 255     assert(_recursions == 0, "invariant");
 256     assert(_owner == Self, "invariant");
 257     return;
 258   }
 259 
 260   if (cur == Self) {
 261     // TODO-FIXME: check for integer overflow!  BUGID 6557169.
 262     _recursions++;
 263     return;
 264   }
 265 
 266   if (Self->is_lock_owned ((address)cur)) {
 267     assert(_recursions == 0, "internal state error");
 268     _recursions = 1;
 269     // Commute owner from a thread-specific on-stack BasicLockObject address to
 270     // a full-fledged "Thread *".
 271     _owner = Self;
 272     return;


 389     // just exited the monitor.
 390   }
 391 
 392   if (event.should_commit()) {
 393     event.set_monitorClass(((oop)this->object())->klass());
 394     event.set_previousOwner((TYPE_THREAD)_previous_owner_tid);
 395     event.set_address((TYPE_ADDRESS)(uintptr_t)(this->object_addr()));
 396     event.commit();
 397   }
 398 
 399   OM_PERFDATA_OP(ContendedLockAttempts, inc());
 400 }
 401 
 402 
 403 // Caveat: TryLock() is not necessarily serializing if it returns failure.
 404 // Callers must compensate as needed.
 405 
 406 int ObjectMonitor::TryLock(Thread * Self) {
 407   void * own = _owner;
 408   if (own != NULL) return 0;
 409   if (Atomic::cmpxchg_ptr (Self, &_owner, NULL) == NULL) {
 410     // Either guarantee _recursions == 0 or set _recursions = 0.
 411     assert(_recursions == 0, "invariant");
 412     assert(_owner == Self, "invariant");
 413     return 1;
 414   }
 415   // The lock had been free momentarily, but we lost the race to the lock.
 416   // Interference -- the CAS failed.
 417   // We can either return -1 or retry.
 418   // Retry doesn't make as much sense because the lock was just acquired.
 419   return -1;
 420 }
 421 
 422 #define MAX_RECHECK_INTERVAL 1000
 423 
 424 void ObjectMonitor::EnterI(TRAPS) {
 425   Thread * const Self = THREAD;
 426   assert(Self->is_Java_thread(), "invariant");
 427   assert(((JavaThread *) Self)->thread_state() == _thread_blocked, "invariant");
 428 
 429   // Try the lock - TATAS


 459   //
 460   // Node acts as a proxy for Self.
 461   // As an aside, if were to ever rewrite the synchronization code mostly
 462   // in Java, WaitNodes, ObjectMonitors, and Events would become 1st-class
 463   // Java objects.  This would avoid awkward lifecycle and liveness issues,
 464   // as well as eliminate a subset of ABA issues.
 465   // TODO: eliminate ObjectWaiter and enqueue either Threads or Events.
 466 
 467   ObjectWaiter node(Self);
 468   Self->_ParkEvent->reset();
 469   node._prev   = (ObjectWaiter *) 0xBAD;
 470   node.TState  = ObjectWaiter::TS_CXQ;
 471 
 472   // Push "Self" onto the front of the _cxq.
 473   // Once on cxq/EntryList, Self stays on-queue until it acquires the lock.
 474   // Note that spinning tends to reduce the rate at which threads
 475   // enqueue and dequeue on EntryList|cxq.
 476   ObjectWaiter * nxt;
 477   for (;;) {
 478     node._next = nxt = _cxq;
 479     if (Atomic::cmpxchg_ptr(&node, &_cxq, nxt) == nxt) break;
 480 
 481     // Interference - the CAS failed because _cxq changed.  Just retry.
 482     // As an optional optimization we retry the lock.
 483     if (TryLock (Self) > 0) {
 484       assert(_succ != Self, "invariant");
 485       assert(_owner == Self, "invariant");
 486       assert(_Responsible != Self, "invariant");
 487       return;
 488     }
 489   }
 490 
 491   // Check for cxq|EntryList edge transition to non-null.  This indicates
 492   // the onset of contention.  While contention persists exiting threads
 493   // will use a ST:MEMBAR:LD 1-1 exit protocol.  When contention abates exit
 494   // operations revert to the faster 1-0 mode.  This enter operation may interleave
 495   // (race) a concurrent 1-0 exit operation, resulting in stranding, so we
 496   // arrange for one of the contending thread to use a timed park() operations
 497   // to detect and recover from the race.  (Stranding is form of progress failure
 498   // where the monitor is unlocked but all the contending threads remain parked).
 499   // That is, at least one of the contended threads will periodically poll _owner.
 500   // One of the contending threads will become the designated "Responsible" thread.
 501   // The Responsible thread uses a timed park instead of a normal indefinite park
 502   // operation -- it periodically wakes and checks for and recovers from potential
 503   // strandings admitted by 1-0 exit operations.   We need at most one Responsible
 504   // thread per-monitor at any given moment.  Only threads on cxq|EntryList may
 505   // be responsible for a monitor.
 506   //
 507   // Currently, one of the contended threads takes on the added role of "Responsible".
 508   // A viable alternative would be to use a dedicated "stranding checker" thread
 509   // that periodically iterated over all the threads (or active monitors) and unparked
 510   // successors where there was risk of stranding.  This would help eliminate the
 511   // timer scalability issues we see on some platforms as we'd only have one thread
 512   // -- the checker -- parked on a timer.
 513 
 514   if ((SyncFlags & 16) == 0 && nxt == NULL && _EntryList == NULL) {
 515     // Try to assume the role of responsible thread for the monitor.
 516     // CONSIDER:  ST vs CAS vs { if (Responsible==null) Responsible=Self }
 517     Atomic::cmpxchg_ptr(Self, &_Responsible, NULL);
 518   }
 519 
 520   // The lock might have been released while this thread was occupied queueing
 521   // itself onto _cxq.  To close the race and avoid "stranding" and
 522   // progress-liveness failure we must resample-retry _owner before parking.
 523   // Note the Dekker/Lamport duality: ST cxq; MEMBAR; LD Owner.
 524   // In this case the ST-MEMBAR is accomplished with CAS().
 525   //
 526   // TODO: Defer all thread state transitions until park-time.
 527   // Since state transitions are heavy and inefficient we'd like
 528   // to defer the state transitions until absolutely necessary,
 529   // and in doing so avoid some transitions ...
 530 
 531   TEVENT(Inflated enter - Contention);
 532   int nWakeups = 0;
 533   int recheckInterval = 1;
 534 
 535   for (;;) {
 536 
 537     if (TryLock(Self) > 0) break;
 538     assert(_owner != Self, "invariant");
 539 
 540     if ((SyncFlags & 2) && _Responsible == NULL) {
 541       Atomic::cmpxchg_ptr(Self, &_Responsible, NULL);
 542     }
 543 
 544     // park self
 545     if (_Responsible == Self || (SyncFlags & 1)) {
 546       TEVENT(Inflated enter - park TIMED);
 547       Self->_ParkEvent->park((jlong) recheckInterval);
 548       // Increase the recheckInterval, but clamp the value.
 549       recheckInterval *= 8;
 550       if (recheckInterval > MAX_RECHECK_INTERVAL) {
 551         recheckInterval = MAX_RECHECK_INTERVAL;
 552       }
 553     } else {
 554       TEVENT(Inflated enter - park UNTIMED);
 555       Self->_ParkEvent->park();
 556     }
 557 
 558     if (TryLock(Self) > 0) break;
 559 
 560     // The lock is still contested.
 561     // Keep a tally of the # of futile wakeups.


 778     assert(nxt == NULL || nxt->TState == ObjectWaiter::TS_ENTER, "invariant");
 779     assert(prv == NULL || prv->TState == ObjectWaiter::TS_ENTER, "invariant");
 780     TEVENT(Unlink from EntryList);
 781   } else {
 782     assert(SelfNode->TState == ObjectWaiter::TS_CXQ, "invariant");
 783     // Inopportune interleaving -- Self is still on the cxq.
 784     // This usually means the enqueue of self raced an exiting thread.
 785     // Normally we'll find Self near the front of the cxq, so
 786     // dequeueing is typically fast.  If needbe we can accelerate
 787     // this with some MCS/CHL-like bidirectional list hints and advisory
 788     // back-links so dequeueing from the interior will normally operate
 789     // in constant-time.
 790     // Dequeue Self from either the head (with CAS) or from the interior
 791     // with a linear-time scan and normal non-atomic memory operations.
 792     // CONSIDER: if Self is on the cxq then simply drain cxq into EntryList
 793     // and then unlink Self from EntryList.  We have to drain eventually,
 794     // so it might as well be now.
 795 
 796     ObjectWaiter * v = _cxq;
 797     assert(v != NULL, "invariant");
 798     if (v != SelfNode || Atomic::cmpxchg_ptr (SelfNode->_next, &_cxq, v) != v) {
 799       // The CAS above can fail from interference IFF a "RAT" arrived.
 800       // In that case Self must be in the interior and can no longer be
 801       // at the head of cxq.
 802       if (v == SelfNode) {
 803         assert(_cxq != v, "invariant");
 804         v = _cxq;          // CAS above failed - start scan at head of list
 805       }
 806       ObjectWaiter * p;
 807       ObjectWaiter * q = NULL;
 808       for (p = v; p != NULL && p != SelfNode; p = p->_next) {
 809         q = p;
 810         assert(p->TState == ObjectWaiter::TS_CXQ, "invariant");
 811       }
 812       assert(v != SelfNode, "invariant");
 813       assert(p == SelfNode, "Node not found on cxq");
 814       assert(p != _cxq, "invariant");
 815       assert(q != NULL, "invariant");
 816       assert(q->_next == p, "invariant");
 817       q->_next = p->_next;
 818     }


 930     _previous_owner_tid = THREAD_TRACE_ID(Self);
 931   }
 932 #endif
 933 
 934   for (;;) {
 935     assert(THREAD == _owner, "invariant");
 936 
 937     if (Knob_ExitPolicy == 0) {
 938       // release semantics: prior loads and stores from within the critical section
 939       // must not float (reorder) past the following store that drops the lock.
 940       // On SPARC that requires MEMBAR #loadstore|#storestore.
 941       // But of course in TSO #loadstore|#storestore is not required.
 942       // I'd like to write one of the following:
 943       // A.  OrderAccess::release() ; _owner = NULL
 944       // B.  OrderAccess::loadstore(); OrderAccess::storestore(); _owner = NULL;
 945       // Unfortunately OrderAccess::release() and OrderAccess::loadstore() both
 946       // store into a _dummy variable.  That store is not needed, but can result
 947       // in massive wasteful coherency traffic on classic SMP systems.
 948       // Instead, I use release_store(), which is implemented as just a simple
 949       // ST on x64, x86 and SPARC.
 950       OrderAccess::release_store_ptr(&_owner, NULL);   // drop the lock
 951       OrderAccess::storeload();                        // See if we need to wake a successor
 952       if ((intptr_t(_EntryList)|intptr_t(_cxq)) == 0 || _succ != NULL) {
 953         TEVENT(Inflated exit - simple egress);
 954         return;
 955       }
 956       TEVENT(Inflated exit - complex egress);
 957       // Other threads are blocked trying to acquire the lock.
 958 
 959       // Normally the exiting thread is responsible for ensuring succession,
 960       // but if other successors are ready or other entering threads are spinning
 961       // then this thread can simply store NULL into _owner and exit without
 962       // waking a successor.  The existence of spinners or ready successors
 963       // guarantees proper succession (liveness).  Responsibility passes to the
 964       // ready or running successors.  The exiting thread delegates the duty.
 965       // More precisely, if a successor already exists this thread is absolved
 966       // of the responsibility of waking (unparking) one.
 967       //
 968       // The _succ variable is critical to reducing futile wakeup frequency.
 969       // _succ identifies the "heir presumptive" thread that has been made
 970       // ready (unparked) but that has not yet run.  We need only one such


 975       // Note that spinners in Enter() also set _succ non-null.
 976       // In the current implementation spinners opportunistically set
 977       // _succ so that exiting threads might avoid waking a successor.
 978       // Another less appealing alternative would be for the exiting thread
 979       // to drop the lock and then spin briefly to see if a spinner managed
 980       // to acquire the lock.  If so, the exiting thread could exit
 981       // immediately without waking a successor, otherwise the exiting
 982       // thread would need to dequeue and wake a successor.
 983       // (Note that we'd need to make the post-drop spin short, but no
 984       // shorter than the worst-case round-trip cache-line migration time.
 985       // The dropped lock needs to become visible to the spinner, and then
 986       // the acquisition of the lock by the spinner must become visible to
 987       // the exiting thread).
 988 
 989       // It appears that an heir-presumptive (successor) must be made ready.
 990       // Only the current lock owner can manipulate the EntryList or
 991       // drain _cxq, so we need to reacquire the lock.  If we fail
 992       // to reacquire the lock the responsibility for ensuring succession
 993       // falls to the new owner.
 994       //
 995       if (Atomic::cmpxchg_ptr (THREAD, &_owner, NULL) != NULL) {
 996         return;
 997       }
 998       TEVENT(Exit - Reacquired);
 999     } else {
1000       if ((intptr_t(_EntryList)|intptr_t(_cxq)) == 0 || _succ != NULL) {
1001         OrderAccess::release_store_ptr(&_owner, NULL);   // drop the lock
1002         OrderAccess::storeload();
1003         // Ratify the previously observed values.
1004         if (_cxq == NULL || _succ != NULL) {
1005           TEVENT(Inflated exit - simple egress);
1006           return;
1007         }
1008 
1009         // inopportune interleaving -- the exiting thread (this thread)
1010         // in the fast-exit path raced an entering thread in the slow-enter
1011         // path.
1012         // We have two choices:
1013         // A.  Try to reacquire the lock.
1014         //     If the CAS() fails return immediately, otherwise
1015         //     we either restart/rerun the exit operation, or simply
1016         //     fall-through into the code below which wakes a successor.
1017         // B.  If the elements forming the EntryList|cxq are TSM
1018         //     we could simply unpark() the lead thread and return
1019         //     without having set _succ.
1020         if (Atomic::cmpxchg_ptr (THREAD, &_owner, NULL) != NULL) {
1021           TEVENT(Inflated exit - reacquired succeeded);
1022           return;
1023         }
1024         TEVENT(Inflated exit - reacquired failed);
1025       } else {
1026         TEVENT(Inflated exit - complex egress);
1027       }
1028     }
1029 
1030     guarantee(_owner == THREAD, "invariant");
1031 
1032     ObjectWaiter * w = NULL;
1033     int QMode = Knob_QMode;
1034 
1035     if (QMode == 2 && _cxq != NULL) {
1036       // QMode == 2 : cxq has precedence over EntryList.
1037       // Try to directly wake a successor from the cxq.
1038       // If successful, the successor will need to unlink itself from cxq.
1039       w = _cxq;
1040       assert(w != NULL, "invariant");
1041       assert(w->TState == ObjectWaiter::TS_CXQ, "Invariant");
1042       ExitEpilog(Self, w);
1043       return;
1044     }
1045 
1046     if (QMode == 3 && _cxq != NULL) {
1047       // Aggressively drain cxq into EntryList at the first opportunity.
1048       // This policy ensure that recently-run threads live at the head of EntryList.
1049       // Drain _cxq into EntryList - bulk transfer.
1050       // First, detach _cxq.
1051       // The following loop is tantamount to: w = swap(&cxq, NULL)
1052       w = _cxq;
1053       for (;;) {
1054         assert(w != NULL, "Invariant");
1055         ObjectWaiter * u = (ObjectWaiter *) Atomic::cmpxchg_ptr(NULL, &_cxq, w);
1056         if (u == w) break;
1057         w = u;
1058       }
1059       assert(w != NULL, "invariant");
1060 
1061       ObjectWaiter * q = NULL;
1062       ObjectWaiter * p;
1063       for (p = w; p != NULL; p = p->_next) {
1064         guarantee(p->TState == ObjectWaiter::TS_CXQ, "Invariant");
1065         p->TState = ObjectWaiter::TS_ENTER;
1066         p->_prev = q;
1067         q = p;
1068       }
1069 
1070       // Append the RATs to the EntryList
1071       // TODO: organize EntryList as a CDLL so we can locate the tail in constant-time.
1072       ObjectWaiter * Tail;
1073       for (Tail = _EntryList; Tail != NULL && Tail->_next != NULL;
1074            Tail = Tail->_next)
1075         /* empty */;
1076       if (Tail == NULL) {
1077         _EntryList = w;
1078       } else {
1079         Tail->_next = w;
1080         w->_prev = Tail;
1081       }
1082 
1083       // Fall thru into code that tries to wake a successor from EntryList
1084     }
1085 
1086     if (QMode == 4 && _cxq != NULL) {
1087       // Aggressively drain cxq into EntryList at the first opportunity.
1088       // This policy ensure that recently-run threads live at the head of EntryList.
1089 
1090       // Drain _cxq into EntryList - bulk transfer.
1091       // First, detach _cxq.
1092       // The following loop is tantamount to: w = swap(&cxq, NULL)
1093       w = _cxq;
1094       for (;;) {
1095         assert(w != NULL, "Invariant");
1096         ObjectWaiter * u = (ObjectWaiter *) Atomic::cmpxchg_ptr(NULL, &_cxq, w);
1097         if (u == w) break;
1098         w = u;
1099       }
1100       assert(w != NULL, "invariant");
1101 
1102       ObjectWaiter * q = NULL;
1103       ObjectWaiter * p;
1104       for (p = w; p != NULL; p = p->_next) {
1105         guarantee(p->TState == ObjectWaiter::TS_CXQ, "Invariant");
1106         p->TState = ObjectWaiter::TS_ENTER;
1107         p->_prev = q;
1108         q = p;
1109       }
1110 
1111       // Prepend the RATs to the EntryList
1112       if (_EntryList != NULL) {
1113         q->_next = _EntryList;
1114         _EntryList->_prev = q;
1115       }
1116       _EntryList = w;


1129       // release the lock "O".  T2 resumes immediately after the ST of null into
1130       // _owner, above.  T2 notices that the EntryList is populated, so it
1131       // reacquires the lock and then finds itself on the EntryList.
1132       // Given all that, we have to tolerate the circumstance where "w" is
1133       // associated with Self.
1134       assert(w->TState == ObjectWaiter::TS_ENTER, "invariant");
1135       ExitEpilog(Self, w);
1136       return;
1137     }
1138 
1139     // If we find that both _cxq and EntryList are null then just
1140     // re-run the exit protocol from the top.
1141     w = _cxq;
1142     if (w == NULL) continue;
1143 
1144     // Drain _cxq into EntryList - bulk transfer.
1145     // First, detach _cxq.
1146     // The following loop is tantamount to: w = swap(&cxq, NULL)
1147     for (;;) {
1148       assert(w != NULL, "Invariant");
1149       ObjectWaiter * u = (ObjectWaiter *) Atomic::cmpxchg_ptr(NULL, &_cxq, w);
1150       if (u == w) break;
1151       w = u;
1152     }
1153     TEVENT(Inflated exit - drain cxq into EntryList);
1154 
1155     assert(w != NULL, "invariant");
1156     assert(_EntryList == NULL, "invariant");
1157 
1158     // Convert the LIFO SLL anchored by _cxq into a DLL.
1159     // The list reorganization step operates in O(LENGTH(w)) time.
1160     // It's critical that this step operate quickly as
1161     // "Self" still holds the outer-lock, restricting parallelism
1162     // and effectively lengthening the critical section.
1163     // Invariant: s chases t chases u.
1164     // TODO-FIXME: consider changing EntryList from a DLL to a CDLL so
1165     // we have faster access to the tail.
1166 
1167     if (QMode == 1) {
1168       // QMode == 1 : drain cxq to EntryList, reversing order
1169       // We also reverse the order of the list.


1262 
1263 
1264 void ObjectMonitor::ExitEpilog(Thread * Self, ObjectWaiter * Wakee) {
1265   assert(_owner == Self, "invariant");
1266 
1267   // Exit protocol:
1268   // 1. ST _succ = wakee
1269   // 2. membar #loadstore|#storestore;
1270   // 2. ST _owner = NULL
1271   // 3. unpark(wakee)
1272 
1273   _succ = Knob_SuccEnabled ? Wakee->_thread : NULL;
1274   ParkEvent * Trigger = Wakee->_event;
1275 
1276   // Hygiene -- once we've set _owner = NULL we can't safely dereference Wakee again.
1277   // The thread associated with Wakee may have grabbed the lock and "Wakee" may be
1278   // out-of-scope (non-extant).
1279   Wakee  = NULL;
1280 
1281   // Drop the lock
1282   OrderAccess::release_store_ptr(&_owner, NULL);
1283   OrderAccess::fence();                               // ST _owner vs LD in unpark()
1284 
1285   if (SafepointSynchronize::do_call_back()) {
1286     TEVENT(unpark before SAFEPOINT);
1287   }
1288 
1289   DTRACE_MONITOR_PROBE(contended__exit, this, object(), Self);
1290   Trigger->unpark();
1291 
1292   // Maintain stats and report events to JVMTI
1293   OM_PERFDATA_OP(Parks, inc());
1294 }
1295 
1296 
1297 // -----------------------------------------------------------------------------
1298 // Class Loader deadlock handling.
1299 //
1300 // complete_exit exits a lock returning recursion count
1301 // complete_exit/reenter operate as a wait without waiting
1302 // complete_exit requires an inflated monitor


1671       } else {
1672         // CONSIDER:  finding the tail currently requires a linear-time walk of
1673         // the EntryList.  We can make tail access constant-time by converting to
1674         // a CDLL instead of using our current DLL.
1675         ObjectWaiter * tail;
1676         for (tail = list; tail->_next != NULL; tail = tail->_next) {}
1677         assert(tail != NULL && tail->_next == NULL, "invariant");
1678         tail->_next = iterator;
1679         iterator->_prev = tail;
1680         iterator->_next = NULL;
1681       }
1682     } else if (policy == 2) {      // prepend to cxq
1683       if (list == NULL) {
1684         iterator->_next = iterator->_prev = NULL;
1685         _EntryList = iterator;
1686       } else {
1687         iterator->TState = ObjectWaiter::TS_CXQ;
1688         for (;;) {
1689           ObjectWaiter * front = _cxq;
1690           iterator->_next = front;
1691           if (Atomic::cmpxchg_ptr(iterator, &_cxq, front) == front) {
1692             break;
1693           }
1694         }
1695       }
1696     } else if (policy == 3) {      // append to cxq
1697       iterator->TState = ObjectWaiter::TS_CXQ;
1698       for (;;) {
1699         ObjectWaiter * tail = _cxq;
1700         if (tail == NULL) {
1701           iterator->_next = NULL;
1702           if (Atomic::cmpxchg_ptr(iterator, &_cxq, NULL) == NULL) {
1703             break;
1704           }
1705         } else {
1706           while (tail->_next != NULL) tail = tail->_next;
1707           tail->_next = iterator;
1708           iterator->_prev = tail;
1709           iterator->_next = NULL;
1710           break;
1711         }
1712       }
1713     } else {
1714       ParkEvent * ev = iterator->_event;
1715       iterator->TState = ObjectWaiter::TS_RUN;
1716       OrderAccess::fence();
1717       ev->unpark();
1718     }
1719 
1720     // _WaitSetLock protects the wait queue, not the EntryList.  We could
1721     // move the add-to-EntryList operation, above, outside the critical section
1722     // protected by _WaitSetLock.  In practice that's not useful.  With the


1963     if (ctr & msk) continue;
1964     ++hits;
1965     if ((hits & 0xF) == 0) {
1966       // The 0xF, above, corresponds to the exponent.
1967       // Consider: (msk+1)|msk
1968       msk = ((msk << 2)|3) & BackOffMask;
1969     }
1970 
1971     // Probe _owner with TATAS
1972     // If this thread observes the monitor transition or flicker
1973     // from locked to unlocked to locked, then the odds that this
1974     // thread will acquire the lock in this spin attempt go down
1975     // considerably.  The same argument applies if the CAS fails
1976     // or if we observe _owner change from one non-null value to
1977     // another non-null value.   In such cases we might abort
1978     // the spin without prejudice or apply a "penalty" to the
1979     // spin count-down variable "ctr", reducing it by 100, say.
1980 
1981     Thread * ox = (Thread *) _owner;
1982     if (ox == NULL) {
1983       ox = (Thread *) Atomic::cmpxchg_ptr(Self, &_owner, NULL);
1984       if (ox == NULL) {
1985         // The CAS succeeded -- this thread acquired ownership
1986         // Take care of some bookkeeping to exit spin state.
1987         if (sss && _succ == Self) {
1988           _succ = NULL;
1989         }
1990         if (MaxSpin > 0) Adjust(&_Spinner, -1);
1991 
1992         // Increase _SpinDuration :
1993         // The spin was successful (profitable) so we tend toward
1994         // longer spin attempts in the future.
1995         // CONSIDER: factor "ctr" into the _SpinDuration adjustment.
1996         // If we acquired the lock early in the spin cycle it
1997         // makes sense to increase _SpinDuration proportionally.
1998         // Note that we don't clamp SpinDuration precisely at SpinLimit.
1999         int x = _SpinDuration;
2000         if (x < Knob_SpinLimit) {
2001           if (x < Knob_Poverty) x = Knob_Poverty;
2002           _SpinDuration = x + Knob_Bonus;
2003         }




 232 //   the LockByte is 0 iff the monitor is owned.  _owner is simply an auxiliary
 233 //   variable, like _recursions, in the scheme.  The threads or Events that form
 234 //   the list would have to be aligned in 256-byte addresses.  A thread would
 235 //   try to acquire the lock or enqueue itself with CAS, but exiting threads
 236 //   could use a 1-0 protocol and simply STB to set the LockByte to 0.
 237 //   Note that is is *not* word-tearing, but it does presume that full-word
 238 //   CAS operations are coherent with intermix with STB operations.  That's true
 239 //   on most common processors.
 240 //
 241 // * See also http://blogs.sun.com/dave
 242 
 243 
 244 // -----------------------------------------------------------------------------
 245 // Enter support
 246 
 247 void ObjectMonitor::enter(TRAPS) {
 248   // The following code is ordered to check the most common cases first
 249   // and to reduce RTS->RTO cache line upgrades on SPARC and IA32 processors.
 250   Thread * const Self = THREAD;
 251 
 252   void * cur = Atomic::cmpxchg(Self, &_owner, (void*)NULL);
 253   if (cur == NULL) {
 254     // Either ASSERT _recursions == 0 or explicitly set _recursions = 0.
 255     assert(_recursions == 0, "invariant");
 256     assert(_owner == Self, "invariant");
 257     return;
 258   }
 259 
 260   if (cur == Self) {
 261     // TODO-FIXME: check for integer overflow!  BUGID 6557169.
 262     _recursions++;
 263     return;
 264   }
 265 
 266   if (Self->is_lock_owned ((address)cur)) {
 267     assert(_recursions == 0, "internal state error");
 268     _recursions = 1;
 269     // Commute owner from a thread-specific on-stack BasicLockObject address to
 270     // a full-fledged "Thread *".
 271     _owner = Self;
 272     return;


 389     // just exited the monitor.
 390   }
 391 
 392   if (event.should_commit()) {
 393     event.set_monitorClass(((oop)this->object())->klass());
 394     event.set_previousOwner((TYPE_THREAD)_previous_owner_tid);
 395     event.set_address((TYPE_ADDRESS)(uintptr_t)(this->object_addr()));
 396     event.commit();
 397   }
 398 
 399   OM_PERFDATA_OP(ContendedLockAttempts, inc());
 400 }
 401 
 402 
 403 // Caveat: TryLock() is not necessarily serializing if it returns failure.
 404 // Callers must compensate as needed.
 405 
 406 int ObjectMonitor::TryLock(Thread * Self) {
 407   void * own = _owner;
 408   if (own != NULL) return 0;
 409   if (Atomic::cmpxchg(Self, &_owner, (void*)NULL) == NULL) {
 410     // Either guarantee _recursions == 0 or set _recursions = 0.
 411     assert(_recursions == 0, "invariant");
 412     assert(_owner == Self, "invariant");
 413     return 1;
 414   }
 415   // The lock had been free momentarily, but we lost the race to the lock.
 416   // Interference -- the CAS failed.
 417   // We can either return -1 or retry.
 418   // Retry doesn't make as much sense because the lock was just acquired.
 419   return -1;
 420 }
 421 
 422 #define MAX_RECHECK_INTERVAL 1000
 423 
 424 void ObjectMonitor::EnterI(TRAPS) {
 425   Thread * const Self = THREAD;
 426   assert(Self->is_Java_thread(), "invariant");
 427   assert(((JavaThread *) Self)->thread_state() == _thread_blocked, "invariant");
 428 
 429   // Try the lock - TATAS


 459   //
 460   // Node acts as a proxy for Self.
 461   // As an aside, if were to ever rewrite the synchronization code mostly
 462   // in Java, WaitNodes, ObjectMonitors, and Events would become 1st-class
 463   // Java objects.  This would avoid awkward lifecycle and liveness issues,
 464   // as well as eliminate a subset of ABA issues.
 465   // TODO: eliminate ObjectWaiter and enqueue either Threads or Events.
 466 
 467   ObjectWaiter node(Self);
 468   Self->_ParkEvent->reset();
 469   node._prev   = (ObjectWaiter *) 0xBAD;
 470   node.TState  = ObjectWaiter::TS_CXQ;
 471 
 472   // Push "Self" onto the front of the _cxq.
 473   // Once on cxq/EntryList, Self stays on-queue until it acquires the lock.
 474   // Note that spinning tends to reduce the rate at which threads
 475   // enqueue and dequeue on EntryList|cxq.
 476   ObjectWaiter * nxt;
 477   for (;;) {
 478     node._next = nxt = _cxq;
 479     if (Atomic::cmpxchg(&node, &_cxq, nxt) == nxt) break;
 480 
 481     // Interference - the CAS failed because _cxq changed.  Just retry.
 482     // As an optional optimization we retry the lock.
 483     if (TryLock (Self) > 0) {
 484       assert(_succ != Self, "invariant");
 485       assert(_owner == Self, "invariant");
 486       assert(_Responsible != Self, "invariant");
 487       return;
 488     }
 489   }
 490 
 491   // Check for cxq|EntryList edge transition to non-null.  This indicates
 492   // the onset of contention.  While contention persists exiting threads
 493   // will use a ST:MEMBAR:LD 1-1 exit protocol.  When contention abates exit
 494   // operations revert to the faster 1-0 mode.  This enter operation may interleave
 495   // (race) a concurrent 1-0 exit operation, resulting in stranding, so we
 496   // arrange for one of the contending thread to use a timed park() operations
 497   // to detect and recover from the race.  (Stranding is form of progress failure
 498   // where the monitor is unlocked but all the contending threads remain parked).
 499   // That is, at least one of the contended threads will periodically poll _owner.
 500   // One of the contending threads will become the designated "Responsible" thread.
 501   // The Responsible thread uses a timed park instead of a normal indefinite park
 502   // operation -- it periodically wakes and checks for and recovers from potential
 503   // strandings admitted by 1-0 exit operations.   We need at most one Responsible
 504   // thread per-monitor at any given moment.  Only threads on cxq|EntryList may
 505   // be responsible for a monitor.
 506   //
 507   // Currently, one of the contended threads takes on the added role of "Responsible".
 508   // A viable alternative would be to use a dedicated "stranding checker" thread
 509   // that periodically iterated over all the threads (or active monitors) and unparked
 510   // successors where there was risk of stranding.  This would help eliminate the
 511   // timer scalability issues we see on some platforms as we'd only have one thread
 512   // -- the checker -- parked on a timer.
 513 
 514   if ((SyncFlags & 16) == 0 && nxt == NULL && _EntryList == NULL) {
 515     // Try to assume the role of responsible thread for the monitor.
 516     // CONSIDER:  ST vs CAS vs { if (Responsible==null) Responsible=Self }
 517     Atomic::cmpxchg(Self, &_Responsible, (Thread*)NULL);
 518   }
 519 
 520   // The lock might have been released while this thread was occupied queueing
 521   // itself onto _cxq.  To close the race and avoid "stranding" and
 522   // progress-liveness failure we must resample-retry _owner before parking.
 523   // Note the Dekker/Lamport duality: ST cxq; MEMBAR; LD Owner.
 524   // In this case the ST-MEMBAR is accomplished with CAS().
 525   //
 526   // TODO: Defer all thread state transitions until park-time.
 527   // Since state transitions are heavy and inefficient we'd like
 528   // to defer the state transitions until absolutely necessary,
 529   // and in doing so avoid some transitions ...
 530 
 531   TEVENT(Inflated enter - Contention);
 532   int nWakeups = 0;
 533   int recheckInterval = 1;
 534 
 535   for (;;) {
 536 
 537     if (TryLock(Self) > 0) break;
 538     assert(_owner != Self, "invariant");
 539 
 540     if ((SyncFlags & 2) && _Responsible == NULL) {
 541       Atomic::cmpxchg(Self, &_Responsible, (Thread*)NULL);
 542     }
 543 
 544     // park self
 545     if (_Responsible == Self || (SyncFlags & 1)) {
 546       TEVENT(Inflated enter - park TIMED);
 547       Self->_ParkEvent->park((jlong) recheckInterval);
 548       // Increase the recheckInterval, but clamp the value.
 549       recheckInterval *= 8;
 550       if (recheckInterval > MAX_RECHECK_INTERVAL) {
 551         recheckInterval = MAX_RECHECK_INTERVAL;
 552       }
 553     } else {
 554       TEVENT(Inflated enter - park UNTIMED);
 555       Self->_ParkEvent->park();
 556     }
 557 
 558     if (TryLock(Self) > 0) break;
 559 
 560     // The lock is still contested.
 561     // Keep a tally of the # of futile wakeups.


 778     assert(nxt == NULL || nxt->TState == ObjectWaiter::TS_ENTER, "invariant");
 779     assert(prv == NULL || prv->TState == ObjectWaiter::TS_ENTER, "invariant");
 780     TEVENT(Unlink from EntryList);
 781   } else {
 782     assert(SelfNode->TState == ObjectWaiter::TS_CXQ, "invariant");
 783     // Inopportune interleaving -- Self is still on the cxq.
 784     // This usually means the enqueue of self raced an exiting thread.
 785     // Normally we'll find Self near the front of the cxq, so
 786     // dequeueing is typically fast.  If needbe we can accelerate
 787     // this with some MCS/CHL-like bidirectional list hints and advisory
 788     // back-links so dequeueing from the interior will normally operate
 789     // in constant-time.
 790     // Dequeue Self from either the head (with CAS) or from the interior
 791     // with a linear-time scan and normal non-atomic memory operations.
 792     // CONSIDER: if Self is on the cxq then simply drain cxq into EntryList
 793     // and then unlink Self from EntryList.  We have to drain eventually,
 794     // so it might as well be now.
 795 
 796     ObjectWaiter * v = _cxq;
 797     assert(v != NULL, "invariant");
 798     if (v != SelfNode || Atomic::cmpxchg(SelfNode->_next, &_cxq, v) != v) {
 799       // The CAS above can fail from interference IFF a "RAT" arrived.
 800       // In that case Self must be in the interior and can no longer be
 801       // at the head of cxq.
 802       if (v == SelfNode) {
 803         assert(_cxq != v, "invariant");
 804         v = _cxq;          // CAS above failed - start scan at head of list
 805       }
 806       ObjectWaiter * p;
 807       ObjectWaiter * q = NULL;
 808       for (p = v; p != NULL && p != SelfNode; p = p->_next) {
 809         q = p;
 810         assert(p->TState == ObjectWaiter::TS_CXQ, "invariant");
 811       }
 812       assert(v != SelfNode, "invariant");
 813       assert(p == SelfNode, "Node not found on cxq");
 814       assert(p != _cxq, "invariant");
 815       assert(q != NULL, "invariant");
 816       assert(q->_next == p, "invariant");
 817       q->_next = p->_next;
 818     }


 930     _previous_owner_tid = THREAD_TRACE_ID(Self);
 931   }
 932 #endif
 933 
 934   for (;;) {
 935     assert(THREAD == _owner, "invariant");
 936 
 937     if (Knob_ExitPolicy == 0) {
 938       // release semantics: prior loads and stores from within the critical section
 939       // must not float (reorder) past the following store that drops the lock.
 940       // On SPARC that requires MEMBAR #loadstore|#storestore.
 941       // But of course in TSO #loadstore|#storestore is not required.
 942       // I'd like to write one of the following:
 943       // A.  OrderAccess::release() ; _owner = NULL
 944       // B.  OrderAccess::loadstore(); OrderAccess::storestore(); _owner = NULL;
 945       // Unfortunately OrderAccess::release() and OrderAccess::loadstore() both
 946       // store into a _dummy variable.  That store is not needed, but can result
 947       // in massive wasteful coherency traffic on classic SMP systems.
 948       // Instead, I use release_store(), which is implemented as just a simple
 949       // ST on x64, x86 and SPARC.
 950       OrderAccess::release_store(&_owner, (void*)NULL);   // drop the lock
 951       OrderAccess::storeload();                        // See if we need to wake a successor
 952       if ((intptr_t(_EntryList)|intptr_t(_cxq)) == 0 || _succ != NULL) {
 953         TEVENT(Inflated exit - simple egress);
 954         return;
 955       }
 956       TEVENT(Inflated exit - complex egress);
 957       // Other threads are blocked trying to acquire the lock.
 958 
 959       // Normally the exiting thread is responsible for ensuring succession,
 960       // but if other successors are ready or other entering threads are spinning
 961       // then this thread can simply store NULL into _owner and exit without
 962       // waking a successor.  The existence of spinners or ready successors
 963       // guarantees proper succession (liveness).  Responsibility passes to the
 964       // ready or running successors.  The exiting thread delegates the duty.
 965       // More precisely, if a successor already exists this thread is absolved
 966       // of the responsibility of waking (unparking) one.
 967       //
 968       // The _succ variable is critical to reducing futile wakeup frequency.
 969       // _succ identifies the "heir presumptive" thread that has been made
 970       // ready (unparked) but that has not yet run.  We need only one such


 975       // Note that spinners in Enter() also set _succ non-null.
 976       // In the current implementation spinners opportunistically set
 977       // _succ so that exiting threads might avoid waking a successor.
 978       // Another less appealing alternative would be for the exiting thread
 979       // to drop the lock and then spin briefly to see if a spinner managed
 980       // to acquire the lock.  If so, the exiting thread could exit
 981       // immediately without waking a successor, otherwise the exiting
 982       // thread would need to dequeue and wake a successor.
 983       // (Note that we'd need to make the post-drop spin short, but no
 984       // shorter than the worst-case round-trip cache-line migration time.
 985       // The dropped lock needs to become visible to the spinner, and then
 986       // the acquisition of the lock by the spinner must become visible to
 987       // the exiting thread).
 988 
 989       // It appears that an heir-presumptive (successor) must be made ready.
 990       // Only the current lock owner can manipulate the EntryList or
 991       // drain _cxq, so we need to reacquire the lock.  If we fail
 992       // to reacquire the lock the responsibility for ensuring succession
 993       // falls to the new owner.
 994       //
 995       if (Atomic::cmpxchg(THREAD, &_owner, (void*)NULL) != NULL) {
 996         return;
 997       }
 998       TEVENT(Exit - Reacquired);
 999     } else {
1000       if ((intptr_t(_EntryList)|intptr_t(_cxq)) == 0 || _succ != NULL) {
1001         OrderAccess::release_store(&_owner, (void*)NULL);   // drop the lock
1002         OrderAccess::storeload();
1003         // Ratify the previously observed values.
1004         if (_cxq == NULL || _succ != NULL) {
1005           TEVENT(Inflated exit - simple egress);
1006           return;
1007         }
1008 
1009         // inopportune interleaving -- the exiting thread (this thread)
1010         // in the fast-exit path raced an entering thread in the slow-enter
1011         // path.
1012         // We have two choices:
1013         // A.  Try to reacquire the lock.
1014         //     If the CAS() fails return immediately, otherwise
1015         //     we either restart/rerun the exit operation, or simply
1016         //     fall-through into the code below which wakes a successor.
1017         // B.  If the elements forming the EntryList|cxq are TSM
1018         //     we could simply unpark() the lead thread and return
1019         //     without having set _succ.
1020         if (Atomic::cmpxchg(THREAD, &_owner, (void*)NULL) != NULL) {
1021           TEVENT(Inflated exit - reacquired succeeded);
1022           return;
1023         }
1024         TEVENT(Inflated exit - reacquired failed);
1025       } else {
1026         TEVENT(Inflated exit - complex egress);
1027       }
1028     }
1029 
1030     guarantee(_owner == THREAD, "invariant");
1031 
1032     ObjectWaiter * w = NULL;
1033     int QMode = Knob_QMode;
1034 
1035     if (QMode == 2 && _cxq != NULL) {
1036       // QMode == 2 : cxq has precedence over EntryList.
1037       // Try to directly wake a successor from the cxq.
1038       // If successful, the successor will need to unlink itself from cxq.
1039       w = _cxq;
1040       assert(w != NULL, "invariant");
1041       assert(w->TState == ObjectWaiter::TS_CXQ, "Invariant");
1042       ExitEpilog(Self, w);
1043       return;
1044     }
1045 
1046     if (QMode == 3 && _cxq != NULL) {
1047       // Aggressively drain cxq into EntryList at the first opportunity.
1048       // This policy ensure that recently-run threads live at the head of EntryList.
1049       // Drain _cxq into EntryList - bulk transfer.
1050       // First, detach _cxq.
1051       // The following loop is tantamount to: w = swap(&cxq, NULL)
1052       w = _cxq;
1053       for (;;) {
1054         assert(w != NULL, "Invariant");
1055         ObjectWaiter * u = Atomic::cmpxchg((ObjectWaiter*)NULL, &_cxq, w);
1056         if (u == w) break;
1057         w = u;
1058       }
1059       assert(w != NULL, "invariant");
1060 
1061       ObjectWaiter * q = NULL;
1062       ObjectWaiter * p;
1063       for (p = w; p != NULL; p = p->_next) {
1064         guarantee(p->TState == ObjectWaiter::TS_CXQ, "Invariant");
1065         p->TState = ObjectWaiter::TS_ENTER;
1066         p->_prev = q;
1067         q = p;
1068       }
1069 
1070       // Append the RATs to the EntryList
1071       // TODO: organize EntryList as a CDLL so we can locate the tail in constant-time.
1072       ObjectWaiter * Tail;
1073       for (Tail = _EntryList; Tail != NULL && Tail->_next != NULL;
1074            Tail = Tail->_next)
1075         /* empty */;
1076       if (Tail == NULL) {
1077         _EntryList = w;
1078       } else {
1079         Tail->_next = w;
1080         w->_prev = Tail;
1081       }
1082 
1083       // Fall thru into code that tries to wake a successor from EntryList
1084     }
1085 
1086     if (QMode == 4 && _cxq != NULL) {
1087       // Aggressively drain cxq into EntryList at the first opportunity.
1088       // This policy ensure that recently-run threads live at the head of EntryList.
1089 
1090       // Drain _cxq into EntryList - bulk transfer.
1091       // First, detach _cxq.
1092       // The following loop is tantamount to: w = swap(&cxq, NULL)
1093       w = _cxq;
1094       for (;;) {
1095         assert(w != NULL, "Invariant");
1096         ObjectWaiter * u = Atomic::cmpxchg((ObjectWaiter*)NULL, &_cxq, w);
1097         if (u == w) break;
1098         w = u;
1099       }
1100       assert(w != NULL, "invariant");
1101 
1102       ObjectWaiter * q = NULL;
1103       ObjectWaiter * p;
1104       for (p = w; p != NULL; p = p->_next) {
1105         guarantee(p->TState == ObjectWaiter::TS_CXQ, "Invariant");
1106         p->TState = ObjectWaiter::TS_ENTER;
1107         p->_prev = q;
1108         q = p;
1109       }
1110 
1111       // Prepend the RATs to the EntryList
1112       if (_EntryList != NULL) {
1113         q->_next = _EntryList;
1114         _EntryList->_prev = q;
1115       }
1116       _EntryList = w;


1129       // release the lock "O".  T2 resumes immediately after the ST of null into
1130       // _owner, above.  T2 notices that the EntryList is populated, so it
1131       // reacquires the lock and then finds itself on the EntryList.
1132       // Given all that, we have to tolerate the circumstance where "w" is
1133       // associated with Self.
1134       assert(w->TState == ObjectWaiter::TS_ENTER, "invariant");
1135       ExitEpilog(Self, w);
1136       return;
1137     }
1138 
1139     // If we find that both _cxq and EntryList are null then just
1140     // re-run the exit protocol from the top.
1141     w = _cxq;
1142     if (w == NULL) continue;
1143 
1144     // Drain _cxq into EntryList - bulk transfer.
1145     // First, detach _cxq.
1146     // The following loop is tantamount to: w = swap(&cxq, NULL)
1147     for (;;) {
1148       assert(w != NULL, "Invariant");
1149       ObjectWaiter * u = Atomic::cmpxchg((ObjectWaiter*)NULL, &_cxq, w);
1150       if (u == w) break;
1151       w = u;
1152     }
1153     TEVENT(Inflated exit - drain cxq into EntryList);
1154 
1155     assert(w != NULL, "invariant");
1156     assert(_EntryList == NULL, "invariant");
1157 
1158     // Convert the LIFO SLL anchored by _cxq into a DLL.
1159     // The list reorganization step operates in O(LENGTH(w)) time.
1160     // It's critical that this step operate quickly as
1161     // "Self" still holds the outer-lock, restricting parallelism
1162     // and effectively lengthening the critical section.
1163     // Invariant: s chases t chases u.
1164     // TODO-FIXME: consider changing EntryList from a DLL to a CDLL so
1165     // we have faster access to the tail.
1166 
1167     if (QMode == 1) {
1168       // QMode == 1 : drain cxq to EntryList, reversing order
1169       // We also reverse the order of the list.


1262 
1263 
1264 void ObjectMonitor::ExitEpilog(Thread * Self, ObjectWaiter * Wakee) {
1265   assert(_owner == Self, "invariant");
1266 
1267   // Exit protocol:
1268   // 1. ST _succ = wakee
1269   // 2. membar #loadstore|#storestore;
1270   // 2. ST _owner = NULL
1271   // 3. unpark(wakee)
1272 
1273   _succ = Knob_SuccEnabled ? Wakee->_thread : NULL;
1274   ParkEvent * Trigger = Wakee->_event;
1275 
1276   // Hygiene -- once we've set _owner = NULL we can't safely dereference Wakee again.
1277   // The thread associated with Wakee may have grabbed the lock and "Wakee" may be
1278   // out-of-scope (non-extant).
1279   Wakee  = NULL;
1280 
1281   // Drop the lock
1282   OrderAccess::release_store(&_owner, (void*)NULL);
1283   OrderAccess::fence();                               // ST _owner vs LD in unpark()
1284 
1285   if (SafepointSynchronize::do_call_back()) {
1286     TEVENT(unpark before SAFEPOINT);
1287   }
1288 
1289   DTRACE_MONITOR_PROBE(contended__exit, this, object(), Self);
1290   Trigger->unpark();
1291 
1292   // Maintain stats and report events to JVMTI
1293   OM_PERFDATA_OP(Parks, inc());
1294 }
1295 
1296 
1297 // -----------------------------------------------------------------------------
1298 // Class Loader deadlock handling.
1299 //
1300 // complete_exit exits a lock returning recursion count
1301 // complete_exit/reenter operate as a wait without waiting
1302 // complete_exit requires an inflated monitor


1671       } else {
1672         // CONSIDER:  finding the tail currently requires a linear-time walk of
1673         // the EntryList.  We can make tail access constant-time by converting to
1674         // a CDLL instead of using our current DLL.
1675         ObjectWaiter * tail;
1676         for (tail = list; tail->_next != NULL; tail = tail->_next) {}
1677         assert(tail != NULL && tail->_next == NULL, "invariant");
1678         tail->_next = iterator;
1679         iterator->_prev = tail;
1680         iterator->_next = NULL;
1681       }
1682     } else if (policy == 2) {      // prepend to cxq
1683       if (list == NULL) {
1684         iterator->_next = iterator->_prev = NULL;
1685         _EntryList = iterator;
1686       } else {
1687         iterator->TState = ObjectWaiter::TS_CXQ;
1688         for (;;) {
1689           ObjectWaiter * front = _cxq;
1690           iterator->_next = front;
1691           if (Atomic::cmpxchg(iterator, &_cxq, front) == front) {
1692             break;
1693           }
1694         }
1695       }
1696     } else if (policy == 3) {      // append to cxq
1697       iterator->TState = ObjectWaiter::TS_CXQ;
1698       for (;;) {
1699         ObjectWaiter * tail = _cxq;
1700         if (tail == NULL) {
1701           iterator->_next = NULL;
1702           if (Atomic::cmpxchg(iterator, &_cxq, (ObjectWaiter*)NULL) == NULL) {
1703             break;
1704           }
1705         } else {
1706           while (tail->_next != NULL) tail = tail->_next;
1707           tail->_next = iterator;
1708           iterator->_prev = tail;
1709           iterator->_next = NULL;
1710           break;
1711         }
1712       }
1713     } else {
1714       ParkEvent * ev = iterator->_event;
1715       iterator->TState = ObjectWaiter::TS_RUN;
1716       OrderAccess::fence();
1717       ev->unpark();
1718     }
1719 
1720     // _WaitSetLock protects the wait queue, not the EntryList.  We could
1721     // move the add-to-EntryList operation, above, outside the critical section
1722     // protected by _WaitSetLock.  In practice that's not useful.  With the


1963     if (ctr & msk) continue;
1964     ++hits;
1965     if ((hits & 0xF) == 0) {
1966       // The 0xF, above, corresponds to the exponent.
1967       // Consider: (msk+1)|msk
1968       msk = ((msk << 2)|3) & BackOffMask;
1969     }
1970 
1971     // Probe _owner with TATAS
1972     // If this thread observes the monitor transition or flicker
1973     // from locked to unlocked to locked, then the odds that this
1974     // thread will acquire the lock in this spin attempt go down
1975     // considerably.  The same argument applies if the CAS fails
1976     // or if we observe _owner change from one non-null value to
1977     // another non-null value.   In such cases we might abort
1978     // the spin without prejudice or apply a "penalty" to the
1979     // spin count-down variable "ctr", reducing it by 100, say.
1980 
1981     Thread * ox = (Thread *) _owner;
1982     if (ox == NULL) {
1983       ox = (Thread*)Atomic::cmpxchg(Self, &_owner, (void*)NULL);
1984       if (ox == NULL) {
1985         // The CAS succeeded -- this thread acquired ownership
1986         // Take care of some bookkeeping to exit spin state.
1987         if (sss && _succ == Self) {
1988           _succ = NULL;
1989         }
1990         if (MaxSpin > 0) Adjust(&_Spinner, -1);
1991 
1992         // Increase _SpinDuration :
1993         // The spin was successful (profitable) so we tend toward
1994         // longer spin attempts in the future.
1995         // CONSIDER: factor "ctr" into the _SpinDuration adjustment.
1996         // If we acquired the lock early in the spin cycle it
1997         // makes sense to increase _SpinDuration proportionally.
1998         // Note that we don't clamp SpinDuration precisely at SpinLimit.
1999         int x = _SpinDuration;
2000         if (x < Knob_SpinLimit) {
2001           if (x < Knob_Poverty) x = Knob_Poverty;
2002           _SpinDuration = x + Knob_Bonus;
2003         }


< prev index next >