71 #include "runtime/atomic.hpp"
72 #include "runtime/biasedLocking.hpp"
73 #include "runtime/fieldDescriptor.inline.hpp"
74 #include "runtime/handles.inline.hpp"
75 #include "runtime/javaCalls.hpp"
76 #include "runtime/mutexLocker.hpp"
77 #include "runtime/orderAccess.hpp"
78 #include "runtime/thread.inline.hpp"
79 #include "services/classLoadingService.hpp"
80 #include "services/threadService.hpp"
81 #include "utilities/dtrace.hpp"
82 #include "utilities/events.hpp"
83 #include "utilities/macros.hpp"
84 #include "utilities/stringUtils.hpp"
85 #ifdef COMPILER1
86 #include "c1/c1_Compiler.hpp"
87 #endif
88 #if INCLUDE_JFR
89 #include "jfr/jfrEvents.hpp"
90 #endif
91
92
93 #ifdef DTRACE_ENABLED
94
95
96 #define HOTSPOT_CLASS_INITIALIZATION_required HOTSPOT_CLASS_INITIALIZATION_REQUIRED
97 #define HOTSPOT_CLASS_INITIALIZATION_recursive HOTSPOT_CLASS_INITIALIZATION_RECURSIVE
98 #define HOTSPOT_CLASS_INITIALIZATION_concurrent HOTSPOT_CLASS_INITIALIZATION_CONCURRENT
99 #define HOTSPOT_CLASS_INITIALIZATION_erroneous HOTSPOT_CLASS_INITIALIZATION_ERRONEOUS
100 #define HOTSPOT_CLASS_INITIALIZATION_super__failed HOTSPOT_CLASS_INITIALIZATION_SUPER_FAILED
101 #define HOTSPOT_CLASS_INITIALIZATION_clinit HOTSPOT_CLASS_INITIALIZATION_CLINIT
102 #define HOTSPOT_CLASS_INITIALIZATION_error HOTSPOT_CLASS_INITIALIZATION_ERROR
103 #define HOTSPOT_CLASS_INITIALIZATION_end HOTSPOT_CLASS_INITIALIZATION_END
104 #define DTRACE_CLASSINIT_PROBE(type, thread_type) \
105 { \
106 char* data = NULL; \
107 int len = 0; \
108 Symbol* clss_name = name(); \
109 if (clss_name != NULL) { \
110 data = (char*)clss_name->bytes(); \
697 HandleMark hm(THREAD);
698 Handle h_init_lock(THREAD, init_lock());
699 ObjectLocker ol(h_init_lock, THREAD, h_init_lock() != NULL);
700
701 // abort if someone beat us to the initialization
702 if (!is_not_initialized()) return; // note: not equivalent to is_initialized()
703
704 ClassState old_state = init_state();
705 link_class_impl(THREAD);
706 if (HAS_PENDING_EXCEPTION) {
707 CLEAR_PENDING_EXCEPTION;
708 // Abort if linking the class throws an exception.
709
710 // Use a test to avoid redundantly resetting the state if there's
711 // no change. Set_init_state() asserts that state changes make
712 // progress, whereas here we might just be spinning in place.
713 if (old_state != _init_state)
714 set_init_state(old_state);
715 } else {
716 // linking successfull, mark class as initialized
717 set_init_state(fully_initialized);
718 fence_and_clear_init_lock();
719 // trace
720 if (log_is_enabled(Info, class, init)) {
721 ResourceMark rm(THREAD);
722 log_info(class, init)("[Initialized %s without side effects]", external_name());
723 }
724 }
725 }
726
727
728 // See "The Virtual Machine Specification" section 2.16.5 for a detailed explanation of the class initialization
729 // process. The step comments refers to the procedure described in that section.
730 // Note: implementation moved to static method to expose the this pointer.
731 void InstanceKlass::initialize(TRAPS) {
732 if (this->should_be_initialized()) {
733 initialize_impl(CHECK);
734 // Note: at this point the class may be initialized
735 // OR it may be in the state of being initialized
736 // in case of recursive initialization!
737 } else {
738 assert(is_initialized(), "sanity check");
739 }
740 }
741
742
743 bool InstanceKlass::verify_code(TRAPS) {
744 // 1) Verify the bytecodes
745 return Verifier::verify(this, should_verify_class(), THREAD);
746 }
747
748 void InstanceKlass::link_class(TRAPS) {
749 assert(is_loaded(), "must be loaded");
750 if (!is_linked()) {
751 link_class_impl(CHECK);
752 }
753 }
754
755 // Called to verify that a class can link during initialization, without
756 // throwing a VerifyError.
757 bool InstanceKlass::link_class_or_fail(TRAPS) {
1078 JvmtiExport::clear_detected_exception(jt);
1079 }
1080 DTRACE_CLASSINIT_PROBE_WAIT(error, -1, wait);
1081 if (e->is_a(SystemDictionary::Error_klass())) {
1082 THROW_OOP(e());
1083 } else {
1084 JavaCallArguments args(e);
1085 THROW_ARG(vmSymbols::java_lang_ExceptionInInitializerError(),
1086 vmSymbols::throwable_void_signature(),
1087 &args);
1088 }
1089 }
1090 DTRACE_CLASSINIT_PROBE_WAIT(end, -1, wait);
1091 }
1092
1093
1094 void InstanceKlass::set_initialization_state_and_notify(ClassState state, TRAPS) {
1095 Handle h_init_lock(THREAD, init_lock());
1096 if (h_init_lock() != NULL) {
1097 ObjectLocker ol(h_init_lock, THREAD);
1098 set_init_thread(NULL); // reset _init_thread before changing _init_state
1099 set_init_state(state);
1100 fence_and_clear_init_lock();
1101 ol.notify_all(CHECK);
1102 } else {
1103 assert(h_init_lock() != NULL, "The initialization state should never be set twice");
1104 set_init_thread(NULL); // reset _init_thread before changing _init_state
1105 set_init_state(state);
1106 }
1107 }
1108
1109 Klass* InstanceKlass::implementor() const {
1110 Klass* volatile* k = adr_implementor();
1111 if (k == NULL) {
1112 return NULL;
1113 } else {
1114 // This load races with inserts, and therefore needs acquire.
1115 Klass* kls = Atomic::load_acquire(k);
1116 if (kls != NULL && !kls->is_loader_alive()) {
1117 return NULL; // don't return unloaded class
|
71 #include "runtime/atomic.hpp"
72 #include "runtime/biasedLocking.hpp"
73 #include "runtime/fieldDescriptor.inline.hpp"
74 #include "runtime/handles.inline.hpp"
75 #include "runtime/javaCalls.hpp"
76 #include "runtime/mutexLocker.hpp"
77 #include "runtime/orderAccess.hpp"
78 #include "runtime/thread.inline.hpp"
79 #include "services/classLoadingService.hpp"
80 #include "services/threadService.hpp"
81 #include "utilities/dtrace.hpp"
82 #include "utilities/events.hpp"
83 #include "utilities/macros.hpp"
84 #include "utilities/stringUtils.hpp"
85 #ifdef COMPILER1
86 #include "c1/c1_Compiler.hpp"
87 #endif
88 #if INCLUDE_JFR
89 #include "jfr/jfrEvents.hpp"
90 #endif
91 #if INCLUDE_TSAN
92 #include "runtime/sharedRuntime.hpp"
93 #endif
94
95
96 #ifdef DTRACE_ENABLED
97
98
99 #define HOTSPOT_CLASS_INITIALIZATION_required HOTSPOT_CLASS_INITIALIZATION_REQUIRED
100 #define HOTSPOT_CLASS_INITIALIZATION_recursive HOTSPOT_CLASS_INITIALIZATION_RECURSIVE
101 #define HOTSPOT_CLASS_INITIALIZATION_concurrent HOTSPOT_CLASS_INITIALIZATION_CONCURRENT
102 #define HOTSPOT_CLASS_INITIALIZATION_erroneous HOTSPOT_CLASS_INITIALIZATION_ERRONEOUS
103 #define HOTSPOT_CLASS_INITIALIZATION_super__failed HOTSPOT_CLASS_INITIALIZATION_SUPER_FAILED
104 #define HOTSPOT_CLASS_INITIALIZATION_clinit HOTSPOT_CLASS_INITIALIZATION_CLINIT
105 #define HOTSPOT_CLASS_INITIALIZATION_error HOTSPOT_CLASS_INITIALIZATION_ERROR
106 #define HOTSPOT_CLASS_INITIALIZATION_end HOTSPOT_CLASS_INITIALIZATION_END
107 #define DTRACE_CLASSINIT_PROBE(type, thread_type) \
108 { \
109 char* data = NULL; \
110 int len = 0; \
111 Symbol* clss_name = name(); \
112 if (clss_name != NULL) { \
113 data = (char*)clss_name->bytes(); \
700 HandleMark hm(THREAD);
701 Handle h_init_lock(THREAD, init_lock());
702 ObjectLocker ol(h_init_lock, THREAD, h_init_lock() != NULL);
703
704 // abort if someone beat us to the initialization
705 if (!is_not_initialized()) return; // note: not equivalent to is_initialized()
706
707 ClassState old_state = init_state();
708 link_class_impl(THREAD);
709 if (HAS_PENDING_EXCEPTION) {
710 CLEAR_PENDING_EXCEPTION;
711 // Abort if linking the class throws an exception.
712
713 // Use a test to avoid redundantly resetting the state if there's
714 // no change. Set_init_state() asserts that state changes make
715 // progress, whereas here we might just be spinning in place.
716 if (old_state != _init_state)
717 set_init_state(old_state);
718 } else {
719 // linking successfull, mark class as initialized
720 TSAN_RUNTIME_ONLY(
721 // Construct a happens-before edge between the write of _init_state to
722 // fully_initialized and the later checking if it's initialized.
723 void* const lock_address = reinterpret_cast<void*>(
724 java_lang_Class::init_lock_addr(java_mirror()));
725 SharedRuntime::tsan_release(lock_address);
726 );
727 set_init_state(fully_initialized);
728 fence_and_clear_init_lock();
729 // trace
730 if (log_is_enabled(Info, class, init)) {
731 ResourceMark rm(THREAD);
732 log_info(class, init)("[Initialized %s without side effects]", external_name());
733 }
734 }
735 }
736
737
738 // See "The Virtual Machine Specification" section 2.16.5 for a detailed explanation of the class initialization
739 // process. The step comments refers to the procedure described in that section.
740 // Note: implementation moved to static method to expose the this pointer.
741 void InstanceKlass::initialize(TRAPS) {
742 if (this->should_be_initialized()) {
743 initialize_impl(CHECK);
744 // Note: at this point the class may be initialized
745 // OR it may be in the state of being initialized
746 // in case of recursive initialization!
747 } else {
748 TSAN_RUNTIME_ONLY(
749 // Construct a happens-before edge between the write of _init_state to
750 // fully_initialized and here.
751 void* const lock_address = reinterpret_cast<void*>(
752 java_lang_Class::init_lock_addr(java_mirror()));
753 SharedRuntime::tsan_acquire(lock_address);
754 );
755 assert(is_initialized(), "sanity check");
756 }
757 }
758
759
760 bool InstanceKlass::verify_code(TRAPS) {
761 // 1) Verify the bytecodes
762 return Verifier::verify(this, should_verify_class(), THREAD);
763 }
764
765 void InstanceKlass::link_class(TRAPS) {
766 assert(is_loaded(), "must be loaded");
767 if (!is_linked()) {
768 link_class_impl(CHECK);
769 }
770 }
771
772 // Called to verify that a class can link during initialization, without
773 // throwing a VerifyError.
774 bool InstanceKlass::link_class_or_fail(TRAPS) {
1095 JvmtiExport::clear_detected_exception(jt);
1096 }
1097 DTRACE_CLASSINIT_PROBE_WAIT(error, -1, wait);
1098 if (e->is_a(SystemDictionary::Error_klass())) {
1099 THROW_OOP(e());
1100 } else {
1101 JavaCallArguments args(e);
1102 THROW_ARG(vmSymbols::java_lang_ExceptionInInitializerError(),
1103 vmSymbols::throwable_void_signature(),
1104 &args);
1105 }
1106 }
1107 DTRACE_CLASSINIT_PROBE_WAIT(end, -1, wait);
1108 }
1109
1110
1111 void InstanceKlass::set_initialization_state_and_notify(ClassState state, TRAPS) {
1112 Handle h_init_lock(THREAD, init_lock());
1113 if (h_init_lock() != NULL) {
1114 ObjectLocker ol(h_init_lock, THREAD);
1115 TSAN_RUNTIME_ONLY(
1116 // Construct a happens-before edge between the write of _init_state to
1117 // fully_initialized and the later checking if it's initialized.
1118 void* const lock_address = reinterpret_cast<void*>(
1119 java_lang_Class::init_lock_addr(java_mirror()));
1120 SharedRuntime::tsan_release(lock_address);
1121 );
1122 set_init_thread(NULL); // reset _init_thread before changing _init_state
1123 set_init_state(state);
1124 fence_and_clear_init_lock();
1125 ol.notify_all(CHECK);
1126 } else {
1127 assert(h_init_lock() != NULL, "The initialization state should never be set twice");
1128 set_init_thread(NULL); // reset _init_thread before changing _init_state
1129 set_init_state(state);
1130 }
1131 }
1132
1133 Klass* InstanceKlass::implementor() const {
1134 Klass* volatile* k = adr_implementor();
1135 if (k == NULL) {
1136 return NULL;
1137 } else {
1138 // This load races with inserts, and therefore needs acquire.
1139 Klass* kls = Atomic::load_acquire(k);
1140 if (kls != NULL && !kls->is_loader_alive()) {
1141 return NULL; // don't return unloaded class
|