src/hotspot/share/runtime/thread.cpp
branchstuefe-statistical-history
changeset 57335 e90b4960bcd6
parent 57244 a535e674d50d
parent 54495 941db9c0b5b5
equal deleted inserted replaced
57246:75f6a833a444 57335:e90b4960bcd6
   238   set_active_handles(NULL);
   238   set_active_handles(NULL);
   239   set_free_handle_block(NULL);
   239   set_free_handle_block(NULL);
   240   set_last_handle_mark(NULL);
   240   set_last_handle_mark(NULL);
   241   DEBUG_ONLY(_missed_ic_stub_refill_verifier = NULL);
   241   DEBUG_ONLY(_missed_ic_stub_refill_verifier = NULL);
   242 
   242 
   243   // This initial value ==> never claimed.
   243   // Initial value of zero ==> never claimed.
   244   _oops_do_parity = 0;
   244   _threads_do_token = 0;
   245   _threads_hazard_ptr = NULL;
   245   _threads_hazard_ptr = NULL;
   246   _threads_list_ptr = NULL;
   246   _threads_list_ptr = NULL;
   247   _nested_threads_hazard_ptr_cnt = 0;
   247   _nested_threads_hazard_ptr_cnt = 0;
   248   _rcu_counter = 0;
   248   _rcu_counter = 0;
   249 
   249 
   885   return os::is_interrupted(thread, clear_interrupted);
   885   return os::is_interrupted(thread, clear_interrupted);
   886 }
   886 }
   887 
   887 
   888 
   888 
   889 // GC Support
   889 // GC Support
   890 bool Thread::claim_oops_do_par_case(int strong_roots_parity) {
   890 bool Thread::claim_par_threads_do(uintx claim_token) {
   891   int thread_parity = _oops_do_parity;
   891   uintx token = _threads_do_token;
   892   if (thread_parity != strong_roots_parity) {
   892   if (token != claim_token) {
   893     jint res = Atomic::cmpxchg(strong_roots_parity, &_oops_do_parity, thread_parity);
   893     uintx res = Atomic::cmpxchg(claim_token, &_threads_do_token, token);
   894     if (res == thread_parity) {
   894     if (res == token) {
   895       return true;
   895       return true;
   896     } else {
   896     }
   897       guarantee(res == strong_roots_parity, "Or else what?");
   897     guarantee(res == claim_token, "invariant");
   898       return false;
       
   899     }
       
   900   }
   898   }
   901   return false;
   899   return false;
   902 }
   900 }
   903 
   901 
   904 void Thread::oops_do(OopClosure* f, CodeBlobClosure* cf) {
   902 void Thread::oops_do(OopClosure* f, CodeBlobClosure* cf) {
  1291 }
  1289 }
  1292 
  1290 
  1293 NonJavaThread::~NonJavaThread() { }
  1291 NonJavaThread::~NonJavaThread() { }
  1294 
  1292 
  1295 void NonJavaThread::add_to_the_list() {
  1293 void NonJavaThread::add_to_the_list() {
  1296   MutexLockerEx lock(NonJavaThreadsList_lock, Mutex::_no_safepoint_check_flag);
  1294   MutexLockerEx ml(NonJavaThreadsList_lock, Mutex::_no_safepoint_check_flag);
  1297   _next = _the_list._head;
  1295   // Initialize BarrierSet-related data before adding to list.
       
  1296   BarrierSet::barrier_set()->on_thread_attach(this);
       
  1297   OrderAccess::release_store(&_next, _the_list._head);
  1298   OrderAccess::release_store(&_the_list._head, this);
  1298   OrderAccess::release_store(&_the_list._head, this);
  1299 }
  1299 }
  1300 
  1300 
  1301 void NonJavaThread::remove_from_the_list() {
  1301 void NonJavaThread::remove_from_the_list() {
  1302   MutexLockerEx lock(NonJavaThreadsList_lock, Mutex::_no_safepoint_check_flag);
  1302   {
  1303   NonJavaThread* volatile* p = &_the_list._head;
  1303     MutexLockerEx ml(NonJavaThreadsList_lock, Mutex::_no_safepoint_check_flag);
  1304   for (NonJavaThread* t = *p; t != NULL; p = &t->_next, t = *p) {
  1304     // Cleanup BarrierSet-related data before removing from list.
  1305     if (t == this) {
  1305     BarrierSet::barrier_set()->on_thread_detach(this);
  1306       *p = this->_next;
  1306     NonJavaThread* volatile* p = &_the_list._head;
  1307       // Wait for any in-progress iterators.  Concurrent synchronize is
  1307     for (NonJavaThread* t = *p; t != NULL; p = &t->_next, t = *p) {
  1308       // not allowed, so do it while holding the list lock.
  1308       if (t == this) {
  1309       _the_list._protect.synchronize();
  1309         *p = _next;
  1310       break;
  1310         break;
  1311     }
  1311       }
  1312   }
  1312     }
       
  1313   }
       
  1314   // Wait for any in-progress iterators.  Concurrent synchronize is not
       
  1315   // allowed, so do it while holding a dedicated lock.  Outside and distinct
       
  1316   // from NJTList_lock in case an iteration attempts to lock it.
       
  1317   MutexLockerEx ml(NonJavaThreadsListSync_lock, Mutex::_no_safepoint_check_flag);
       
  1318   _the_list._protect.synchronize();
       
  1319   _next = NULL;                 // Safe to drop the link now.
  1313 }
  1320 }
  1314 
  1321 
  1315 void NonJavaThread::pre_run() {
  1322 void NonJavaThread::pre_run() {
  1316   // Initialize BarrierSet-related data before adding to list.
       
  1317   assert(BarrierSet::barrier_set() != NULL, "invariant");
       
  1318   BarrierSet::barrier_set()->on_thread_attach(this);
       
  1319   add_to_the_list();
  1323   add_to_the_list();
  1320 
  1324 
  1321   // This is slightly odd in that NamedThread is a subclass, but
  1325   // This is slightly odd in that NamedThread is a subclass, but
  1322   // in fact name() is defined in Thread
  1326   // in fact name() is defined in Thread
  1323   assert(this->name() != NULL, "thread name was not set before it was started");
  1327   assert(this->name() != NULL, "thread name was not set before it was started");
  1324   this->set_native_thread_name(this->name());
  1328   this->set_native_thread_name(this->name());
  1325 }
  1329 }
  1326 
  1330 
  1327 void NonJavaThread::post_run() {
  1331 void NonJavaThread::post_run() {
  1328   JFR_ONLY(Jfr::on_thread_exit(this);)
  1332   JFR_ONLY(Jfr::on_thread_exit(this);)
  1329   // Clean up BarrierSet data before removing from list.
       
  1330   BarrierSet::barrier_set()->on_thread_detach(this);
       
  1331   remove_from_the_list();
  1333   remove_from_the_list();
  1332   // Ensure thread-local-storage is cleared before termination.
  1334   // Ensure thread-local-storage is cleared before termination.
  1333   Thread::clear_thread_current();
  1335   Thread::clear_thread_current();
  1334 }
  1336 }
  1335 
  1337 
  1506 
  1508 
  1507   // Signal that it is terminated
  1509   // Signal that it is terminated
  1508   {
  1510   {
  1509     MutexLockerEx mu(Terminator_lock, Mutex::_no_safepoint_check_flag);
  1511     MutexLockerEx mu(Terminator_lock, Mutex::_no_safepoint_check_flag);
  1510     _watcher_thread = NULL;
  1512     _watcher_thread = NULL;
  1511     Terminator_lock->notify();
  1513     Terminator_lock->notify_all();
  1512   }
  1514   }
  1513 }
  1515 }
  1514 
  1516 
  1515 void WatcherThread::start() {
  1517 void WatcherThread::start() {
  1516   assert(PeriodicTask_lock->owned_by_self(), "PeriodicTask_lock required");
  1518   assert(PeriodicTask_lock->owned_by_self(), "PeriodicTask_lock required");
  1835 
  1837 
  1836   this->cache_global_variables();
  1838   this->cache_global_variables();
  1837 
  1839 
  1838   // Thread is now sufficiently initialized to be handled by the safepoint code as being
  1840   // Thread is now sufficiently initialized to be handled by the safepoint code as being
  1839   // in the VM. Change thread state from _thread_new to _thread_in_vm
  1841   // in the VM. Change thread state from _thread_new to _thread_in_vm
  1840   ThreadStateTransition::transition_and_fence(this, _thread_new, _thread_in_vm);
  1842   ThreadStateTransition::transition(this, _thread_new, _thread_in_vm);
       
  1843   // Before a thread is on the threads list it is always safe, so after leaving the
       
  1844   // _thread_new we should emit a instruction barrier. The distance to modified code
       
  1845   // from here is probably far enough, but this is consistent and safe.
       
  1846   OrderAccess::cross_modify_fence();
  1841 
  1847 
  1842   assert(JavaThread::current() == this, "sanity check");
  1848   assert(JavaThread::current() == this, "sanity check");
  1843   assert(!Thread::current()->owns_locks(), "sanity check");
  1849   assert(!Thread::current()->owns_locks(), "sanity check");
  1844 
  1850 
  1845   DTRACE_THREAD_PROBE(start, this);
  1851   DTRACE_THREAD_PROBE(start, this);
  2015 
  2021 
  2016   if (log_is_enabled(Debug, os, thread, timer)) {
  2022   if (log_is_enabled(Debug, os, thread, timer)) {
  2017     _timer_exit_phase1.stop();
  2023     _timer_exit_phase1.stop();
  2018     _timer_exit_phase2.start();
  2024     _timer_exit_phase2.start();
  2019   }
  2025   }
       
  2026 
       
  2027   // Capture daemon status before the thread is marked as terminated.
       
  2028   bool daemon = is_daemon(threadObj());
       
  2029 
  2020   // Notify waiters on thread object. This has to be done after exit() is called
  2030   // Notify waiters on thread object. This has to be done after exit() is called
  2021   // on the thread (if the thread is the last thread in a daemon ThreadGroup the
  2031   // on the thread (if the thread is the last thread in a daemon ThreadGroup the
  2022   // group should have the destroyed bit set before waiters are notified).
  2032   // group should have the destroyed bit set before waiters are notified).
  2023   ensure_join(this);
  2033   ensure_join(this);
  2024   assert(!this->has_pending_exception(), "ensure_join should have cleared");
  2034   assert(!this->has_pending_exception(), "ensure_join should have cleared");
  2083   if (log_is_enabled(Debug, os, thread, timer)) {
  2093   if (log_is_enabled(Debug, os, thread, timer)) {
  2084     _timer_exit_phase3.stop();
  2094     _timer_exit_phase3.stop();
  2085     _timer_exit_phase4.start();
  2095     _timer_exit_phase4.start();
  2086   }
  2096   }
  2087   // Remove from list of active threads list, and notify VM thread if we are the last non-daemon thread
  2097   // Remove from list of active threads list, and notify VM thread if we are the last non-daemon thread
  2088   Threads::remove(this);
  2098   Threads::remove(this, daemon);
  2089 
  2099 
  2090   if (log_is_enabled(Debug, os, thread, timer)) {
  2100   if (log_is_enabled(Debug, os, thread, timer)) {
  2091     _timer_exit_phase4.stop();
  2101     _timer_exit_phase4.stop();
  2092     ResourceMark rm(this);
  2102     ResourceMark rm(this);
  2093     log_debug(os, thread, timer)("name='%s'"
  2103     log_debug(os, thread, timer)("name='%s'"
  2101                                  _timer_exit_phase3.milliseconds(),
  2111                                  _timer_exit_phase3.milliseconds(),
  2102                                  _timer_exit_phase4.milliseconds());
  2112                                  _timer_exit_phase4.milliseconds());
  2103   }
  2113   }
  2104 }
  2114 }
  2105 
  2115 
  2106 void JavaThread::cleanup_failed_attach_current_thread() {
  2116 void JavaThread::cleanup_failed_attach_current_thread(bool is_daemon) {
  2107   if (active_handles() != NULL) {
  2117   if (active_handles() != NULL) {
  2108     JNIHandleBlock* block = active_handles();
  2118     JNIHandleBlock* block = active_handles();
  2109     set_active_handles(NULL);
  2119     set_active_handles(NULL);
  2110     JNIHandleBlock::release_block(block);
  2120     JNIHandleBlock::release_block(block);
  2111   }
  2121   }
  2123     tlab().retire();
  2133     tlab().retire();
  2124   }
  2134   }
  2125 
  2135 
  2126   BarrierSet::barrier_set()->on_thread_detach(this);
  2136   BarrierSet::barrier_set()->on_thread_detach(this);
  2127 
  2137 
  2128   Threads::remove(this);
  2138   Threads::remove(this, is_daemon);
  2129   this->smr_delete();
  2139   this->smr_delete();
  2130 }
  2140 }
  2131 
  2141 
  2132 JavaThread* JavaThread::active() {
  2142 JavaThread* JavaThread::active() {
  2133   Thread* thread = Thread::current();
  2143   Thread* thread = Thread::current();
  2264          "must have handled the async condition, if no exception");
  2274          "must have handled the async condition, if no exception");
  2265 }
  2275 }
  2266 
  2276 
  2267 void JavaThread::handle_special_runtime_exit_condition(bool check_asyncs) {
  2277 void JavaThread::handle_special_runtime_exit_condition(bool check_asyncs) {
  2268   //
  2278   //
  2269   // Check for pending external suspend. Internal suspend requests do
  2279   // Check for pending external suspend.
  2270   // not use handle_special_runtime_exit_condition().
       
  2271   // If JNIEnv proxies are allowed, don't self-suspend if the target
  2280   // If JNIEnv proxies are allowed, don't self-suspend if the target
  2272   // thread is not the current thread. In older versions of jdbx, jdbx
  2281   // thread is not the current thread. In older versions of jdbx, jdbx
  2273   // threads could call into the VM with another thread's JNIEnv so we
  2282   // threads could call into the VM with another thread's JNIEnv so we
  2274   // can be here operating on behalf of a suspended thread (4432884).
  2283   // can be here operating on behalf of a suspended thread (4432884).
  2275   bool do_self_suspend = is_external_suspend_with_lock();
  2284   bool do_self_suspend = is_external_suspend_with_lock();
  2276   if (do_self_suspend && (!AllowJNIEnvProxy || this == JavaThread::current())) {
  2285   if (do_self_suspend && (!AllowJNIEnvProxy || this == JavaThread::current())) {
  2277     //
       
  2278     // Because thread is external suspended the safepoint code will count
       
  2279     // thread as at a safepoint. This can be odd because we can be here
       
  2280     // as _thread_in_Java which would normally transition to _thread_blocked
       
  2281     // at a safepoint. We would like to mark the thread as _thread_blocked
       
  2282     // before calling java_suspend_self like all other callers of it but
       
  2283     // we must then observe proper safepoint protocol. (We can't leave
       
  2284     // _thread_blocked with a safepoint in progress). However we can be
       
  2285     // here as _thread_in_native_trans so we can't use a normal transition
       
  2286     // constructor/destructor pair because they assert on that type of
       
  2287     // transition. We could do something like:
       
  2288     //
       
  2289     // JavaThreadState state = thread_state();
       
  2290     // set_thread_state(_thread_in_vm);
       
  2291     // {
       
  2292     //   ThreadBlockInVM tbivm(this);
       
  2293     //   java_suspend_self()
       
  2294     // }
       
  2295     // set_thread_state(_thread_in_vm_trans);
       
  2296     // if (safepoint) block;
       
  2297     // set_thread_state(state);
       
  2298     //
       
  2299     // but that is pretty messy. Instead we just go with the way the
       
  2300     // code has worked before and note that this is the only path to
       
  2301     // java_suspend_self that doesn't put the thread in _thread_blocked
       
  2302     // mode.
       
  2303 
       
  2304     frame_anchor()->make_walkable(this);
  2286     frame_anchor()->make_walkable(this);
  2305     java_suspend_self();
  2287     java_suspend_self_with_safepoint_check();
  2306 
  2288   }
  2307     // We might be here for reasons in addition to the self-suspend request
  2289 
  2308     // so check for other async requests.
  2290   // We might be here for reasons in addition to the self-suspend request
  2309   }
  2291   // so check for other async requests.
  2310 
       
  2311   if (check_asyncs) {
  2292   if (check_asyncs) {
  2312     check_and_handle_async_exceptions();
  2293     check_and_handle_async_exceptions();
  2313   }
  2294   }
  2314 
  2295 
  2315   JFR_ONLY(SUSPEND_THREAD_CONDITIONAL(this);)
  2296   JFR_ONLY(SUSPEND_THREAD_CONDITIONAL(this);)
  2424 //       thread. java_suspend_self() is the second stage of cooperative
  2405 //       thread. java_suspend_self() is the second stage of cooperative
  2425 //       suspension for external suspend requests and should only be used
  2406 //       suspension for external suspend requests and should only be used
  2426 //       to complete an external suspend request.
  2407 //       to complete an external suspend request.
  2427 //
  2408 //
  2428 int JavaThread::java_suspend_self() {
  2409 int JavaThread::java_suspend_self() {
       
  2410   assert(thread_state() == _thread_blocked, "wrong state for java_suspend_self()");
  2429   int ret = 0;
  2411   int ret = 0;
  2430 
  2412 
  2431   // we are in the process of exiting so don't suspend
  2413   // we are in the process of exiting so don't suspend
  2432   if (is_exiting()) {
  2414   if (is_exiting()) {
  2433     clear_external_suspend();
  2415     clear_external_suspend();
  2467     // _ext_suspended flag is cleared by java_resume()
  2449     // _ext_suspended flag is cleared by java_resume()
  2468     while (is_ext_suspended()) {
  2450     while (is_ext_suspended()) {
  2469       this->SR_lock()->wait(Mutex::_no_safepoint_check_flag);
  2451       this->SR_lock()->wait(Mutex::_no_safepoint_check_flag);
  2470     }
  2452     }
  2471   }
  2453   }
  2472 
       
  2473   return ret;
  2454   return ret;
       
  2455 }
       
  2456 
       
  2457 // Helper routine to set up the correct thread state before calling java_suspend_self.
       
  2458 // This is called when regular thread-state transition helpers can't be used because
       
  2459 // we can be in various states, in particular _thread_in_native_trans.
       
  2460 // Because this thread is external suspended the safepoint code will count it as at
       
  2461 // a safepoint, regardless of what its actual current thread-state is. But
       
  2462 // is_ext_suspend_completed() may be waiting to see a thread transition from
       
  2463 // _thread_in_native_trans to _thread_blocked. So we set the thread state directly
       
  2464 // to _thread_blocked. The problem with setting thread state directly is that a
       
  2465 // safepoint could happen just after java_suspend_self() returns after being resumed,
       
  2466 // and the VM thread will see the _thread_blocked state. So we must check for a safepoint
       
  2467 // after restoring the state to make sure we won't leave while a safepoint is in progress.
       
  2468 // However, not all initial-states are allowed when performing a safepoint check, as we
       
  2469 // should never be blocking at a safepoint whilst in those states. Of these 'bad' states
       
  2470 // only _thread_in_native is possible when executing this code (based on our two callers).
       
  2471 // A thread that is _thread_in_native is already safepoint-safe and so it doesn't matter
       
  2472 // whether the VMThread sees the _thread_blocked state, or the _thread_in_native state,
       
  2473 // and so we don't need the explicit safepoint check.
       
  2474 
       
  2475 void JavaThread::java_suspend_self_with_safepoint_check() {
       
  2476   assert(this == Thread::current(), "invariant");
       
  2477   JavaThreadState state = thread_state();
       
  2478   set_thread_state(_thread_blocked);
       
  2479   java_suspend_self();
       
  2480   set_thread_state_fence(state);
       
  2481   // Since we are not using a regular thread-state transition helper here,
       
  2482   // we must manually emit the instruction barrier after leaving a safe state.
       
  2483   OrderAccess::cross_modify_fence();
       
  2484   if (state != _thread_in_native) {
       
  2485     SafepointMechanism::block_if_requested(this);
       
  2486   }
  2474 }
  2487 }
  2475 
  2488 
  2476 #ifdef ASSERT
  2489 #ifdef ASSERT
  2477 // Verify the JavaThread has not yet been published in the Threads::list, and
  2490 // Verify the JavaThread has not yet been published in the Threads::list, and
  2478 // hence doesn't need protection from concurrent access at this stage.
  2491 // hence doesn't need protection from concurrent access at this stage.
  2502   // If JNIEnv proxies are allowed, don't self-suspend if the target
  2515   // If JNIEnv proxies are allowed, don't self-suspend if the target
  2503   // thread is not the current thread. In older versions of jdbx, jdbx
  2516   // thread is not the current thread. In older versions of jdbx, jdbx
  2504   // threads could call into the VM with another thread's JNIEnv so we
  2517   // threads could call into the VM with another thread's JNIEnv so we
  2505   // can be here operating on behalf of a suspended thread (4432884).
  2518   // can be here operating on behalf of a suspended thread (4432884).
  2506   if (do_self_suspend && (!AllowJNIEnvProxy || curJT == thread)) {
  2519   if (do_self_suspend && (!AllowJNIEnvProxy || curJT == thread)) {
  2507     JavaThreadState state = thread->thread_state();
  2520     thread->java_suspend_self_with_safepoint_check();
  2508 
  2521   } else {
  2509     // We mark this thread_blocked state as a suspend-equivalent so
  2522     SafepointMechanism::block_if_requested(curJT);
  2510     // that a caller to is_ext_suspend_completed() won't be confused.
  2523   }
  2511     // The suspend-equivalent state is cleared by java_suspend_self().
       
  2512     thread->set_suspend_equivalent();
       
  2513 
       
  2514     // If the safepoint code sees the _thread_in_native_trans state, it will
       
  2515     // wait until the thread changes to other thread state. There is no
       
  2516     // guarantee on how soon we can obtain the SR_lock and complete the
       
  2517     // self-suspend request. It would be a bad idea to let safepoint wait for
       
  2518     // too long. Temporarily change the state to _thread_blocked to
       
  2519     // let the VM thread know that this thread is ready for GC. The problem
       
  2520     // of changing thread state is that safepoint could happen just after
       
  2521     // java_suspend_self() returns after being resumed, and VM thread will
       
  2522     // see the _thread_blocked state. We must check for safepoint
       
  2523     // after restoring the state and make sure we won't leave while a safepoint
       
  2524     // is in progress.
       
  2525     thread->set_thread_state(_thread_blocked);
       
  2526     thread->java_suspend_self();
       
  2527     thread->set_thread_state(state);
       
  2528 
       
  2529     InterfaceSupport::serialize_thread_state_with_handler(thread);
       
  2530   }
       
  2531 
       
  2532   SafepointMechanism::block_if_requested(curJT);
       
  2533 
  2524 
  2534   if (thread->is_deopt_suspend()) {
  2525   if (thread->is_deopt_suspend()) {
  2535     thread->clear_deopt_suspend();
  2526     thread->clear_deopt_suspend();
  2536     RegisterMap map(thread, false);
  2527     RegisterMap map(thread, false);
  2537     frame f = thread->last_frame();
  2528     frame f = thread->last_frame();
  2948   if (jvmti_thread_state() != NULL) {
  2939   if (jvmti_thread_state() != NULL) {
  2949     jvmti_thread_state()->oops_do(f);
  2940     jvmti_thread_state()->oops_do(f);
  2950   }
  2941   }
  2951 }
  2942 }
  2952 
  2943 
       
  2944 #ifdef ASSERT
       
  2945 void JavaThread::verify_states_for_handshake() {
       
  2946   // This checks that the thread has a correct frame state during a handshake.
       
  2947   assert((!has_last_Java_frame() && java_call_counter() == 0) ||
       
  2948          (has_last_Java_frame() && java_call_counter() > 0),
       
  2949          "unexpected frame info: has_last_frame=%d, java_call_counter=%d",
       
  2950          has_last_Java_frame(), java_call_counter());
       
  2951 }
       
  2952 #endif
       
  2953 
  2953 void JavaThread::nmethods_do(CodeBlobClosure* cf) {
  2954 void JavaThread::nmethods_do(CodeBlobClosure* cf) {
  2954   assert((!has_last_Java_frame() && java_call_counter() == 0) ||
  2955   assert((!has_last_Java_frame() && java_call_counter() == 0) ||
  2955          (has_last_Java_frame() && java_call_counter() > 0), "wrong java_sp info!");
  2956          (has_last_Java_frame() && java_call_counter() > 0),
       
  2957          "unexpected frame info: has_last_frame=%d, java_call_counter=%d",
       
  2958          has_last_Java_frame(), java_call_counter());
  2956 
  2959 
  2957   if (has_last_Java_frame()) {
  2960   if (has_last_Java_frame()) {
  2958     // Traverse the execution stack
  2961     // Traverse the execution stack
  2959     for (StackFrameStream fst(this); !fst.is_done(); fst.next()) {
  2962     for (StackFrameStream fst(this); !fst.is_done(); fst.next()) {
  2960       fst.current()->nmethods_do(cf);
  2963       fst.current()->nmethods_do(cf);
  2961     }
  2964     }
  2962   }
  2965   }
  2963 }
  2966 }
  2964 
  2967 
  2965 void JavaThread::metadata_do(void f(Metadata*)) {
  2968 void JavaThread::metadata_do(MetadataClosure* f) {
  2966   if (has_last_Java_frame()) {
  2969   if (has_last_Java_frame()) {
  2967     // Traverse the execution stack to call f() on the methods in the stack
  2970     // Traverse the execution stack to call f() on the methods in the stack
  2968     for (StackFrameStream fst(this); !fst.is_done(); fst.next()) {
  2971     for (StackFrameStream fst(this); !fst.is_done(); fst.next()) {
  2969       fst.current()->metadata_do(f);
  2972       fst.current()->metadata_do(f);
  2970     }
  2973     }
  3470 
  3473 
  3471 JavaThread* Threads::_thread_list = NULL;
  3474 JavaThread* Threads::_thread_list = NULL;
  3472 int         Threads::_number_of_threads = 0;
  3475 int         Threads::_number_of_threads = 0;
  3473 int         Threads::_number_of_non_daemon_threads = 0;
  3476 int         Threads::_number_of_non_daemon_threads = 0;
  3474 int         Threads::_return_code = 0;
  3477 int         Threads::_return_code = 0;
  3475 int         Threads::_thread_claim_parity = 0;
  3478 uintx       Threads::_thread_claim_token = 1; // Never zero.
  3476 size_t      JavaThread::_stack_size_at_create = 0;
  3479 size_t      JavaThread::_stack_size_at_create = 0;
  3477 
  3480 
  3478 #ifdef ASSERT
  3481 #ifdef ASSERT
  3479 bool        Threads::_vm_complete = false;
  3482 bool        Threads::_vm_complete = false;
  3480 #endif
  3483 #endif
  3530   java_threads_do(tc);
  3533   java_threads_do(tc);
  3531   non_java_threads_do(tc);
  3534   non_java_threads_do(tc);
  3532 }
  3535 }
  3533 
  3536 
  3534 void Threads::possibly_parallel_threads_do(bool is_par, ThreadClosure* tc) {
  3537 void Threads::possibly_parallel_threads_do(bool is_par, ThreadClosure* tc) {
  3535   int cp = Threads::thread_claim_parity();
  3538   uintx claim_token = Threads::thread_claim_token();
  3536   ALL_JAVA_THREADS(p) {
  3539   ALL_JAVA_THREADS(p) {
  3537     if (p->claim_oops_do(is_par, cp)) {
  3540     if (p->claim_threads_do(is_par, claim_token)) {
  3538       tc->do_thread(p);
  3541       tc->do_thread(p);
  3539     }
  3542     }
  3540   }
  3543   }
  3541   VMThread* vmt = VMThread::vm_thread();
  3544   VMThread* vmt = VMThread::vm_thread();
  3542   if (vmt->claim_oops_do(is_par, cp)) {
  3545   if (vmt->claim_threads_do(is_par, claim_token)) {
  3543     tc->do_thread(vmt);
  3546     tc->do_thread(vmt);
  3544   }
  3547   }
  3545 }
  3548 }
  3546 
  3549 
  3547 // The system initialization in the library has three phases.
  3550 // The system initialization in the library has three phases.
  3624   Handle thread_group = create_initial_thread_group(CHECK);
  3627   Handle thread_group = create_initial_thread_group(CHECK);
  3625   Universe::set_main_thread_group(thread_group());
  3628   Universe::set_main_thread_group(thread_group());
  3626   initialize_class(vmSymbols::java_lang_Thread(), CHECK);
  3629   initialize_class(vmSymbols::java_lang_Thread(), CHECK);
  3627   oop thread_object = create_initial_thread(thread_group, main_thread, CHECK);
  3630   oop thread_object = create_initial_thread(thread_group, main_thread, CHECK);
  3628   main_thread->set_threadObj(thread_object);
  3631   main_thread->set_threadObj(thread_object);
       
  3632 
  3629   // Set thread status to running since main thread has
  3633   // Set thread status to running since main thread has
  3630   // been started and running.
  3634   // been started and running.
  3631   java_lang_Thread::set_thread_status(thread_object,
  3635   java_lang_Thread::set_thread_status(thread_object,
  3632                                       java_lang_Thread::RUNNABLE);
  3636                                       java_lang_Thread::RUNNABLE);
  3633 
  3637 
  3634   // The VM creates objects of this class.
  3638   // The VM creates objects of this class.
  3635   initialize_class(vmSymbols::java_lang_Module(), CHECK);
  3639   initialize_class(vmSymbols::java_lang_Module(), CHECK);
       
  3640 
       
  3641 #ifdef ASSERT
       
  3642   InstanceKlass *k = SystemDictionary::UnsafeConstants_klass();
       
  3643   assert(k->is_not_initialized(), "UnsafeConstants should not already be initialized");
       
  3644 #endif
       
  3645 
       
  3646   // initialize the hardware-specific constants needed by Unsafe
       
  3647   initialize_class(vmSymbols::jdk_internal_misc_UnsafeConstants(), CHECK);
       
  3648   jdk_internal_misc_UnsafeConstants::set_unsafe_constants();
  3636 
  3649 
  3637   // The VM preresolves methods to these classes. Make sure that they get initialized
  3650   // The VM preresolves methods to these classes. Make sure that they get initialized
  3638   initialize_class(vmSymbols::java_lang_reflect_Method(), CHECK);
  3651   initialize_class(vmSymbols::java_lang_reflect_Method(), CHECK);
  3639   initialize_class(vmSymbols::java_lang_ref_Finalizer(), CHECK);
  3652   initialize_class(vmSymbols::java_lang_ref_Finalizer(), CHECK);
  3640 
  3653 
  4464 
  4477 
  4465   // Possible GC point.
  4478   // Possible GC point.
  4466   Events::log(p, "Thread added: " INTPTR_FORMAT, p2i(p));
  4479   Events::log(p, "Thread added: " INTPTR_FORMAT, p2i(p));
  4467 }
  4480 }
  4468 
  4481 
  4469 void Threads::remove(JavaThread* p) {
  4482 void Threads::remove(JavaThread* p, bool is_daemon) {
  4470 
  4483 
  4471   // Reclaim the objectmonitors from the omInUseList and omFreeList of the moribund thread.
  4484   // Reclaim the ObjectMonitors from the omInUseList and omFreeList of the moribund thread.
  4472   ObjectSynchronizer::omFlush(p);
  4485   ObjectSynchronizer::omFlush(p);
  4473 
  4486 
  4474   // Extra scope needed for Thread_lock, so we can check
  4487   // Extra scope needed for Thread_lock, so we can check
  4475   // that we do not remove thread without safepoint code notice
  4488   // that we do not remove thread without safepoint code notice
  4476   { MutexLocker ml(Threads_lock);
  4489   { MutexLocker ml(Threads_lock);
  4493     } else {
  4506     } else {
  4494       _thread_list = p->next();
  4507       _thread_list = p->next();
  4495     }
  4508     }
  4496 
  4509 
  4497     _number_of_threads--;
  4510     _number_of_threads--;
  4498     oop threadObj = p->threadObj();
  4511     if (!is_daemon) {
  4499     bool daemon = true;
       
  4500     if (!is_daemon(threadObj)) {
       
  4501       _number_of_non_daemon_threads--;
  4512       _number_of_non_daemon_threads--;
  4502       daemon = false;
       
  4503 
  4513 
  4504       // Only one thread left, do a notify on the Threads_lock so a thread waiting
  4514       // Only one thread left, do a notify on the Threads_lock so a thread waiting
  4505       // on destroy_vm will wake up.
  4515       // on destroy_vm will wake up.
  4506       if (number_of_non_daemon_threads() == 1) {
  4516       if (number_of_non_daemon_threads() == 1) {
  4507         Threads_lock->notify_all();
  4517         Threads_lock->notify_all();
  4508       }
  4518       }
  4509     }
  4519     }
  4510     ThreadService::remove_thread(p, daemon);
  4520     ThreadService::remove_thread(p, is_daemon);
  4511 
  4521 
  4512     // Make sure that safepoint code disregard this thread. This is needed since
  4522     // Make sure that safepoint code disregard this thread. This is needed since
  4513     // the thread might mess around with locks after this point. This can cause it
  4523     // the thread might mess around with locks after this point. This can cause it
  4514     // to do callbacks into the safepoint code. However, the safepoint code is not aware
  4524     // to do callbacks into the safepoint code. However, the safepoint code is not aware
  4515     // of this thread since it is removed from the queue.
  4525     // of this thread since it is removed from the queue.
  4532     p->oops_do(f, cf);
  4542     p->oops_do(f, cf);
  4533   }
  4543   }
  4534   VMThread::vm_thread()->oops_do(f, cf);
  4544   VMThread::vm_thread()->oops_do(f, cf);
  4535 }
  4545 }
  4536 
  4546 
  4537 void Threads::change_thread_claim_parity() {
  4547 void Threads::change_thread_claim_token() {
  4538   // Set the new claim parity.
  4548   if (++_thread_claim_token == 0) {
  4539   assert(_thread_claim_parity >= 0 && _thread_claim_parity <= 2,
  4549     // On overflow of the token counter, there is a risk of future
  4540          "Not in range.");
  4550     // collisions between a new global token value and a stale token
  4541   _thread_claim_parity++;
  4551     // for a thread, because not all iterations visit all threads.
  4542   if (_thread_claim_parity == 3) _thread_claim_parity = 1;
  4552     // (Though it's pretty much a theoretical concern for non-trivial
  4543   assert(_thread_claim_parity >= 1 && _thread_claim_parity <= 2,
  4553     // token counter sizes.)  To deal with the possibility, reset all
  4544          "Not in range.");
  4554     // the thread tokens to zero on global token overflow.
       
  4555     struct ResetClaims : public ThreadClosure {
       
  4556       virtual void do_thread(Thread* t) {
       
  4557         t->claim_threads_do(false, 0);
       
  4558       }
       
  4559     } reset_claims;
       
  4560     Threads::threads_do(&reset_claims);
       
  4561     // On overflow, update the global token to non-zero, to
       
  4562     // avoid the special "never claimed" initial thread value.
       
  4563     _thread_claim_token = 1;
       
  4564   }
  4545 }
  4565 }
  4546 
  4566 
  4547 #ifdef ASSERT
  4567 #ifdef ASSERT
       
  4568 void assert_thread_claimed(const char* kind, Thread* t, uintx expected) {
       
  4569   const uintx token = t->threads_do_token();
       
  4570   assert(token == expected,
       
  4571          "%s " PTR_FORMAT " has incorrect value " UINTX_FORMAT " != "
       
  4572          UINTX_FORMAT, kind, p2i(t), token, expected);
       
  4573 }
       
  4574 
  4548 void Threads::assert_all_threads_claimed() {
  4575 void Threads::assert_all_threads_claimed() {
  4549   ALL_JAVA_THREADS(p) {
  4576   ALL_JAVA_THREADS(p) {
  4550     const int thread_parity = p->oops_do_parity();
  4577     assert_thread_claimed("Thread", p, _thread_claim_token);
  4551     assert((thread_parity == _thread_claim_parity),
  4578   }
  4552            "Thread " PTR_FORMAT " has incorrect parity %d != %d", p2i(p), thread_parity, _thread_claim_parity);
  4579   assert_thread_claimed("VMThread", VMThread::vm_thread(), _thread_claim_token);
  4553   }
       
  4554   VMThread* vmt = VMThread::vm_thread();
       
  4555   const int thread_parity = vmt->oops_do_parity();
       
  4556   assert((thread_parity == _thread_claim_parity),
       
  4557          "VMThread " PTR_FORMAT " has incorrect parity %d != %d", p2i(vmt), thread_parity, _thread_claim_parity);
       
  4558 }
  4580 }
  4559 #endif // ASSERT
  4581 #endif // ASSERT
  4560 
  4582 
  4561 class ParallelOopsDoThreadClosure : public ThreadClosure {
  4583 class ParallelOopsDoThreadClosure : public ThreadClosure {
  4562 private:
  4584 private:
  4583       p->nmethods_do(cf);
  4605       p->nmethods_do(cf);
  4584     }
  4606     }
  4585   }
  4607   }
  4586 }
  4608 }
  4587 
  4609 
  4588 void Threads::metadata_do(void f(Metadata*)) {
  4610 void Threads::metadata_do(MetadataClosure* f) {
  4589   ALL_JAVA_THREADS(p) {
  4611   ALL_JAVA_THREADS(p) {
  4590     p->metadata_do(f);
  4612     p->metadata_do(f);
  4591   }
  4613   }
  4592 }
  4614 }
  4593 
  4615 
  4782 
  4804 
  4783   st->print_cr("Threads with active compile tasks:");
  4805   st->print_cr("Threads with active compile tasks:");
  4784   print_threads_compiling(st, buf, buflen);
  4806   print_threads_compiling(st, buf, buflen);
  4785 }
  4807 }
  4786 
  4808 
  4787 void Threads::print_threads_compiling(outputStream* st, char* buf, int buflen) {
  4809 void Threads::print_threads_compiling(outputStream* st, char* buf, int buflen, bool short_form) {
  4788   ALL_JAVA_THREADS(thread) {
  4810   ALL_JAVA_THREADS(thread) {
  4789     if (thread->is_Compiler_thread()) {
  4811     if (thread->is_Compiler_thread()) {
  4790       CompilerThread* ct = (CompilerThread*) thread;
  4812       CompilerThread* ct = (CompilerThread*) thread;
  4791 
  4813 
  4792       // Keep task in local variable for NULL check.
  4814       // Keep task in local variable for NULL check.
  4795       // though, just returned to a free list.
  4817       // though, just returned to a free list.
  4796       CompileTask* task = ct->task();
  4818       CompileTask* task = ct->task();
  4797       if (task != NULL) {
  4819       if (task != NULL) {
  4798         thread->print_name_on_error(st, buf, buflen);
  4820         thread->print_name_on_error(st, buf, buflen);
  4799         st->print("  ");
  4821         st->print("  ");
  4800         task->print(st, NULL, true, true);
  4822         task->print(st, NULL, short_form, true);
  4801       }
  4823       }
  4802     }
  4824     }
  4803   }
  4825   }
  4804 }
  4826 }
  4805 
  4827