< prev index next >

src/hotspot/share/runtime/objectMonitor.cpp

Print this page




 228 void* ObjectMonitor::operator new[] (size_t size) throw() {
 229   return operator new (size);
 230 }
 231 void ObjectMonitor::operator delete(void* p) {
 232   FreeHeap(p);
 233 }
 234 void ObjectMonitor::operator delete[] (void *p) {
 235   operator delete(p);
 236 }
 237 
 238 // -----------------------------------------------------------------------------
 239 // Enter support
 240 
 241 void ObjectMonitor::enter(TRAPS) {
 242   // The following code is ordered to check the most common cases first
 243   // and to reduce RTS->RTO cache line upgrades on SPARC and IA32 processors.
 244   Thread * const Self = THREAD;
 245 
 246   void * cur = Atomic::cmpxchg(Self, &_owner, (void*)NULL);
 247   if (cur == NULL) {

 248     assert(_recursions == 0, "invariant");

 249     return;
 250   }
 251 
 252   if (cur == Self) {
 253     // TODO-FIXME: check for integer overflow!  BUGID 6557169.
 254     _recursions++;
 255     return;
 256   }
 257 
 258   if (Self->is_lock_owned ((address)cur)) {
 259     assert(_recursions == 0, "internal state error");
 260     _recursions = 1;
 261     // Commute owner from a thread-specific on-stack BasicLockObject address to
 262     // a full-fledged "Thread *".
 263     _owner = Self;
 264     return;
 265   }
 266 
 267   // We've encountered genuine contention.
 268   assert(Self->_Stalled == 0, "invariant");


 386     // The current thread already owns the monitor and is not going to
 387     // call park() for the remainder of the monitor enter protocol. So
 388     // it doesn't matter if the JVMTI_EVENT_MONITOR_CONTENDED_ENTERED
 389     // event handler consumed an unpark() issued by the thread that
 390     // just exited the monitor.
 391   }
 392   if (event.should_commit()) {
 393     event.set_previousOwner((uintptr_t)_previous_owner_tid);
 394     event.commit();
 395   }
 396   OM_PERFDATA_OP(ContendedLockAttempts, inc());
 397 }
 398 
 399 // Caveat: TryLock() is not necessarily serializing if it returns failure.
 400 // Callers must compensate as needed.
 401 
 402 int ObjectMonitor::TryLock(Thread * Self) {
 403   void * own = _owner;
 404   if (own != NULL) return 0;
 405   if (Atomic::replace_if_null(Self, &_owner)) {

 406     assert(_recursions == 0, "invariant");

 407     return 1;
 408   }
 409   // The lock had been free momentarily, but we lost the race to the lock.
 410   // Interference -- the CAS failed.
 411   // We can either return -1 or retry.
 412   // Retry doesn't make as much sense because the lock was just acquired.
 413   return -1;
 414 }
 415 
 416 // Convert the fields used by is_busy() to a string that can be
 417 // used for diagnostic output.
 418 const char* ObjectMonitor::is_busy_to_string(stringStream* ss) {
 419   ss->print("is_busy: contentions=%d, waiters=%d, owner=" INTPTR_FORMAT
 420             ", cxq=" INTPTR_FORMAT ", EntryList=" INTPTR_FORMAT, _contentions,
 421             _waiters, p2i(_owner), p2i(_cxq), p2i(_EntryList));
 422   return ss->base();
 423 }
 424 
 425 #define MAX_RECHECK_INTERVAL 1000
 426 
 427 void ObjectMonitor::EnterI(TRAPS) {
 428   Thread * const Self = THREAD;
 429   assert(Self->is_Java_thread(), "invariant");
 430   assert(((JavaThread *) Self)->thread_state() == _thread_blocked, "invariant");
 431 
 432   // Try the lock - TATAS
 433   if (TryLock (Self) > 0) {
 434     assert(_succ != Self, "invariant");
 435     assert(_owner == Self, "invariant");
 436     assert(_Responsible != Self, "invariant");
 437     return;
 438   }
 439 
 440   assert(InitDone, "Unexpectedly not initialized");
 441 
 442   // We try one round of spinning *before* enqueueing Self.




 228 void* ObjectMonitor::operator new[] (size_t size) throw() {
 229   return operator new (size);
 230 }
 231 void ObjectMonitor::operator delete(void* p) {
 232   FreeHeap(p);
 233 }
 234 void ObjectMonitor::operator delete[] (void *p) {
 235   operator delete(p);
 236 }
 237 
 238 // -----------------------------------------------------------------------------
 239 // Enter support
 240 
 241 void ObjectMonitor::enter(TRAPS) {
 242   // The following code is ordered to check the most common cases first
 243   // and to reduce RTS->RTO cache line upgrades on SPARC and IA32 processors.
 244   Thread * const Self = THREAD;
 245 
 246   void * cur = Atomic::cmpxchg(Self, &_owner, (void*)NULL);
 247   if (cur == NULL) {
 248     // Either ASSERT _recursions == 0 or explicitly set _recursions = 0.
 249     assert(_recursions == 0, "invariant");
 250     assert(_owner == Self, "invariant");
 251     return;
 252   }
 253 
 254   if (cur == Self) {
 255     // TODO-FIXME: check for integer overflow!  BUGID 6557169.
 256     _recursions++;
 257     return;
 258   }
 259 
 260   if (Self->is_lock_owned ((address)cur)) {
 261     assert(_recursions == 0, "internal state error");
 262     _recursions = 1;
 263     // Commute owner from a thread-specific on-stack BasicLockObject address to
 264     // a full-fledged "Thread *".
 265     _owner = Self;
 266     return;
 267   }
 268 
 269   // We've encountered genuine contention.
 270   assert(Self->_Stalled == 0, "invariant");


 388     // The current thread already owns the monitor and is not going to
 389     // call park() for the remainder of the monitor enter protocol. So
 390     // it doesn't matter if the JVMTI_EVENT_MONITOR_CONTENDED_ENTERED
 391     // event handler consumed an unpark() issued by the thread that
 392     // just exited the monitor.
 393   }
 394   if (event.should_commit()) {
 395     event.set_previousOwner((uintptr_t)_previous_owner_tid);
 396     event.commit();
 397   }
 398   OM_PERFDATA_OP(ContendedLockAttempts, inc());
 399 }
 400 
 401 // Caveat: TryLock() is not necessarily serializing if it returns failure.
 402 // Callers must compensate as needed.
 403 
 404 int ObjectMonitor::TryLock(Thread * Self) {
 405   void * own = _owner;
 406   if (own != NULL) return 0;
 407   if (Atomic::replace_if_null(Self, &_owner)) {
 408     // Either guarantee _recursions == 0 or set _recursions = 0.
 409     assert(_recursions == 0, "invariant");
 410     assert(_owner == Self, "invariant");
 411     return 1;
 412   }
 413   // The lock had been free momentarily, but we lost the race to the lock.
 414   // Interference -- the CAS failed.
 415   // We can either return -1 or retry.
 416   // Retry doesn't make as much sense because the lock was just acquired.
 417   return -1;









 418 }
 419 
 420 #define MAX_RECHECK_INTERVAL 1000
 421 
 422 void ObjectMonitor::EnterI(TRAPS) {
 423   Thread * const Self = THREAD;
 424   assert(Self->is_Java_thread(), "invariant");
 425   assert(((JavaThread *) Self)->thread_state() == _thread_blocked, "invariant");
 426 
 427   // Try the lock - TATAS
 428   if (TryLock (Self) > 0) {
 429     assert(_succ != Self, "invariant");
 430     assert(_owner == Self, "invariant");
 431     assert(_Responsible != Self, "invariant");
 432     return;
 433   }
 434 
 435   assert(InitDone, "Unexpectedly not initialized");
 436 
 437   // We try one round of spinning *before* enqueueing Self.


< prev index next >