< prev index next >

src/hotspot/share/runtime/thread.cpp

Print this page




 839     if (wait_for_suspend) {
 840       // We are allowed to wait for the external suspend to complete
 841       // so give the other thread a chance to get suspended.
 842       if (!wait_for_ext_suspend_completion(SuspendRetryCount,
 843                                            SuspendRetryDelay, bits)) {
 844         // Didn't make it so let the caller know.
 845         return false;
 846       }
 847     }
 848     // We aren't allowed to wait for the external suspend to complete
 849     // so if the other thread isn't externally suspended we need to
 850     // let the caller know.
 851     else if (!is_ext_suspend_completed_with_lock(bits)) {
 852       return false;
 853     }
 854   }
 855 
 856   return true;
 857 }
 858 
 859 void Thread::interrupt(Thread* thread) {
 860   debug_only(check_for_dangling_thread_pointer(thread);)
 861   os::interrupt(thread);
 862 }
 863 
 864 bool Thread::is_interrupted(Thread* thread, bool clear_interrupted) {
 865   debug_only(check_for_dangling_thread_pointer(thread);)
 866   // Note:  If clear_interrupted==false, this simply fetches and
 867   // returns the value of the field osthread()->interrupted().
 868   return os::is_interrupted(thread, clear_interrupted);
 869 }
 870 
 871 
 872 // GC Support
 873 bool Thread::claim_par_threads_do(uintx claim_token) {
 874   uintx token = _threads_do_token;
 875   if (token != claim_token) {
 876     uintx res = Atomic::cmpxchg(claim_token, &_threads_do_token, token);
 877     if (res == token) {
 878       return true;
 879     }
 880     guarantee(res == claim_token, "invariant");
 881   }
 882   return false;
 883 }
 884 
 885 void Thread::oops_do(OopClosure* f, CodeBlobClosure* cf) {
 886   active_handles()->oops_do(f);
 887   // Do oop for ThreadShadow
 888   f->do_oop((oop*)&_pending_exception);
 889   handle_area()->oops_do(f);
 890 
 891   // We scan thread local monitor lists here, and the remaining global


1709   if (SafepointMechanism::uses_thread_local_poll()) {
1710     SafepointMechanism::initialize_header(this);
1711   }
1712 
1713   _class_to_be_initialized = NULL;
1714 
1715   pd_initialize();
1716 }
1717 
1718 JavaThread::JavaThread(bool is_attaching_via_jni) :
1719                        Thread() {
1720   initialize();
1721   if (is_attaching_via_jni) {
1722     _jni_attach_state = _attaching_via_jni;
1723   } else {
1724     _jni_attach_state = _not_attaching_via_jni;
1725   }
1726   assert(deferred_card_mark().is_empty(), "Default MemRegion ctor");
1727 }
1728 


















































1729 bool JavaThread::reguard_stack(address cur_sp) {
1730   if (_stack_guard_state != stack_guard_yellow_reserved_disabled
1731       && _stack_guard_state != stack_guard_reserved_disabled) {
1732     return true; // Stack already guarded or guard pages not needed.
1733   }
1734 
1735   if (register_stack_overflow()) {
1736     // For those architectures which have separate register and
1737     // memory stacks, we must check the register stack to see if
1738     // it has overflowed.
1739     return false;
1740   }
1741 
1742   // Java code never executes within the yellow zone: the latter is only
1743   // there to provoke an exception during stack banging.  If java code
1744   // is executing there, either StackShadowPages should be larger, or
1745   // some exception code in c1, c2 or the interpreter isn't unwinding
1746   // when it should.
1747   guarantee(cur_sp > stack_reserved_zone_base(),
1748             "not enough space to reguard - increase StackShadowPages");


2353           if (!StressCompiledExceptionHandlers && compiled_frame.can_be_deoptimized()) {
2354             Deoptimization::deoptimize(this, compiled_frame, &reg_map);
2355           }
2356         }
2357       }
2358 
2359       // Set async. pending exception in thread.
2360       set_pending_async_exception(java_throwable);
2361 
2362       if (log_is_enabled(Info, exceptions)) {
2363          ResourceMark rm;
2364         log_info(exceptions)("Pending Async. exception installed of type: %s",
2365                              InstanceKlass::cast(_pending_async_exception->klass())->external_name());
2366       }
2367       // for AbortVMOnException flag
2368       Exceptions::debug_check_abort(_pending_async_exception->klass()->external_name());
2369     }
2370   }
2371 
2372 
2373   // Interrupt thread so it will wake up from a potential wait()
2374   Thread::interrupt(this);
2375 }
2376 
2377 // External suspension mechanism.
2378 //
2379 // Tell the VM to suspend a thread when ever it knows that it does not hold on
2380 // to any VM_locks and it is at a transition
2381 // Self-suspension will happen on the transition out of the vm.
2382 // Catch "this" coming in from JNIEnv pointers when the thread has been freed
2383 //
2384 // Guarantees on return:
2385 //   + Target thread will not execute any new bytecode (that's why we need to
2386 //     force a safepoint)
2387 //   + Target thread will not enter any new monitors
2388 //
2389 void JavaThread::java_suspend() {
2390   ThreadsListHandle tlh;
2391   if (!tlh.includes(this) || threadObj() == NULL || is_exiting()) {
2392     return;
2393   }
2394 


3344 // java.lang.Thread.sleep support
3345 // Returns true if sleep time elapsed as expected, and false
3346 // if the thread was interrupted.
3347 bool JavaThread::sleep(jlong millis) {
3348   assert(this == Thread::current(),  "thread consistency check");
3349 
3350   ParkEvent * const slp = this->_SleepEvent;
3351   // Because there can be races with thread interruption sending an unpark()
3352   // to the event, we explicitly reset it here to avoid an immediate return.
3353   // The actual interrupt state will be checked before we park().
3354   slp->reset();
3355   // Thread interruption establishes a happens-before ordering in the
3356   // Java Memory Model, so we need to ensure we synchronize with the
3357   // interrupt state.
3358   OrderAccess::fence();
3359 
3360   jlong prevtime = os::javaTimeNanos();
3361 
3362   for (;;) {
3363     // interruption has precedence over timing out
3364     if (os::is_interrupted(this, true)) {
3365       return false;
3366     }
3367 
3368     if (millis <= 0) {
3369       return true;
3370     }
3371 
3372     {
3373       ThreadBlockInVM tbivm(this);
3374       OSThreadWaitState osts(this->osthread(), false /* not Object.wait() */);
3375 
3376       this->set_suspend_equivalent();
3377       // cleared by handle_special_suspend_equivalent_condition() or
3378       // java_suspend_self() via check_and_wait_while_suspended()
3379 
3380       slp->park(millis);
3381 
3382       // were we externally suspended while we were waiting?
3383       this->check_and_wait_while_suspended();
3384     }




 839     if (wait_for_suspend) {
 840       // We are allowed to wait for the external suspend to complete
 841       // so give the other thread a chance to get suspended.
 842       if (!wait_for_ext_suspend_completion(SuspendRetryCount,
 843                                            SuspendRetryDelay, bits)) {
 844         // Didn't make it so let the caller know.
 845         return false;
 846       }
 847     }
 848     // We aren't allowed to wait for the external suspend to complete
 849     // so if the other thread isn't externally suspended we need to
 850     // let the caller know.
 851     else if (!is_ext_suspend_completed_with_lock(bits)) {
 852       return false;
 853     }
 854   }
 855 
 856   return true;
 857 }
 858 













 859 // GC Support
 860 bool Thread::claim_par_threads_do(uintx claim_token) {
 861   uintx token = _threads_do_token;
 862   if (token != claim_token) {
 863     uintx res = Atomic::cmpxchg(claim_token, &_threads_do_token, token);
 864     if (res == token) {
 865       return true;
 866     }
 867     guarantee(res == claim_token, "invariant");
 868   }
 869   return false;
 870 }
 871 
 872 void Thread::oops_do(OopClosure* f, CodeBlobClosure* cf) {
 873   active_handles()->oops_do(f);
 874   // Do oop for ThreadShadow
 875   f->do_oop((oop*)&_pending_exception);
 876   handle_area()->oops_do(f);
 877 
 878   // We scan thread local monitor lists here, and the remaining global


1696   if (SafepointMechanism::uses_thread_local_poll()) {
1697     SafepointMechanism::initialize_header(this);
1698   }
1699 
1700   _class_to_be_initialized = NULL;
1701 
1702   pd_initialize();
1703 }
1704 
1705 JavaThread::JavaThread(bool is_attaching_via_jni) :
1706                        Thread() {
1707   initialize();
1708   if (is_attaching_via_jni) {
1709     _jni_attach_state = _attaching_via_jni;
1710   } else {
1711     _jni_attach_state = _not_attaching_via_jni;
1712   }
1713   assert(deferred_card_mark().is_empty(), "Default MemRegion ctor");
1714 }
1715 
1716 
1717 // interrupt support
1718 
1719 void JavaThread::interrupt() {
1720   debug_only(check_for_dangling_thread_pointer(this);)
1721 
1722     if (!osthread()->interrupted()) {
1723     osthread()->set_interrupted(true);
1724     // More than one thread can get here with the same value of osthread,
1725     // resulting in multiple notifications.  We do, however, want the store
1726     // to interrupted() to be visible to other threads before we execute unpark().
1727     OrderAccess::fence();
1728 
1729     // For JavaThread::sleep. Historically we only unpark if changing to the interrupted
1730     // state, in contrast to the other events below. Not clear exactly why.
1731     _SleepEvent->unpark();
1732   }
1733 
1734   // For JSR166. Unpark even if interrupt status already was set.
1735   parker()->unpark();
1736 
1737   // For ObjectMonitor and JvmtiRawMonitor
1738   _ParkEvent->unpark();
1739 }
1740 
1741 
1742 bool JavaThread::is_interrupted(bool clear_interrupted) {
1743   debug_only(check_for_dangling_thread_pointer(this);)
1744   bool interrupted = osthread()->interrupted();
1745 
1746   // NOTE that since there is no "lock" around the interrupt and
1747   // is_interrupted operations, there is the possibility that the
1748   // interrupted flag (in osThread) will be "false" but that the
1749   // low-level events will be in the signaled state. This is
1750   // intentional. The effect of this is that Object.wait() and
1751   // LockSupport.park() will appear to have a spurious wakeup, which
1752   // is allowed and not harmful, and the possibility is so rare that
1753   // it is not worth the added complexity to add yet another lock.
1754   // For the sleep event an explicit reset is performed on entry
1755   // to JavaThread::sleep, so there is no early return. It has also been
1756   // recommended not to put the interrupted flag into the "event"
1757   // structure because it hides the issue.
1758   if (interrupted && clear_interrupted) {
1759     osthread()->set_interrupted(false);
1760     // consider thread->_SleepEvent->reset() ... optional optimization
1761   }
1762 
1763   return interrupted;
1764 }
1765 
1766 bool JavaThread::reguard_stack(address cur_sp) {
1767   if (_stack_guard_state != stack_guard_yellow_reserved_disabled
1768       && _stack_guard_state != stack_guard_reserved_disabled) {
1769     return true; // Stack already guarded or guard pages not needed.
1770   }
1771 
1772   if (register_stack_overflow()) {
1773     // For those architectures which have separate register and
1774     // memory stacks, we must check the register stack to see if
1775     // it has overflowed.
1776     return false;
1777   }
1778 
1779   // Java code never executes within the yellow zone: the latter is only
1780   // there to provoke an exception during stack banging.  If java code
1781   // is executing there, either StackShadowPages should be larger, or
1782   // some exception code in c1, c2 or the interpreter isn't unwinding
1783   // when it should.
1784   guarantee(cur_sp > stack_reserved_zone_base(),
1785             "not enough space to reguard - increase StackShadowPages");


2390           if (!StressCompiledExceptionHandlers && compiled_frame.can_be_deoptimized()) {
2391             Deoptimization::deoptimize(this, compiled_frame, &reg_map);
2392           }
2393         }
2394       }
2395 
2396       // Set async. pending exception in thread.
2397       set_pending_async_exception(java_throwable);
2398 
2399       if (log_is_enabled(Info, exceptions)) {
2400          ResourceMark rm;
2401         log_info(exceptions)("Pending Async. exception installed of type: %s",
2402                              InstanceKlass::cast(_pending_async_exception->klass())->external_name());
2403       }
2404       // for AbortVMOnException flag
2405       Exceptions::debug_check_abort(_pending_async_exception->klass()->external_name());
2406     }
2407   }
2408 
2409 
2410   // Interrupt thread so it will wake up from a potential wait()/sleep()/park()
2411   this->interrupt();
2412 }
2413 
2414 // External suspension mechanism.
2415 //
2416 // Tell the VM to suspend a thread when ever it knows that it does not hold on
2417 // to any VM_locks and it is at a transition
2418 // Self-suspension will happen on the transition out of the vm.
2419 // Catch "this" coming in from JNIEnv pointers when the thread has been freed
2420 //
2421 // Guarantees on return:
2422 //   + Target thread will not execute any new bytecode (that's why we need to
2423 //     force a safepoint)
2424 //   + Target thread will not enter any new monitors
2425 //
2426 void JavaThread::java_suspend() {
2427   ThreadsListHandle tlh;
2428   if (!tlh.includes(this) || threadObj() == NULL || is_exiting()) {
2429     return;
2430   }
2431 


3381 // java.lang.Thread.sleep support
3382 // Returns true if sleep time elapsed as expected, and false
3383 // if the thread was interrupted.
3384 bool JavaThread::sleep(jlong millis) {
3385   assert(this == Thread::current(),  "thread consistency check");
3386 
3387   ParkEvent * const slp = this->_SleepEvent;
3388   // Because there can be races with thread interruption sending an unpark()
3389   // to the event, we explicitly reset it here to avoid an immediate return.
3390   // The actual interrupt state will be checked before we park().
3391   slp->reset();
3392   // Thread interruption establishes a happens-before ordering in the
3393   // Java Memory Model, so we need to ensure we synchronize with the
3394   // interrupt state.
3395   OrderAccess::fence();
3396 
3397   jlong prevtime = os::javaTimeNanos();
3398 
3399   for (;;) {
3400     // interruption has precedence over timing out
3401     if (this->is_interrupted(true)) {
3402       return false;
3403     }
3404 
3405     if (millis <= 0) {
3406       return true;
3407     }
3408 
3409     {
3410       ThreadBlockInVM tbivm(this);
3411       OSThreadWaitState osts(this->osthread(), false /* not Object.wait() */);
3412 
3413       this->set_suspend_equivalent();
3414       // cleared by handle_special_suspend_equivalent_condition() or
3415       // java_suspend_self() via check_and_wait_while_suspended()
3416 
3417       slp->park(millis);
3418 
3419       // were we externally suspended while we were waiting?
3420       this->check_and_wait_while_suspended();
3421     }


< prev index next >