hotspot/src/share/vm/runtime/mutex.cpp
changeset 26683 a02753d5a0b2
parent 25472 381638db28e6
child 26684 d1221849ea3d
equal deleted inserted replaced
26331:8f17e084029b 26683:a02753d5a0b2
   405 
   405 
   406 static int ParkCommon (ParkEvent * ev, jlong timo) {
   406 static int ParkCommon (ParkEvent * ev, jlong timo) {
   407   // Diagnostic support - periodically unwedge blocked threads
   407   // Diagnostic support - periodically unwedge blocked threads
   408   intx nmt = NativeMonitorTimeout;
   408   intx nmt = NativeMonitorTimeout;
   409   if (nmt > 0 && (nmt < timo || timo <= 0)) {
   409   if (nmt > 0 && (nmt < timo || timo <= 0)) {
   410      timo = nmt;
   410     timo = nmt;
   411   }
   411   }
   412   int err = OS_OK;
   412   int err = OS_OK;
   413   if (0 == timo) {
   413   if (0 == timo) {
   414     ev->park();
   414     ev->park();
   415   } else {
   415   } else {
   588     assert(RelaxAssert || w != Thread::current()->_MutexEvent, "invariant");
   588     assert(RelaxAssert || w != Thread::current()->_MutexEvent, "invariant");
   589     _EntryList = w->ListNext;
   589     _EntryList = w->ListNext;
   590     // as a diagnostic measure consider setting w->_ListNext = BAD
   590     // as a diagnostic measure consider setting w->_ListNext = BAD
   591     assert(UNS(_OnDeck) == _LBIT, "invariant");
   591     assert(UNS(_OnDeck) == _LBIT, "invariant");
   592     _OnDeck = w;           // pass OnDeck to w.
   592     _OnDeck = w;           // pass OnDeck to w.
   593                             // w will clear OnDeck once it acquires the outer lock
   593     // w will clear OnDeck once it acquires the outer lock
   594 
   594 
   595     // Another optional optimization ...
   595     // Another optional optimization ...
   596     // For heavily contended locks it's not uncommon that some other
   596     // For heavily contended locks it's not uncommon that some other
   597     // thread acquired the lock while this thread was arranging succession.
   597     // thread acquired the lock while this thread was arranging succession.
   598     // Try to defer the unpark() operation - Delegate the responsibility
   598     // Try to defer the unpark() operation - Delegate the responsibility
  1080   guarantee(!as_suspend_equivalent || !no_safepoint_check, "invariant");
  1080   guarantee(!as_suspend_equivalent || !no_safepoint_check, "invariant");
  1081   // !no_safepoint_check logically implies java_thread
  1081   // !no_safepoint_check logically implies java_thread
  1082   guarantee(no_safepoint_check || Self->is_Java_thread(), "invariant");
  1082   guarantee(no_safepoint_check || Self->is_Java_thread(), "invariant");
  1083 
  1083 
  1084   #ifdef ASSERT
  1084   #ifdef ASSERT
  1085     Monitor * least = get_least_ranked_lock_besides_this(Self->owned_locks());
  1085   Monitor * least = get_least_ranked_lock_besides_this(Self->owned_locks());
  1086     assert(least != this, "Specification of get_least_... call above");
  1086   assert(least != this, "Specification of get_least_... call above");
  1087     if (least != NULL && least->rank() <= special) {
  1087   if (least != NULL && least->rank() <= special) {
  1088       tty->print("Attempting to wait on monitor %s/%d while holding"
  1088     tty->print("Attempting to wait on monitor %s/%d while holding"
  1089                  " lock %s/%d -- possible deadlock",
  1089                " lock %s/%d -- possible deadlock",
  1090                  name(), rank(), least->name(), least->rank());
  1090                name(), rank(), least->name(), least->rank());
  1091       assert(false, "Shouldn't block(wait) while holding a lock of rank special");
  1091     assert(false, "Shouldn't block(wait) while holding a lock of rank special");
  1092     }
  1092   }
  1093   #endif // ASSERT
  1093   #endif // ASSERT
  1094 
  1094 
  1095   int wait_status;
  1095   int wait_status;
  1096   // conceptually set the owner to NULL in anticipation of
  1096   // conceptually set the owner to NULL in anticipation of
  1097   // abdicating the lock in wait
  1097   // abdicating the lock in wait
  1171 }
  1171 }
  1172 
  1172 
  1173 Mutex::Mutex (int Rank, const char * name, bool allow_vm_block) {
  1173 Mutex::Mutex (int Rank, const char * name, bool allow_vm_block) {
  1174   ClearMonitor((Monitor *) this, name);
  1174   ClearMonitor((Monitor *) this, name);
  1175 #ifdef ASSERT
  1175 #ifdef ASSERT
  1176  _allow_vm_block   = allow_vm_block;
  1176   _allow_vm_block   = allow_vm_block;
  1177  _rank             = Rank;
  1177   _rank             = Rank;
  1178 #endif
  1178 #endif
  1179 }
  1179 }
  1180 
  1180 
  1181 bool Monitor::owned_by_self() const {
  1181 bool Monitor::owned_by_self() const {
  1182   bool ret = _owner == Thread::current();
  1182   bool ret = _owner == Thread::current();
  1278     _owner = new_owner; // set the owner
  1278     _owner = new_owner; // set the owner
  1279 
  1279 
  1280     // link "this" into the owned locks list
  1280     // link "this" into the owned locks list
  1281 
  1281 
  1282     #ifdef ASSERT  // Thread::_owned_locks is under the same ifdef
  1282     #ifdef ASSERT  // Thread::_owned_locks is under the same ifdef
  1283       Monitor* locks = get_least_ranked_lock(new_owner->owned_locks());
  1283     Monitor* locks = get_least_ranked_lock(new_owner->owned_locks());
  1284                     // Mutex::set_owner_implementation is a friend of Thread
  1284     // Mutex::set_owner_implementation is a friend of Thread
  1285 
  1285 
  1286       assert(this->rank() >= 0, "bad lock rank");
  1286     assert(this->rank() >= 0, "bad lock rank");
  1287 
  1287 
  1288       // Deadlock avoidance rules require us to acquire Mutexes only in
  1288     // Deadlock avoidance rules require us to acquire Mutexes only in
  1289       // a global total order. For example m1 is the lowest ranked mutex
  1289     // a global total order. For example m1 is the lowest ranked mutex
  1290       // that the thread holds and m2 is the mutex the thread is trying
  1290     // that the thread holds and m2 is the mutex the thread is trying
  1291       // to acquire, then  deadlock avoidance rules require that the rank
  1291     // to acquire, then  deadlock avoidance rules require that the rank
  1292       // of m2 be less  than the rank of m1.
  1292     // of m2 be less  than the rank of m1.
  1293       // The rank Mutex::native  is an exception in that it is not subject
  1293     // The rank Mutex::native  is an exception in that it is not subject
  1294       // to the verification rules.
  1294     // to the verification rules.
  1295       // Here are some further notes relating to mutex acquisition anomalies:
  1295     // Here are some further notes relating to mutex acquisition anomalies:
  1296       // . under Solaris, the interrupt lock gets acquired when doing
  1296     // . under Solaris, the interrupt lock gets acquired when doing
  1297       //   profiling, so any lock could be held.
  1297     //   profiling, so any lock could be held.
  1298       // . it is also ok to acquire Safepoint_lock at the very end while we
  1298     // . it is also ok to acquire Safepoint_lock at the very end while we
  1299       //   already hold Terminator_lock - may happen because of periodic safepoints
  1299     //   already hold Terminator_lock - may happen because of periodic safepoints
  1300       if (this->rank() != Mutex::native &&
  1300     if (this->rank() != Mutex::native &&
  1301           this->rank() != Mutex::suspend_resume &&
  1301         this->rank() != Mutex::suspend_resume &&
  1302           locks != NULL && locks->rank() <= this->rank() &&
  1302         locks != NULL && locks->rank() <= this->rank() &&
  1303           !SafepointSynchronize::is_at_safepoint() &&
  1303         !SafepointSynchronize::is_at_safepoint() &&
  1304           this != Interrupt_lock && this != ProfileVM_lock &&
  1304         this != Interrupt_lock && this != ProfileVM_lock &&
  1305           !(this == Safepoint_lock && contains(locks, Terminator_lock) &&
  1305         !(this == Safepoint_lock && contains(locks, Terminator_lock) &&
  1306             SafepointSynchronize::is_synchronizing())) {
  1306         SafepointSynchronize::is_synchronizing())) {
  1307         new_owner->print_owned_locks();
  1307       new_owner->print_owned_locks();
  1308         fatal(err_msg("acquiring lock %s/%d out of order with lock %s/%d -- "
  1308       fatal(err_msg("acquiring lock %s/%d out of order with lock %s/%d -- "
  1309                       "possible deadlock", this->name(), this->rank(),
  1309                     "possible deadlock", this->name(), this->rank(),
  1310                       locks->name(), locks->rank()));
  1310                     locks->name(), locks->rank()));
  1311       }
  1311     }
  1312 
  1312 
  1313       this->_next = new_owner->_owned_locks;
  1313     this->_next = new_owner->_owned_locks;
  1314       new_owner->_owned_locks = this;
  1314     new_owner->_owned_locks = this;
  1315     #endif
  1315     #endif
  1316 
  1316 
  1317   } else {
  1317   } else {
  1318     // the thread is releasing this lock
  1318     // the thread is releasing this lock
  1319 
  1319 
  1324     assert(old_owner == Thread::current(), "removing the owner thread of an unowned mutex");
  1324     assert(old_owner == Thread::current(), "removing the owner thread of an unowned mutex");
  1325 
  1325 
  1326     _owner = NULL; // set the owner
  1326     _owner = NULL; // set the owner
  1327 
  1327 
  1328     #ifdef ASSERT
  1328     #ifdef ASSERT
  1329       Monitor *locks = old_owner->owned_locks();
  1329     Monitor *locks = old_owner->owned_locks();
  1330 
  1330 
  1331       // remove "this" from the owned locks list
  1331     // remove "this" from the owned locks list
  1332 
  1332 
  1333       Monitor *prev = NULL;
  1333     Monitor *prev = NULL;
  1334       bool found = false;
  1334     bool found = false;
  1335       for (; locks != NULL; prev = locks, locks = locks->next()) {
  1335     for (; locks != NULL; prev = locks, locks = locks->next()) {
  1336         if (locks == this) {
  1336       if (locks == this) {
  1337           found = true;
  1337         found = true;
  1338           break;
  1338         break;
  1339         }
       
  1340       }
  1339       }
  1341       assert(found, "Removing a lock not owned");
  1340     }
  1342       if (prev == NULL) {
  1341     assert(found, "Removing a lock not owned");
  1343         old_owner->_owned_locks = _next;
  1342     if (prev == NULL) {
  1344       } else {
  1343       old_owner->_owned_locks = _next;
  1345         prev->_next = _next;
  1344     } else {
  1346       }
  1345       prev->_next = _next;
  1347       _next = NULL;
  1346     }
       
  1347     _next = NULL;
  1348     #endif
  1348     #endif
  1349   }
  1349   }
  1350 }
  1350 }
  1351 
  1351 
  1352 
  1352 
  1358     if (thread->is_VM_thread() && !allow_vm_block()) {
  1358     if (thread->is_VM_thread() && !allow_vm_block()) {
  1359       fatal(err_msg("VM thread using lock %s (not allowed to block on)",
  1359       fatal(err_msg("VM thread using lock %s (not allowed to block on)",
  1360                     name()));
  1360                     name()));
  1361     }
  1361     }
  1362     debug_only(if (rank() != Mutex::special) \
  1362     debug_only(if (rank() != Mutex::special) \
  1363       thread->check_for_valid_safepoint_state(false);)
  1363                thread->check_for_valid_safepoint_state(false);)
  1364   }
  1364   }
  1365   if (thread->is_Watcher_thread()) {
  1365   if (thread->is_Watcher_thread()) {
  1366     assert(!WatcherThread::watcher_thread()->has_crash_protection(),
  1366     assert(!WatcherThread::watcher_thread()->has_crash_protection(),
  1367         "locking not allowed when crash protection is set");
  1367            "locking not allowed when crash protection is set");
  1368   }
  1368   }
  1369 }
  1369 }
  1370 
  1370 
  1371 void Monitor::check_block_state(Thread *thread) {
  1371 void Monitor::check_block_state(Thread *thread) {
  1372   if (!_allow_vm_block && thread->is_VM_thread()) {
  1372   if (!_allow_vm_block && thread->is_VM_thread()) {