54 #include "oops/verifyOopClosure.hpp"
55 #include "prims/jvm_misc.hpp"
56 #include "prims/jvmtiExport.hpp"
57 #include "prims/jvmtiThreadState.hpp"
58 #include "prims/privilegedStack.hpp"
59 #include "runtime/arguments.hpp"
60 #include "runtime/atomic.hpp"
61 #include "runtime/biasedLocking.hpp"
62 #include "runtime/commandLineFlagConstraintList.hpp"
63 #include "runtime/commandLineFlagWriteableList.hpp"
64 #include "runtime/commandLineFlagRangeList.hpp"
65 #include "runtime/deoptimization.hpp"
66 #include "runtime/frame.inline.hpp"
67 #include "runtime/globals.hpp"
68 #include "runtime/handshake.hpp"
69 #include "runtime/init.hpp"
70 #include "runtime/interfaceSupport.hpp"
71 #include "runtime/java.hpp"
72 #include "runtime/javaCalls.hpp"
73 #include "runtime/jniPeriodicChecker.hpp"
74 #include "runtime/timerTrace.hpp"
75 #include "runtime/memprofiler.hpp"
76 #include "runtime/mutexLocker.hpp"
77 #include "runtime/objectMonitor.hpp"
78 #include "runtime/orderAccess.inline.hpp"
79 #include "runtime/osThread.hpp"
80 #include "runtime/safepoint.hpp"
81 #include "runtime/safepointMechanism.inline.hpp"
82 #include "runtime/sharedRuntime.hpp"
83 #include "runtime/statSampler.hpp"
84 #include "runtime/stubRoutines.hpp"
85 #include "runtime/sweeper.hpp"
86 #include "runtime/task.hpp"
87 #include "runtime/thread.inline.hpp"
88 #include "runtime/threadCritical.hpp"
89 #include "runtime/vframe.hpp"
90 #include "runtime/vframeArray.hpp"
91 #include "runtime/vframe_hp.hpp"
92 #include "runtime/vmThread.hpp"
93 #include "runtime/vm_operations.hpp"
94 #include "runtime/vm_version.hpp"
95 #include "services/attachListener.hpp"
96 #include "services/management.hpp"
97 #include "services/memTracker.hpp"
98 #include "services/threadService.hpp"
99 #include "trace/traceMacros.hpp"
100 #include "trace/tracing.hpp"
101 #include "utilities/align.hpp"
102 #include "utilities/defaultStream.hpp"
103 #include "utilities/dtrace.hpp"
104 #include "utilities/events.hpp"
105 #include "utilities/macros.hpp"
106 #include "utilities/preserveException.hpp"
107 #include "utilities/vmError.hpp"
108 #if INCLUDE_ALL_GCS
109 #include "gc/cms/concurrentMarkSweepThread.hpp"
110 #include "gc/g1/concurrentMarkThread.inline.hpp"
111 #include "gc/parallel/pcTasks.hpp"
112 #endif // INCLUDE_ALL_GCS
113 #if INCLUDE_JVMCI
114 #include "jvmci/jvmciCompiler.hpp"
115 #include "jvmci/jvmciRuntime.hpp"
116 #include "logging/logHandle.hpp"
117 #endif
118 #ifdef COMPILER1
119 #include "c1/c1_Compiler.hpp"
120 #endif
121 #ifdef COMPILER2
122 #include "opto/c2compiler.hpp"
123 #include "opto/idealGraphPrinter.hpp"
124 #endif
125 #if INCLUDE_RTM_OPT
126 #include "runtime/rtmLocking.hpp"
178 AllocFailStrategy::RETURN_NULL);
179 void* aligned_addr = align_up(real_malloc_addr, alignment);
180 assert(((uintptr_t) aligned_addr + (uintptr_t) size) <=
181 ((uintptr_t) real_malloc_addr + (uintptr_t) aligned_size),
182 "JavaThread alignment code overflowed allocated storage");
183 if (aligned_addr != real_malloc_addr) {
184 log_info(biasedlocking)("Aligned thread " INTPTR_FORMAT " to " INTPTR_FORMAT,
185 p2i(real_malloc_addr),
186 p2i(aligned_addr));
187 }
188 ((Thread*) aligned_addr)->_real_malloc_address = real_malloc_addr;
189 return aligned_addr;
190 } else {
191 return throw_excpt? AllocateHeap(size, flags, CURRENT_PC)
192 : AllocateHeap(size, flags, CURRENT_PC, AllocFailStrategy::RETURN_NULL);
193 }
194 }
195
196 void Thread::operator delete(void* p) {
197 if (UseBiasedLocking) {
198 void* real_malloc_addr = ((Thread*) p)->_real_malloc_address;
199 FreeHeap(real_malloc_addr);
200 } else {
201 FreeHeap(p);
202 }
203 }
204
205
206 // Base class for all threads: VMThread, WatcherThread, ConcurrentMarkSweepThread,
207 // JavaThread
208
209
210 Thread::Thread() {
211 // stack and get_thread
212 set_stack_base(NULL);
213 set_stack_size(0);
214 set_self_raw_id(0);
215 set_lgrp_id(-1);
216 DEBUG_ONLY(clear_suspendible_thread();)
217
218 // allocated data structures
219 set_osthread(NULL);
220 set_resource_area(new (mtThread)ResourceArea());
221 DEBUG_ONLY(_current_resource_mark = NULL;)
222 set_handle_area(new (mtThread) HandleArea(NULL));
223 set_metadata_handles(new (ResourceObj::C_HEAP, mtClass) GrowableArray<Metadata*>(30, true));
224 set_active_handles(NULL);
225 set_free_handle_block(NULL);
226 set_last_handle_mark(NULL);
227
228 // This initial value ==> never claimed.
229 _oops_do_parity = 0;
230
231 // the handle mark links itself to last_handle_mark
232 new HandleMark(this);
233
234 // plain initialization
235 debug_only(_owned_locks = NULL;)
236 debug_only(_allow_allocation_count = 0;)
237 NOT_PRODUCT(_allow_safepoint_count = 0;)
238 NOT_PRODUCT(_skip_gcalot = false;)
239 _jvmti_env_iteration_count = 0;
240 set_allocated_bytes(0);
241 _vm_operation_started_count = 0;
242 _vm_operation_completed_count = 0;
243 _current_pending_monitor = NULL;
244 _current_pending_monitor_is_from_java = true;
245 _current_waiting_monitor = NULL;
246 _num_nested_signal = 0;
247 omFreeList = NULL;
248 omFreeCount = 0;
249 omFreeProvision = 32;
381 _SR_lock = NULL;
382
383 // osthread() can be NULL, if creation of thread failed.
384 if (osthread() != NULL) os::free_thread(osthread());
385
386 // clear Thread::current if thread is deleting itself.
387 // Needed to ensure JNI correctly detects non-attached threads.
388 if (this == Thread::current()) {
389 clear_thread_current();
390 }
391
392 CHECK_UNHANDLED_OOPS_ONLY(if (CheckUnhandledOops) delete unhandled_oops();)
393 }
394
395 // NOTE: dummy function for assertion purpose.
396 void Thread::run() {
397 ShouldNotReachHere();
398 }
399
400 #ifdef ASSERT
401 // Private method to check for dangling thread pointer
402 void check_for_dangling_thread_pointer(Thread *thread) {
403 assert(!thread->is_Java_thread() || Thread::current() == thread || Threads_lock->owned_by_self(),
404 "possibility of dangling Thread pointer");
405 }
406 #endif
407
408 ThreadPriority Thread::get_priority(const Thread* const thread) {
409 ThreadPriority priority;
410 // Can return an error!
411 (void)os::get_priority(thread, priority);
412 assert(MinPriority <= priority && priority <= MaxPriority, "non-Java priority found");
413 return priority;
414 }
415
416 void Thread::set_priority(Thread* thread, ThreadPriority priority) {
417 debug_only(check_for_dangling_thread_pointer(thread);)
418 // Can return an error!
419 (void)os::set_priority(thread, priority);
420 }
421
422
423 void Thread::start(Thread* thread) {
715
716 if (!pending) {
717 // A cancelled suspend request is the only false return from
718 // is_ext_suspend_completed() that keeps us from staying in the
719 // retry loop.
720 *bits |= 0x00080000;
721 return false;
722 }
723
724 if (is_suspended) {
725 *bits |= 0x00100000;
726 return true;
727 }
728 } // end retry loop
729
730 // thread did not suspend after all our retries
731 *bits |= 0x00200000;
732 return false;
733 }
734
735 #ifndef PRODUCT
736 void JavaThread::record_jump(address target, address instr, const char* file,
737 int line) {
738
739 // This should not need to be atomic as the only way for simultaneous
740 // updates is via interrupts. Even then this should be rare or non-existent
741 // and we don't care that much anyway.
742
743 int index = _jmp_ring_index;
744 _jmp_ring_index = (index + 1) & (jump_ring_buffer_size - 1);
745 _jmp_ring[index]._target = (intptr_t) target;
746 _jmp_ring[index]._instruction = (intptr_t) instr;
747 _jmp_ring[index]._file = file;
748 _jmp_ring[index]._line = line;
749 }
750 #endif // PRODUCT
751
752 void Thread::interrupt(Thread* thread) {
753 debug_only(check_for_dangling_thread_pointer(thread);)
754 os::interrupt(thread);
793 void Thread::metadata_handles_do(void f(Metadata*)) {
794 // Only walk the Handles in Thread.
795 if (metadata_handles() != NULL) {
796 for (int i = 0; i< metadata_handles()->length(); i++) {
797 f(metadata_handles()->at(i));
798 }
799 }
800 }
801
802 void Thread::print_on(outputStream* st) const {
803 // get_priority assumes osthread initialized
804 if (osthread() != NULL) {
805 int os_prio;
806 if (os::get_native_priority(this, &os_prio) == OS_OK) {
807 st->print("os_prio=%d ", os_prio);
808 }
809 st->print("tid=" INTPTR_FORMAT " ", p2i(this));
810 ext().print_on(st);
811 osthread()->print_on(st);
812 }
813 debug_only(if (WizardMode) print_owned_locks_on(st);)
814 }
815
816 // Thread::print_on_error() is called by fatal error handler. Don't use
817 // any lock or allocate memory.
818 void Thread::print_on_error(outputStream* st, char* buf, int buflen) const {
819 assert(!(is_Compiler_thread() || is_Java_thread()), "Can't call name() here if it allocates");
820
821 if (is_VM_thread()) { st->print("VMThread"); }
822 else if (is_GC_task_thread()) { st->print("GCTaskThread"); }
823 else if (is_Watcher_thread()) { st->print("WatcherThread"); }
824 else if (is_ConcurrentGC_thread()) { st->print("ConcurrentGCThread"); }
825 else { st->print("Thread"); }
826
827 if (is_Named_thread()) {
828 st->print(" \"%s\"", name());
829 }
830
831 st->print(" [stack: " PTR_FORMAT "," PTR_FORMAT "]",
832 p2i(stack_end()), p2i(stack_base()));
833
834 if (osthread()) {
835 st->print(" [id=%d]", osthread()->thread_id());
836 }
837 }
838
839 void Thread::print_value_on(outputStream* st) const {
840 if (is_Named_thread()) {
841 st->print(" \"%s\" ", name());
842 }
843 st->print(INTPTR_FORMAT, p2i(this)); // print address
844 }
845
846 #ifdef ASSERT
847 void Thread::print_owned_locks_on(outputStream* st) const {
848 Monitor *cur = _owned_locks;
849 if (cur == NULL) {
850 st->print(" (no locks) ");
851 } else {
852 st->print_cr(" Locks owned:");
853 while (cur) {
854 cur->print_on(st);
855 cur = cur->next();
856 }
857 }
858 }
859
860 static int ref_use_count = 0;
861
862 bool Thread::owns_locks_but_compiled_lock() const {
863 for (Monitor *cur = _owned_locks; cur; cur = cur->next()) {
864 if (cur != Compile_lock) return true;
865 }
866 return false;
867 }
868
869
870 #endif
871
872 #ifndef PRODUCT
873
874 // The flag: potential_vm_operation notifies if this particular safepoint state could potential
875 // invoke the vm-thread (i.e., and oop allocation). In that case, we also have to make sure that
876 // no threads which allow_vm_block's are held
877 void Thread::check_for_valid_safepoint_state(bool potential_vm_operation) {
878 // Check if current thread is allowed to block at a safepoint
879 if (!(_allow_safepoint_count == 0)) {
880 fatal("Possible safepoint reached by thread that does not allow it");
881 }
882 if (is_Java_thread() && ((JavaThread*)this)->thread_state() != _thread_in_vm) {
883 fatal("LEAF method calling lock?");
884 }
885
886 #ifdef ASSERT
887 if (potential_vm_operation && is_Java_thread()
888 && !Universe::is_bootstrapping()) {
889 // Make sure we do not hold any locks that the VM thread also uses.
890 // This could potentially lead to deadlocks
891 for (Monitor *cur = _owned_locks; cur; cur = cur->next()) {
892 // Threads_lock is special, since the safepoint synchronization will not start before this is
893 // acquired. Hence, a JavaThread cannot be holding it at a safepoint. So is VMOperationRequest_lock,
894 // since it is used to transfer control between JavaThreads and the VMThread
895 // Do not *exclude* any locks unless you are absolutely sure it is correct. Ask someone else first!
1381
1382 void WatcherThread::print_on(outputStream* st) const {
1383 st->print("\"%s\" ", name());
1384 Thread::print_on(st);
1385 st->cr();
1386 }
1387
1388 // ======= JavaThread ========
1389
1390 #if INCLUDE_JVMCI
1391
1392 jlong* JavaThread::_jvmci_old_thread_counters;
1393
1394 bool jvmci_counters_include(JavaThread* thread) {
1395 oop threadObj = thread->threadObj();
1396 return !JVMCICountersExcludeCompiler || !thread->is_Compiler_thread();
1397 }
1398
1399 void JavaThread::collect_counters(typeArrayOop array) {
1400 if (JVMCICounterSize > 0) {
1401 MutexLocker tl(Threads_lock);
1402 for (int i = 0; i < array->length(); i++) {
1403 array->long_at_put(i, _jvmci_old_thread_counters[i]);
1404 }
1405 for (JavaThread* tp = Threads::first(); tp != NULL; tp = tp->next()) {
1406 if (jvmci_counters_include(tp)) {
1407 for (int i = 0; i < array->length(); i++) {
1408 array->long_at_put(i, array->long_at(i) + tp->_jvmci_counters[i]);
1409 }
1410 }
1411 }
1412 }
1413 }
1414
1415 #endif // INCLUDE_JVMCI
1416
1417 // A JavaThread is a normal Java thread
1418
1419 void JavaThread::initialize() {
1420 // Initialize fields
1421
1422 set_saved_exception_pc(NULL);
1423 set_threadObj(NULL);
1424 _anchor.clear();
1425 set_entry_point(NULL);
1426 set_jni_functions(jni_functions());
1427 set_callee_target(NULL);
1428 set_vm_result(NULL);
1429 set_vm_result_2(NULL);
1430 set_vframe_array_head(NULL);
1431 set_vframe_array_last(NULL);
1432 set_deferred_locals(NULL);
1433 set_deopt_mark(NULL);
1434 set_deopt_compiled_method(NULL);
1435 clear_must_deopt_id();
1436 set_monitor_chunks(NULL);
1437 set_next(NULL);
1438 set_thread_state(_thread_new);
1439 _terminated = _not_terminated;
1440 _privileged_stack_top = NULL;
1441 _array_for_gc = NULL;
1442 _suspend_equivalent = false;
1443 _in_deopt_handler = 0;
1444 _doing_unsafe_access = false;
1445 _stack_guard_state = stack_guard_unused;
1446 #if INCLUDE_JVMCI
1447 _pending_monitorenter = false;
1448 _pending_deoptimization = -1;
1449 _pending_failed_speculation = NULL;
1450 _pending_transfer_to_interpreter = false;
1451 _adjusting_comp_level = false;
1452 _jvmci._alternate_call_target = NULL;
1453 assert(_jvmci._implicit_exception_pc == NULL, "must be");
1454 if (JVMCICounterSize > 0) {
1455 _jvmci_counters = NEW_C_HEAP_ARRAY(jlong, JVMCICounterSize, mtInternal);
1456 memset(_jvmci_counters, 0, sizeof(jlong) * JVMCICounterSize);
1457 } else {
1698 void JavaThread::thread_main_inner() {
1699 assert(JavaThread::current() == this, "sanity check");
1700 assert(this->threadObj() != NULL, "just checking");
1701
1702 // Execute thread entry point unless this thread has a pending exception
1703 // or has been stopped before starting.
1704 // Note: Due to JVM_StopThread we can have pending exceptions already!
1705 if (!this->has_pending_exception() &&
1706 !java_lang_Thread::is_stillborn(this->threadObj())) {
1707 {
1708 ResourceMark rm(this);
1709 this->set_native_thread_name(this->get_thread_name());
1710 }
1711 HandleMark hm(this);
1712 this->entry_point()(this, this);
1713 }
1714
1715 DTRACE_THREAD_PROBE(stop, this);
1716
1717 this->exit(false);
1718 delete this;
1719 }
1720
1721
1722 static void ensure_join(JavaThread* thread) {
1723 // We do not need to grap the Threads_lock, since we are operating on ourself.
1724 Handle threadObj(thread, thread->threadObj());
1725 assert(threadObj.not_null(), "java thread object must exist");
1726 ObjectLocker lock(threadObj, thread);
1727 // Ignore pending exception (ThreadDeath), since we are exiting anyway
1728 thread->clear_pending_exception();
1729 // Thread is exiting. So set thread_status field in java.lang.Thread class to TERMINATED.
1730 java_lang_Thread::set_thread_status(threadObj(), java_lang_Thread::TERMINATED);
1731 // Clear the native thread instance - this makes isAlive return false and allows the join()
1732 // to complete once we've done the notify_all below
1733 java_lang_Thread::set_thread(threadObj(), NULL);
1734 lock.notify_all(thread);
1735 // Ignore pending exception (ThreadDeath), since we are exiting anyway
1736 thread->clear_pending_exception();
1737 }
1738
1739
1740 // For any new cleanup additions, please check to see if they need to be applied to
1741 // cleanup_failed_attach_current_thread as well.
1742 void JavaThread::exit(bool destroy_vm, ExitType exit_type) {
1743 assert(this == JavaThread::current(), "thread consistency check");
1744
1745 HandleMark hm(this);
1746 Handle uncaught_exception(this, this->pending_exception());
1747 this->clear_pending_exception();
1748 Handle threadObj(this, this->threadObj());
1749 assert(threadObj.not_null(), "Java thread object should be created");
1750
1751 // FIXIT: This code should be moved into else part, when reliable 1.2/1.3 check is in place
1752 {
1753 EXCEPTION_MARK;
1754
1755 CLEAR_PENDING_EXCEPTION;
1756 }
1757 if (!destroy_vm) {
1758 if (uncaught_exception.not_null()) {
1759 EXCEPTION_MARK;
1760 // Call method Thread.dispatchUncaughtException().
1761 Klass* thread_klass = SystemDictionary::Thread_klass();
1762 JavaValue result(T_VOID);
1763 JavaCalls::call_virtual(&result,
1764 threadObj, thread_klass,
1824 // Implied else:
1825 // Things get a little tricky here. We have a pending external
1826 // suspend request, but we are holding the SR_lock so we
1827 // can't just self-suspend. So we temporarily drop the lock
1828 // and then self-suspend.
1829 }
1830
1831 ThreadBlockInVM tbivm(this);
1832 java_suspend_self();
1833
1834 // We're done with this suspend request, but we have to loop around
1835 // and check again. Eventually we will get SR_lock without a pending
1836 // external suspend request and will be able to mark ourselves as
1837 // exiting.
1838 }
1839 // no more external suspends are allowed at this point
1840 } else {
1841 // before_exit() has already posted JVMTI THREAD_END events
1842 }
1843
1844 // Notify waiters on thread object. This has to be done after exit() is called
1845 // on the thread (if the thread is the last thread in a daemon ThreadGroup the
1846 // group should have the destroyed bit set before waiters are notified).
1847 ensure_join(this);
1848 assert(!this->has_pending_exception(), "ensure_join should have cleared");
1849
1850 // 6282335 JNI DetachCurrentThread spec states that all Java monitors
1851 // held by this thread must be released. The spec does not distinguish
1852 // between JNI-acquired and regular Java monitors. We can only see
1853 // regular Java monitors here if monitor enter-exit matching is broken.
1854 //
1855 // Optionally release any monitors for regular JavaThread exits. This
1856 // is provided as a work around for any bugs in monitor enter-exit
1857 // matching. This can be expensive so it is not enabled by default.
1858 //
1859 // ensure_join() ignores IllegalThreadStateExceptions, and so does
1860 // ObjectSynchronizer::release_monitors_owned_by_thread().
1861 if (exit_type == jni_detach || ObjectMonitor::Knob_ExitRelease) {
1862 // Sanity check even though JNI DetachCurrentThread() would have
1863 // returned JNI_ERR if there was a Java frame. JavaThread exit
1864 // should be done executing Java code by the time we get here.
1865 assert(!this->has_last_Java_frame(),
1866 "should not have a Java frame when detaching or exiting");
1867 ObjectSynchronizer::release_monitors_owned_by_thread(this);
1868 assert(!this->has_pending_exception(), "release_monitors should have cleared");
1869 }
1897
1898 // We must flush any deferred card marks before removing a thread from
1899 // the list of active threads.
1900 Universe::heap()->flush_deferred_store_barrier(this);
1901 assert(deferred_card_mark().is_empty(), "Should have been flushed");
1902
1903 #if INCLUDE_ALL_GCS
1904 // We must flush the G1-related buffers before removing a thread
1905 // from the list of active threads. We must do this after any deferred
1906 // card marks have been flushed (above) so that any entries that are
1907 // added to the thread's dirty card queue as a result are not lost.
1908 if (UseG1GC) {
1909 flush_barrier_queues();
1910 }
1911 #endif // INCLUDE_ALL_GCS
1912
1913 log_info(os, thread)("JavaThread %s (tid: " UINTX_FORMAT ").",
1914 exit_type == JavaThread::normal_exit ? "exiting" : "detaching",
1915 os::current_thread_id());
1916
1917 // Remove from list of active threads list, and notify VM thread if we are the last non-daemon thread
1918 Threads::remove(this);
1919
1920 // If someone set a handshake on us just as we entered exit path, we simple cancel it.
1921 if (ThreadLocalHandshakes) {
1922 cancel_handshake();
1923 }
1924 }
1925
1926 #if INCLUDE_ALL_GCS
1927 // Flush G1-related queues.
1928 void JavaThread::flush_barrier_queues() {
1929 satb_mark_queue().flush();
1930 dirty_card_queue().flush();
1931 }
1932
1933 void JavaThread::initialize_queues() {
1934 assert(!SafepointSynchronize::is_at_safepoint(),
1935 "we should not be at a safepoint");
1936
1937 SATBMarkQueue& satb_queue = satb_mark_queue();
1938 SATBMarkQueueSet& satb_queue_set = satb_mark_queue_set();
1939 // The SATB queue should have been constructed with its active
1940 // field set to false.
1941 assert(!satb_queue.is_active(), "SATB queue should not be active");
1942 assert(satb_queue.is_empty(), "SATB queue should be empty");
1943 // If we are creating the thread during a marking cycle, we should
1963 if (free_handle_block() != NULL) {
1964 JNIHandleBlock* block = free_handle_block();
1965 set_free_handle_block(NULL);
1966 JNIHandleBlock::release_block(block);
1967 }
1968
1969 // These have to be removed while this is still a valid thread.
1970 remove_stack_guard_pages();
1971
1972 if (UseTLAB) {
1973 tlab().make_parsable(true); // retire TLAB, if any
1974 }
1975
1976 #if INCLUDE_ALL_GCS
1977 if (UseG1GC) {
1978 flush_barrier_queues();
1979 }
1980 #endif // INCLUDE_ALL_GCS
1981
1982 Threads::remove(this);
1983 delete this;
1984 }
1985
1986
1987
1988
1989 JavaThread* JavaThread::active() {
1990 Thread* thread = Thread::current();
1991 if (thread->is_Java_thread()) {
1992 return (JavaThread*) thread;
1993 } else {
1994 assert(thread->is_VM_thread(), "this must be a vm thread");
1995 VM_Operation* op = ((VMThread*) thread)->vm_operation();
1996 JavaThread *ret=op == NULL ? NULL : (JavaThread *)op->calling_thread();
1997 assert(ret->is_Java_thread(), "must be a Java thread");
1998 return ret;
1999 }
2000 }
2001
2002 bool JavaThread::is_lock_owned(address adr) const {
2003 if (Thread::is_lock_owned(adr)) return true;
2218 }
2219
2220
2221 // Interrupt thread so it will wake up from a potential wait()
2222 Thread::interrupt(this);
2223 }
2224
2225 // External suspension mechanism.
2226 //
2227 // Tell the VM to suspend a thread when ever it knows that it does not hold on
2228 // to any VM_locks and it is at a transition
2229 // Self-suspension will happen on the transition out of the vm.
2230 // Catch "this" coming in from JNIEnv pointers when the thread has been freed
2231 //
2232 // Guarantees on return:
2233 // + Target thread will not execute any new bytecode (that's why we need to
2234 // force a safepoint)
2235 // + Target thread will not enter any new monitors
2236 //
2237 void JavaThread::java_suspend() {
2238 { MutexLocker mu(Threads_lock);
2239 if (!Threads::includes(this) || is_exiting() || this->threadObj() == NULL) {
2240 return;
2241 }
2242 }
2243
2244 { MutexLockerEx ml(SR_lock(), Mutex::_no_safepoint_check_flag);
2245 if (!is_external_suspend()) {
2246 // a racing resume has cancelled us; bail out now
2247 return;
2248 }
2249
2250 // suspend is done
2251 uint32_t debug_bits = 0;
2252 // Warning: is_ext_suspend_completed() may temporarily drop the
2253 // SR_lock to allow the thread to reach a stable thread state if
2254 // it is currently in a transient thread state.
2255 if (is_ext_suspend_completed(false /* !called_by_wait */,
2256 SuspendRetryDelay, &debug_bits)) {
2257 return;
2258 }
2259 }
2260
2261 VM_ThreadSuspend vm_suspend;
2262 VMThread::execute(&vm_suspend);
2310 // it. This would be a "bad thing (TM)" and cause the stack walker
2311 // to crash. We stay self-suspended until there are no more pending
2312 // external suspend requests.
2313 while (is_external_suspend()) {
2314 ret++;
2315 this->set_ext_suspended();
2316
2317 // _ext_suspended flag is cleared by java_resume()
2318 while (is_ext_suspended()) {
2319 this->SR_lock()->wait(Mutex::_no_safepoint_check_flag);
2320 }
2321 }
2322
2323 return ret;
2324 }
2325
2326 #ifdef ASSERT
2327 // verify the JavaThread has not yet been published in the Threads::list, and
2328 // hence doesn't need protection from concurrent access at this stage
2329 void JavaThread::verify_not_published() {
2330 if (!Threads_lock->owned_by_self()) {
2331 MutexLockerEx ml(Threads_lock, Mutex::_no_safepoint_check_flag);
2332 assert(!Threads::includes(this),
2333 "java thread shouldn't have been published yet!");
2334 } else {
2335 assert(!Threads::includes(this),
2336 "java thread shouldn't have been published yet!");
2337 }
2338 }
2339 #endif
2340
2341 // Slow path when the native==>VM/Java barriers detect a safepoint is in
2342 // progress or when _suspend_flags is non-zero.
2343 // Current thread needs to self-suspend if there is a suspend request and/or
2344 // block if a safepoint is in progress.
2345 // Async exception ISN'T checked.
2346 // Note only the ThreadInVMfromNative transition can call this function
2347 // directly and when thread state is _thread_in_native_trans
2348 void JavaThread::check_safepoint_and_suspend_for_native_trans(JavaThread *thread) {
2349 assert(thread->thread_state() == _thread_in_native_trans, "wrong state");
2350
2351 JavaThread *curJT = JavaThread::current();
2352 bool do_self_suspend = thread->is_external_suspend();
2353
2354 assert(!curJT->has_last_Java_frame() || curJT->frame_anchor()->walkable(), "Unwalkable stack in native->vm transition");
2355
2356 // If JNIEnv proxies are allowed, don't self-suspend if the target
2357 // thread is not the current thread. In older versions of jdbx, jdbx
2434 check_special_condition_for_native_trans(thread);
2435
2436 // Finish the transition
2437 thread->set_thread_state(_thread_in_Java);
2438
2439 if (thread->do_critical_native_unlock()) {
2440 ThreadInVMfromJavaNoAsyncException tiv(thread);
2441 GCLocker::unlock_critical(thread);
2442 thread->clear_critical_native_unlock();
2443 }
2444 }
2445
2446 // We need to guarantee the Threads_lock here, since resumes are not
2447 // allowed during safepoint synchronization
2448 // Can only resume from an external suspension
2449 void JavaThread::java_resume() {
2450 assert_locked_or_safepoint(Threads_lock);
2451
2452 // Sanity check: thread is gone, has started exiting or the thread
2453 // was not externally suspended.
2454 if (!Threads::includes(this) || is_exiting() || !is_external_suspend()) {
2455 return;
2456 }
2457
2458 MutexLockerEx ml(SR_lock(), Mutex::_no_safepoint_check_flag);
2459
2460 clear_external_suspend();
2461
2462 if (is_ext_suspended()) {
2463 clear_ext_suspended();
2464 SR_lock()->notify_all();
2465 }
2466 }
2467
2468 size_t JavaThread::_stack_red_zone_size = 0;
2469 size_t JavaThread::_stack_yellow_zone_size = 0;
2470 size_t JavaThread::_stack_reserved_zone_size = 0;
2471 size_t JavaThread::_stack_shadow_zone_size = 0;
2472
2473 void JavaThread::create_stack_guard_pages() {
2474 if (!os::uses_stack_guard_pages() || _stack_guard_state != stack_guard_unused) { return; }
2902 void JavaThread::print_name_on_error(outputStream* st, char *buf, int buflen) const {
2903 st->print("%s", get_thread_name_string(buf, buflen));
2904 }
2905
2906 // Called by fatal error handler. The difference between this and
2907 // JavaThread::print() is that we can't grab lock or allocate memory.
2908 void JavaThread::print_on_error(outputStream* st, char *buf, int buflen) const {
2909 st->print("JavaThread \"%s\"", get_thread_name_string(buf, buflen));
2910 oop thread_obj = threadObj();
2911 if (thread_obj != NULL) {
2912 if (java_lang_Thread::is_daemon(thread_obj)) st->print(" daemon");
2913 }
2914 st->print(" [");
2915 st->print("%s", _get_thread_state_name(_thread_state));
2916 if (osthread()) {
2917 st->print(", id=%d", osthread()->thread_id());
2918 }
2919 st->print(", stack(" PTR_FORMAT "," PTR_FORMAT ")",
2920 p2i(stack_end()), p2i(stack_base()));
2921 st->print("]");
2922 return;
2923 }
2924
2925 // Verification
2926
2927 static void frame_verify(frame* f, const RegisterMap *map) { f->verify(map); }
2928
2929 void JavaThread::verify() {
2930 // Verify oops in the thread.
2931 oops_do(&VerifyOopClosure::verify_oop, NULL);
2932
2933 // Verify the stack frames.
2934 frames_do(frame_verify);
2935 }
2936
2937 // CR 6300358 (sub-CR 2137150)
2938 // Most callers of this method assume that it can't return NULL but a
2939 // thread may not have a name whilst it is in the process of attaching to
2940 // the VM - see CR 6412693, and there are places where a JavaThread can be
2941 // seen prior to having it's threadObj set (eg JNI attaching threads and
3306 // a scan.
3307 cf->do_code_blob(_scanned_compiled_method);
3308 }
3309 }
3310
3311
3312 // ======= Threads ========
3313
3314 // The Threads class links together all active threads, and provides
3315 // operations over all threads. It is protected by its own Mutex
3316 // lock, which is also used in other contexts to protect thread
3317 // operations from having the thread being operated on from exiting
3318 // and going away unexpectedly (e.g., safepoint synchronization)
3319
3320 JavaThread* Threads::_thread_list = NULL;
3321 int Threads::_number_of_threads = 0;
3322 int Threads::_number_of_non_daemon_threads = 0;
3323 int Threads::_return_code = 0;
3324 int Threads::_thread_claim_parity = 0;
3325 size_t JavaThread::_stack_size_at_create = 0;
3326 #ifdef ASSERT
3327 bool Threads::_vm_complete = false;
3328 #endif
3329
3330 // All JavaThreads
3331 #define ALL_JAVA_THREADS(X) for (JavaThread* X = _thread_list; X; X = X->next())
3332
3333 // All JavaThreads + all non-JavaThreads (i.e., every thread in the system)
3334 void Threads::threads_do(ThreadClosure* tc) {
3335 assert_locked_or_safepoint(Threads_lock);
3336 // ALL_JAVA_THREADS iterates through all JavaThreads
3337 ALL_JAVA_THREADS(p) {
3338 tc->do_thread(p);
3339 }
3340 // Someday we could have a table or list of all non-JavaThreads.
3341 // For now, just manually iterate through them.
3342 tc->do_thread(VMThread::vm_thread());
3343 Universe::heap()->gc_threads_do(tc);
3344 WatcherThread *wt = WatcherThread::watcher_thread();
3345 // Strictly speaking, the following NULL check isn't sufficient to make sure
3346 // the data for WatcherThread is still valid upon being examined. However,
3347 // considering that WatchThread terminates when the VM is on the way to
3348 // exit at safepoint, the chance of the above is extremely small. The right
3349 // way to prevent termination of WatcherThread would be to acquire
3350 // Terminator_lock, but we can't do that without violating the lock rank
3351 // checking in some cases.
3412 if (result.get_jint() != JNI_OK) {
3413 vm_exit_during_initialization(); // no message or exception
3414 }
3415
3416 universe_post_module_init();
3417 }
3418
3419 // Phase 3. final setup - set security manager, system class loader and TCCL
3420 //
3421 // This will instantiate and set the security manager, set the system class
3422 // loader as well as the thread context class loader. The security manager
3423 // and system class loader may be a custom class loaded from -Xbootclasspath/a,
3424 // other modules or the application's classpath.
3425 static void call_initPhase3(TRAPS) {
3426 Klass* klass = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_System(), true, CHECK);
3427 JavaValue result(T_VOID);
3428 JavaCalls::call_static(&result, klass, vmSymbols::initPhase3_name(),
3429 vmSymbols::void_method_signature(), CHECK);
3430 }
3431
3432 void Threads::initialize_java_lang_classes(JavaThread* main_thread, TRAPS) {
3433 TraceTime timer("Initialize java.lang classes", TRACETIME_LOG(Info, startuptime));
3434
3435 if (EagerXrunInit && Arguments::init_libraries_at_startup()) {
3436 create_vm_init_libraries();
3437 }
3438
3439 initialize_class(vmSymbols::java_lang_String(), CHECK);
3440
3441 // Inject CompactStrings value after the static initializers for String ran.
3442 java_lang_String::set_compact_strings(CompactStrings);
3443
3444 // Initialize java_lang.System (needed before creating the thread)
3445 initialize_class(vmSymbols::java_lang_System(), CHECK);
3446 // The VM creates & returns objects of this class. Make sure it's initialized.
3447 initialize_class(vmSymbols::java_lang_Class(), CHECK);
3448 initialize_class(vmSymbols::java_lang_ThreadGroup(), CHECK);
3449 Handle thread_group = create_initial_thread_group(CHECK);
3450 Universe::set_main_thread_group(thread_group());
3451 initialize_class(vmSymbols::java_lang_Thread(), CHECK);
3592 #if INCLUDE_JVMCI
3593 if (JVMCICounterSize > 0) {
3594 JavaThread::_jvmci_old_thread_counters = NEW_C_HEAP_ARRAY(jlong, JVMCICounterSize, mtInternal);
3595 memset(JavaThread::_jvmci_old_thread_counters, 0, sizeof(jlong) * JVMCICounterSize);
3596 } else {
3597 JavaThread::_jvmci_old_thread_counters = NULL;
3598 }
3599 #endif // INCLUDE_JVMCI
3600
3601 // Attach the main thread to this os thread
3602 JavaThread* main_thread = new JavaThread();
3603 main_thread->set_thread_state(_thread_in_vm);
3604 main_thread->initialize_thread_current();
3605 // must do this before set_active_handles
3606 main_thread->record_stack_base_and_size();
3607 main_thread->set_active_handles(JNIHandleBlock::allocate_block());
3608
3609 if (!main_thread->set_as_starting_thread()) {
3610 vm_shutdown_during_initialization(
3611 "Failed necessary internal allocation. Out of swap space");
3612 delete main_thread;
3613 *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again
3614 return JNI_ENOMEM;
3615 }
3616
3617 // Enable guard page *after* os::create_main_thread(), otherwise it would
3618 // crash Linux VM, see notes in os_linux.cpp.
3619 main_thread->create_stack_guard_pages();
3620
3621 // Initialize Java-Level synchronization subsystem
3622 ObjectMonitor::Initialize();
3623
3624 // Initialize global modules
3625 jint status = init_globals();
3626 if (status != JNI_OK) {
3627 delete main_thread;
3628 *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again
3629 return status;
3630 }
3631
3632 if (TRACE_INITIALIZE() != JNI_OK) {
3633 vm_exit_during_initialization("Failed to initialize tracing backend");
3634 }
3635
3636 // Should be done after the heap is fully created
3637 main_thread->cache_global_variables();
3638
3639 HandleMark hm;
3640
3641 { MutexLocker mu(Threads_lock);
3642 Threads::add(main_thread);
3643 }
3644
3645 // Any JVMTI raw monitors entered in onload will transition into
3646 // real raw monitor. VM is setup enough here for raw monitor enter.
3647 JvmtiExport::transition_pending_onload_raw_monitors();
4013 AgentLibrary* agent;
4014
4015 for (agent = Arguments::libraries(); agent != NULL; agent = agent->next()) {
4016 OnLoadEntry_t on_load_entry = lookup_jvm_on_load(agent);
4017
4018 if (on_load_entry != NULL) {
4019 // Invoke the JVM_OnLoad function
4020 JavaThread* thread = JavaThread::current();
4021 ThreadToNativeFromVM ttn(thread);
4022 HandleMark hm(thread);
4023 jint err = (*on_load_entry)(&main_vm, agent->options(), NULL);
4024 if (err != JNI_OK) {
4025 vm_exit_during_initialization("-Xrun library failed to init", agent->name());
4026 }
4027 } else {
4028 vm_exit_during_initialization("Could not find JVM_OnLoad function in -Xrun library", agent->name());
4029 }
4030 }
4031 }
4032
4033 JavaThread* Threads::find_java_thread_from_java_tid(jlong java_tid) {
4034 assert(Threads_lock->owned_by_self(), "Must hold Threads_lock");
4035
4036 JavaThread* java_thread = NULL;
4037 // Sequential search for now. Need to do better optimization later.
4038 for (JavaThread* thread = Threads::first(); thread != NULL; thread = thread->next()) {
4039 oop tobj = thread->threadObj();
4040 if (!thread->is_exiting() &&
4041 tobj != NULL &&
4042 java_tid == java_lang_Thread::thread_id(tobj)) {
4043 java_thread = thread;
4044 break;
4045 }
4046 }
4047 return java_thread;
4048 }
4049
4050
4051 // Last thread running calls java.lang.Shutdown.shutdown()
4052 void JavaThread::invoke_shutdown_hooks() {
4053 HandleMark hm(this);
4054
4055 // We could get here with a pending exception, if so clear it now.
4056 if (this->has_pending_exception()) {
4057 this->clear_pending_exception();
4058 }
4059
4060 EXCEPTION_MARK;
4061 Klass* shutdown_klass =
4062 SystemDictionary::resolve_or_null(vmSymbols::java_lang_Shutdown(),
4063 THREAD);
4064 if (shutdown_klass != NULL) {
4065 // SystemDictionary::resolve_or_null will return null if there was
4066 // an exception. If we cannot load the Shutdown class, just don't
4067 // call Shutdown.shutdown() at all. This will mean the shutdown hooks
4068 // and finalizers (if runFinalizersOnExit is set) won't be run.
4069 // Note that if a shutdown hook was registered or runFinalizersOnExit
4070 // was called, the Shutdown class would have already been loaded
4155 VMThread::wait_for_vm_thread_exit();
4156 assert(SafepointSynchronize::is_at_safepoint(), "VM thread should exit at Safepoint");
4157 VMThread::destroy();
4158 }
4159
4160 // clean up ideal graph printers
4161 #if defined(COMPILER2) && !defined(PRODUCT)
4162 IdealGraphPrinter::clean_up();
4163 #endif
4164
4165 // Now, all Java threads are gone except daemon threads. Daemon threads
4166 // running Java code or in VM are stopped by the Safepoint. However,
4167 // daemon threads executing native code are still running. But they
4168 // will be stopped at native=>Java/VM barriers. Note that we can't
4169 // simply kill or suspend them, as it is inherently deadlock-prone.
4170
4171 VM_Exit::set_vm_exited();
4172
4173 notify_vm_shutdown();
4174
4175 delete thread;
4176
4177 #if INCLUDE_JVMCI
4178 if (JVMCICounterSize > 0) {
4179 FREE_C_HEAP_ARRAY(jlong, JavaThread::_jvmci_old_thread_counters);
4180 }
4181 #endif
4182
4183 // exit_globals() will delete tty
4184 exit_globals();
4185
4186 LogConfiguration::finalize();
4187
4188 return true;
4189 }
4190
4191
4192 jboolean Threads::is_supported_jni_version_including_1_1(jint version) {
4193 if (version == JNI_VERSION_1_1) return JNI_TRUE;
4194 return is_supported_jni_version(version);
4195 }
4196
4197
4198 jboolean Threads::is_supported_jni_version(jint version) {
4199 if (version == JNI_VERSION_1_2) return JNI_TRUE;
4200 if (version == JNI_VERSION_1_4) return JNI_TRUE;
4201 if (version == JNI_VERSION_1_6) return JNI_TRUE;
4202 if (version == JNI_VERSION_1_8) return JNI_TRUE;
4203 if (version == JNI_VERSION_9) return JNI_TRUE;
4204 if (version == JNI_VERSION_10) return JNI_TRUE;
4205 return JNI_FALSE;
4206 }
4207
4208
4209 void Threads::add(JavaThread* p, bool force_daemon) {
4210 // The threads lock must be owned at this point
4211 assert_locked_or_safepoint(Threads_lock);
4212
4213 // See the comment for this method in thread.hpp for its purpose and
4214 // why it is called here.
4215 p->initialize_queues();
4216 p->set_next(_thread_list);
4217 _thread_list = p;
4218 _number_of_threads++;
4219 oop threadObj = p->threadObj();
4220 bool daemon = true;
4221 // Bootstrapping problem: threadObj can be null for initial
4222 // JavaThread (or for threads attached via JNI)
4223 if ((!force_daemon) && (threadObj == NULL || !java_lang_Thread::is_daemon(threadObj))) {
4224 _number_of_non_daemon_threads++;
4225 daemon = false;
4226 }
4227
4228 ThreadService::add_thread(p, daemon);
4229
4230 // Possible GC point.
4231 Events::log(p, "Thread added: " INTPTR_FORMAT, p2i(p));
4232 }
4233
4234 void Threads::remove(JavaThread* p) {
4235
4236 // Reclaim the objectmonitors from the omInUseList and omFreeList of the moribund thread.
4237 ObjectSynchronizer::omFlush(p);
4238
4239 // Extra scope needed for Thread_lock, so we can check
4240 // that we do not remove thread without safepoint code notice
4241 { MutexLocker ml(Threads_lock);
4242
4243 assert(includes(p), "p must be present");
4244
4245 JavaThread* current = _thread_list;
4246 JavaThread* prev = NULL;
4247
4248 while (current != p) {
4249 prev = current;
4250 current = current->next();
4251 }
4252
4253 if (prev) {
4254 prev->set_next(current->next());
4255 } else {
4256 _thread_list = p->next();
4257 }
4258 _number_of_threads--;
4259 oop threadObj = p->threadObj();
4260 bool daemon = true;
4261 if (threadObj == NULL || !java_lang_Thread::is_daemon(threadObj)) {
4262 _number_of_non_daemon_threads--;
4263 daemon = false;
4264
4265 // Only one thread left, do a notify on the Threads_lock so a thread waiting
4266 // on destroy_vm will wake up.
4267 if (number_of_non_daemon_threads() == 1) {
4268 Threads_lock->notify_all();
4269 }
4270 }
4271 ThreadService::remove_thread(p, daemon);
4272
4273 // Make sure that safepoint code disregard this thread. This is needed since
4274 // the thread might mess around with locks after this point. This can cause it
4275 // to do callbacks into the safepoint code. However, the safepoint code is not aware
4276 // of this thread since it is removed from the queue.
4277 p->set_terminated_value();
4278 } // unlock Threads_lock
4279
4280 // Since Events::log uses a lock, we grab it outside the Threads_lock
4281 Events::log(p, "Thread exited: " INTPTR_FORMAT, p2i(p));
4282 }
4283
4284 // Threads_lock must be held when this is called (or must be called during a safepoint)
4285 bool Threads::includes(JavaThread* p) {
4286 assert(Threads_lock->is_locked(), "sanity check");
4287 ALL_JAVA_THREADS(q) {
4288 if (q == p) {
4289 return true;
4290 }
4291 }
4292 return false;
4293 }
4294
4295 // Operations on the Threads list for GC. These are not explicitly locked,
4296 // but the garbage collector must provide a safe context for them to run.
4297 // In particular, these things should never be called when the Threads_lock
4298 // is held by some other thread. (Note: the Safepoint abstraction also
4299 // uses the Threads_lock to guarantee this property. It also makes sure that
4300 // all threads gets blocked when exiting or starting).
4301
4302 void Threads::oops_do(OopClosure* f, CodeBlobClosure* cf) {
4303 ALL_JAVA_THREADS(p) {
4304 p->oops_do(f, cf);
4305 }
4306 VMThread::vm_thread()->oops_do(f, cf);
4307 }
4308
4309 void Threads::change_thread_claim_parity() {
4310 // Set the new claim parity.
4311 assert(_thread_claim_parity >= 0 && _thread_claim_parity <= 2,
4312 "Not in range.");
4313 _thread_claim_parity++;
4314 if (_thread_claim_parity == 3) _thread_claim_parity = 1;
4387 ThreadHandlesClosure(void f(Metadata*)) : _f(f) {}
4388 virtual void do_thread(Thread* thread) {
4389 thread->metadata_handles_do(_f);
4390 }
4391 };
4392
4393 void Threads::metadata_handles_do(void f(Metadata*)) {
4394 // Only walk the Handles in Thread.
4395 ThreadHandlesClosure handles_closure(f);
4396 threads_do(&handles_closure);
4397 }
4398
4399 void Threads::deoptimized_wrt_marked_nmethods() {
4400 ALL_JAVA_THREADS(p) {
4401 p->deoptimized_wrt_marked_nmethods();
4402 }
4403 }
4404
4405
4406 // Get count Java threads that are waiting to enter the specified monitor.
4407 GrowableArray<JavaThread*>* Threads::get_pending_threads(int count,
4408 address monitor,
4409 bool doLock) {
4410 assert(doLock || SafepointSynchronize::is_at_safepoint(),
4411 "must grab Threads_lock or be at safepoint");
4412 GrowableArray<JavaThread*>* result = new GrowableArray<JavaThread*>(count);
4413
4414 int i = 0;
4415 {
4416 MutexLockerEx ml(doLock ? Threads_lock : NULL);
4417 ALL_JAVA_THREADS(p) {
4418 if (!p->can_call_java()) continue;
4419
4420 address pending = (address)p->current_pending_monitor();
4421 if (pending == monitor) { // found a match
4422 if (i < count) result->append(p); // save the first count matches
4423 i++;
4424 }
4425 }
4426 }
4427 return result;
4428 }
4429
4430
4431 JavaThread *Threads::owning_thread_from_monitor_owner(address owner,
4432 bool doLock) {
4433 assert(doLock ||
4434 Threads_lock->owned_by_self() ||
4435 SafepointSynchronize::is_at_safepoint(),
4436 "must grab Threads_lock or be at safepoint");
4437
4438 // NULL owner means not locked so we can skip the search
4439 if (owner == NULL) return NULL;
4440
4441 {
4442 MutexLockerEx ml(doLock ? Threads_lock : NULL);
4443 ALL_JAVA_THREADS(p) {
4444 // first, see if owner is the address of a Java thread
4445 if (owner == (address)p) return p;
4446 }
4447 }
4448 // Cannot assert on lack of success here since this function may be
4449 // used by code that is trying to report useful problem information
4450 // like deadlock detection.
4451 if (UseHeavyMonitors) return NULL;
4452
4453 // If we didn't find a matching Java thread and we didn't force use of
4454 // heavyweight monitors, then the owner is the stack address of the
4455 // Lock Word in the owning Java thread's stack.
4456 //
4457 JavaThread* the_owner = NULL;
4458 {
4459 MutexLockerEx ml(doLock ? Threads_lock : NULL);
4460 ALL_JAVA_THREADS(q) {
4461 if (q->is_lock_owned(owner)) {
4462 the_owner = q;
4463 break;
4464 }
4465 }
4466 }
4467 // cannot assert on lack of success here; see above comment
4468 return the_owner;
4469 }
4470
4471 // Threads::print_on() is called at safepoint by VM_PrintThreads operation.
4472 void Threads::print_on(outputStream* st, bool print_stacks,
4473 bool internal_format, bool print_concurrent_locks) {
4474 char buf[32];
4475 st->print_raw_cr(os::local_time_string(buf, sizeof(buf)));
4476
4477 st->print_cr("Full thread dump %s (%s %s):",
4478 Abstract_VM_Version::vm_name(),
4479 Abstract_VM_Version::vm_release(),
4480 Abstract_VM_Version::vm_info_string());
4481 st->cr();
4482
4483 #if INCLUDE_SERVICES
4484 // Dump concurrent locks
4485 ConcurrentLocksDump concurrent_locks;
4486 if (print_concurrent_locks) {
4487 concurrent_locks.dump_at_safepoint();
4488 }
4489 #endif // INCLUDE_SERVICES
4490
4491 ALL_JAVA_THREADS(p) {
4492 ResourceMark rm;
4493 p->print_on(st);
4494 if (print_stacks) {
4495 if (internal_format) {
4496 p->trace_stack();
4497 } else {
4498 p->print_stack_on(st);
4499 }
4500 }
4501 st->cr();
4502 #if INCLUDE_SERVICES
4503 if (print_concurrent_locks) {
4504 concurrent_locks.print_locks_on(p, st);
4505 }
4506 #endif // INCLUDE_SERVICES
4507 }
4508
4509 VMThread::vm_thread()->print_on(st);
4510 st->cr();
4511 Universe::heap()->print_gc_threads_on(st);
4512 WatcherThread* wt = WatcherThread::watcher_thread();
4513 if (wt != NULL) {
4514 wt->print_on(st);
4515 st->cr();
4516 }
4517 st->flush();
4518 }
4519
4520 void Threads::print_on_error(Thread* this_thread, outputStream* st, Thread* current, char* buf,
4521 int buflen, bool* found_current) {
4522 if (this_thread != NULL) {
4523 bool is_current = (current == this_thread);
4524 *found_current = *found_current || is_current;
4525 st->print("%s", is_current ? "=>" : " ");
4526
4527 st->print(PTR_FORMAT, p2i(this_thread));
4528 st->print(" ");
4529 this_thread->print_on_error(st, buf, buflen);
4530 st->cr();
4531 }
4532 }
4533
4534 class PrintOnErrorClosure : public ThreadClosure {
4535 outputStream* _st;
4536 Thread* _current;
4537 char* _buf;
4538 int _buflen;
4539 bool* _found_current;
4540 public:
4541 PrintOnErrorClosure(outputStream* st, Thread* current, char* buf,
4542 int buflen, bool* found_current) :
4543 _st(st), _current(current), _buf(buf), _buflen(buflen), _found_current(found_current) {}
4544
4545 virtual void do_thread(Thread* thread) {
4546 Threads::print_on_error(thread, _st, _current, _buf, _buflen, _found_current);
4547 }
4548 };
4549
4550 // Threads::print_on_error() is called by fatal error handler. It's possible
4551 // that VM is not at safepoint and/or current thread is inside signal handler.
4552 // Don't print stack trace, as the stack may not be walkable. Don't allocate
4553 // memory (even in resource area), it might deadlock the error handler.
4554 void Threads::print_on_error(outputStream* st, Thread* current, char* buf,
4555 int buflen) {
4556 bool found_current = false;
4557 st->print_cr("Java Threads: ( => current thread )");
4558 ALL_JAVA_THREADS(thread) {
4559 print_on_error(thread, st, current, buf, buflen, &found_current);
4560 }
4561 st->cr();
4562
4563 st->print_cr("Other Threads:");
4564 print_on_error(VMThread::vm_thread(), st, current, buf, buflen, &found_current);
4565 print_on_error(WatcherThread::watcher_thread(), st, current, buf, buflen, &found_current);
4566
4567 PrintOnErrorClosure print_closure(st, current, buf, buflen, &found_current);
4568 Universe::heap()->gc_threads_do(&print_closure);
4569
4570 if (!found_current) {
4571 st->cr();
4572 st->print("=>" PTR_FORMAT " (exited) ", p2i(current));
4573 current->print_on_error(st, buf, buflen);
4574 st->cr();
4575 }
4576 st->cr();
4577 st->print_cr("Threads with active compile tasks:");
4578 print_threads_compiling(st, buf, buflen);
4579 }
4580
4581 void Threads::print_threads_compiling(outputStream* st, char* buf, int buflen) {
4582 ALL_JAVA_THREADS(thread) {
4583 if (thread->is_Compiler_thread()) {
4584 CompilerThread* ct = (CompilerThread*) thread;
4585 if (ct->task() != NULL) {
4586 thread->print_name_on_error(st, buf, buflen);
4587 ct->task()->print(st, NULL, true, true);
4588 }
4589 }
4590 }
4591 }
4592
4593
4594 // Internal SpinLock and Mutex
4595 // Based on ParkEvent
4596
|
54 #include "oops/verifyOopClosure.hpp"
55 #include "prims/jvm_misc.hpp"
56 #include "prims/jvmtiExport.hpp"
57 #include "prims/jvmtiThreadState.hpp"
58 #include "prims/privilegedStack.hpp"
59 #include "runtime/arguments.hpp"
60 #include "runtime/atomic.hpp"
61 #include "runtime/biasedLocking.hpp"
62 #include "runtime/commandLineFlagConstraintList.hpp"
63 #include "runtime/commandLineFlagWriteableList.hpp"
64 #include "runtime/commandLineFlagRangeList.hpp"
65 #include "runtime/deoptimization.hpp"
66 #include "runtime/frame.inline.hpp"
67 #include "runtime/globals.hpp"
68 #include "runtime/handshake.hpp"
69 #include "runtime/init.hpp"
70 #include "runtime/interfaceSupport.hpp"
71 #include "runtime/java.hpp"
72 #include "runtime/javaCalls.hpp"
73 #include "runtime/jniPeriodicChecker.hpp"
74 #include "runtime/memprofiler.hpp"
75 #include "runtime/mutexLocker.hpp"
76 #include "runtime/objectMonitor.hpp"
77 #include "runtime/orderAccess.inline.hpp"
78 #include "runtime/osThread.hpp"
79 #include "runtime/prefetch.inline.hpp"
80 #include "runtime/safepoint.hpp"
81 #include "runtime/safepointMechanism.inline.hpp"
82 #include "runtime/sharedRuntime.hpp"
83 #include "runtime/statSampler.hpp"
84 #include "runtime/stubRoutines.hpp"
85 #include "runtime/sweeper.hpp"
86 #include "runtime/task.hpp"
87 #include "runtime/thread.inline.hpp"
88 #include "runtime/threadCritical.hpp"
89 #include "runtime/threadSMR.inline.hpp"
90 #include "runtime/timer.hpp"
91 #include "runtime/timerTrace.hpp"
92 #include "runtime/vframe.hpp"
93 #include "runtime/vframeArray.hpp"
94 #include "runtime/vframe_hp.hpp"
95 #include "runtime/vmThread.hpp"
96 #include "runtime/vm_operations.hpp"
97 #include "runtime/vm_version.hpp"
98 #include "services/attachListener.hpp"
99 #include "services/management.hpp"
100 #include "services/memTracker.hpp"
101 #include "services/threadService.hpp"
102 #include "trace/traceMacros.hpp"
103 #include "trace/tracing.hpp"
104 #include "utilities/align.hpp"
105 #include "utilities/defaultStream.hpp"
106 #include "utilities/dtrace.hpp"
107 #include "utilities/events.hpp"
108 #include "utilities/macros.hpp"
109 #include "utilities/preserveException.hpp"
110 #include "utilities/resourceHash.hpp"
111 #include "utilities/vmError.hpp"
112 #if INCLUDE_ALL_GCS
113 #include "gc/cms/concurrentMarkSweepThread.hpp"
114 #include "gc/g1/concurrentMarkThread.inline.hpp"
115 #include "gc/parallel/pcTasks.hpp"
116 #endif // INCLUDE_ALL_GCS
117 #if INCLUDE_JVMCI
118 #include "jvmci/jvmciCompiler.hpp"
119 #include "jvmci/jvmciRuntime.hpp"
120 #include "logging/logHandle.hpp"
121 #endif
122 #ifdef COMPILER1
123 #include "c1/c1_Compiler.hpp"
124 #endif
125 #ifdef COMPILER2
126 #include "opto/c2compiler.hpp"
127 #include "opto/idealGraphPrinter.hpp"
128 #endif
129 #if INCLUDE_RTM_OPT
130 #include "runtime/rtmLocking.hpp"
182 AllocFailStrategy::RETURN_NULL);
183 void* aligned_addr = align_up(real_malloc_addr, alignment);
184 assert(((uintptr_t) aligned_addr + (uintptr_t) size) <=
185 ((uintptr_t) real_malloc_addr + (uintptr_t) aligned_size),
186 "JavaThread alignment code overflowed allocated storage");
187 if (aligned_addr != real_malloc_addr) {
188 log_info(biasedlocking)("Aligned thread " INTPTR_FORMAT " to " INTPTR_FORMAT,
189 p2i(real_malloc_addr),
190 p2i(aligned_addr));
191 }
192 ((Thread*) aligned_addr)->_real_malloc_address = real_malloc_addr;
193 return aligned_addr;
194 } else {
195 return throw_excpt? AllocateHeap(size, flags, CURRENT_PC)
196 : AllocateHeap(size, flags, CURRENT_PC, AllocFailStrategy::RETURN_NULL);
197 }
198 }
199
200 void Thread::operator delete(void* p) {
201 if (UseBiasedLocking) {
202 FreeHeap(((Thread*) p)->_real_malloc_address);
203 } else {
204 FreeHeap(p);
205 }
206 }
207
208 void JavaThread::smr_delete() {
209 if (_on_thread_list) {
210 Threads::smr_delete(this);
211 } else {
212 delete this;
213 }
214 }
215
216 // Base class for all threads: VMThread, WatcherThread, ConcurrentMarkSweepThread,
217 // JavaThread
218
219
220 Thread::Thread() {
221 // stack and get_thread
222 set_stack_base(NULL);
223 set_stack_size(0);
224 set_self_raw_id(0);
225 set_lgrp_id(-1);
226 DEBUG_ONLY(clear_suspendible_thread();)
227
228 // allocated data structures
229 set_osthread(NULL);
230 set_resource_area(new (mtThread)ResourceArea());
231 DEBUG_ONLY(_current_resource_mark = NULL;)
232 set_handle_area(new (mtThread) HandleArea(NULL));
233 set_metadata_handles(new (ResourceObj::C_HEAP, mtClass) GrowableArray<Metadata*>(30, true));
234 set_active_handles(NULL);
235 set_free_handle_block(NULL);
236 set_last_handle_mark(NULL);
237
238 // This initial value ==> never claimed.
239 _oops_do_parity = 0;
240 _threads_hazard_ptr = NULL;
241 _nested_threads_hazard_ptr = NULL;
242 _nested_threads_hazard_ptr_cnt = 0;
243
244 // the handle mark links itself to last_handle_mark
245 new HandleMark(this);
246
247 // plain initialization
248 debug_only(_owned_locks = NULL;)
249 debug_only(_allow_allocation_count = 0;)
250 NOT_PRODUCT(_allow_safepoint_count = 0;)
251 NOT_PRODUCT(_skip_gcalot = false;)
252 _jvmti_env_iteration_count = 0;
253 set_allocated_bytes(0);
254 _vm_operation_started_count = 0;
255 _vm_operation_completed_count = 0;
256 _current_pending_monitor = NULL;
257 _current_pending_monitor_is_from_java = true;
258 _current_waiting_monitor = NULL;
259 _num_nested_signal = 0;
260 omFreeList = NULL;
261 omFreeCount = 0;
262 omFreeProvision = 32;
394 _SR_lock = NULL;
395
396 // osthread() can be NULL, if creation of thread failed.
397 if (osthread() != NULL) os::free_thread(osthread());
398
399 // clear Thread::current if thread is deleting itself.
400 // Needed to ensure JNI correctly detects non-attached threads.
401 if (this == Thread::current()) {
402 clear_thread_current();
403 }
404
405 CHECK_UNHANDLED_OOPS_ONLY(if (CheckUnhandledOops) delete unhandled_oops();)
406 }
407
408 // NOTE: dummy function for assertion purpose.
409 void Thread::run() {
410 ShouldNotReachHere();
411 }
412
413 #ifdef ASSERT
414 // A JavaThread is considered "dangling" if it is not the current
415 // thread, has been added the Threads list, the system is not at a
416 // safepoint and the Thread is not "protected".
417 //
418 void Thread::check_for_dangling_thread_pointer(Thread *thread) {
419 assert(!thread->is_Java_thread() || Thread::current() == thread ||
420 !((JavaThread *) thread)->on_thread_list() ||
421 SafepointSynchronize::is_at_safepoint() ||
422 Threads::is_a_protected_JavaThread_with_lock((JavaThread *) thread),
423 "possibility of dangling Thread pointer");
424 }
425 #endif
426
427 ThreadPriority Thread::get_priority(const Thread* const thread) {
428 ThreadPriority priority;
429 // Can return an error!
430 (void)os::get_priority(thread, priority);
431 assert(MinPriority <= priority && priority <= MaxPriority, "non-Java priority found");
432 return priority;
433 }
434
435 void Thread::set_priority(Thread* thread, ThreadPriority priority) {
436 debug_only(check_for_dangling_thread_pointer(thread);)
437 // Can return an error!
438 (void)os::set_priority(thread, priority);
439 }
440
441
442 void Thread::start(Thread* thread) {
734
735 if (!pending) {
736 // A cancelled suspend request is the only false return from
737 // is_ext_suspend_completed() that keeps us from staying in the
738 // retry loop.
739 *bits |= 0x00080000;
740 return false;
741 }
742
743 if (is_suspended) {
744 *bits |= 0x00100000;
745 return true;
746 }
747 } // end retry loop
748
749 // thread did not suspend after all our retries
750 *bits |= 0x00200000;
751 return false;
752 }
753
754 // Called from API entry points which perform stack walking. If the
755 // associated JavaThread is the current thread, then wait_for_suspend
756 // is not used. Otherwise, it determines if we should wait for the
757 // "other" thread to complete external suspension. (NOTE: in future
758 // releases the suspension mechanism should be reimplemented so this
759 // is not necessary.)
760 //
761 bool
762 JavaThread::is_thread_fully_suspended(bool wait_for_suspend, uint32_t *bits) {
763 if (this != JavaThread::current()) {
764 // "other" threads require special handling.
765 if (wait_for_suspend) {
766 // We are allowed to wait for the external suspend to complete
767 // so give the other thread a chance to get suspended.
768 if (!wait_for_ext_suspend_completion(SuspendRetryCount,
769 SuspendRetryDelay, bits)) {
770 // Didn't make it so let the caller know.
771 return false;
772 }
773 }
774 // We aren't allowed to wait for the external suspend to complete
775 // so if the other thread isn't externally suspended we need to
776 // let the caller know.
777 else if (!is_ext_suspend_completed_with_lock(bits)) {
778 return false;
779 }
780 }
781
782 return true;
783 }
784
785 #ifndef PRODUCT
786 void JavaThread::record_jump(address target, address instr, const char* file,
787 int line) {
788
789 // This should not need to be atomic as the only way for simultaneous
790 // updates is via interrupts. Even then this should be rare or non-existent
791 // and we don't care that much anyway.
792
793 int index = _jmp_ring_index;
794 _jmp_ring_index = (index + 1) & (jump_ring_buffer_size - 1);
795 _jmp_ring[index]._target = (intptr_t) target;
796 _jmp_ring[index]._instruction = (intptr_t) instr;
797 _jmp_ring[index]._file = file;
798 _jmp_ring[index]._line = line;
799 }
800 #endif // PRODUCT
801
802 void Thread::interrupt(Thread* thread) {
803 debug_only(check_for_dangling_thread_pointer(thread);)
804 os::interrupt(thread);
843 void Thread::metadata_handles_do(void f(Metadata*)) {
844 // Only walk the Handles in Thread.
845 if (metadata_handles() != NULL) {
846 for (int i = 0; i< metadata_handles()->length(); i++) {
847 f(metadata_handles()->at(i));
848 }
849 }
850 }
851
852 void Thread::print_on(outputStream* st) const {
853 // get_priority assumes osthread initialized
854 if (osthread() != NULL) {
855 int os_prio;
856 if (os::get_native_priority(this, &os_prio) == OS_OK) {
857 st->print("os_prio=%d ", os_prio);
858 }
859 st->print("tid=" INTPTR_FORMAT " ", p2i(this));
860 ext().print_on(st);
861 osthread()->print_on(st);
862 }
863 if (_threads_hazard_ptr != NULL) {
864 st->print("_threads_hazard_ptr=" INTPTR_FORMAT, p2i(_threads_hazard_ptr));
865 }
866 if (_nested_threads_hazard_ptr != NULL) {
867 print_nested_threads_hazard_ptrs_on(st);
868 }
869 st->print(" ");
870 debug_only(if (WizardMode) print_owned_locks_on(st);)
871 }
872
873 void Thread::print_nested_threads_hazard_ptrs_on(outputStream* st) const {
874 assert(_nested_threads_hazard_ptr != NULL, "must be set to print");
875
876 if (EnableThreadSMRStatistics) {
877 st->print(", _nested_threads_hazard_ptr_cnt=%u", _nested_threads_hazard_ptr_cnt);
878 }
879 st->print(", _nested_threads_hazard_ptrs=");
880 for (NestedThreadsList* node = _nested_threads_hazard_ptr; node != NULL;
881 node = node->next()) {
882 if (node != _nested_threads_hazard_ptr) {
883 // First node does not need a comma-space separator.
884 st->print(", ");
885 }
886 st->print(INTPTR_FORMAT, p2i(node->t_list()));
887 }
888 }
889
890 // Thread::print_on_error() is called by fatal error handler. Don't use
891 // any lock or allocate memory.
892 void Thread::print_on_error(outputStream* st, char* buf, int buflen) const {
893 assert(!(is_Compiler_thread() || is_Java_thread()), "Can't call name() here if it allocates");
894
895 if (is_VM_thread()) { st->print("VMThread"); }
896 else if (is_GC_task_thread()) { st->print("GCTaskThread"); }
897 else if (is_Watcher_thread()) { st->print("WatcherThread"); }
898 else if (is_ConcurrentGC_thread()) { st->print("ConcurrentGCThread"); }
899 else { st->print("Thread"); }
900
901 if (is_Named_thread()) {
902 st->print(" \"%s\"", name());
903 }
904
905 st->print(" [stack: " PTR_FORMAT "," PTR_FORMAT "]",
906 p2i(stack_end()), p2i(stack_base()));
907
908 if (osthread()) {
909 st->print(" [id=%d]", osthread()->thread_id());
910 }
911
912 if (_threads_hazard_ptr != NULL) {
913 st->print(" _threads_hazard_ptr=" INTPTR_FORMAT, p2i(_threads_hazard_ptr));
914 }
915 if (_nested_threads_hazard_ptr != NULL) {
916 print_nested_threads_hazard_ptrs_on(st);
917 }
918 }
919
920 void Thread::print_value_on(outputStream* st) const {
921 if (is_Named_thread()) {
922 st->print(" \"%s\" ", name());
923 }
924 st->print(INTPTR_FORMAT, p2i(this)); // print address
925 }
926
927 #ifdef ASSERT
928 void Thread::print_owned_locks_on(outputStream* st) const {
929 Monitor *cur = _owned_locks;
930 if (cur == NULL) {
931 st->print(" (no locks) ");
932 } else {
933 st->print_cr(" Locks owned:");
934 while (cur) {
935 cur->print_on(st);
936 cur = cur->next();
937 }
938 }
939 }
940
941 static int ref_use_count = 0;
942
943 bool Thread::owns_locks_but_compiled_lock() const {
944 for (Monitor *cur = _owned_locks; cur; cur = cur->next()) {
945 if (cur != Compile_lock) return true;
946 }
947 return false;
948 }
949
950
951 #endif
952
953 #ifndef PRODUCT
954
955 // The flag: potential_vm_operation notifies if this particular safepoint state could potentially
956 // invoke the vm-thread (e.g., an oop allocation). In that case, we also have to make sure that
957 // no threads which allow_vm_block's are held
958 void Thread::check_for_valid_safepoint_state(bool potential_vm_operation) {
959 // Check if current thread is allowed to block at a safepoint
960 if (!(_allow_safepoint_count == 0)) {
961 fatal("Possible safepoint reached by thread that does not allow it");
962 }
963 if (is_Java_thread() && ((JavaThread*)this)->thread_state() != _thread_in_vm) {
964 fatal("LEAF method calling lock?");
965 }
966
967 #ifdef ASSERT
968 if (potential_vm_operation && is_Java_thread()
969 && !Universe::is_bootstrapping()) {
970 // Make sure we do not hold any locks that the VM thread also uses.
971 // This could potentially lead to deadlocks
972 for (Monitor *cur = _owned_locks; cur; cur = cur->next()) {
973 // Threads_lock is special, since the safepoint synchronization will not start before this is
974 // acquired. Hence, a JavaThread cannot be holding it at a safepoint. So is VMOperationRequest_lock,
975 // since it is used to transfer control between JavaThreads and the VMThread
976 // Do not *exclude* any locks unless you are absolutely sure it is correct. Ask someone else first!
1462
1463 void WatcherThread::print_on(outputStream* st) const {
1464 st->print("\"%s\" ", name());
1465 Thread::print_on(st);
1466 st->cr();
1467 }
1468
1469 // ======= JavaThread ========
1470
1471 #if INCLUDE_JVMCI
1472
1473 jlong* JavaThread::_jvmci_old_thread_counters;
1474
1475 bool jvmci_counters_include(JavaThread* thread) {
1476 oop threadObj = thread->threadObj();
1477 return !JVMCICountersExcludeCompiler || !thread->is_Compiler_thread();
1478 }
1479
1480 void JavaThread::collect_counters(typeArrayOop array) {
1481 if (JVMCICounterSize > 0) {
1482 // dcubed - Looks like the Threads_lock is for stable access
1483 // to _jvmci_old_thread_counters and _jvmci_counters.
1484 MutexLocker tl(Threads_lock);
1485 JavaThreadIteratorWithHandle jtiwh;
1486 for (int i = 0; i < array->length(); i++) {
1487 array->long_at_put(i, _jvmci_old_thread_counters[i]);
1488 }
1489 for (; JavaThread *tp = jtiwh.next(); ) {
1490 if (jvmci_counters_include(tp)) {
1491 for (int i = 0; i < array->length(); i++) {
1492 array->long_at_put(i, array->long_at(i) + tp->_jvmci_counters[i]);
1493 }
1494 }
1495 }
1496 }
1497 }
1498
1499 #endif // INCLUDE_JVMCI
1500
1501 // A JavaThread is a normal Java thread
1502
1503 void JavaThread::initialize() {
1504 // Initialize fields
1505
1506 set_saved_exception_pc(NULL);
1507 set_threadObj(NULL);
1508 _anchor.clear();
1509 set_entry_point(NULL);
1510 set_jni_functions(jni_functions());
1511 set_callee_target(NULL);
1512 set_vm_result(NULL);
1513 set_vm_result_2(NULL);
1514 set_vframe_array_head(NULL);
1515 set_vframe_array_last(NULL);
1516 set_deferred_locals(NULL);
1517 set_deopt_mark(NULL);
1518 set_deopt_compiled_method(NULL);
1519 clear_must_deopt_id();
1520 set_monitor_chunks(NULL);
1521 set_next(NULL);
1522 _on_thread_list = false;
1523 set_thread_state(_thread_new);
1524 _terminated = _not_terminated;
1525 _privileged_stack_top = NULL;
1526 _array_for_gc = NULL;
1527 _suspend_equivalent = false;
1528 _in_deopt_handler = 0;
1529 _doing_unsafe_access = false;
1530 _stack_guard_state = stack_guard_unused;
1531 #if INCLUDE_JVMCI
1532 _pending_monitorenter = false;
1533 _pending_deoptimization = -1;
1534 _pending_failed_speculation = NULL;
1535 _pending_transfer_to_interpreter = false;
1536 _adjusting_comp_level = false;
1537 _jvmci._alternate_call_target = NULL;
1538 assert(_jvmci._implicit_exception_pc == NULL, "must be");
1539 if (JVMCICounterSize > 0) {
1540 _jvmci_counters = NEW_C_HEAP_ARRAY(jlong, JVMCICounterSize, mtInternal);
1541 memset(_jvmci_counters, 0, sizeof(jlong) * JVMCICounterSize);
1542 } else {
1783 void JavaThread::thread_main_inner() {
1784 assert(JavaThread::current() == this, "sanity check");
1785 assert(this->threadObj() != NULL, "just checking");
1786
1787 // Execute thread entry point unless this thread has a pending exception
1788 // or has been stopped before starting.
1789 // Note: Due to JVM_StopThread we can have pending exceptions already!
1790 if (!this->has_pending_exception() &&
1791 !java_lang_Thread::is_stillborn(this->threadObj())) {
1792 {
1793 ResourceMark rm(this);
1794 this->set_native_thread_name(this->get_thread_name());
1795 }
1796 HandleMark hm(this);
1797 this->entry_point()(this, this);
1798 }
1799
1800 DTRACE_THREAD_PROBE(stop, this);
1801
1802 this->exit(false);
1803 this->smr_delete();
1804 }
1805
1806
1807 static void ensure_join(JavaThread* thread) {
1808 // We do not need to grab the Threads_lock, since we are operating on ourself.
1809 Handle threadObj(thread, thread->threadObj());
1810 assert(threadObj.not_null(), "java thread object must exist");
1811 ObjectLocker lock(threadObj, thread);
1812 // Ignore pending exception (ThreadDeath), since we are exiting anyway
1813 thread->clear_pending_exception();
1814 // Thread is exiting. So set thread_status field in java.lang.Thread class to TERMINATED.
1815 java_lang_Thread::set_thread_status(threadObj(), java_lang_Thread::TERMINATED);
1816 // Clear the native thread instance - this makes isAlive return false and allows the join()
1817 // to complete once we've done the notify_all below
1818 java_lang_Thread::set_thread(threadObj(), NULL);
1819 lock.notify_all(thread);
1820 // Ignore pending exception (ThreadDeath), since we are exiting anyway
1821 thread->clear_pending_exception();
1822 }
1823
1824
1825 // For any new cleanup additions, please check to see if they need to be applied to
1826 // cleanup_failed_attach_current_thread as well.
1827 void JavaThread::exit(bool destroy_vm, ExitType exit_type) {
1828 assert(this == JavaThread::current(), "thread consistency check");
1829
1830 elapsedTimer _timer_exit_phase1;
1831 elapsedTimer _timer_exit_phase2;
1832 elapsedTimer _timer_exit_phase3;
1833 elapsedTimer _timer_exit_phase4;
1834
1835 if (log_is_enabled(Debug, os, thread, timer)) {
1836 _timer_exit_phase1.start();
1837 }
1838
1839 HandleMark hm(this);
1840 Handle uncaught_exception(this, this->pending_exception());
1841 this->clear_pending_exception();
1842 Handle threadObj(this, this->threadObj());
1843 assert(threadObj.not_null(), "Java thread object should be created");
1844
1845 // FIXIT: This code should be moved into else part, when reliable 1.2/1.3 check is in place
1846 {
1847 EXCEPTION_MARK;
1848
1849 CLEAR_PENDING_EXCEPTION;
1850 }
1851 if (!destroy_vm) {
1852 if (uncaught_exception.not_null()) {
1853 EXCEPTION_MARK;
1854 // Call method Thread.dispatchUncaughtException().
1855 Klass* thread_klass = SystemDictionary::Thread_klass();
1856 JavaValue result(T_VOID);
1857 JavaCalls::call_virtual(&result,
1858 threadObj, thread_klass,
1918 // Implied else:
1919 // Things get a little tricky here. We have a pending external
1920 // suspend request, but we are holding the SR_lock so we
1921 // can't just self-suspend. So we temporarily drop the lock
1922 // and then self-suspend.
1923 }
1924
1925 ThreadBlockInVM tbivm(this);
1926 java_suspend_self();
1927
1928 // We're done with this suspend request, but we have to loop around
1929 // and check again. Eventually we will get SR_lock without a pending
1930 // external suspend request and will be able to mark ourselves as
1931 // exiting.
1932 }
1933 // no more external suspends are allowed at this point
1934 } else {
1935 // before_exit() has already posted JVMTI THREAD_END events
1936 }
1937
1938 if (log_is_enabled(Debug, os, thread, timer)) {
1939 _timer_exit_phase1.stop();
1940 _timer_exit_phase2.start();
1941 }
1942 // Notify waiters on thread object. This has to be done after exit() is called
1943 // on the thread (if the thread is the last thread in a daemon ThreadGroup the
1944 // group should have the destroyed bit set before waiters are notified).
1945 ensure_join(this);
1946 assert(!this->has_pending_exception(), "ensure_join should have cleared");
1947
1948 if (log_is_enabled(Debug, os, thread, timer)) {
1949 _timer_exit_phase2.stop();
1950 _timer_exit_phase3.start();
1951 }
1952 // 6282335 JNI DetachCurrentThread spec states that all Java monitors
1953 // held by this thread must be released. The spec does not distinguish
1954 // between JNI-acquired and regular Java monitors. We can only see
1955 // regular Java monitors here if monitor enter-exit matching is broken.
1956 //
1957 // Optionally release any monitors for regular JavaThread exits. This
1958 // is provided as a work around for any bugs in monitor enter-exit
1959 // matching. This can be expensive so it is not enabled by default.
1960 //
1961 // ensure_join() ignores IllegalThreadStateExceptions, and so does
1962 // ObjectSynchronizer::release_monitors_owned_by_thread().
1963 if (exit_type == jni_detach || ObjectMonitor::Knob_ExitRelease) {
1964 // Sanity check even though JNI DetachCurrentThread() would have
1965 // returned JNI_ERR if there was a Java frame. JavaThread exit
1966 // should be done executing Java code by the time we get here.
1967 assert(!this->has_last_Java_frame(),
1968 "should not have a Java frame when detaching or exiting");
1969 ObjectSynchronizer::release_monitors_owned_by_thread(this);
1970 assert(!this->has_pending_exception(), "release_monitors should have cleared");
1971 }
1999
2000 // We must flush any deferred card marks before removing a thread from
2001 // the list of active threads.
2002 Universe::heap()->flush_deferred_store_barrier(this);
2003 assert(deferred_card_mark().is_empty(), "Should have been flushed");
2004
2005 #if INCLUDE_ALL_GCS
2006 // We must flush the G1-related buffers before removing a thread
2007 // from the list of active threads. We must do this after any deferred
2008 // card marks have been flushed (above) so that any entries that are
2009 // added to the thread's dirty card queue as a result are not lost.
2010 if (UseG1GC) {
2011 flush_barrier_queues();
2012 }
2013 #endif // INCLUDE_ALL_GCS
2014
2015 log_info(os, thread)("JavaThread %s (tid: " UINTX_FORMAT ").",
2016 exit_type == JavaThread::normal_exit ? "exiting" : "detaching",
2017 os::current_thread_id());
2018
2019 if (log_is_enabled(Debug, os, thread, timer)) {
2020 _timer_exit_phase3.stop();
2021 _timer_exit_phase4.start();
2022 }
2023 // Remove from list of active threads list, and notify VM thread if we are the last non-daemon thread
2024 Threads::remove(this);
2025
2026 // If someone set a handshake on us just as we entered exit path, we simple cancel it.
2027 if (ThreadLocalHandshakes) {
2028 cancel_handshake();
2029 }
2030
2031 if (log_is_enabled(Debug, os, thread, timer)) {
2032 _timer_exit_phase4.stop();
2033 ResourceMark rm(this);
2034 log_debug(os, thread, timer)("name='%s'"
2035 ", exit-phase1=" JLONG_FORMAT
2036 ", exit-phase2=" JLONG_FORMAT
2037 ", exit-phase3=" JLONG_FORMAT
2038 ", exit-phase4=" JLONG_FORMAT,
2039 get_thread_name(),
2040 _timer_exit_phase1.milliseconds(),
2041 _timer_exit_phase2.milliseconds(),
2042 _timer_exit_phase3.milliseconds(),
2043 _timer_exit_phase4.milliseconds());
2044 }
2045 }
2046
2047 #if INCLUDE_ALL_GCS
2048 // Flush G1-related queues.
2049 void JavaThread::flush_barrier_queues() {
2050 satb_mark_queue().flush();
2051 dirty_card_queue().flush();
2052 }
2053
2054 void JavaThread::initialize_queues() {
2055 assert(!SafepointSynchronize::is_at_safepoint(),
2056 "we should not be at a safepoint");
2057
2058 SATBMarkQueue& satb_queue = satb_mark_queue();
2059 SATBMarkQueueSet& satb_queue_set = satb_mark_queue_set();
2060 // The SATB queue should have been constructed with its active
2061 // field set to false.
2062 assert(!satb_queue.is_active(), "SATB queue should not be active");
2063 assert(satb_queue.is_empty(), "SATB queue should be empty");
2064 // If we are creating the thread during a marking cycle, we should
2084 if (free_handle_block() != NULL) {
2085 JNIHandleBlock* block = free_handle_block();
2086 set_free_handle_block(NULL);
2087 JNIHandleBlock::release_block(block);
2088 }
2089
2090 // These have to be removed while this is still a valid thread.
2091 remove_stack_guard_pages();
2092
2093 if (UseTLAB) {
2094 tlab().make_parsable(true); // retire TLAB, if any
2095 }
2096
2097 #if INCLUDE_ALL_GCS
2098 if (UseG1GC) {
2099 flush_barrier_queues();
2100 }
2101 #endif // INCLUDE_ALL_GCS
2102
2103 Threads::remove(this);
2104 this->smr_delete();
2105 }
2106
2107
2108
2109
2110 JavaThread* JavaThread::active() {
2111 Thread* thread = Thread::current();
2112 if (thread->is_Java_thread()) {
2113 return (JavaThread*) thread;
2114 } else {
2115 assert(thread->is_VM_thread(), "this must be a vm thread");
2116 VM_Operation* op = ((VMThread*) thread)->vm_operation();
2117 JavaThread *ret=op == NULL ? NULL : (JavaThread *)op->calling_thread();
2118 assert(ret->is_Java_thread(), "must be a Java thread");
2119 return ret;
2120 }
2121 }
2122
2123 bool JavaThread::is_lock_owned(address adr) const {
2124 if (Thread::is_lock_owned(adr)) return true;
2339 }
2340
2341
2342 // Interrupt thread so it will wake up from a potential wait()
2343 Thread::interrupt(this);
2344 }
2345
2346 // External suspension mechanism.
2347 //
2348 // Tell the VM to suspend a thread when ever it knows that it does not hold on
2349 // to any VM_locks and it is at a transition
2350 // Self-suspension will happen on the transition out of the vm.
2351 // Catch "this" coming in from JNIEnv pointers when the thread has been freed
2352 //
2353 // Guarantees on return:
2354 // + Target thread will not execute any new bytecode (that's why we need to
2355 // force a safepoint)
2356 // + Target thread will not enter any new monitors
2357 //
2358 void JavaThread::java_suspend() {
2359 ThreadsListHandle tlh;
2360 if (!tlh.includes(this) || threadObj() == NULL || is_exiting()) {
2361 return;
2362 }
2363
2364 { MutexLockerEx ml(SR_lock(), Mutex::_no_safepoint_check_flag);
2365 if (!is_external_suspend()) {
2366 // a racing resume has cancelled us; bail out now
2367 return;
2368 }
2369
2370 // suspend is done
2371 uint32_t debug_bits = 0;
2372 // Warning: is_ext_suspend_completed() may temporarily drop the
2373 // SR_lock to allow the thread to reach a stable thread state if
2374 // it is currently in a transient thread state.
2375 if (is_ext_suspend_completed(false /* !called_by_wait */,
2376 SuspendRetryDelay, &debug_bits)) {
2377 return;
2378 }
2379 }
2380
2381 VM_ThreadSuspend vm_suspend;
2382 VMThread::execute(&vm_suspend);
2430 // it. This would be a "bad thing (TM)" and cause the stack walker
2431 // to crash. We stay self-suspended until there are no more pending
2432 // external suspend requests.
2433 while (is_external_suspend()) {
2434 ret++;
2435 this->set_ext_suspended();
2436
2437 // _ext_suspended flag is cleared by java_resume()
2438 while (is_ext_suspended()) {
2439 this->SR_lock()->wait(Mutex::_no_safepoint_check_flag);
2440 }
2441 }
2442
2443 return ret;
2444 }
2445
2446 #ifdef ASSERT
2447 // verify the JavaThread has not yet been published in the Threads::list, and
2448 // hence doesn't need protection from concurrent access at this stage
2449 void JavaThread::verify_not_published() {
2450 ThreadsListHandle tlh;
2451 assert(!tlh.includes(this), "JavaThread shouldn't have been published yet!");
2452 }
2453 #endif
2454
2455 // Slow path when the native==>VM/Java barriers detect a safepoint is in
2456 // progress or when _suspend_flags is non-zero.
2457 // Current thread needs to self-suspend if there is a suspend request and/or
2458 // block if a safepoint is in progress.
2459 // Async exception ISN'T checked.
2460 // Note only the ThreadInVMfromNative transition can call this function
2461 // directly and when thread state is _thread_in_native_trans
2462 void JavaThread::check_safepoint_and_suspend_for_native_trans(JavaThread *thread) {
2463 assert(thread->thread_state() == _thread_in_native_trans, "wrong state");
2464
2465 JavaThread *curJT = JavaThread::current();
2466 bool do_self_suspend = thread->is_external_suspend();
2467
2468 assert(!curJT->has_last_Java_frame() || curJT->frame_anchor()->walkable(), "Unwalkable stack in native->vm transition");
2469
2470 // If JNIEnv proxies are allowed, don't self-suspend if the target
2471 // thread is not the current thread. In older versions of jdbx, jdbx
2548 check_special_condition_for_native_trans(thread);
2549
2550 // Finish the transition
2551 thread->set_thread_state(_thread_in_Java);
2552
2553 if (thread->do_critical_native_unlock()) {
2554 ThreadInVMfromJavaNoAsyncException tiv(thread);
2555 GCLocker::unlock_critical(thread);
2556 thread->clear_critical_native_unlock();
2557 }
2558 }
2559
2560 // We need to guarantee the Threads_lock here, since resumes are not
2561 // allowed during safepoint synchronization
2562 // Can only resume from an external suspension
2563 void JavaThread::java_resume() {
2564 assert_locked_or_safepoint(Threads_lock);
2565
2566 // Sanity check: thread is gone, has started exiting or the thread
2567 // was not externally suspended.
2568 ThreadsListHandle tlh;
2569 if (!tlh.includes(this) || is_exiting() || !is_external_suspend()) {
2570 return;
2571 }
2572
2573 MutexLockerEx ml(SR_lock(), Mutex::_no_safepoint_check_flag);
2574
2575 clear_external_suspend();
2576
2577 if (is_ext_suspended()) {
2578 clear_ext_suspended();
2579 SR_lock()->notify_all();
2580 }
2581 }
2582
2583 size_t JavaThread::_stack_red_zone_size = 0;
2584 size_t JavaThread::_stack_yellow_zone_size = 0;
2585 size_t JavaThread::_stack_reserved_zone_size = 0;
2586 size_t JavaThread::_stack_shadow_zone_size = 0;
2587
2588 void JavaThread::create_stack_guard_pages() {
2589 if (!os::uses_stack_guard_pages() || _stack_guard_state != stack_guard_unused) { return; }
3017 void JavaThread::print_name_on_error(outputStream* st, char *buf, int buflen) const {
3018 st->print("%s", get_thread_name_string(buf, buflen));
3019 }
3020
3021 // Called by fatal error handler. The difference between this and
3022 // JavaThread::print() is that we can't grab lock or allocate memory.
3023 void JavaThread::print_on_error(outputStream* st, char *buf, int buflen) const {
3024 st->print("JavaThread \"%s\"", get_thread_name_string(buf, buflen));
3025 oop thread_obj = threadObj();
3026 if (thread_obj != NULL) {
3027 if (java_lang_Thread::is_daemon(thread_obj)) st->print(" daemon");
3028 }
3029 st->print(" [");
3030 st->print("%s", _get_thread_state_name(_thread_state));
3031 if (osthread()) {
3032 st->print(", id=%d", osthread()->thread_id());
3033 }
3034 st->print(", stack(" PTR_FORMAT "," PTR_FORMAT ")",
3035 p2i(stack_end()), p2i(stack_base()));
3036 st->print("]");
3037
3038 if (_threads_hazard_ptr != NULL) {
3039 st->print(" _threads_hazard_ptr=" INTPTR_FORMAT, p2i(_threads_hazard_ptr));
3040 }
3041 if (_nested_threads_hazard_ptr != NULL) {
3042 print_nested_threads_hazard_ptrs_on(st);
3043 }
3044 return;
3045 }
3046
3047 // Verification
3048
3049 static void frame_verify(frame* f, const RegisterMap *map) { f->verify(map); }
3050
3051 void JavaThread::verify() {
3052 // Verify oops in the thread.
3053 oops_do(&VerifyOopClosure::verify_oop, NULL);
3054
3055 // Verify the stack frames.
3056 frames_do(frame_verify);
3057 }
3058
3059 // CR 6300358 (sub-CR 2137150)
3060 // Most callers of this method assume that it can't return NULL but a
3061 // thread may not have a name whilst it is in the process of attaching to
3062 // the VM - see CR 6412693, and there are places where a JavaThread can be
3063 // seen prior to having it's threadObj set (eg JNI attaching threads and
3428 // a scan.
3429 cf->do_code_blob(_scanned_compiled_method);
3430 }
3431 }
3432
3433
3434 // ======= Threads ========
3435
3436 // The Threads class links together all active threads, and provides
3437 // operations over all threads. It is protected by its own Mutex
3438 // lock, which is also used in other contexts to protect thread
3439 // operations from having the thread being operated on from exiting
3440 // and going away unexpectedly (e.g., safepoint synchronization)
3441
3442 JavaThread* Threads::_thread_list = NULL;
3443 int Threads::_number_of_threads = 0;
3444 int Threads::_number_of_non_daemon_threads = 0;
3445 int Threads::_return_code = 0;
3446 int Threads::_thread_claim_parity = 0;
3447 size_t JavaThread::_stack_size_at_create = 0;
3448 Monitor* Threads::_smr_delete_lock =
3449 new Monitor(Monitor::special, "smr_delete_lock",
3450 false /* allow_vm_block */,
3451 Monitor::_safepoint_check_never);
3452 // The '_cnt', '_max' and '_times" fields are enabled via
3453 // -XX:+EnableThreadSMRStatistics:
3454 uint Threads::_smr_delete_lock_wait_cnt = 0;
3455 uint Threads::_smr_delete_lock_wait_max = 0;
3456 volatile jint Threads::_smr_delete_notify = 0;
3457 volatile jint Threads::_smr_deleted_thread_cnt = 0;
3458 volatile jint Threads::_smr_deleted_thread_time_max = 0;
3459 volatile jint Threads::_smr_deleted_thread_times = 0;
3460 ThreadsList* volatile Threads::_smr_java_thread_list = new ThreadsList(0);
3461 long Threads::_smr_java_thread_list_alloc_cnt = 1;
3462 long Threads::_smr_java_thread_list_free_cnt = 0;
3463 uint Threads::_smr_java_thread_list_max = 0;
3464 uint Threads::_smr_nested_thread_list_max = 0;
3465 volatile jint Threads::_smr_tlh_cnt = 0;
3466 volatile jint Threads::_smr_tlh_time_max = 0;
3467 volatile jint Threads::_smr_tlh_times = 0;
3468 ThreadsList* Threads::_smr_to_delete_list = NULL;
3469 uint Threads::_smr_to_delete_list_cnt = 0;
3470 uint Threads::_smr_to_delete_list_max = 0;
3471
3472 #ifdef ASSERT
3473 bool Threads::_vm_complete = false;
3474 #endif
3475
3476 static inline void *prefetch_and_load_ptr(void **addr, intx prefetch_interval) {
3477 Prefetch::read((void*)addr, prefetch_interval);
3478 return *addr;
3479 }
3480
3481 // Possibly the ugliest for loop the world has seen. C++ does not allow
3482 // multiple types in the declaration section of the for loop. In this case
3483 // we are only dealing with pointers and hence can cast them. It looks ugly
3484 // but macros are ugly and therefore it's fine to make things absurdly ugly.
3485 #define DO_JAVA_THREADS(LIST, X) \
3486 for (JavaThread *MACRO_scan_interval = (JavaThread*)(uintptr_t)PrefetchScanIntervalInBytes, \
3487 *MACRO_list = (JavaThread*)(LIST), \
3488 **MACRO_end = ((JavaThread**)((ThreadsList*)MACRO_list)->threads()) + ((ThreadsList*)MACRO_list)->length(), \
3489 **MACRO_current_p = (JavaThread**)((ThreadsList*)MACRO_list)->threads(), \
3490 *X = (JavaThread*)prefetch_and_load_ptr((void**)MACRO_current_p, (intx)MACRO_scan_interval); \
3491 MACRO_current_p != MACRO_end; \
3492 MACRO_current_p++, \
3493 X = (JavaThread*)prefetch_and_load_ptr((void**)MACRO_current_p, (intx)MACRO_scan_interval))
3494
3495 // All JavaThreads
3496 #define ALL_JAVA_THREADS(X) DO_JAVA_THREADS(get_smr_java_thread_list(), X)
3497
3498 // All JavaThreads + all non-JavaThreads (i.e., every thread in the system)
3499 void Threads::threads_do(ThreadClosure* tc) {
3500 assert_locked_or_safepoint(Threads_lock);
3501 // ALL_JAVA_THREADS iterates through all JavaThreads
3502 ALL_JAVA_THREADS(p) {
3503 tc->do_thread(p);
3504 }
3505 // Someday we could have a table or list of all non-JavaThreads.
3506 // For now, just manually iterate through them.
3507 tc->do_thread(VMThread::vm_thread());
3508 Universe::heap()->gc_threads_do(tc);
3509 WatcherThread *wt = WatcherThread::watcher_thread();
3510 // Strictly speaking, the following NULL check isn't sufficient to make sure
3511 // the data for WatcherThread is still valid upon being examined. However,
3512 // considering that WatchThread terminates when the VM is on the way to
3513 // exit at safepoint, the chance of the above is extremely small. The right
3514 // way to prevent termination of WatcherThread would be to acquire
3515 // Terminator_lock, but we can't do that without violating the lock rank
3516 // checking in some cases.
3577 if (result.get_jint() != JNI_OK) {
3578 vm_exit_during_initialization(); // no message or exception
3579 }
3580
3581 universe_post_module_init();
3582 }
3583
3584 // Phase 3. final setup - set security manager, system class loader and TCCL
3585 //
3586 // This will instantiate and set the security manager, set the system class
3587 // loader as well as the thread context class loader. The security manager
3588 // and system class loader may be a custom class loaded from -Xbootclasspath/a,
3589 // other modules or the application's classpath.
3590 static void call_initPhase3(TRAPS) {
3591 Klass* klass = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_System(), true, CHECK);
3592 JavaValue result(T_VOID);
3593 JavaCalls::call_static(&result, klass, vmSymbols::initPhase3_name(),
3594 vmSymbols::void_method_signature(), CHECK);
3595 }
3596
3597 // Safe Memory Reclamation (SMR) support:
3598 //
3599
3600 // Acquire a stable ThreadsList.
3601 //
3602 ThreadsList *Threads::acquire_stable_list(Thread *self, bool is_ThreadsListSetter) {
3603 assert(self != NULL, "sanity check");
3604 // acquire_stable_list_nested_path() will grab the Threads_lock
3605 // so let's make sure the ThreadsListHandle is in a safe place.
3606 // ThreadsListSetter cannot make this check on this code path.
3607 debug_only(if (!is_ThreadsListSetter && StrictSafepointChecks) self->check_for_valid_safepoint_state(/* potential_vm_operation */ false);)
3608
3609 if (self->get_threads_hazard_ptr() == NULL) {
3610 // The typical case is first.
3611 return acquire_stable_list_fast_path(self);
3612 }
3613
3614 // The nested case is rare.
3615 return acquire_stable_list_nested_path(self);
3616 }
3617
3618 // Fast path (and lock free) way to acquire a stable ThreadsList.
3619 //
3620 ThreadsList *Threads::acquire_stable_list_fast_path(Thread *self) {
3621 assert(self != NULL, "sanity check");
3622 assert(self->get_threads_hazard_ptr() == NULL, "sanity check");
3623 assert(self->get_nested_threads_hazard_ptr() == NULL,
3624 "cannot have a nested hazard ptr with a NULL regular hazard ptr");
3625
3626 ThreadsList* threads;
3627
3628 // Stable recording of a hazard ptr for SMR. This code does not use
3629 // locks so its use of the _smr_java_thread_list & _threads_hazard_ptr
3630 // fields is racy relative to code that uses those fields with locks.
3631 // OrderAccess and Atomic functions are used to deal with those races.
3632 //
3633 while (true) {
3634 threads = get_smr_java_thread_list();
3635
3636 // Publish a tagged hazard ptr to denote that the hazard ptr is not
3637 // yet verified as being stable. Due to the fence after the hazard
3638 // ptr write, it will be sequentially consistent w.r.t. the
3639 // sequentially consistent writes of the ThreadsList, even on
3640 // non-multiple copy atomic machines where stores can be observed
3641 // in different order from different observer threads.
3642 ThreadsList* unverified_threads = Thread::tag_hazard_ptr(threads);
3643 self->set_threads_hazard_ptr(unverified_threads);
3644
3645 // If _smr_java_thread_list has changed, we have lost a race with
3646 // Threads::add() or Threads::remove() and have to try again.
3647 if (get_smr_java_thread_list() != threads) {
3648 continue;
3649 }
3650
3651 // We try to remove the tag which will verify the hazard ptr as
3652 // being stable. This exchange can race with a scanning thread
3653 // which might invalidate the tagged hazard ptr to keep it from
3654 // being followed to access JavaThread ptrs. If we lose the race,
3655 // we simply retry. If we win the race, then the stable hazard
3656 // ptr is officially published.
3657 if (self->cmpxchg_threads_hazard_ptr(threads, unverified_threads) == unverified_threads) {
3658 break;
3659 }
3660 }
3661
3662 // A stable hazard ptr has been published letting other threads know
3663 // that the ThreadsList and the JavaThreads reachable from this list
3664 // are protected and hence they should not be deleted until everyone
3665 // agrees it is safe to do so.
3666
3667 return threads;
3668 }
3669
3670 // Acquire a nested stable ThreadsList; this is rare so it uses
3671 // Threads_lock.
3672 //
3673 ThreadsList *Threads::acquire_stable_list_nested_path(Thread *self) {
3674 assert(self != NULL, "sanity check");
3675 assert(self->get_threads_hazard_ptr() != NULL,
3676 "cannot have a NULL regular hazard ptr when acquiring a nested hazard ptr");
3677
3678 // The thread already has a hazard ptr (ThreadsList ref) so we need
3679 // to create a nested ThreadsListHandle with the current ThreadsList
3680 // since it might be different than our current hazard ptr. The need
3681 // for a nested ThreadsListHandle is rare so we do this while holding
3682 // the Threads_lock so we don't race with the scanning code; the code
3683 // is so much simpler this way.
3684
3685 NestedThreadsList* node;
3686 {
3687 // Only grab the Threads_lock if we don't already own it.
3688 MutexLockerEx ml(Threads_lock->owned_by_self() ? NULL : Threads_lock);
3689 node = new NestedThreadsList(get_smr_java_thread_list());
3690 // We insert at the front of the list to match up with the delete
3691 // in release_stable_list().
3692 node->set_next(self->get_nested_threads_hazard_ptr());
3693 self->set_nested_threads_hazard_ptr(node);
3694 if (EnableThreadSMRStatistics) {
3695 self->inc_nested_threads_hazard_ptr_cnt();
3696 if (self->nested_threads_hazard_ptr_cnt() > _smr_nested_thread_list_max) {
3697 _smr_nested_thread_list_max = self->nested_threads_hazard_ptr_cnt();
3698 }
3699 }
3700 }
3701 log_debug(thread, smr)("tid=" UINTX_FORMAT ": Threads::acquire_stable_list: add NestedThreadsList node containing ThreadsList=" INTPTR_FORMAT, os::current_thread_id(), p2i(node->t_list()));
3702
3703 return node->t_list();
3704 }
3705
3706 // Release a stable ThreadsList.
3707 //
3708 void Threads::release_stable_list(Thread *self) {
3709 assert(self != NULL, "sanity check");
3710 // release_stable_list_nested_path() will grab the Threads_lock
3711 // so let's make sure the ThreadsListHandle is in a safe place.
3712 debug_only(if (StrictSafepointChecks) self->check_for_valid_safepoint_state(/* potential_vm_operation */ false);)
3713
3714 if (self->get_nested_threads_hazard_ptr() == NULL) {
3715 // The typical case is first.
3716 release_stable_list_fast_path(self);
3717 return;
3718 }
3719
3720 // The nested case is rare.
3721 release_stable_list_nested_path(self);
3722 }
3723
3724 // Fast path way to release a stable ThreadsList. The release portion
3725 // is lock-free, but the wake up portion is not.
3726 //
3727 void Threads::release_stable_list_fast_path(Thread *self) {
3728 assert(self != NULL, "sanity check");
3729 assert(self->get_threads_hazard_ptr() != NULL, "sanity check");
3730 assert(self->get_nested_threads_hazard_ptr() == NULL,
3731 "cannot have a nested hazard ptr when releasing a regular hazard ptr");
3732
3733 // After releasing the hazard ptr, other threads may go ahead and
3734 // free up some memory temporarily used by a ThreadsList snapshot.
3735 self->set_threads_hazard_ptr(NULL);
3736
3737 // We use double-check locking to reduce traffic on the system
3738 // wide smr_delete_lock.
3739 if (Threads::smr_delete_notify()) {
3740 // An exiting thread might be waiting in smr_delete(); we need to
3741 // check with smr_delete_lock to be sure.
3742 release_stable_list_wake_up((char *) "regular hazard ptr");
3743 }
3744 }
3745
3746 // Release a nested stable ThreadsList; this is rare so it uses
3747 // Threads_lock.
3748 //
3749 void Threads::release_stable_list_nested_path(Thread *self) {
3750 assert(self != NULL, "sanity check");
3751 assert(self->get_nested_threads_hazard_ptr() != NULL, "sanity check");
3752 assert(self->get_threads_hazard_ptr() != NULL,
3753 "must have a regular hazard ptr to have nested hazard ptrs");
3754
3755 // We have a nested ThreadsListHandle so we have to release it first.
3756 // The need for a nested ThreadsListHandle is rare so we do this while
3757 // holding the Threads_lock so we don't race with the scanning code;
3758 // the code is so much simpler this way.
3759
3760 NestedThreadsList *node;
3761 {
3762 // Only grab the Threads_lock if we don't already own it.
3763 MutexLockerEx ml(Threads_lock->owned_by_self() ? NULL : Threads_lock);
3764 // We remove from the front of the list to match up with the insert
3765 // in acquire_stable_list().
3766 node = self->get_nested_threads_hazard_ptr();
3767 self->set_nested_threads_hazard_ptr(node->next());
3768 if (EnableThreadSMRStatistics) {
3769 self->dec_nested_threads_hazard_ptr_cnt();
3770 }
3771 }
3772
3773 // An exiting thread might be waiting in smr_delete(); we need to
3774 // check with smr_delete_lock to be sure.
3775 release_stable_list_wake_up((char *) "nested hazard ptr");
3776
3777 log_debug(thread, smr)("tid=" UINTX_FORMAT ": Threads::release_stable_list: delete NestedThreadsList node containing ThreadsList=" INTPTR_FORMAT, os::current_thread_id(), p2i(node->t_list()));
3778
3779 delete node;
3780 }
3781
3782 // Wake up portion of the release stable ThreadsList protocol;
3783 // uses the smr_delete_lock().
3784 //
3785 void Threads::release_stable_list_wake_up(char *log_str) {
3786 assert(log_str != NULL, "sanity check");
3787
3788 // Note: smr_delete_lock is held in smr_delete() for the entire
3789 // hazard ptr search so that we do not lose this notify() if
3790 // the exiting thread has to wait. That code path also holds
3791 // Threads_lock (which was grabbed before smr_delete_lock) so that
3792 // threads_do() can be called. This means the system can't start a
3793 // safepoint which means this thread can't take too long to get to
3794 // a safepoint because of being blocked on smr_delete_lock.
3795 //
3796 MonitorLockerEx ml(Threads::smr_delete_lock(), Monitor::_no_safepoint_check_flag);
3797 if (Threads::smr_delete_notify()) {
3798 // Notify any exiting JavaThreads that are waiting in smr_delete()
3799 // that we've released a ThreadsList.
3800 ml.notify_all();
3801 log_debug(thread, smr)("tid=" UINTX_FORMAT ": Threads::release_stable_list notified %s", os::current_thread_id(), log_str);
3802 }
3803 }
3804
3805 void Threads::initialize_java_lang_classes(JavaThread* main_thread, TRAPS) {
3806 TraceTime timer("Initialize java.lang classes", TRACETIME_LOG(Info, startuptime));
3807
3808 if (EagerXrunInit && Arguments::init_libraries_at_startup()) {
3809 create_vm_init_libraries();
3810 }
3811
3812 initialize_class(vmSymbols::java_lang_String(), CHECK);
3813
3814 // Inject CompactStrings value after the static initializers for String ran.
3815 java_lang_String::set_compact_strings(CompactStrings);
3816
3817 // Initialize java_lang.System (needed before creating the thread)
3818 initialize_class(vmSymbols::java_lang_System(), CHECK);
3819 // The VM creates & returns objects of this class. Make sure it's initialized.
3820 initialize_class(vmSymbols::java_lang_Class(), CHECK);
3821 initialize_class(vmSymbols::java_lang_ThreadGroup(), CHECK);
3822 Handle thread_group = create_initial_thread_group(CHECK);
3823 Universe::set_main_thread_group(thread_group());
3824 initialize_class(vmSymbols::java_lang_Thread(), CHECK);
3965 #if INCLUDE_JVMCI
3966 if (JVMCICounterSize > 0) {
3967 JavaThread::_jvmci_old_thread_counters = NEW_C_HEAP_ARRAY(jlong, JVMCICounterSize, mtInternal);
3968 memset(JavaThread::_jvmci_old_thread_counters, 0, sizeof(jlong) * JVMCICounterSize);
3969 } else {
3970 JavaThread::_jvmci_old_thread_counters = NULL;
3971 }
3972 #endif // INCLUDE_JVMCI
3973
3974 // Attach the main thread to this os thread
3975 JavaThread* main_thread = new JavaThread();
3976 main_thread->set_thread_state(_thread_in_vm);
3977 main_thread->initialize_thread_current();
3978 // must do this before set_active_handles
3979 main_thread->record_stack_base_and_size();
3980 main_thread->set_active_handles(JNIHandleBlock::allocate_block());
3981
3982 if (!main_thread->set_as_starting_thread()) {
3983 vm_shutdown_during_initialization(
3984 "Failed necessary internal allocation. Out of swap space");
3985 main_thread->smr_delete();
3986 *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again
3987 return JNI_ENOMEM;
3988 }
3989
3990 // Enable guard page *after* os::create_main_thread(), otherwise it would
3991 // crash Linux VM, see notes in os_linux.cpp.
3992 main_thread->create_stack_guard_pages();
3993
3994 // Initialize Java-Level synchronization subsystem
3995 ObjectMonitor::Initialize();
3996
3997 // Initialize global modules
3998 jint status = init_globals();
3999 if (status != JNI_OK) {
4000 main_thread->smr_delete();
4001 *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again
4002 return status;
4003 }
4004
4005 if (TRACE_INITIALIZE() != JNI_OK) {
4006 vm_exit_during_initialization("Failed to initialize tracing backend");
4007 }
4008
4009 // Should be done after the heap is fully created
4010 main_thread->cache_global_variables();
4011
4012 HandleMark hm;
4013
4014 { MutexLocker mu(Threads_lock);
4015 Threads::add(main_thread);
4016 }
4017
4018 // Any JVMTI raw monitors entered in onload will transition into
4019 // real raw monitor. VM is setup enough here for raw monitor enter.
4020 JvmtiExport::transition_pending_onload_raw_monitors();
4386 AgentLibrary* agent;
4387
4388 for (agent = Arguments::libraries(); agent != NULL; agent = agent->next()) {
4389 OnLoadEntry_t on_load_entry = lookup_jvm_on_load(agent);
4390
4391 if (on_load_entry != NULL) {
4392 // Invoke the JVM_OnLoad function
4393 JavaThread* thread = JavaThread::current();
4394 ThreadToNativeFromVM ttn(thread);
4395 HandleMark hm(thread);
4396 jint err = (*on_load_entry)(&main_vm, agent->options(), NULL);
4397 if (err != JNI_OK) {
4398 vm_exit_during_initialization("-Xrun library failed to init", agent->name());
4399 }
4400 } else {
4401 vm_exit_during_initialization("Could not find JVM_OnLoad function in -Xrun library", agent->name());
4402 }
4403 }
4404 }
4405
4406
4407 // Last thread running calls java.lang.Shutdown.shutdown()
4408 void JavaThread::invoke_shutdown_hooks() {
4409 HandleMark hm(this);
4410
4411 // We could get here with a pending exception, if so clear it now.
4412 if (this->has_pending_exception()) {
4413 this->clear_pending_exception();
4414 }
4415
4416 EXCEPTION_MARK;
4417 Klass* shutdown_klass =
4418 SystemDictionary::resolve_or_null(vmSymbols::java_lang_Shutdown(),
4419 THREAD);
4420 if (shutdown_klass != NULL) {
4421 // SystemDictionary::resolve_or_null will return null if there was
4422 // an exception. If we cannot load the Shutdown class, just don't
4423 // call Shutdown.shutdown() at all. This will mean the shutdown hooks
4424 // and finalizers (if runFinalizersOnExit is set) won't be run.
4425 // Note that if a shutdown hook was registered or runFinalizersOnExit
4426 // was called, the Shutdown class would have already been loaded
4511 VMThread::wait_for_vm_thread_exit();
4512 assert(SafepointSynchronize::is_at_safepoint(), "VM thread should exit at Safepoint");
4513 VMThread::destroy();
4514 }
4515
4516 // clean up ideal graph printers
4517 #if defined(COMPILER2) && !defined(PRODUCT)
4518 IdealGraphPrinter::clean_up();
4519 #endif
4520
4521 // Now, all Java threads are gone except daemon threads. Daemon threads
4522 // running Java code or in VM are stopped by the Safepoint. However,
4523 // daemon threads executing native code are still running. But they
4524 // will be stopped at native=>Java/VM barriers. Note that we can't
4525 // simply kill or suspend them, as it is inherently deadlock-prone.
4526
4527 VM_Exit::set_vm_exited();
4528
4529 notify_vm_shutdown();
4530
4531 // We are after VM_Exit::set_vm_exited() so we can't call
4532 // thread->smr_delete() or we will block on the Threads_lock.
4533 // Deleting the shutdown thread here is safe because another
4534 // JavaThread cannot have an active ThreadsListHandle for
4535 // this JavaThread.
4536 delete thread;
4537
4538 #if INCLUDE_JVMCI
4539 if (JVMCICounterSize > 0) {
4540 FREE_C_HEAP_ARRAY(jlong, JavaThread::_jvmci_old_thread_counters);
4541 }
4542 #endif
4543
4544 // exit_globals() will delete tty
4545 exit_globals();
4546
4547 LogConfiguration::finalize();
4548
4549 return true;
4550 }
4551
4552
4553 jboolean Threads::is_supported_jni_version_including_1_1(jint version) {
4554 if (version == JNI_VERSION_1_1) return JNI_TRUE;
4555 return is_supported_jni_version(version);
4556 }
4557
4558
4559 jboolean Threads::is_supported_jni_version(jint version) {
4560 if (version == JNI_VERSION_1_2) return JNI_TRUE;
4561 if (version == JNI_VERSION_1_4) return JNI_TRUE;
4562 if (version == JNI_VERSION_1_6) return JNI_TRUE;
4563 if (version == JNI_VERSION_1_8) return JNI_TRUE;
4564 if (version == JNI_VERSION_9) return JNI_TRUE;
4565 if (version == JNI_VERSION_10) return JNI_TRUE;
4566 return JNI_FALSE;
4567 }
4568
4569 // Hash table of pointers found by a scan. Used for collecting hazard
4570 // pointers (ThreadsList references). Also used for collecting JavaThreads
4571 // that are indirectly referenced by hazard ptrs. An instance of this
4572 // class only contains one type of pointer.
4573 //
4574 class ThreadScanHashtable : public CHeapObj<mtThread> {
4575 private:
4576 static bool ptr_equals(void * const& s1, void * const& s2) {
4577 return s1 == s2;
4578 }
4579
4580 static unsigned int ptr_hash(void * const& s1) {
4581 return (unsigned int)(((uint32_t)(uintptr_t)s1) * 2654435761u);
4582 }
4583
4584 int _table_size;
4585 // ResourceHashtable SIZE is specified at compile time so our
4586 // dynamic _table_size is unused for now; 1031 is the first prime
4587 // after 1024.
4588 typedef ResourceHashtable<void *, int, &ThreadScanHashtable::ptr_hash,
4589 &ThreadScanHashtable::ptr_equals, 1031,
4590 ResourceObj::C_HEAP, mtThread> PtrTable;
4591 PtrTable * _ptrs;
4592
4593 public:
4594 // ResourceHashtable is passed to various functions and populated in
4595 // different places so we allocate it using C_HEAP to make it immune
4596 // from any ResourceMarks that happen to be in the code paths.
4597 ThreadScanHashtable(int table_size) : _table_size(table_size), _ptrs(new (ResourceObj::C_HEAP, mtThread) PtrTable()) {}
4598
4599 ~ThreadScanHashtable() { delete _ptrs; }
4600
4601 bool has_entry(void *pointer) {
4602 int *val_ptr = _ptrs->get(pointer);
4603 return val_ptr != NULL && *val_ptr == 1;
4604 }
4605
4606 void add_entry(void *pointer) {
4607 _ptrs->put(pointer, 1);
4608 }
4609 };
4610
4611 // Closure to gather JavaThreads indirectly referenced by hazard ptrs
4612 // (ThreadsList references) into a hash table. This closure handles part 2
4613 // of the dance - adding all the JavaThreads referenced by the hazard
4614 // pointer (ThreadsList reference) to the hash table.
4615 //
4616 class AddThreadHazardPointerThreadClosure : public ThreadClosure {
4617 private:
4618 ThreadScanHashtable *_table;
4619
4620 public:
4621 AddThreadHazardPointerThreadClosure(ThreadScanHashtable *table) : _table(table) {}
4622
4623 virtual void do_thread(Thread *thread) {
4624 if (!_table->has_entry((void*)thread)) {
4625 // The same JavaThread might be on more than one ThreadsList or
4626 // more than one thread might be using the same ThreadsList. In
4627 // either case, we only need a single entry for a JavaThread.
4628 _table->add_entry((void*)thread);
4629 }
4630 }
4631 };
4632
4633 // Closure to gather JavaThreads indirectly referenced by hazard ptrs
4634 // (ThreadsList references) into a hash table. This closure handles part 1
4635 // of the dance - hazard ptr chain walking and dispatch to another
4636 // closure.
4637 //
4638 class ScanHazardPtrGatherProtectedThreadsClosure : public ThreadClosure {
4639 private:
4640 ThreadScanHashtable *_table;
4641 public:
4642 ScanHazardPtrGatherProtectedThreadsClosure(ThreadScanHashtable *table) : _table(table) {}
4643
4644 virtual void do_thread(Thread *thread) {
4645 assert_locked_or_safepoint(Threads_lock);
4646
4647 if (thread == NULL) return;
4648
4649 // This code races with Threads::acquire_stable_list() which is
4650 // lock-free so we have to handle some special situations.
4651 //
4652 ThreadsList *current_list = NULL;
4653 while (true) {
4654 current_list = thread->get_threads_hazard_ptr();
4655 // No hazard ptr so nothing more to do.
4656 if (current_list == NULL) {
4657 assert(thread->get_nested_threads_hazard_ptr() == NULL,
4658 "cannot have a nested hazard ptr with a NULL regular hazard ptr");
4659 return;
4660 }
4661
4662 // If the hazard ptr is verified as stable (since it is not tagged),
4663 // then it is safe to use.
4664 if (!Thread::is_hazard_ptr_tagged(current_list)) break;
4665
4666 // The hazard ptr is tagged as not yet verified as being stable
4667 // so we are racing with acquire_stable_list(). This exchange
4668 // attempts to invalidate the hazard ptr. If we win the race,
4669 // then we can ignore this unstable hazard ptr and the other
4670 // thread will retry the attempt to publish a stable hazard ptr.
4671 // If we lose the race, then we retry our attempt to look at the
4672 // hazard ptr.
4673 if (thread->cmpxchg_threads_hazard_ptr(NULL, current_list) == current_list) return;
4674 }
4675
4676 // The current JavaThread has a hazard ptr (ThreadsList reference)
4677 // which might be _smr_java_thread_list or it might be an older
4678 // ThreadsList that has been removed but not freed. In either case,
4679 // the hazard ptr is protecting all the JavaThreads on that
4680 // ThreadsList.
4681 AddThreadHazardPointerThreadClosure add_cl(_table);
4682 current_list->threads_do(&add_cl);
4683
4684 // Any NestedThreadsLists are also protecting JavaThreads so
4685 // gather those also; the ThreadsLists may be different.
4686 for (NestedThreadsList* node = thread->get_nested_threads_hazard_ptr();
4687 node != NULL; node = node->next()) {
4688 node->t_list()->threads_do(&add_cl);
4689 }
4690 }
4691 };
4692
4693 // Closure to print JavaThreads that have a hazard ptr (ThreadsList
4694 // reference) that contains an indirect reference to a specific JavaThread.
4695 //
4696 class ScanHazardPtrPrintMatchingThreadsClosure : public ThreadClosure {
4697 private:
4698 JavaThread *_thread;
4699 public:
4700 ScanHazardPtrPrintMatchingThreadsClosure(JavaThread *thread) : _thread(thread) {}
4701
4702 virtual void do_thread(Thread *thread) {
4703 assert_locked_or_safepoint(Threads_lock);
4704
4705 if (thread == NULL) return;
4706 ThreadsList *current_list = thread->get_threads_hazard_ptr();
4707 if (current_list == NULL) {
4708 assert(thread->get_nested_threads_hazard_ptr() == NULL,
4709 "cannot have a nested hazard ptr with a NULL regular hazard ptr");
4710 return;
4711 }
4712 // If the hazard ptr is unverified, then ignore it.
4713 if (Thread::is_hazard_ptr_tagged(current_list)) return;
4714
4715 // The current JavaThread has a hazard ptr (ThreadsList reference)
4716 // which might be _smr_java_thread_list or it might be an older
4717 // ThreadsList that has been removed but not freed. In either case,
4718 // the hazard ptr is protecting all the JavaThreads on that
4719 // ThreadsList, but we only care about matching a specific JavaThread.
4720 DO_JAVA_THREADS(current_list, p) {
4721 if (p == _thread) {
4722 log_debug(thread, smr)("tid=" UINTX_FORMAT ": Threads::smr_delete: thread1=" INTPTR_FORMAT " has a hazard pointer for thread2=" INTPTR_FORMAT, os::current_thread_id(), p2i(thread), p2i(_thread));
4723 break;
4724 }
4725 }
4726
4727 // Any NestedThreadsLists are also protecting JavaThreads so
4728 // check those also; the ThreadsLists may be different.
4729 for (NestedThreadsList* node = thread->get_nested_threads_hazard_ptr();
4730 node != NULL; node = node->next()) {
4731 DO_JAVA_THREADS(node->t_list(), p) {
4732 if (p == _thread) {
4733 log_debug(thread, smr)("tid=" UINTX_FORMAT ": Threads::smr_delete: thread1=" INTPTR_FORMAT " has a nested hazard pointer for thread2=" INTPTR_FORMAT, os::current_thread_id(), p2i(thread), p2i(_thread));
4734 return;
4735 }
4736 }
4737 }
4738 }
4739 };
4740
4741 // Return true if the specified JavaThread is protected by a hazard
4742 // pointer (ThreadsList reference). Otherwise, returns false.
4743 //
4744 bool Threads::is_a_protected_JavaThread(JavaThread *thread) {
4745 assert_locked_or_safepoint(Threads_lock);
4746
4747 // Hash table size should be first power of two higher than twice
4748 // the length of the Threads list.
4749 int hash_table_size = MIN2(_number_of_threads, 32) << 1;
4750 hash_table_size--;
4751 hash_table_size |= hash_table_size >> 1;
4752 hash_table_size |= hash_table_size >> 2;
4753 hash_table_size |= hash_table_size >> 4;
4754 hash_table_size |= hash_table_size >> 8;
4755 hash_table_size |= hash_table_size >> 16;
4756 hash_table_size++;
4757
4758 // Gather a hash table of the JavaThreads indirectly referenced by
4759 // hazard ptrs.
4760 ThreadScanHashtable *scan_table = new ThreadScanHashtable(hash_table_size);
4761 ScanHazardPtrGatherProtectedThreadsClosure scan_cl(scan_table);
4762 Threads::threads_do(&scan_cl);
4763
4764 bool thread_is_protected = false;
4765 if (scan_table->has_entry((void*)thread)) {
4766 thread_is_protected = true;
4767 }
4768 delete scan_table;
4769 return thread_is_protected;
4770 }
4771
4772 // Safely delete a JavaThread when it is no longer in use by a
4773 // ThreadsListHandle.
4774 //
4775 void Threads::smr_delete(JavaThread *thread) {
4776 assert(!Threads_lock->owned_by_self(), "sanity");
4777
4778 bool has_logged_once = false;
4779 elapsedTimer timer;
4780 if (EnableThreadSMRStatistics) {
4781 timer.start();
4782 }
4783
4784 while (true) {
4785 {
4786 // No safepoint check because this JavaThread is not on the
4787 // Threads list.
4788 MutexLockerEx ml(Threads_lock, Mutex::_no_safepoint_check_flag);
4789 // Cannot use a MonitorLockerEx helper here because we have
4790 // to drop the Threads_lock first if we wait.
4791 Threads::smr_delete_lock()->lock_without_safepoint_check();
4792 // Set the smr_delete_notify flag after we grab smr_delete_lock
4793 // and before we scan hazard ptrs because we're doing
4794 // double-check locking in release_stable_list().
4795 Threads::set_smr_delete_notify();
4796
4797 if (!is_a_protected_JavaThread(thread)) {
4798 // This is the common case.
4799 Threads::clear_smr_delete_notify();
4800 Threads::smr_delete_lock()->unlock();
4801 break;
4802 }
4803 if (!has_logged_once) {
4804 has_logged_once = true;
4805 log_debug(thread, smr)("tid=" UINTX_FORMAT ": Threads::smr_delete: thread=" INTPTR_FORMAT " is not deleted.", os::current_thread_id(), p2i(thread));
4806 if (log_is_enabled(Debug, os, thread)) {
4807 ScanHazardPtrPrintMatchingThreadsClosure scan_cl(thread);
4808 Threads::threads_do(&scan_cl);
4809 }
4810 }
4811 } // We have to drop the Threads_lock to wait or delete the thread
4812
4813 if (EnableThreadSMRStatistics) {
4814 _smr_delete_lock_wait_cnt++;
4815 if (_smr_delete_lock_wait_cnt > _smr_delete_lock_wait_max) {
4816 _smr_delete_lock_wait_max = _smr_delete_lock_wait_cnt;
4817 }
4818 }
4819 // Wait for a release_stable_list() call before we check again. No
4820 // safepoint check, no timeout, and not as suspend equivalent flag
4821 // because this JavaThread is not on the Threads list.
4822 Threads::smr_delete_lock()->wait(Mutex::_no_safepoint_check_flag, 0,
4823 !Mutex::_as_suspend_equivalent_flag);
4824 if (EnableThreadSMRStatistics) {
4825 _smr_delete_lock_wait_cnt--;
4826 }
4827
4828 Threads::clear_smr_delete_notify();
4829 Threads::smr_delete_lock()->unlock();
4830 // Retry the whole scenario.
4831 }
4832
4833 delete thread;
4834 if (EnableThreadSMRStatistics) {
4835 timer.stop();
4836 jint millis = (jint)timer.milliseconds();
4837 Threads::inc_smr_deleted_thread_cnt();
4838 Threads::add_smr_deleted_thread_times(millis);
4839 Threads::update_smr_deleted_thread_time_max(millis);
4840 }
4841
4842 log_debug(thread, smr)("tid=" UINTX_FORMAT ": Threads::smr_delete: thread=" INTPTR_FORMAT " is deleted.", os::current_thread_id(), p2i(thread));
4843 }
4844
4845 bool Threads::smr_delete_notify() {
4846 // Use load_acquire() in order to see any updates to _smr_delete_notify
4847 // earlier than when smr_delete_lock is grabbed.
4848 return (OrderAccess::load_acquire(&_smr_delete_notify) != 0);
4849 }
4850
4851 // set_smr_delete_notify() and clear_smr_delete_notify() are called
4852 // under the protection of the smr_delete_lock, but we also use an
4853 // Atomic operation to ensure the memory update is seen earlier than
4854 // when the smr_delete_lock is dropped.
4855 //
4856 void Threads::set_smr_delete_notify() {
4857 Atomic::inc(&_smr_delete_notify);
4858 }
4859
4860 void Threads::clear_smr_delete_notify() {
4861 Atomic::dec(&_smr_delete_notify);
4862 }
4863
4864 // Closure to gather hazard ptrs (ThreadsList references) into a hash table.
4865 //
4866 class ScanHazardPtrGatherThreadsListClosure : public ThreadClosure {
4867 private:
4868 ThreadScanHashtable *_table;
4869 public:
4870 ScanHazardPtrGatherThreadsListClosure(ThreadScanHashtable *table) : _table(table) {}
4871
4872 virtual void do_thread(Thread* thread) {
4873 assert_locked_or_safepoint(Threads_lock);
4874
4875 if (thread == NULL) return;
4876 ThreadsList *threads = thread->get_threads_hazard_ptr();
4877 if (threads == NULL) {
4878 assert(thread->get_nested_threads_hazard_ptr() == NULL,
4879 "cannot have a nested hazard ptr with a NULL regular hazard ptr");
4880 return;
4881 }
4882 // In this closure we always ignore the tag that might mark this
4883 // hazard ptr as not yet verified. If we happen to catch an
4884 // unverified hazard ptr that is subsequently discarded (not
4885 // published), then the only side effect is that we might keep a
4886 // to-be-deleted ThreadsList alive a little longer.
4887 threads = Thread::untag_hazard_ptr(threads);
4888 if (!_table->has_entry((void*)threads)) {
4889 _table->add_entry((void*)threads);
4890 }
4891
4892 // Any NestedThreadsLists are also protecting JavaThreads so
4893 // gather those also; the ThreadsLists may be different.
4894 for (NestedThreadsList* node = thread->get_nested_threads_hazard_ptr();
4895 node != NULL; node = node->next()) {
4896 threads = node->t_list();
4897 if (!_table->has_entry((void*)threads)) {
4898 _table->add_entry((void*)threads);
4899 }
4900 }
4901 }
4902 };
4903
4904 // Safely free a ThreadsList after a Threads::add() or Threads::remove().
4905 // The specified ThreadsList may not get deleted during this call if it
4906 // is still in-use (referenced by a hazard ptr). Other ThreadsLists
4907 // in the chain may get deleted by this call if they are no longer in-use.
4908 void Threads::smr_free_list(ThreadsList* threads) {
4909 assert_locked_or_safepoint(Threads_lock);
4910
4911 threads->set_next_list(_smr_to_delete_list);
4912 _smr_to_delete_list = threads;
4913 if (EnableThreadSMRStatistics) {
4914 _smr_to_delete_list_cnt++;
4915 if (_smr_to_delete_list_cnt > _smr_to_delete_list_max) {
4916 _smr_to_delete_list_max = _smr_to_delete_list_cnt;
4917 }
4918 }
4919
4920 // Hash table size should be first power of two higher than twice the length of the ThreadsList
4921 int hash_table_size = MIN2(_number_of_threads, 32) << 1;
4922 hash_table_size--;
4923 hash_table_size |= hash_table_size >> 1;
4924 hash_table_size |= hash_table_size >> 2;
4925 hash_table_size |= hash_table_size >> 4;
4926 hash_table_size |= hash_table_size >> 8;
4927 hash_table_size |= hash_table_size >> 16;
4928 hash_table_size++;
4929
4930 // Gather a hash table of the current hazard ptrs:
4931 ThreadScanHashtable *scan_table = new ThreadScanHashtable(hash_table_size);
4932 ScanHazardPtrGatherThreadsListClosure scan_cl(scan_table);
4933 Threads::threads_do(&scan_cl);
4934
4935 // Walk through the linked list of pending freeable ThreadsLists
4936 // and free the ones that are not referenced from hazard ptrs.
4937 ThreadsList* current = _smr_to_delete_list;
4938 ThreadsList* prev = NULL;
4939 ThreadsList* next = NULL;
4940 bool threads_is_freed = false;
4941 while (current != NULL) {
4942 next = current->next_list();
4943 if (!scan_table->has_entry((void*)current)) {
4944 // This ThreadsList is not referenced by a hazard ptr.
4945 if (prev != NULL) {
4946 prev->set_next_list(next);
4947 }
4948 if (_smr_to_delete_list == current) {
4949 _smr_to_delete_list = next;
4950 }
4951
4952 log_debug(thread, smr)("tid=" UINTX_FORMAT ": Threads::smr_free_list: threads=" INTPTR_FORMAT " is freed.", os::current_thread_id(), p2i(current));
4953 if (current == threads) threads_is_freed = true;
4954 delete current;
4955 if (EnableThreadSMRStatistics) {
4956 _smr_java_thread_list_free_cnt++;
4957 _smr_to_delete_list_cnt--;
4958 }
4959 } else {
4960 prev = current;
4961 }
4962 current = next;
4963 }
4964
4965 if (!threads_is_freed) {
4966 // Only report "is not freed" on the original call to
4967 // smr_free_list() for this ThreadsList.
4968 log_debug(thread, smr)("tid=" UINTX_FORMAT ": Threads::smr_free_list: threads=" INTPTR_FORMAT " is not freed.", os::current_thread_id(), p2i(threads));
4969 }
4970
4971 delete scan_table;
4972 }
4973
4974 // Remove a JavaThread from a ThreadsList. The returned ThreadsList is a
4975 // new copy of the specified ThreadsList with the specified JavaThread
4976 // removed.
4977 ThreadsList *ThreadsList::remove_thread(ThreadsList* list, JavaThread* java_thread) {
4978 assert(list->_length > 0, "sanity");
4979
4980 uint i = 0;
4981 DO_JAVA_THREADS(list, current) {
4982 if (current == java_thread) {
4983 break;
4984 }
4985 i++;
4986 }
4987 assert(i < list->_length, "did not find JavaThread on the list");
4988 const uint index = i;
4989 const uint new_length = list->_length - 1;
4990 const uint head_length = index;
4991 const uint tail_length = (new_length >= index) ? (new_length - index) : 0;
4992 ThreadsList *const new_list = new ThreadsList(new_length);
4993
4994 if (head_length > 0) {
4995 Copy::disjoint_words((HeapWord*)list->_threads, (HeapWord*)new_list->_threads, head_length);
4996 }
4997 if (tail_length > 0) {
4998 Copy::disjoint_words((HeapWord*)list->_threads + index + 1, (HeapWord*)new_list->_threads + index, tail_length);
4999 }
5000
5001 return new_list;
5002 }
5003
5004 // Add a JavaThread to a ThreadsList. The returned ThreadsList is a
5005 // new copy of the specified ThreadsList with the specified JavaThread
5006 // appended to the end.
5007 ThreadsList *ThreadsList::add_thread(ThreadsList *list, JavaThread *java_thread) {
5008 const uint index = list->_length;
5009 const uint new_length = index + 1;
5010 const uint head_length = index;
5011 ThreadsList *const new_list = new ThreadsList(new_length);
5012
5013 if (head_length > 0) {
5014 Copy::disjoint_words((HeapWord*)list->_threads, (HeapWord*)new_list->_threads, head_length);
5015 }
5016 *(JavaThread**)(new_list->_threads + index) = java_thread;
5017
5018 return new_list;
5019 }
5020
5021 int ThreadsList::find_index_of_JavaThread(JavaThread *target) {
5022 if (target == NULL) {
5023 return -1;
5024 }
5025 for (uint i = 0; i < length(); i++) {
5026 if (target == thread_at(i)) {
5027 return (int)i;
5028 }
5029 }
5030 return -1;
5031 }
5032
5033 JavaThread* ThreadsList::find_JavaThread_from_java_tid(jlong java_tid) const {
5034 DO_JAVA_THREADS(this, thread) {
5035 oop tobj = thread->threadObj();
5036 // Ignore the thread if it hasn't run yet, has exited
5037 // or is starting to exit.
5038 if (tobj != NULL && !thread->is_exiting() &&
5039 java_tid == java_lang_Thread::thread_id(tobj)) {
5040 // found a match
5041 return thread;
5042 }
5043 }
5044 return NULL;
5045 }
5046
5047 bool ThreadsList::includes(const JavaThread * const p) const {
5048 if (p == NULL) {
5049 return false;
5050 }
5051 DO_JAVA_THREADS(this, q) {
5052 if (q == p) {
5053 return true;
5054 }
5055 }
5056 return false;
5057 }
5058
5059 void Threads::add(JavaThread* p, bool force_daemon) {
5060 // The threads lock must be owned at this point
5061 assert_locked_or_safepoint(Threads_lock);
5062
5063 // See the comment for this method in thread.hpp for its purpose and
5064 // why it is called here.
5065 p->initialize_queues();
5066 p->set_next(_thread_list);
5067 _thread_list = p;
5068
5069 // Once a JavaThread is added to the Threads list, smr_delete() has
5070 // to be used to delete it. Otherwise we can just delete it directly.
5071 p->set_on_thread_list();
5072
5073 _number_of_threads++;
5074 oop threadObj = p->threadObj();
5075 bool daemon = true;
5076 // Bootstrapping problem: threadObj can be null for initial
5077 // JavaThread (or for threads attached via JNI)
5078 if ((!force_daemon) && (threadObj == NULL || !java_lang_Thread::is_daemon(threadObj))) {
5079 _number_of_non_daemon_threads++;
5080 daemon = false;
5081 }
5082
5083 ThreadService::add_thread(p, daemon);
5084
5085 // Maintain fast thread list
5086 ThreadsList *new_list = ThreadsList::add_thread(get_smr_java_thread_list(), p);
5087 if (EnableThreadSMRStatistics) {
5088 _smr_java_thread_list_alloc_cnt++;
5089 if (new_list->length() > _smr_java_thread_list_max) {
5090 _smr_java_thread_list_max = new_list->length();
5091 }
5092 }
5093 // Initial _smr_java_thread_list will not generate a "Threads::add" mesg.
5094 log_debug(thread, smr)("tid=" UINTX_FORMAT ": Threads::add: new ThreadsList=" INTPTR_FORMAT, os::current_thread_id(), p2i(new_list));
5095
5096 ThreadsList *old_list = xchg_smr_java_thread_list(new_list);
5097 smr_free_list(old_list);
5098
5099 // Possible GC point.
5100 Events::log(p, "Thread added: " INTPTR_FORMAT, p2i(p));
5101 }
5102
5103 void Threads::remove(JavaThread* p) {
5104
5105 // Reclaim the objectmonitors from the omInUseList and omFreeList of the moribund thread.
5106 ObjectSynchronizer::omFlush(p);
5107
5108 // Extra scope needed for Thread_lock, so we can check
5109 // that we do not remove thread without safepoint code notice
5110 { MutexLocker ml(Threads_lock);
5111
5112 assert(get_smr_java_thread_list()->includes(p), "p must be present");
5113
5114 // Maintain fast thread list
5115 ThreadsList *new_list = ThreadsList::remove_thread(get_smr_java_thread_list(), p);
5116 if (EnableThreadSMRStatistics) {
5117 _smr_java_thread_list_alloc_cnt++;
5118 // This list is smaller so no need to check for a "longest" update.
5119 }
5120
5121 // Final _smr_java_thread_list will not generate a "Threads::remove" mesg.
5122 log_debug(thread, smr)("tid=" UINTX_FORMAT ": Threads::remove: new ThreadsList=" INTPTR_FORMAT, os::current_thread_id(), p2i(new_list));
5123
5124 ThreadsList *old_list = xchg_smr_java_thread_list(new_list);
5125 smr_free_list(old_list);
5126
5127 JavaThread* current = _thread_list;
5128 JavaThread* prev = NULL;
5129
5130 while (current != p) {
5131 prev = current;
5132 current = current->next();
5133 }
5134
5135 if (prev) {
5136 prev->set_next(current->next());
5137 } else {
5138 _thread_list = p->next();
5139 }
5140
5141 _number_of_threads--;
5142 oop threadObj = p->threadObj();
5143 bool daemon = true;
5144 if (threadObj == NULL || !java_lang_Thread::is_daemon(threadObj)) {
5145 _number_of_non_daemon_threads--;
5146 daemon = false;
5147
5148 // Only one thread left, do a notify on the Threads_lock so a thread waiting
5149 // on destroy_vm will wake up.
5150 if (number_of_non_daemon_threads() == 1) {
5151 Threads_lock->notify_all();
5152 }
5153 }
5154 ThreadService::remove_thread(p, daemon);
5155
5156 // Make sure that safepoint code disregard this thread. This is needed since
5157 // the thread might mess around with locks after this point. This can cause it
5158 // to do callbacks into the safepoint code. However, the safepoint code is not aware
5159 // of this thread since it is removed from the queue.
5160 p->set_terminated_value();
5161 } // unlock Threads_lock
5162
5163 // Since Events::log uses a lock, we grab it outside the Threads_lock
5164 Events::log(p, "Thread exited: " INTPTR_FORMAT, p2i(p));
5165 }
5166
5167 // Operations on the Threads list for GC. These are not explicitly locked,
5168 // but the garbage collector must provide a safe context for them to run.
5169 // In particular, these things should never be called when the Threads_lock
5170 // is held by some other thread. (Note: the Safepoint abstraction also
5171 // uses the Threads_lock to guarantee this property. It also makes sure that
5172 // all threads gets blocked when exiting or starting).
5173
5174 void Threads::oops_do(OopClosure* f, CodeBlobClosure* cf) {
5175 ALL_JAVA_THREADS(p) {
5176 p->oops_do(f, cf);
5177 }
5178 VMThread::vm_thread()->oops_do(f, cf);
5179 }
5180
5181 void Threads::change_thread_claim_parity() {
5182 // Set the new claim parity.
5183 assert(_thread_claim_parity >= 0 && _thread_claim_parity <= 2,
5184 "Not in range.");
5185 _thread_claim_parity++;
5186 if (_thread_claim_parity == 3) _thread_claim_parity = 1;
5259 ThreadHandlesClosure(void f(Metadata*)) : _f(f) {}
5260 virtual void do_thread(Thread* thread) {
5261 thread->metadata_handles_do(_f);
5262 }
5263 };
5264
5265 void Threads::metadata_handles_do(void f(Metadata*)) {
5266 // Only walk the Handles in Thread.
5267 ThreadHandlesClosure handles_closure(f);
5268 threads_do(&handles_closure);
5269 }
5270
5271 void Threads::deoptimized_wrt_marked_nmethods() {
5272 ALL_JAVA_THREADS(p) {
5273 p->deoptimized_wrt_marked_nmethods();
5274 }
5275 }
5276
5277
5278 // Get count Java threads that are waiting to enter the specified monitor.
5279 GrowableArray<JavaThread*>* Threads::get_pending_threads(ThreadsList * t_list,
5280 int count,
5281 address monitor) {
5282 GrowableArray<JavaThread*>* result = new GrowableArray<JavaThread*>(count);
5283
5284 int i = 0;
5285 DO_JAVA_THREADS(t_list, p) {
5286 if (!p->can_call_java()) continue;
5287
5288 address pending = (address)p->current_pending_monitor();
5289 if (pending == monitor) { // found a match
5290 if (i < count) result->append(p); // save the first count matches
5291 i++;
5292 }
5293 }
5294
5295 return result;
5296 }
5297
5298
5299 JavaThread *Threads::owning_thread_from_monitor_owner(ThreadsList * t_list,
5300 address owner) {
5301 // NULL owner means not locked so we can skip the search
5302 if (owner == NULL) return NULL;
5303
5304 DO_JAVA_THREADS(t_list, p) {
5305 // first, see if owner is the address of a Java thread
5306 if (owner == (address)p) return p;
5307 }
5308
5309 // Cannot assert on lack of success here since this function may be
5310 // used by code that is trying to report useful problem information
5311 // like deadlock detection.
5312 if (UseHeavyMonitors) return NULL;
5313
5314 // If we didn't find a matching Java thread and we didn't force use of
5315 // heavyweight monitors, then the owner is the stack address of the
5316 // Lock Word in the owning Java thread's stack.
5317 //
5318 JavaThread* the_owner = NULL;
5319 DO_JAVA_THREADS(t_list, q) {
5320 if (q->is_lock_owned(owner)) {
5321 the_owner = q;
5322 break;
5323 }
5324 }
5325
5326 // cannot assert on lack of success here; see above comment
5327 return the_owner;
5328 }
5329
5330 // Threads::print_on() is called at safepoint by VM_PrintThreads operation.
5331 void Threads::print_on(outputStream* st, bool print_stacks,
5332 bool internal_format, bool print_concurrent_locks) {
5333 char buf[32];
5334 st->print_raw_cr(os::local_time_string(buf, sizeof(buf)));
5335
5336 st->print_cr("Full thread dump %s (%s %s):",
5337 Abstract_VM_Version::vm_name(),
5338 Abstract_VM_Version::vm_release(),
5339 Abstract_VM_Version::vm_info_string());
5340 st->cr();
5341
5342 #if INCLUDE_SERVICES
5343 // Dump concurrent locks
5344 ConcurrentLocksDump concurrent_locks;
5345 if (print_concurrent_locks) {
5346 concurrent_locks.dump_at_safepoint();
5347 }
5348 #endif // INCLUDE_SERVICES
5349
5350 print_smr_info_on(st);
5351 st->cr();
5352
5353 ALL_JAVA_THREADS(p) {
5354 ResourceMark rm;
5355 p->print_on(st);
5356 if (print_stacks) {
5357 if (internal_format) {
5358 p->trace_stack();
5359 } else {
5360 p->print_stack_on(st);
5361 }
5362 }
5363 st->cr();
5364 #if INCLUDE_SERVICES
5365 if (print_concurrent_locks) {
5366 concurrent_locks.print_locks_on(p, st);
5367 }
5368 #endif // INCLUDE_SERVICES
5369 }
5370
5371 VMThread::vm_thread()->print_on(st);
5372 st->cr();
5373 Universe::heap()->print_gc_threads_on(st);
5374 WatcherThread* wt = WatcherThread::watcher_thread();
5375 if (wt != NULL) {
5376 wt->print_on(st);
5377 st->cr();
5378 }
5379
5380 st->flush();
5381 }
5382
5383 // Log Threads class SMR info.
5384 void Threads::log_smr_statistics() {
5385 LogTarget(Info, thread, smr) log;
5386 if (log.is_enabled()) {
5387 LogStream out(log);
5388 print_smr_info_on(&out);
5389 }
5390 }
5391
5392 // Print Threads class SMR info.
5393 void Threads::print_smr_info_on(outputStream* st) {
5394 // Only grab the Threads_lock if we don't already own it
5395 // and if we are not reporting an error.
5396 MutexLockerEx ml((Threads_lock->owned_by_self() || VMError::is_error_reported()) ? NULL : Threads_lock);
5397
5398 st->print_cr("Threads class SMR info:");
5399 st->print_cr("_smr_java_thread_list=" INTPTR_FORMAT ", length=%u, "
5400 "elements={", p2i(_smr_java_thread_list),
5401 _smr_java_thread_list->length());
5402 print_smr_info_elements_on(st, _smr_java_thread_list);
5403 st->print_cr("}");
5404 if (_smr_to_delete_list != NULL) {
5405 st->print_cr("_smr_to_delete_list=" INTPTR_FORMAT ", length=%u, "
5406 "elements={", p2i(_smr_to_delete_list),
5407 _smr_to_delete_list->length());
5408 print_smr_info_elements_on(st, _smr_to_delete_list);
5409 st->print_cr("}");
5410 for (ThreadsList *t_list = _smr_to_delete_list->next_list();
5411 t_list != NULL; t_list = t_list->next_list()) {
5412 st->print("next-> " INTPTR_FORMAT ", length=%u, "
5413 "elements={", p2i(t_list), t_list->length());
5414 print_smr_info_elements_on(st, t_list);
5415 st->print_cr("}");
5416 }
5417 }
5418 if (!EnableThreadSMRStatistics) {
5419 return;
5420 }
5421 st->print_cr("_smr_java_thread_list_alloc_cnt=%ld, "
5422 "_smr_java_thread_list_free_cnt=%ld, "
5423 "_smr_java_thread_list_max=%u, "
5424 "_smr_nested_thread_list_max=%u",
5425 _smr_java_thread_list_alloc_cnt,
5426 _smr_java_thread_list_free_cnt,
5427 _smr_java_thread_list_max,
5428 _smr_nested_thread_list_max);
5429 if (_smr_tlh_cnt > 0) {
5430 st->print_cr("_smr_tlh_cnt=" INT32_FORMAT
5431 ", _smr_tlh_times=" INT32_FORMAT
5432 ", avg_smr_tlh_time=%0.2f"
5433 ", _smr_tlh_time_max=" INT32_FORMAT,
5434 _smr_tlh_cnt, _smr_tlh_times,
5435 ((double) _smr_tlh_times / _smr_tlh_cnt),
5436 _smr_tlh_time_max);
5437 }
5438 if (_smr_deleted_thread_cnt > 0) {
5439 st->print_cr("_smr_deleted_thread_cnt=" INT32_FORMAT
5440 ", _smr_deleted_thread_times=" INT32_FORMAT
5441 ", avg_smr_deleted_thread_time=%0.2f"
5442 ", _smr_deleted_thread_time_max=" INT32_FORMAT,
5443 _smr_deleted_thread_cnt, _smr_deleted_thread_times,
5444 ((double) _smr_deleted_thread_times / _smr_deleted_thread_cnt),
5445 _smr_deleted_thread_time_max);
5446 }
5447 st->print_cr("_smr_delete_lock_wait_cnt=%u, _smr_delete_lock_wait_max=%u",
5448 _smr_delete_lock_wait_cnt, _smr_delete_lock_wait_max);
5449 st->print_cr("_smr_to_delete_list_cnt=%u, _smr_to_delete_list_max=%u",
5450 _smr_to_delete_list_cnt, _smr_to_delete_list_max);
5451 }
5452
5453 // Print ThreadsList elements (4 per line).
5454 void Threads::print_smr_info_elements_on(outputStream* st,
5455 ThreadsList* t_list) {
5456 uint cnt = 0;
5457 JavaThreadIterator jti(t_list);
5458 for (JavaThread *jt = jti.first(); jt != NULL; jt = jti.next()) {
5459 st->print(INTPTR_FORMAT, p2i(jt));
5460 if (cnt < t_list->length() - 1) {
5461 // Separate with comma or comma-space except for the last one.
5462 if (((cnt + 1) % 4) == 0) {
5463 // Four INTPTR_FORMAT fit on an 80 column line so end the
5464 // current line with just a comma.
5465 st->print_cr(",");
5466 } else {
5467 // Not the last one on the current line so use comma-space:
5468 st->print(", ");
5469 }
5470 } else {
5471 // Last one so just end the current line.
5472 st->cr();
5473 }
5474 cnt++;
5475 }
5476 }
5477
5478 void Threads::print_on_error(Thread* this_thread, outputStream* st, Thread* current, char* buf,
5479 int buflen, bool* found_current) {
5480 if (this_thread != NULL) {
5481 bool is_current = (current == this_thread);
5482 *found_current = *found_current || is_current;
5483 st->print("%s", is_current ? "=>" : " ");
5484
5485 st->print(PTR_FORMAT, p2i(this_thread));
5486 st->print(" ");
5487 this_thread->print_on_error(st, buf, buflen);
5488 st->cr();
5489 }
5490 }
5491
5492 class PrintOnErrorClosure : public ThreadClosure {
5493 outputStream* _st;
5494 Thread* _current;
5495 char* _buf;
5496 int _buflen;
5497 bool* _found_current;
5498 public:
5499 PrintOnErrorClosure(outputStream* st, Thread* current, char* buf,
5500 int buflen, bool* found_current) :
5501 _st(st), _current(current), _buf(buf), _buflen(buflen), _found_current(found_current) {}
5502
5503 virtual void do_thread(Thread* thread) {
5504 Threads::print_on_error(thread, _st, _current, _buf, _buflen, _found_current);
5505 }
5506 };
5507
5508 // Threads::print_on_error() is called by fatal error handler. It's possible
5509 // that VM is not at safepoint and/or current thread is inside signal handler.
5510 // Don't print stack trace, as the stack may not be walkable. Don't allocate
5511 // memory (even in resource area), it might deadlock the error handler.
5512 void Threads::print_on_error(outputStream* st, Thread* current, char* buf,
5513 int buflen) {
5514 print_smr_info_on(st);
5515 st->cr();
5516
5517 bool found_current = false;
5518 st->print_cr("Java Threads: ( => current thread )");
5519 ALL_JAVA_THREADS(thread) {
5520 print_on_error(thread, st, current, buf, buflen, &found_current);
5521 }
5522 st->cr();
5523
5524 st->print_cr("Other Threads:");
5525 print_on_error(VMThread::vm_thread(), st, current, buf, buflen, &found_current);
5526 print_on_error(WatcherThread::watcher_thread(), st, current, buf, buflen, &found_current);
5527
5528 PrintOnErrorClosure print_closure(st, current, buf, buflen, &found_current);
5529 Universe::heap()->gc_threads_do(&print_closure);
5530
5531 if (!found_current) {
5532 st->cr();
5533 st->print("=>" PTR_FORMAT " (exited) ", p2i(current));
5534 current->print_on_error(st, buf, buflen);
5535 st->cr();
5536 }
5537 st->cr();
5538
5539 st->print_cr("Threads with active compile tasks:");
5540 print_threads_compiling(st, buf, buflen);
5541 }
5542
5543 void Threads::print_threads_compiling(outputStream* st, char* buf, int buflen) {
5544 ALL_JAVA_THREADS(thread) {
5545 if (thread->is_Compiler_thread()) {
5546 CompilerThread* ct = (CompilerThread*) thread;
5547 if (ct->task() != NULL) {
5548 thread->print_name_on_error(st, buf, buflen);
5549 ct->task()->print(st, NULL, true, true);
5550 }
5551 }
5552 }
5553 }
5554
5555
5556 // Internal SpinLock and Mutex
5557 // Based on ParkEvent
5558
|