1 /*
2 * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
1653
1654 // Thread is now sufficient initialized to be handled by the safepoint code as being
1655 // in the VM. Change thread state from _thread_new to _thread_in_vm
1656 ThreadStateTransition::transition_and_fence(this, _thread_new, _thread_in_vm);
1657
1658 assert(JavaThread::current() == this, "sanity check");
1659 assert(!Thread::current()->owns_locks(), "sanity check");
1660
1661 DTRACE_THREAD_PROBE(start, this);
1662
1663 // This operation might block. We call that after all safepoint checks for a new thread has
1664 // been completed.
1665 this->set_active_handles(JNIHandleBlock::allocate_block());
1666
1667 if (JvmtiExport::should_post_thread_life()) {
1668 JvmtiExport::post_thread_start(this);
1669 }
1670
1671 EventThreadStart event;
1672 if (event.should_commit()) {
1673 event.set_javalangthread(java_lang_Thread::thread_id(this->threadObj()));
1674 event.commit();
1675 }
1676
1677 // We call another function to do the rest so we are sure that the stack addresses used
1678 // from there will be lower than the stack base just computed
1679 thread_main_inner();
1680
1681 // Note, thread is no longer valid at this point!
1682 }
1683
1684
1685 void JavaThread::thread_main_inner() {
1686 assert(JavaThread::current() == this, "sanity check");
1687 assert(this->threadObj() != NULL, "just checking");
1688
1689 // Execute thread entry point unless this thread has a pending exception
1690 // or has been stopped before starting.
1691 // Note: Due to JVM_StopThread we can have pending exceptions already!
1692 if (!this->has_pending_exception() &&
1693 !java_lang_Thread::is_stillborn(this->threadObj())) {
1787 threadObj, // Arg 1
1788 uncaught_exception, // Arg 2
1789 THREAD);
1790 }
1791 if (HAS_PENDING_EXCEPTION) {
1792 ResourceMark rm(this);
1793 jio_fprintf(defaultStream::error_stream(),
1794 "\nException: %s thrown from the UncaughtExceptionHandler"
1795 " in thread \"%s\"\n",
1796 pending_exception()->klass()->external_name(),
1797 get_thread_name());
1798 CLEAR_PENDING_EXCEPTION;
1799 }
1800 }
1801 }
1802
1803 // Called before the java thread exit since we want to read info
1804 // from java_lang_Thread object
1805 EventThreadEnd event;
1806 if (event.should_commit()) {
1807 event.set_javalangthread(java_lang_Thread::thread_id(this->threadObj()));
1808 event.commit();
1809 }
1810
1811 // Call after last event on thread
1812 EVENT_THREAD_EXIT(this);
1813
1814 // Call Thread.exit(). We try 3 times in case we got another Thread.stop during
1815 // the execution of the method. If that is not enough, then we don't really care. Thread.stop
1816 // is deprecated anyhow.
1817 if (!is_Compiler_thread()) {
1818 int count = 3;
1819 while (java_lang_Thread::threadGroup(threadObj()) != NULL && (count-- > 0)) {
1820 EXCEPTION_MARK;
1821 JavaValue result(T_VOID);
1822 KlassHandle thread_klass(THREAD, SystemDictionary::Thread_klass());
1823 JavaCalls::call_virtual(&result,
1824 threadObj, thread_klass,
1825 vmSymbols::exit_method_name(),
1826 vmSymbols::void_method_signature(),
1827 THREAD);
2168 // }
2169 // set_thread_state(_thread_in_vm_trans);
2170 // if (safepoint) block;
2171 // set_thread_state(state);
2172 //
2173 // but that is pretty messy. Instead we just go with the way the
2174 // code has worked before and note that this is the only path to
2175 // java_suspend_self that doesn't put the thread in _thread_blocked
2176 // mode.
2177
2178 frame_anchor()->make_walkable(this);
2179 java_suspend_self();
2180
2181 // We might be here for reasons in addition to the self-suspend request
2182 // so check for other async requests.
2183 }
2184
2185 if (check_asyncs) {
2186 check_and_handle_async_exceptions();
2187 }
2188 }
2189
2190 void JavaThread::send_thread_stop(oop java_throwable) {
2191 assert(Thread::current()->is_VM_thread(), "should be in the vm thread");
2192 assert(Threads_lock->is_locked(), "Threads_lock should be locked by safepoint code");
2193 assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped");
2194
2195 // Do not throw asynchronous exceptions against the compiler thread
2196 // (the compiler thread should not be a Java thread -- fix in 1.4.2)
2197 if (is_Compiler_thread()) return;
2198
2199 {
2200 // Actually throw the Throwable against the target Thread - however
2201 // only if there is no thread death exception installed already.
2202 if (_pending_async_exception == NULL || !_pending_async_exception->is_a(SystemDictionary::ThreadDeath_klass())) {
2203 // If the topmost frame is a runtime stub, then we are calling into
2204 // OptoRuntime from compiled code. Some runtime stubs (new, monitor_exit..)
2205 // must deoptimize the caller before continuing, as the compiled exception handler table
2206 // may not be valid
2207 if (has_last_Java_frame()) {
2406 if (SafepointSynchronize::do_call_back()) {
2407 // If we are safepointing, then block the caller which may not be
2408 // the same as the target thread (see above).
2409 SafepointSynchronize::block(curJT);
2410 }
2411
2412 if (thread->is_deopt_suspend()) {
2413 thread->clear_deopt_suspend();
2414 RegisterMap map(thread, false);
2415 frame f = thread->last_frame();
2416 while ( f.id() != thread->must_deopt_id() && ! f.is_first_frame()) {
2417 f = f.sender(&map);
2418 }
2419 if (f.id() == thread->must_deopt_id()) {
2420 thread->clear_must_deopt_id();
2421 f.deoptimize(thread);
2422 } else {
2423 fatal("missed deoptimization!");
2424 }
2425 }
2426 }
2427
2428 // Slow path when the native==>VM/Java barriers detect a safepoint is in
2429 // progress or when _suspend_flags is non-zero.
2430 // Current thread needs to self-suspend if there is a suspend request and/or
2431 // block if a safepoint is in progress.
2432 // Also check for pending async exception (not including unsafe access error).
2433 // Note only the native==>VM/Java barriers can call this function and when
2434 // thread state is _thread_in_native_trans.
2435 void JavaThread::check_special_condition_for_native_trans(JavaThread *thread) {
2436 check_safepoint_and_suspend_for_native_trans(thread);
2437
2438 if (thread->has_async_exception()) {
2439 // We are in _thread_in_native_trans state, don't handle unsafe
2440 // access error since that may block.
2441 thread->check_and_handle_async_exceptions(false);
2442 }
2443 }
2444
2445 // This is a variant of the normal
|
1 /*
2 * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
1653
1654 // Thread is now sufficient initialized to be handled by the safepoint code as being
1655 // in the VM. Change thread state from _thread_new to _thread_in_vm
1656 ThreadStateTransition::transition_and_fence(this, _thread_new, _thread_in_vm);
1657
1658 assert(JavaThread::current() == this, "sanity check");
1659 assert(!Thread::current()->owns_locks(), "sanity check");
1660
1661 DTRACE_THREAD_PROBE(start, this);
1662
1663 // This operation might block. We call that after all safepoint checks for a new thread has
1664 // been completed.
1665 this->set_active_handles(JNIHandleBlock::allocate_block());
1666
1667 if (JvmtiExport::should_post_thread_life()) {
1668 JvmtiExport::post_thread_start(this);
1669 }
1670
1671 EventThreadStart event;
1672 if (event.should_commit()) {
1673 event.set_thread(THREAD_TRACE_ID(this));
1674 event.commit();
1675 }
1676
1677 // We call another function to do the rest so we are sure that the stack addresses used
1678 // from there will be lower than the stack base just computed
1679 thread_main_inner();
1680
1681 // Note, thread is no longer valid at this point!
1682 }
1683
1684
1685 void JavaThread::thread_main_inner() {
1686 assert(JavaThread::current() == this, "sanity check");
1687 assert(this->threadObj() != NULL, "just checking");
1688
1689 // Execute thread entry point unless this thread has a pending exception
1690 // or has been stopped before starting.
1691 // Note: Due to JVM_StopThread we can have pending exceptions already!
1692 if (!this->has_pending_exception() &&
1693 !java_lang_Thread::is_stillborn(this->threadObj())) {
1787 threadObj, // Arg 1
1788 uncaught_exception, // Arg 2
1789 THREAD);
1790 }
1791 if (HAS_PENDING_EXCEPTION) {
1792 ResourceMark rm(this);
1793 jio_fprintf(defaultStream::error_stream(),
1794 "\nException: %s thrown from the UncaughtExceptionHandler"
1795 " in thread \"%s\"\n",
1796 pending_exception()->klass()->external_name(),
1797 get_thread_name());
1798 CLEAR_PENDING_EXCEPTION;
1799 }
1800 }
1801 }
1802
1803 // Called before the java thread exit since we want to read info
1804 // from java_lang_Thread object
1805 EventThreadEnd event;
1806 if (event.should_commit()) {
1807 event.set_thread(THREAD_TRACE_ID(this));
1808 event.commit();
1809 }
1810
1811 // Call after last event on thread
1812 EVENT_THREAD_EXIT(this);
1813
1814 // Call Thread.exit(). We try 3 times in case we got another Thread.stop during
1815 // the execution of the method. If that is not enough, then we don't really care. Thread.stop
1816 // is deprecated anyhow.
1817 if (!is_Compiler_thread()) {
1818 int count = 3;
1819 while (java_lang_Thread::threadGroup(threadObj()) != NULL && (count-- > 0)) {
1820 EXCEPTION_MARK;
1821 JavaValue result(T_VOID);
1822 KlassHandle thread_klass(THREAD, SystemDictionary::Thread_klass());
1823 JavaCalls::call_virtual(&result,
1824 threadObj, thread_klass,
1825 vmSymbols::exit_method_name(),
1826 vmSymbols::void_method_signature(),
1827 THREAD);
2168 // }
2169 // set_thread_state(_thread_in_vm_trans);
2170 // if (safepoint) block;
2171 // set_thread_state(state);
2172 //
2173 // but that is pretty messy. Instead we just go with the way the
2174 // code has worked before and note that this is the only path to
2175 // java_suspend_self that doesn't put the thread in _thread_blocked
2176 // mode.
2177
2178 frame_anchor()->make_walkable(this);
2179 java_suspend_self();
2180
2181 // We might be here for reasons in addition to the self-suspend request
2182 // so check for other async requests.
2183 }
2184
2185 if (check_asyncs) {
2186 check_and_handle_async_exceptions();
2187 }
2188 #if INCLUDE_TRACE
2189 if (is_trace_suspend()) {
2190 TRACE_SUSPEND_THREAD(this);
2191 }
2192 #endif
2193 }
2194
2195 void JavaThread::send_thread_stop(oop java_throwable) {
2196 assert(Thread::current()->is_VM_thread(), "should be in the vm thread");
2197 assert(Threads_lock->is_locked(), "Threads_lock should be locked by safepoint code");
2198 assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped");
2199
2200 // Do not throw asynchronous exceptions against the compiler thread
2201 // (the compiler thread should not be a Java thread -- fix in 1.4.2)
2202 if (is_Compiler_thread()) return;
2203
2204 {
2205 // Actually throw the Throwable against the target Thread - however
2206 // only if there is no thread death exception installed already.
2207 if (_pending_async_exception == NULL || !_pending_async_exception->is_a(SystemDictionary::ThreadDeath_klass())) {
2208 // If the topmost frame is a runtime stub, then we are calling into
2209 // OptoRuntime from compiled code. Some runtime stubs (new, monitor_exit..)
2210 // must deoptimize the caller before continuing, as the compiled exception handler table
2211 // may not be valid
2212 if (has_last_Java_frame()) {
2411 if (SafepointSynchronize::do_call_back()) {
2412 // If we are safepointing, then block the caller which may not be
2413 // the same as the target thread (see above).
2414 SafepointSynchronize::block(curJT);
2415 }
2416
2417 if (thread->is_deopt_suspend()) {
2418 thread->clear_deopt_suspend();
2419 RegisterMap map(thread, false);
2420 frame f = thread->last_frame();
2421 while ( f.id() != thread->must_deopt_id() && ! f.is_first_frame()) {
2422 f = f.sender(&map);
2423 }
2424 if (f.id() == thread->must_deopt_id()) {
2425 thread->clear_must_deopt_id();
2426 f.deoptimize(thread);
2427 } else {
2428 fatal("missed deoptimization!");
2429 }
2430 }
2431 #if INCLUDE_TRACE
2432 if (thread->is_trace_suspend()) {
2433 TRACE_SUSPEND_THREAD(thread);
2434 }
2435 #endif
2436 }
2437
2438 // Slow path when the native==>VM/Java barriers detect a safepoint is in
2439 // progress or when _suspend_flags is non-zero.
2440 // Current thread needs to self-suspend if there is a suspend request and/or
2441 // block if a safepoint is in progress.
2442 // Also check for pending async exception (not including unsafe access error).
2443 // Note only the native==>VM/Java barriers can call this function and when
2444 // thread state is _thread_in_native_trans.
2445 void JavaThread::check_special_condition_for_native_trans(JavaThread *thread) {
2446 check_safepoint_and_suspend_for_native_trans(thread);
2447
2448 if (thread->has_async_exception()) {
2449 // We are in _thread_in_native_trans state, don't handle unsafe
2450 // access error since that may block.
2451 thread->check_and_handle_async_exceptions(false);
2452 }
2453 }
2454
2455 // This is a variant of the normal
|