72 #include "oops/verifyOopClosure.hpp"
73 #include "prims/jvm_misc.hpp"
74 #include "prims/jvmtiDeferredUpdates.hpp"
75 #include "prims/jvmtiExport.hpp"
76 #include "prims/jvmtiThreadState.hpp"
77 #include "runtime/arguments.hpp"
78 #include "runtime/atomic.hpp"
79 #include "runtime/biasedLocking.hpp"
80 #include "runtime/fieldDescriptor.inline.hpp"
81 #include "runtime/flags/jvmFlagLimit.hpp"
82 #include "runtime/deoptimization.hpp"
83 #include "runtime/frame.inline.hpp"
84 #include "runtime/handles.inline.hpp"
85 #include "runtime/handshake.hpp"
86 #include "runtime/init.hpp"
87 #include "runtime/interfaceSupport.inline.hpp"
88 #include "runtime/java.hpp"
89 #include "runtime/javaCalls.hpp"
90 #include "runtime/jniHandles.inline.hpp"
91 #include "runtime/jniPeriodicChecker.hpp"
92 #include "runtime/monitorDeflationThread.hpp"
93 #include "runtime/mutexLocker.hpp"
94 #include "runtime/nonJavaThread.hpp"
95 #include "runtime/objectMonitor.hpp"
96 #include "runtime/orderAccess.hpp"
97 #include "runtime/osThread.hpp"
98 #include "runtime/prefetch.inline.hpp"
99 #include "runtime/safepoint.hpp"
100 #include "runtime/safepointMechanism.inline.hpp"
101 #include "runtime/safepointVerifiers.hpp"
102 #include "runtime/serviceThread.hpp"
103 #include "runtime/sharedRuntime.hpp"
104 #include "runtime/stackFrameStream.inline.hpp"
105 #include "runtime/stackWatermarkSet.hpp"
106 #include "runtime/statSampler.hpp"
107 #include "runtime/task.hpp"
108 #include "runtime/thread.inline.hpp"
109 #include "runtime/threadCritical.hpp"
110 #include "runtime/threadSMR.inline.hpp"
111 #include "runtime/threadStatisticalInfo.hpp"
112 #include "runtime/threadWXSetters.inline.hpp"
113 #include "runtime/timer.hpp"
114 #include "runtime/timerTrace.hpp"
115 #include "runtime/vframe.inline.hpp"
685 void Thread::print_owned_locks_on(outputStream* st) const {
686 Mutex* cur = _owned_locks;
687 if (cur == NULL) {
688 st->print(" (no locks) ");
689 } else {
690 st->print_cr(" Locks owned:");
691 while (cur) {
692 cur->print_on(st);
693 cur = cur->next();
694 }
695 }
696 }
697 #endif // ASSERT
698
699 // We had to move these methods here, because vm threads get into ObjectSynchronizer::enter
700 // However, there is a note in JavaThread::is_lock_owned() about the VM threads not being
701 // used for compilation in the future. If that change is made, the need for these methods
702 // should be revisited, and they should be removed if possible.
703
704 bool Thread::is_lock_owned(address adr) const {
705 return is_in_full_stack(adr);
706 }
707
708 bool Thread::set_as_starting_thread() {
709 assert(_starting_thread == NULL, "already initialized: "
710 "_starting_thread=" INTPTR_FORMAT, p2i(_starting_thread));
711 // NOTE: this must be called inside the main thread.
712 DEBUG_ONLY(_starting_thread = this;)
713 return os::create_main_thread(this->as_Java_thread());
714 }
715
716 static void initialize_class(Symbol* class_name, TRAPS) {
717 Klass* klass = SystemDictionary::resolve_or_fail(class_name, true, CHECK);
718 InstanceKlass::cast(klass)->initialize(CHECK);
719 }
720
721
722 // Creates the initial ThreadGroup
723 static Handle create_initial_thread_group(TRAPS) {
724 Handle system_instance = JavaCalls::construct_new_instance(
1074
1075 // JVMTI PopFrame support
1076 _popframe_condition(popframe_inactive),
1077 _frames_to_pop_failed_realloc(0),
1078
1079 _handshake(this),
1080
1081 _popframe_preserved_args(nullptr),
1082 _popframe_preserved_args_size(0),
1083
1084 _jvmti_thread_state(nullptr),
1085 _interp_only_mode(0),
1086 _should_post_on_exceptions_flag(JNI_FALSE),
1087 _thread_stat(new ThreadStatistics()),
1088
1089 _parker(),
1090 _cached_monitor_info(nullptr),
1091
1092 _class_to_be_initialized(nullptr),
1093
1094 _SleepEvent(ParkEvent::Allocate(this))
1095 {
1096 set_jni_functions(jni_functions());
1097
1098 #if INCLUDE_JVMCI
1099 assert(_jvmci._implicit_exception_pc == nullptr, "must be");
1100 if (JVMCICounterSize > 0) {
1101 resize_counters(0, (int) JVMCICounterSize);
1102 }
1103 #endif // INCLUDE_JVMCI
1104
1105 // Setup safepoint state info for this thread
1106 ThreadSafepointState::create(this);
1107
1108 SafepointMechanism::initialize_header(this);
1109
1110 set_requires_cross_modify_fence(false);
1111
1112 pd_initialize();
1113 assert(deferred_card_mark().is_empty(), "Default MemRegion ctor");
1114 }
1552 tlab().retire();
1553 }
1554
1555 Threads::remove(this, is_daemon);
1556 this->smr_delete();
1557 }
1558
1559 JavaThread* JavaThread::active() {
1560 Thread* thread = Thread::current();
1561 if (thread->is_Java_thread()) {
1562 return thread->as_Java_thread();
1563 } else {
1564 assert(thread->is_VM_thread(), "this must be a vm thread");
1565 VM_Operation* op = ((VMThread*) thread)->vm_operation();
1566 JavaThread *ret = op == NULL ? NULL : op->calling_thread()->as_Java_thread();
1567 return ret;
1568 }
1569 }
1570
1571 bool JavaThread::is_lock_owned(address adr) const {
1572 if (Thread::is_lock_owned(adr)) return true;
1573
1574 for (MonitorChunk* chunk = monitor_chunks(); chunk != NULL; chunk = chunk->next()) {
1575 if (chunk->contains(adr)) return true;
1576 }
1577
1578 return false;
1579 }
1580
1581 oop JavaThread::exception_oop() const {
1582 return Atomic::load(&_exception_oop);
1583 }
1584
1585 void JavaThread::set_exception_oop(oop o) {
1586 Atomic::store(&_exception_oop, o);
1587 }
1588
1589 void JavaThread::add_monitor_chunk(MonitorChunk* chunk) {
1590 chunk->set_next(monitor_chunks());
1591 set_monitor_chunks(chunk);
1592 }
2002 // written
2003 GrowableArray<jvmtiDeferredLocalVariableSet*>* list = JvmtiDeferredUpdates::deferred_locals(this);
2004 if (list != NULL) {
2005 for (int i = 0; i < list->length(); i++) {
2006 list->at(i)->oops_do(f);
2007 }
2008 }
2009
2010 // Traverse instance variables at the end since the GC may be moving things
2011 // around using this function
2012 f->do_oop((oop*) &_vm_result);
2013 f->do_oop((oop*) &_exception_oop);
2014 f->do_oop((oop*) &_pending_async_exception);
2015 #if INCLUDE_JVMCI
2016 f->do_oop((oop*) &_jvmci_reserved_oop0);
2017 #endif
2018
2019 if (jvmti_thread_state() != NULL) {
2020 jvmti_thread_state()->oops_do(f, cf);
2021 }
2022 }
2023
2024 void JavaThread::oops_do_frames(OopClosure* f, CodeBlobClosure* cf) {
2025 if (!has_last_Java_frame()) {
2026 return;
2027 }
2028 // Finish any pending lazy GC activity for the frames
2029 StackWatermarkSet::finish_processing(this, NULL /* context */, StackWatermarkKind::gc);
2030 // Traverse the execution stack
2031 for (StackFrameStream fst(this, true /* update */, false /* process_frames */); !fst.is_done(); fst.next()) {
2032 fst.current()->oops_do(f, cf, fst.register_map());
2033 }
2034 }
2035
2036 #ifdef ASSERT
2037 void JavaThread::verify_states_for_handshake() {
2038 // This checks that the thread has a correct frame state during a handshake.
2039 verify_frame_info();
2040 }
2041 #endif
3692
3693 int i = 0;
3694 DO_JAVA_THREADS(t_list, p) {
3695 if (!p->can_call_java()) continue;
3696
3697 // The first stage of async deflation does not affect any field
3698 // used by this comparison so the ObjectMonitor* is usable here.
3699 address pending = (address)p->current_pending_monitor();
3700 if (pending == monitor) { // found a match
3701 if (i < count) result->append(p); // save the first count matches
3702 i++;
3703 }
3704 }
3705
3706 return result;
3707 }
3708
3709
3710 JavaThread *Threads::owning_thread_from_monitor_owner(ThreadsList * t_list,
3711 address owner) {
3712 // NULL owner means not locked so we can skip the search
3713 if (owner == NULL) return NULL;
3714
3715 DO_JAVA_THREADS(t_list, p) {
3716 // first, see if owner is the address of a Java thread
3717 if (owner == (address)p) return p;
3718 }
3719
3720 // Cannot assert on lack of success here since this function may be
3721 // used by code that is trying to report useful problem information
3722 // like deadlock detection.
3723 if (UseHeavyMonitors) return NULL;
3724
3725 // If we didn't find a matching Java thread and we didn't force use of
3726 // heavyweight monitors, then the owner is the stack address of the
3727 // Lock Word in the owning Java thread's stack.
3728 //
3729 JavaThread* the_owner = NULL;
3730 DO_JAVA_THREADS(t_list, q) {
3731 if (q->is_lock_owned(owner)) {
3732 the_owner = q;
3733 break;
3734 }
3735 }
3736
3737 // cannot assert on lack of success here; see above comment
3738 return the_owner;
3739 }
3740
3741 class PrintOnClosure : public ThreadClosure {
3742 private:
3743 outputStream* _st;
3744
3745 public:
3746 PrintOnClosure(outputStream* st) :
3747 _st(st) {}
3748
3749 virtual void do_thread(Thread* thread) {
3750 if (thread != NULL) {
3751 thread->print_on(_st);
3752 _st->cr();
3753 }
3754 }
3755 };
3756
3757 // Threads::print_on() is called at safepoint by VM_PrintThreads operation.
3758 void Threads::print_on(outputStream* st, bool print_stacks,
3759 bool internal_format, bool print_concurrent_locks,
3760 bool print_extended_info) {
|
72 #include "oops/verifyOopClosure.hpp"
73 #include "prims/jvm_misc.hpp"
74 #include "prims/jvmtiDeferredUpdates.hpp"
75 #include "prims/jvmtiExport.hpp"
76 #include "prims/jvmtiThreadState.hpp"
77 #include "runtime/arguments.hpp"
78 #include "runtime/atomic.hpp"
79 #include "runtime/biasedLocking.hpp"
80 #include "runtime/fieldDescriptor.inline.hpp"
81 #include "runtime/flags/jvmFlagLimit.hpp"
82 #include "runtime/deoptimization.hpp"
83 #include "runtime/frame.inline.hpp"
84 #include "runtime/handles.inline.hpp"
85 #include "runtime/handshake.hpp"
86 #include "runtime/init.hpp"
87 #include "runtime/interfaceSupport.inline.hpp"
88 #include "runtime/java.hpp"
89 #include "runtime/javaCalls.hpp"
90 #include "runtime/jniHandles.inline.hpp"
91 #include "runtime/jniPeriodicChecker.hpp"
92 #include "runtime/lockStack.inline.hpp"
93 #include "runtime/monitorDeflationThread.hpp"
94 #include "runtime/mutexLocker.hpp"
95 #include "runtime/nonJavaThread.hpp"
96 #include "runtime/objectMonitor.inline.hpp"
97 #include "runtime/orderAccess.hpp"
98 #include "runtime/osThread.hpp"
99 #include "runtime/prefetch.inline.hpp"
100 #include "runtime/safepoint.hpp"
101 #include "runtime/safepointMechanism.inline.hpp"
102 #include "runtime/safepointVerifiers.hpp"
103 #include "runtime/serviceThread.hpp"
104 #include "runtime/sharedRuntime.hpp"
105 #include "runtime/stackFrameStream.inline.hpp"
106 #include "runtime/stackWatermarkSet.hpp"
107 #include "runtime/statSampler.hpp"
108 #include "runtime/task.hpp"
109 #include "runtime/thread.inline.hpp"
110 #include "runtime/threadCritical.hpp"
111 #include "runtime/threadSMR.inline.hpp"
112 #include "runtime/threadStatisticalInfo.hpp"
113 #include "runtime/threadWXSetters.inline.hpp"
114 #include "runtime/timer.hpp"
115 #include "runtime/timerTrace.hpp"
116 #include "runtime/vframe.inline.hpp"
686 void Thread::print_owned_locks_on(outputStream* st) const {
687 Mutex* cur = _owned_locks;
688 if (cur == NULL) {
689 st->print(" (no locks) ");
690 } else {
691 st->print_cr(" Locks owned:");
692 while (cur) {
693 cur->print_on(st);
694 cur = cur->next();
695 }
696 }
697 }
698 #endif // ASSERT
699
700 // We had to move these methods here, because vm threads get into ObjectSynchronizer::enter
701 // However, there is a note in JavaThread::is_lock_owned() about the VM threads not being
702 // used for compilation in the future. If that change is made, the need for these methods
703 // should be revisited, and they should be removed if possible.
704
705 bool Thread::is_lock_owned(address adr) const {
706 assert(!UseFastLocking, "maybe not call that?");
707 return is_in_full_stack(adr);
708 }
709
710 bool Thread::set_as_starting_thread() {
711 assert(_starting_thread == NULL, "already initialized: "
712 "_starting_thread=" INTPTR_FORMAT, p2i(_starting_thread));
713 // NOTE: this must be called inside the main thread.
714 DEBUG_ONLY(_starting_thread = this;)
715 return os::create_main_thread(this->as_Java_thread());
716 }
717
718 static void initialize_class(Symbol* class_name, TRAPS) {
719 Klass* klass = SystemDictionary::resolve_or_fail(class_name, true, CHECK);
720 InstanceKlass::cast(klass)->initialize(CHECK);
721 }
722
723
724 // Creates the initial ThreadGroup
725 static Handle create_initial_thread_group(TRAPS) {
726 Handle system_instance = JavaCalls::construct_new_instance(
1076
1077 // JVMTI PopFrame support
1078 _popframe_condition(popframe_inactive),
1079 _frames_to_pop_failed_realloc(0),
1080
1081 _handshake(this),
1082
1083 _popframe_preserved_args(nullptr),
1084 _popframe_preserved_args_size(0),
1085
1086 _jvmti_thread_state(nullptr),
1087 _interp_only_mode(0),
1088 _should_post_on_exceptions_flag(JNI_FALSE),
1089 _thread_stat(new ThreadStatistics()),
1090
1091 _parker(),
1092 _cached_monitor_info(nullptr),
1093
1094 _class_to_be_initialized(nullptr),
1095
1096 _SleepEvent(ParkEvent::Allocate(this)),
1097 _lock_stack()
1098 {
1099 set_jni_functions(jni_functions());
1100
1101 #if INCLUDE_JVMCI
1102 assert(_jvmci._implicit_exception_pc == nullptr, "must be");
1103 if (JVMCICounterSize > 0) {
1104 resize_counters(0, (int) JVMCICounterSize);
1105 }
1106 #endif // INCLUDE_JVMCI
1107
1108 // Setup safepoint state info for this thread
1109 ThreadSafepointState::create(this);
1110
1111 SafepointMechanism::initialize_header(this);
1112
1113 set_requires_cross_modify_fence(false);
1114
1115 pd_initialize();
1116 assert(deferred_card_mark().is_empty(), "Default MemRegion ctor");
1117 }
1555 tlab().retire();
1556 }
1557
1558 Threads::remove(this, is_daemon);
1559 this->smr_delete();
1560 }
1561
1562 JavaThread* JavaThread::active() {
1563 Thread* thread = Thread::current();
1564 if (thread->is_Java_thread()) {
1565 return thread->as_Java_thread();
1566 } else {
1567 assert(thread->is_VM_thread(), "this must be a vm thread");
1568 VM_Operation* op = ((VMThread*) thread)->vm_operation();
1569 JavaThread *ret = op == NULL ? NULL : op->calling_thread()->as_Java_thread();
1570 return ret;
1571 }
1572 }
1573
1574 bool JavaThread::is_lock_owned(address adr) const {
1575 assert(!UseFastLocking, "should not be called with fast-locking");
1576 if (Thread::is_lock_owned(adr)) return true;
1577
1578 for (MonitorChunk* chunk = monitor_chunks(); chunk != NULL; chunk = chunk->next()) {
1579 if (chunk->contains(adr)) return true;
1580 }
1581
1582 return false;
1583 }
1584
1585 oop JavaThread::exception_oop() const {
1586 return Atomic::load(&_exception_oop);
1587 }
1588
1589 void JavaThread::set_exception_oop(oop o) {
1590 Atomic::store(&_exception_oop, o);
1591 }
1592
1593 void JavaThread::add_monitor_chunk(MonitorChunk* chunk) {
1594 chunk->set_next(monitor_chunks());
1595 set_monitor_chunks(chunk);
1596 }
2006 // written
2007 GrowableArray<jvmtiDeferredLocalVariableSet*>* list = JvmtiDeferredUpdates::deferred_locals(this);
2008 if (list != NULL) {
2009 for (int i = 0; i < list->length(); i++) {
2010 list->at(i)->oops_do(f);
2011 }
2012 }
2013
2014 // Traverse instance variables at the end since the GC may be moving things
2015 // around using this function
2016 f->do_oop((oop*) &_vm_result);
2017 f->do_oop((oop*) &_exception_oop);
2018 f->do_oop((oop*) &_pending_async_exception);
2019 #if INCLUDE_JVMCI
2020 f->do_oop((oop*) &_jvmci_reserved_oop0);
2021 #endif
2022
2023 if (jvmti_thread_state() != NULL) {
2024 jvmti_thread_state()->oops_do(f, cf);
2025 }
2026
2027 if (!UseHeavyMonitors && UseFastLocking) {
2028 lock_stack().oops_do(f);
2029 }
2030 }
2031
2032 void JavaThread::oops_do_frames(OopClosure* f, CodeBlobClosure* cf) {
2033 if (!has_last_Java_frame()) {
2034 return;
2035 }
2036 // Finish any pending lazy GC activity for the frames
2037 StackWatermarkSet::finish_processing(this, NULL /* context */, StackWatermarkKind::gc);
2038 // Traverse the execution stack
2039 for (StackFrameStream fst(this, true /* update */, false /* process_frames */); !fst.is_done(); fst.next()) {
2040 fst.current()->oops_do(f, cf, fst.register_map());
2041 }
2042 }
2043
2044 #ifdef ASSERT
2045 void JavaThread::verify_states_for_handshake() {
2046 // This checks that the thread has a correct frame state during a handshake.
2047 verify_frame_info();
2048 }
2049 #endif
3700
3701 int i = 0;
3702 DO_JAVA_THREADS(t_list, p) {
3703 if (!p->can_call_java()) continue;
3704
3705 // The first stage of async deflation does not affect any field
3706 // used by this comparison so the ObjectMonitor* is usable here.
3707 address pending = (address)p->current_pending_monitor();
3708 if (pending == monitor) { // found a match
3709 if (i < count) result->append(p); // save the first count matches
3710 i++;
3711 }
3712 }
3713
3714 return result;
3715 }
3716
3717
3718 JavaThread *Threads::owning_thread_from_monitor_owner(ThreadsList * t_list,
3719 address owner) {
3720 assert(!UseFastLocking, "only with stack-locking");
3721 // NULL owner means not locked so we can skip the search
3722 if (owner == NULL) return NULL;
3723
3724 DO_JAVA_THREADS(t_list, p) {
3725 // first, see if owner is the address of a Java thread
3726 if (owner == (address)p) return p;
3727 }
3728
3729 // Cannot assert on lack of success here since this function may be
3730 // used by code that is trying to report useful problem information
3731 // like deadlock detection.
3732 if (UseHeavyMonitors) return NULL;
3733
3734 // If we didn't find a matching Java thread and we didn't force use of
3735 // heavyweight monitors, then the owner is the stack address of the
3736 // Lock Word in the owning Java thread's stack.
3737 //
3738 JavaThread* the_owner = NULL;
3739 DO_JAVA_THREADS(t_list, q) {
3740 if (q->is_lock_owned(owner)) {
3741 the_owner = q;
3742 break;
3743 }
3744 }
3745
3746 // cannot assert on lack of success here; see above comment
3747 return the_owner;
3748 }
3749
3750 JavaThread* Threads::owning_thread_from_object(ThreadsList * t_list, oop obj) {
3751 assert(UseFastLocking, "Only with fast-locking");
3752 DO_JAVA_THREADS(t_list, q) {
3753 if (q->lock_stack().contains(obj)) {
3754 return q;
3755 }
3756 }
3757 return NULL;
3758 }
3759
3760 JavaThread* Threads::owning_thread_from_monitor(ThreadsList* t_list, ObjectMonitor* monitor) {
3761 if (UseFastLocking) {
3762 void* raw_owner = monitor->owner_raw();
3763 if (raw_owner == ANONYMOUS_OWNER) {
3764 return owning_thread_from_object(t_list, monitor->object());
3765 } else if (raw_owner == DEFLATER_MARKER) {
3766 return NULL;
3767 } else {
3768 Thread* owner = reinterpret_cast<Thread*>(raw_owner);
3769 #ifdef ASSERT
3770 if (owner != NULL) {
3771 bool found = false;
3772 DO_JAVA_THREADS(t_list, q) {
3773 if (q == owner) {
3774 found = true;;
3775 break;
3776 }
3777 }
3778 assert(found, "owner is not a thread: " PTR_FORMAT, p2i(owner));
3779 }
3780 #endif
3781 assert(owner == NULL || owner->is_Java_thread(), "only JavaThreads own monitors");
3782 return reinterpret_cast<JavaThread*>(owner);
3783 }
3784 } else {
3785 return owning_thread_from_monitor_owner(t_list, (address)monitor->owner());
3786 }
3787 }
3788
3789 class PrintOnClosure : public ThreadClosure {
3790 private:
3791 outputStream* _st;
3792
3793 public:
3794 PrintOnClosure(outputStream* st) :
3795 _st(st) {}
3796
3797 virtual void do_thread(Thread* thread) {
3798 if (thread != NULL) {
3799 thread->print_on(_st);
3800 _st->cr();
3801 }
3802 }
3803 };
3804
3805 // Threads::print_on() is called at safepoint by VM_PrintThreads operation.
3806 void Threads::print_on(outputStream* st, bool print_stacks,
3807 bool internal_format, bool print_concurrent_locks,
3808 bool print_extended_info) {
|