1641 return !op.failed();
1642 }
1643
1644 #endif // INCLUDE_JVMCI
1645
1646 // A JavaThread is a normal Java thread
1647
1648 void JavaThread::initialize() {
1649 // Initialize fields
1650
1651 set_saved_exception_pc(NULL);
1652 set_threadObj(NULL);
1653 _anchor.clear();
1654 set_entry_point(NULL);
1655 set_jni_functions(jni_functions());
1656 set_callee_target(NULL);
1657 set_vm_result(NULL);
1658 set_vm_result_2(NULL);
1659 set_vframe_array_head(NULL);
1660 set_vframe_array_last(NULL);
1661 set_deferred_locals(NULL);
1662 set_deopt_mark(NULL);
1663 set_deopt_compiled_method(NULL);
1664 set_monitor_chunks(NULL);
1665 _on_thread_list = false;
1666 set_thread_state(_thread_new);
1667 _terminated = _not_terminated;
1668 _array_for_gc = NULL;
1669 _suspend_equivalent = false;
1670 _in_deopt_handler = 0;
1671 _doing_unsafe_access = false;
1672 _stack_guard_state = stack_guard_unused;
1673 #if INCLUDE_JVMCI
1674 _pending_monitorenter = false;
1675 _pending_deoptimization = -1;
1676 _pending_failed_speculation = 0;
1677 _pending_transfer_to_interpreter = false;
1678 _in_retryable_allocation = false;
1679 _jvmci._alternate_call_target = NULL;
1680 assert(_jvmci._implicit_exception_pc == NULL, "must be");
1681 _jvmci_counters = NULL;
1815 // Free any remaining previous UnrollBlock
1816 vframeArray* old_array = vframe_array_last();
1817
1818 if (old_array != NULL) {
1819 Deoptimization::UnrollBlock* old_info = old_array->unroll_block();
1820 old_array->set_unroll_block(NULL);
1821 delete old_info;
1822 delete old_array;
1823 }
1824
1825 GrowableArray<jvmtiDeferredLocalVariableSet*>* deferred = deferred_locals();
1826 if (deferred != NULL) {
1827 // This can only happen if thread is destroyed before deoptimization occurs.
1828 assert(deferred->length() != 0, "empty array!");
1829 do {
1830 jvmtiDeferredLocalVariableSet* dlv = deferred->at(0);
1831 deferred->remove_at(0);
1832 // individual jvmtiDeferredLocalVariableSet are CHeapObj's
1833 delete dlv;
1834 } while (deferred->length() != 0);
1835 delete deferred;
1836 }
1837
1838 // All Java related clean up happens in exit
1839 ThreadSafepointState::destroy(this);
1840 if (_thread_stat != NULL) delete _thread_stat;
1841
1842 #if INCLUDE_JVMCI
1843 if (JVMCICounterSize > 0) {
1844 if (jvmci_counters_include(this)) {
1845 for (int i = 0; i < JVMCICounterSize; i++) {
1846 _jvmci_old_thread_counters[i] += _jvmci_counters[i];
1847 }
1848 }
1849 FREE_C_HEAP_ARRAY(jlong, _jvmci_counters);
1850 }
1851 #endif // INCLUDE_JVMCI
1852 }
1853
1854
1855 // First JavaThread specific code executed by a new Java thread.
2309
2310 assert(condition == _no_async_condition || has_pending_exception() ||
2311 (!check_unsafe_error && condition == _async_unsafe_access_error),
2312 "must have handled the async condition, if no exception");
2313 }
2314
2315 void JavaThread::handle_special_runtime_exit_condition(bool check_asyncs) {
2316
2317 // Check for pending external suspend.
2318 if (is_external_suspend_with_lock()) {
2319 frame_anchor()->make_walkable(this);
2320 java_suspend_self_with_safepoint_check();
2321 }
2322
2323 // We might be here for reasons in addition to the self-suspend request
2324 // so check for other async requests.
2325 if (check_asyncs) {
2326 check_and_handle_async_exceptions();
2327 }
2328
2329 JFR_ONLY(SUSPEND_THREAD_CONDITIONAL(this);)
2330 }
2331
2332 void JavaThread::send_thread_stop(oop java_throwable) {
2333 assert(Thread::current()->is_VM_thread(), "should be in the vm thread");
2334 assert(Threads_lock->is_locked(), "Threads_lock should be locked by safepoint code");
2335 assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped");
2336
2337 // Do not throw asynchronous exceptions against the compiler thread
2338 // (the compiler thread should not be a Java thread -- fix in 1.4.2)
2339 if (!can_call_java()) return;
2340
2341 {
2342 // Actually throw the Throwable against the target Thread - however
2343 // only if there is no thread death exception installed already.
2344 if (_pending_async_exception == NULL || !_pending_async_exception->is_a(SystemDictionary::ThreadDeath_klass())) {
2345 // If the topmost frame is a runtime stub, then we are calling into
2346 // OptoRuntime from compiled code. Some runtime stubs (new, monitor_exit..)
2347 // must deoptimize the caller before continuing, as the compiled exception handler table
2348 // may not be valid
2502 // should never be blocking at a safepoint whilst in those states. Of these 'bad' states
2503 // only _thread_in_native is possible when executing this code (based on our two callers).
2504 // A thread that is _thread_in_native is already safepoint-safe and so it doesn't matter
2505 // whether the VMThread sees the _thread_blocked state, or the _thread_in_native state,
2506 // and so we don't need the explicit safepoint check.
2507
2508 void JavaThread::java_suspend_self_with_safepoint_check() {
2509 assert(this == Thread::current(), "invariant");
2510 JavaThreadState state = thread_state();
2511 set_thread_state(_thread_blocked);
2512 java_suspend_self();
2513 set_thread_state_fence(state);
2514 // Since we are not using a regular thread-state transition helper here,
2515 // we must manually emit the instruction barrier after leaving a safe state.
2516 OrderAccess::cross_modify_fence();
2517 if (state != _thread_in_native) {
2518 SafepointMechanism::block_if_requested(this);
2519 }
2520 }
2521
2522 #ifdef ASSERT
2523 // Verify the JavaThread has not yet been published in the Threads::list, and
2524 // hence doesn't need protection from concurrent access at this stage.
2525 void JavaThread::verify_not_published() {
2526 // Cannot create a ThreadsListHandle here and check !tlh.includes(this)
2527 // since an unpublished JavaThread doesn't participate in the
2528 // Thread-SMR protocol for keeping a ThreadsList alive.
2529 assert(!on_thread_list(), "JavaThread shouldn't have been published yet!");
2530 }
2531 #endif
2532
2533 // Slow path when the native==>VM/Java barriers detect a safepoint is in
2534 // progress or when _suspend_flags is non-zero.
2535 // Current thread needs to self-suspend if there is a suspend request and/or
2536 // block if a safepoint is in progress.
2537 // Async exception ISN'T checked.
2538 // Note only the ThreadInVMfromNative transition can call this function
2539 // directly and when thread state is _thread_in_native_trans
2540 void JavaThread::check_safepoint_and_suspend_for_native_trans(JavaThread *thread) {
2541 assert(thread->thread_state() == _thread_in_native_trans, "wrong state");
2542
2543 assert(!thread->has_last_Java_frame() || thread->frame_anchor()->walkable(), "Unwalkable stack in native->vm transition");
2544
2545 if (thread->is_external_suspend()) {
2546 thread->java_suspend_self_with_safepoint_check();
2547 } else {
2548 SafepointMechanism::block_if_requested(thread);
2549 }
2550
2551 JFR_ONLY(SUSPEND_THREAD_CONDITIONAL(thread);)
2552 }
2553
2554 // Slow path when the native==>VM/Java barriers detect a safepoint is in
2555 // progress or when _suspend_flags is non-zero.
2556 // Current thread needs to self-suspend if there is a suspend request and/or
2557 // block if a safepoint is in progress.
2558 // Also check for pending async exception (not including unsafe access error).
2559 // Note only the native==>VM/Java barriers can call this function and when
2560 // thread state is _thread_in_native_trans.
2561 void JavaThread::check_special_condition_for_native_trans(JavaThread *thread) {
2562 check_safepoint_and_suspend_for_native_trans(thread);
2563
2564 if (thread->has_async_exception()) {
2565 // We are in _thread_in_native_trans state, don't handle unsafe
2566 // access error since that may block.
2567 thread->check_and_handle_async_exceptions(false);
2568 }
2569 }
2570
3390 void CodeCacheSweeperThread::oops_do(OopClosure* f, CodeBlobClosure* cf) {
3391 JavaThread::oops_do(f, cf);
3392 if (_scanned_compiled_method != NULL && cf != NULL) {
3393 // Safepoints can occur when the sweeper is scanning an nmethod so
3394 // process it here to make sure it isn't unloaded in the middle of
3395 // a scan.
3396 cf->do_code_blob(_scanned_compiled_method);
3397 }
3398 }
3399
3400 void CodeCacheSweeperThread::nmethods_do(CodeBlobClosure* cf) {
3401 JavaThread::nmethods_do(cf);
3402 if (_scanned_compiled_method != NULL && cf != NULL) {
3403 // Safepoints can occur when the sweeper is scanning an nmethod so
3404 // process it here to make sure it isn't unloaded in the middle of
3405 // a scan.
3406 cf->do_code_blob(_scanned_compiled_method);
3407 }
3408 }
3409
3410
3411 // ======= Threads ========
3412
3413 // The Threads class links together all active threads, and provides
3414 // operations over all threads. It is protected by the Threads_lock,
3415 // which is also used in other global contexts like safepointing.
3416 // ThreadsListHandles are used to safely perform operations on one
3417 // or more threads without the risk of the thread exiting during the
3418 // operation.
3419 //
3420 // Note: The Threads_lock is currently more widely used than we
3421 // would like. We are actively migrating Threads_lock uses to other
3422 // mechanisms in order to reduce Threads_lock contention.
3423
3424 int Threads::_number_of_threads = 0;
3425 int Threads::_number_of_non_daemon_threads = 0;
3426 int Threads::_return_code = 0;
3427 uintx Threads::_thread_claim_token = 1; // Never zero.
3428 size_t JavaThread::_stack_size_at_create = 0;
3429
|
1641 return !op.failed();
1642 }
1643
1644 #endif // INCLUDE_JVMCI
1645
1646 // A JavaThread is a normal Java thread
1647
1648 void JavaThread::initialize() {
1649 // Initialize fields
1650
1651 set_saved_exception_pc(NULL);
1652 set_threadObj(NULL);
1653 _anchor.clear();
1654 set_entry_point(NULL);
1655 set_jni_functions(jni_functions());
1656 set_callee_target(NULL);
1657 set_vm_result(NULL);
1658 set_vm_result_2(NULL);
1659 set_vframe_array_head(NULL);
1660 set_vframe_array_last(NULL);
1661 reset_deferred_updates();
1662 set_deopt_mark(NULL);
1663 set_deopt_compiled_method(NULL);
1664 set_monitor_chunks(NULL);
1665 _on_thread_list = false;
1666 set_thread_state(_thread_new);
1667 _terminated = _not_terminated;
1668 _array_for_gc = NULL;
1669 _suspend_equivalent = false;
1670 _in_deopt_handler = 0;
1671 _doing_unsafe_access = false;
1672 _stack_guard_state = stack_guard_unused;
1673 #if INCLUDE_JVMCI
1674 _pending_monitorenter = false;
1675 _pending_deoptimization = -1;
1676 _pending_failed_speculation = 0;
1677 _pending_transfer_to_interpreter = false;
1678 _in_retryable_allocation = false;
1679 _jvmci._alternate_call_target = NULL;
1680 assert(_jvmci._implicit_exception_pc == NULL, "must be");
1681 _jvmci_counters = NULL;
1815 // Free any remaining previous UnrollBlock
1816 vframeArray* old_array = vframe_array_last();
1817
1818 if (old_array != NULL) {
1819 Deoptimization::UnrollBlock* old_info = old_array->unroll_block();
1820 old_array->set_unroll_block(NULL);
1821 delete old_info;
1822 delete old_array;
1823 }
1824
1825 GrowableArray<jvmtiDeferredLocalVariableSet*>* deferred = deferred_locals();
1826 if (deferred != NULL) {
1827 // This can only happen if thread is destroyed before deoptimization occurs.
1828 assert(deferred->length() != 0, "empty array!");
1829 do {
1830 jvmtiDeferredLocalVariableSet* dlv = deferred->at(0);
1831 deferred->remove_at(0);
1832 // individual jvmtiDeferredLocalVariableSet are CHeapObj's
1833 delete dlv;
1834 } while (deferred->length() != 0);
1835 delete deferred_updates();
1836 }
1837
1838 // All Java related clean up happens in exit
1839 ThreadSafepointState::destroy(this);
1840 if (_thread_stat != NULL) delete _thread_stat;
1841
1842 #if INCLUDE_JVMCI
1843 if (JVMCICounterSize > 0) {
1844 if (jvmci_counters_include(this)) {
1845 for (int i = 0; i < JVMCICounterSize; i++) {
1846 _jvmci_old_thread_counters[i] += _jvmci_counters[i];
1847 }
1848 }
1849 FREE_C_HEAP_ARRAY(jlong, _jvmci_counters);
1850 }
1851 #endif // INCLUDE_JVMCI
1852 }
1853
1854
1855 // First JavaThread specific code executed by a new Java thread.
2309
2310 assert(condition == _no_async_condition || has_pending_exception() ||
2311 (!check_unsafe_error && condition == _async_unsafe_access_error),
2312 "must have handled the async condition, if no exception");
2313 }
2314
2315 void JavaThread::handle_special_runtime_exit_condition(bool check_asyncs) {
2316
2317 // Check for pending external suspend.
2318 if (is_external_suspend_with_lock()) {
2319 frame_anchor()->make_walkable(this);
2320 java_suspend_self_with_safepoint_check();
2321 }
2322
2323 // We might be here for reasons in addition to the self-suspend request
2324 // so check for other async requests.
2325 if (check_asyncs) {
2326 check_and_handle_async_exceptions();
2327 }
2328
2329 if (is_ea_obj_deopt_suspend()) {
2330 frame_anchor()->make_walkable(this);
2331 wait_for_object_deoptimization();
2332 }
2333
2334 JFR_ONLY(SUSPEND_THREAD_CONDITIONAL(this);)
2335 }
2336
2337 void JavaThread::send_thread_stop(oop java_throwable) {
2338 assert(Thread::current()->is_VM_thread(), "should be in the vm thread");
2339 assert(Threads_lock->is_locked(), "Threads_lock should be locked by safepoint code");
2340 assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped");
2341
2342 // Do not throw asynchronous exceptions against the compiler thread
2343 // (the compiler thread should not be a Java thread -- fix in 1.4.2)
2344 if (!can_call_java()) return;
2345
2346 {
2347 // Actually throw the Throwable against the target Thread - however
2348 // only if there is no thread death exception installed already.
2349 if (_pending_async_exception == NULL || !_pending_async_exception->is_a(SystemDictionary::ThreadDeath_klass())) {
2350 // If the topmost frame is a runtime stub, then we are calling into
2351 // OptoRuntime from compiled code. Some runtime stubs (new, monitor_exit..)
2352 // must deoptimize the caller before continuing, as the compiled exception handler table
2353 // may not be valid
2507 // should never be blocking at a safepoint whilst in those states. Of these 'bad' states
2508 // only _thread_in_native is possible when executing this code (based on our two callers).
2509 // A thread that is _thread_in_native is already safepoint-safe and so it doesn't matter
2510 // whether the VMThread sees the _thread_blocked state, or the _thread_in_native state,
2511 // and so we don't need the explicit safepoint check.
2512
2513 void JavaThread::java_suspend_self_with_safepoint_check() {
2514 assert(this == Thread::current(), "invariant");
2515 JavaThreadState state = thread_state();
2516 set_thread_state(_thread_blocked);
2517 java_suspend_self();
2518 set_thread_state_fence(state);
2519 // Since we are not using a regular thread-state transition helper here,
2520 // we must manually emit the instruction barrier after leaving a safe state.
2521 OrderAccess::cross_modify_fence();
2522 if (state != _thread_in_native) {
2523 SafepointMechanism::block_if_requested(this);
2524 }
2525 }
2526
2527 void JavaThread::wait_for_object_deoptimization() {
2528 assert(!has_last_Java_frame() || frame_anchor()->walkable(), "should have walkable stack");
2529 assert(this == Thread::current(), "invariant");
2530 JavaThreadState state = thread_state();
2531
2532 do {
2533 set_thread_state(_thread_blocked);
2534 set_suspend_equivalent();
2535 MonitorLocker ml(JvmtiObjReallocRelock_lock, Monitor::_no_safepoint_check_flag);
2536 if (is_ea_obj_deopt_suspend()) {
2537 ml.wait();
2538 }
2539 if (handle_special_suspend_equivalent_condition()) {
2540 MutexUnlocker mu(JvmtiObjReallocRelock_lock, Monitor::_no_safepoint_check_flag);
2541 java_suspend_self();
2542 }
2543 set_thread_state_fence(state);
2544 } while (is_ea_obj_deopt_suspend());
2545
2546 // Since we are not using a regular thread-state transition helper here,
2547 // we must manually emit the instruction barrier after leaving a safe state.
2548 OrderAccess::cross_modify_fence();
2549 if (state != _thread_in_native) {
2550 SafepointMechanism::block_if_requested(this);
2551 }
2552 }
2553
2554 #ifdef ASSERT
2555 // Verify the JavaThread has not yet been published in the Threads::list, and
2556 // hence doesn't need protection from concurrent access at this stage.
2557 void JavaThread::verify_not_published() {
2558 // Cannot create a ThreadsListHandle here and check !tlh.includes(this)
2559 // since an unpublished JavaThread doesn't participate in the
2560 // Thread-SMR protocol for keeping a ThreadsList alive.
2561 assert(!on_thread_list(), "JavaThread shouldn't have been published yet!");
2562 }
2563 #endif
2564
2565 // Slow path when the native==>VM/Java barriers detect a safepoint is in
2566 // progress or when _suspend_flags is non-zero.
2567 // Current thread needs to self-suspend if there is a suspend request and/or
2568 // block if a safepoint is in progress.
2569 // Async exception ISN'T checked.
2570 // Note only the ThreadInVMfromNative transition can call this function
2571 // directly and when thread state is _thread_in_native_trans
2572 void JavaThread::check_safepoint_and_suspend_for_native_trans(JavaThread *thread) {
2573 assert(thread->thread_state() == _thread_in_native_trans, "wrong state");
2574
2575 assert(!thread->has_last_Java_frame() || thread->frame_anchor()->walkable(), "Unwalkable stack in native->vm transition");
2576
2577 if (thread->is_external_suspend()) {
2578 thread->java_suspend_self_with_safepoint_check();
2579 } else {
2580 SafepointMechanism::block_if_requested(thread);
2581 }
2582
2583 if (thread->is_ea_obj_deopt_suspend()) {
2584 thread->wait_for_object_deoptimization();
2585 }
2586
2587 JFR_ONLY(SUSPEND_THREAD_CONDITIONAL(thread);)
2588 }
2589
2590 // Slow path when the native==>VM/Java barriers detect a safepoint is in
2591 // progress or when _suspend_flags is non-zero.
2592 // Current thread needs to self-suspend if there is a suspend request and/or
2593 // block if a safepoint is in progress.
2594 // Also check for pending async exception (not including unsafe access error).
2595 // Note only the native==>VM/Java barriers can call this function and when
2596 // thread state is _thread_in_native_trans.
2597 void JavaThread::check_special_condition_for_native_trans(JavaThread *thread) {
2598 check_safepoint_and_suspend_for_native_trans(thread);
2599
2600 if (thread->has_async_exception()) {
2601 // We are in _thread_in_native_trans state, don't handle unsafe
2602 // access error since that may block.
2603 thread->check_and_handle_async_exceptions(false);
2604 }
2605 }
2606
3426 void CodeCacheSweeperThread::oops_do(OopClosure* f, CodeBlobClosure* cf) {
3427 JavaThread::oops_do(f, cf);
3428 if (_scanned_compiled_method != NULL && cf != NULL) {
3429 // Safepoints can occur when the sweeper is scanning an nmethod so
3430 // process it here to make sure it isn't unloaded in the middle of
3431 // a scan.
3432 cf->do_code_blob(_scanned_compiled_method);
3433 }
3434 }
3435
3436 void CodeCacheSweeperThread::nmethods_do(CodeBlobClosure* cf) {
3437 JavaThread::nmethods_do(cf);
3438 if (_scanned_compiled_method != NULL && cf != NULL) {
3439 // Safepoints can occur when the sweeper is scanning an nmethod so
3440 // process it here to make sure it isn't unloaded in the middle of
3441 // a scan.
3442 cf->do_code_blob(_scanned_compiled_method);
3443 }
3444 }
3445
3446 #if defined(ASSERT) && COMPILER2_OR_JVMCI
3447 static void deopt_objs_alot_thread_entry(JavaThread* thread, TRAPS) {
3448 Deoptimization::deoptimize_objects_alot_loop();
3449 }
3450
3451 DeoptimizeObjectsALotThread::DeoptimizeObjectsALotThread()
3452 : JavaThread(&deopt_objs_alot_thread_entry) {
3453 }
3454 #endif // defined(ASSERT) && COMPILER2_OR_JVMCI
3455
3456 // ======= Threads ========
3457
3458 // The Threads class links together all active threads, and provides
3459 // operations over all threads. It is protected by the Threads_lock,
3460 // which is also used in other global contexts like safepointing.
3461 // ThreadsListHandles are used to safely perform operations on one
3462 // or more threads without the risk of the thread exiting during the
3463 // operation.
3464 //
3465 // Note: The Threads_lock is currently more widely used than we
3466 // would like. We are actively migrating Threads_lock uses to other
3467 // mechanisms in order to reduce Threads_lock contention.
3468
3469 int Threads::_number_of_threads = 0;
3470 int Threads::_number_of_non_daemon_threads = 0;
3471 int Threads::_return_code = 0;
3472 uintx Threads::_thread_claim_token = 1; // Never zero.
3473 size_t JavaThread::_stack_size_at_create = 0;
3474
|