--- old/src/cpu/x86/vm/macroAssembler_x86.cpp Fri Jul 3 16:39:40 2015 +++ new/src/cpu/x86/vm/macroAssembler_x86.cpp Fri Jul 3 16:39:39 2015 @@ -1781,6 +1781,7 @@ cmpxchgptr(scrReg, Address(boxReg, OM_OFFSET_NO_MONITOR_VALUE_TAG(owner))); } else if ((EmitSync & 128) == 0) { // avoid ST-before-CAS + // register juggle because we need tmpReg for cmpxchgptr below movptr(scrReg, boxReg); movptr(boxReg, tmpReg); // consider: LEA box, [tmp-2] @@ -1814,7 +1815,10 @@ } cmpxchgptr(scrReg, Address(boxReg, OM_OFFSET_NO_MONITOR_VALUE_TAG(owner))); movptr(Address(scrReg, 0), 3); // box->_displaced_header = 3 + // If we weren't able to swing _owner from NULL to the BasicLock + // then take the slow path. jccb (Assembler::notZero, DONE_LABEL); + // update _owner from from BasicLock to thread get_thread (scrReg); // beware: clobbers ICCs movptr(Address(boxReg, OM_OFFSET_NO_MONITOR_VALUE_TAG(owner)), scrReg); xorptr(boxReg, boxReg); // set icc.ZFlag = 1 to indicate success @@ -2083,6 +2087,9 @@ xorptr(boxReg, boxReg); // box is really EAX if (os::is_MP()) { lock(); } cmpxchgptr(rsp, Address(tmpReg, OM_OFFSET_NO_MONITOR_VALUE_TAG(owner))); + // There's no successor so we tried to regrab the lock with + // the placeholder value. If that didn't work, then another + // grabbed the lock so we're done (and exit was a success). jccb (Assembler::notEqual, LSuccess); // Since we're low on registers we installed rsp as a placeholding in _owner. // Now install Self over rsp. This is safe as we're transitioning from @@ -2190,6 +2197,9 @@ movptr(boxReg, (int32_t)NULL_WORD); if (os::is_MP()) { lock(); } cmpxchgptr(r15_thread, Address(tmpReg, OM_OFFSET_NO_MONITOR_VALUE_TAG(owner))); + // There's no successor so we tried to regrab the lock. + // If that didn't work, then another grabbed the lock + // so we're done (and exit was a success). jccb (Assembler::notEqual, LSuccess); // Intentional fall-through into slow-path --- old/src/share/vm/oops/markOop.cpp Fri Jul 3 16:39:42 2015 +++ new/src/share/vm/oops/markOop.cpp Fri Jul 3 16:39:42 2015 @@ -32,26 +32,32 @@ void markOopDesc::print_on(outputStream* st) const { if (is_marked()) { st->print(" marked(" INTPTR_FORMAT ")", value()); + } else if (has_monitor()) { + // have to check has_monitor() before is_locked() + st->print(" monitor(" INTPTR_FORMAT ")=", value()); + ObjectMonitor* mon = monitor(); + if (mon == NULL) { + st->print("NULL"); // this should not happen + } else { + BasicLock * bl = (BasicLock *) mon->owner(); + st->print("{count=" INTPTR_FORMAT ",waiters=" INTPTR_FORMAT + ",recursions=" INTPTR_FORMAT ",owner=" INTPTR_FORMAT "}", + mon->count(), mon->waiters(), mon->recursions(), p2i(bl)); + } } else if (is_locked()) { st->print(" locked(" INTPTR_FORMAT ")->", value()); if (is_neutral()) { st->print("is_neutral"); - if (has_no_hash()) st->print(" no_hash"); - else st->print(" hash=" INTPTR_FORMAT, hash()); + if (has_no_hash()) { + st->print(" no_hash"); + } else { + st->print(" hash=" INTPTR_FORMAT, hash()); + } st->print(" age=%d", age()); } else if (has_bias_pattern()) { st->print("is_biased"); JavaThread* jt = biased_locker(); st->print(" biased_locker=" INTPTR_FORMAT, p2i(jt)); - } else if (has_monitor()) { - ObjectMonitor* mon = monitor(); - if (mon == NULL) - st->print("monitor=NULL"); - else { - BasicLock * bl = (BasicLock *) mon->owner(); - st->print("monitor={count=" INTPTR_FORMAT ",waiters=" INTPTR_FORMAT ",recursions=" INTPTR_FORMAT ",owner=" INTPTR_FORMAT "}", - mon->count(), mon->waiters(), mon->recursions(), p2i(bl)); - } } else { st->print("??"); } --- old/src/share/vm/runtime/globals.hpp Fri Jul 3 16:39:45 2015 +++ new/src/share/vm/runtime/globals.hpp Fri Jul 3 16:39:44 2015 @@ -672,6 +672,9 @@ "Print information about Java monitor locks when the stacks are" \ "dumped") \ \ + diagnostic(bool, VerboseStackTrace, false, \ + "Print extra information when the stacks are dumped") \ + \ product_pd(bool, UseLargePages, \ "Use large page memory") \ \ @@ -2791,6 +2794,12 @@ product(bool, DisplayVMOutputToStdout, false, \ "If DisplayVMOutput is true, display all VM output to stdout") \ \ + diagnostic(bool, GuaranteeOnMonitorMismatch, false, \ + "Guarantee on monitor mismatch detected at JavaThread exit") \ + \ + product(bool, JavaThreadExitReleasesMonitors, false, \ + "JavaThread exit() releases monitors owned by thread") \ + \ product(bool, UseHeavyMonitors, false, \ "use heavyweight instead of lightweight Java monitors") \ \ --- old/src/share/vm/runtime/objectMonitor.cpp Fri Jul 3 16:39:47 2015 +++ new/src/share/vm/runtime/objectMonitor.cpp Fri Jul 3 16:39:46 2015 @@ -1,5 +1,5 @@ /* - * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1998, 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -251,24 +251,6 @@ // ----------------------------------------------------------------------------- // Enter support -bool ObjectMonitor::try_enter(Thread* THREAD) { - if (THREAD != _owner) { - if (THREAD->is_lock_owned ((address)_owner)) { - assert(_recursions == 0, "internal state error"); - _owner = THREAD; - _recursions = 1; - return true; - } - if (Atomic::cmpxchg_ptr (THREAD, &_owner, NULL) != NULL) { - return false; - } - return true; - } else { - _recursions++; - return true; - } -} - void NOINLINE ObjectMonitor::enter(TRAPS) { // The following code is ordered to check the most common cases first // and to reduce RTS->RTO cache line upgrades on SPARC and IA32 processors. @@ -2272,7 +2254,7 @@ } inline void ObjectMonitor::AddWaiter(ObjectWaiter* node) { - assert(node != NULL, "should not dequeue NULL node"); + assert(node != NULL, "should not add NULL node"); assert(node->_prev == NULL, "node already in list"); assert(node->_next == NULL, "node already in list"); // put node at end of queue (circular doubly linked list) --- old/src/share/vm/runtime/objectMonitor.hpp Fri Jul 3 16:39:49 2015 +++ new/src/share/vm/runtime/objectMonitor.hpp Fri Jul 3 16:39:49 2015 @@ -1,5 +1,5 @@ /* - * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1998, 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -317,7 +317,6 @@ void print(); #endif - bool try_enter(TRAPS); void enter(TRAPS); void exit(bool not_suspended, TRAPS); void wait(jlong millis, bool interruptable, TRAPS); --- old/src/share/vm/runtime/synchronizer.cpp Fri Jul 3 16:39:52 2015 +++ new/src/share/vm/runtime/synchronizer.cpp Fri Jul 3 16:39:51 2015 @@ -1643,6 +1643,10 @@ ReleaseJavaMonitorsClosure(Thread* thread) : THREAD(thread) {} void do_monitor(ObjectMonitor* mid) { if (mid->owner() == THREAD) { + guarantee(!GuaranteeOnMonitorMismatch, + err_msg("exiting JavaThread=" INTPTR_FORMAT + " unexpectedly owns ObjectMonitor=" INTPTR_FORMAT, + THREAD, mid)); (void)mid->complete_exit(CHECK); } } --- old/src/share/vm/runtime/thread.cpp Fri Jul 3 16:39:54 2015 +++ new/src/share/vm/runtime/thread.cpp Fri Jul 3 16:39:53 2015 @@ -1807,9 +1807,16 @@ // owned monitors at this point MUST be JNI-acquired monitors which are // pre-inflated and in the monitor cache. // - // ensure_join() ignores IllegalThreadStateExceptions, and so does this. - if (exit_type == jni_detach && JNIDetachReleasesMonitors) { - assert(!this->has_last_Java_frame(), "detaching with Java frames?"); + // Optionally release any monitors for regular JavaThread exits. This + // is provided as a work around for any bugs in monitor enter-exit + // matching. This can be expensive so it is not enabled by default. + // + // ensure_join() ignores IllegalThreadStateExceptions, and so does + // ObjectSynchronizer::release_monitors_owned_by_thread(). + if ((exit_type == jni_detach && JNIDetachReleasesMonitors) || + JavaThreadExitReleasesMonitors) { + assert(!this->has_last_Java_frame(), + "should not have a Java frame when detaching or exiting"); ObjectSynchronizer::release_monitors_owned_by_thread(this); assert(!this->has_pending_exception(), "release_monitors should have cleared"); } --- old/src/share/vm/runtime/vframe.cpp Fri Jul 3 16:39:56 2015 +++ new/src/share/vm/runtime/vframe.cpp Fri Jul 3 16:39:55 2015 @@ -160,17 +160,29 @@ void javaVFrame::print_lock_info_on(outputStream* st, int frame_count) { ResourceMark rm; - // If this is the first frame, and java.lang.Object.wait(...) then print out the receiver. + // If this is the first frame and it is java.lang.Object.wait(...) + // then print out the receiver. Locals are not always available, + // e.g., compiled native frames have no scope so there are no locals. if (frame_count == 0) { if (method()->name() == vmSymbols::wait_name() && method()->method_holder()->name() == vmSymbols::java_lang_Object()) { + const char *wait_state = "waiting on"; // assume we are waiting + // It would be nice to distinguish between "waiting on" and + // "waited on". Currently, "waiting on" here with a + // java.lang.Thread.State == "WAITING (on object monitor)" + // earlier in the output means that the monitor has not yet been + // notified and java.lang.Thread.State == "BLOCKED (on object + // monitor)" earlier in the output means that the monitor has + // been notified and the thread is blocked on reentry. StackValueCollection* locs = locals(); if (!locs->is_empty()) { StackValue* sv = locs->at(0); if (sv->type() == T_OBJECT) { Handle o = locs->at(0)->get_obj(); - print_locked_object_class_name(st, o, "waiting on"); + print_locked_object_class_name(st, o, wait_state); } + } else { + st->print_cr("\t- %s ", wait_state); } } else if (thread()->current_park_blocker() != NULL) { oop obj = thread()->current_park_blocker(); @@ -180,7 +192,8 @@ } - // Print out all monitors that we have locked or are trying to lock + // Print out all monitors that we have locked, are trying to lock + // or are trying to relock after a wait(). GrowableArray* mons = monitors(); if (!mons->is_empty()) { bool found_first_monitor = false; @@ -202,14 +215,14 @@ if (monitor->owner() != NULL) { // the monitor is associated with an object, i.e., it is locked - // First, assume we have the monitor locked. If we haven't found an - // owned monitor before and this is the first frame, then we need to - // see if we have completed the lock or we are blocked trying to - // acquire it - we can only be blocked if the monitor is inflated - markOop mark = NULL; const char *lock_state = "locked"; // assume we have the monitor locked if (!found_first_monitor && frame_count == 0) { + // If this is the first frame and we haven't found an owned + // monitor before, then we need to see if we have completed + // the lock or if we are blocked trying to acquire it. Only + // an inflated monitor that is first on the monitor list in + // the first frame can block us on a monitor enter. mark = monitor->owner()->mark(); if (mark->has_monitor() && ( // we have marked ourself as pending on this monitor @@ -219,13 +232,33 @@ )) { lock_state = "waiting to lock"; } else { - mark = NULL; // Disable printing below + // We own the monitor which is not as interesting so + // disable the extra printing below. + mark = NULL; } + } else if (frame_count != 0) { + // This is not the first frame so we either own this monitor + // or we owned the monitor before and called wait(). Because + // wait() could have been called on any monitor in a lower + // numbered frame on the stack, we have to check all the + // monitors on the list for this frame. + mark = monitor->owner()->mark(); + if (mark->has_monitor() && + ( // we have marked ourself as pending on this monitor + mark->monitor() == thread()->current_pending_monitor() || + // we are not the owner of this monitor + !mark->monitor()->is_entered(thread()) + )) { + lock_state = "waiting to relock"; + } else { + // We own the monitor which is not as interesting so + // disable the extra printing below. + mark = NULL; + } } print_locked_object_class_name(st, monitor->owner(), lock_state); - if (Verbose && mark != NULL) { - // match with format above, replacing "-" with " ". - st->print("\t lockbits="); + if (VerboseStackTrace && mark != NULL) { + st->print("\t- lockbits="); mark->print_on(st); st->cr(); }