src/share/vm/runtime/thread.cpp
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File hotspot Sdiff src/share/vm/runtime

src/share/vm/runtime/thread.cpp

Print this page
rev 5732 : [mq]: comments2


 750       // retry loop.
 751       *bits |= 0x00080000;
 752       return false;
 753     }
 754 
 755     if (is_suspended) {
 756       *bits |= 0x00100000;
 757       return true;
 758     }
 759   } // end retry loop
 760 
 761   // thread did not suspend after all our retries
 762   *bits |= 0x00200000;
 763   return false;
 764 }
 765 
 766 #ifndef PRODUCT
 767 void JavaThread::record_jump(address target, address instr, const char* file, int line) {
 768 
 769   // This should not need to be atomic as the only way for simultaneous
 770   // updates is via interrupts. Even then this should be rare or non-existant
 771   // and we don't care that much anyway.
 772 
 773   int index = _jmp_ring_index;
 774   _jmp_ring_index = (index + 1 ) & (jump_ring_buffer_size - 1);
 775   _jmp_ring[index]._target = (intptr_t) target;
 776   _jmp_ring[index]._instruction = (intptr_t) instr;
 777   _jmp_ring[index]._file = file;
 778   _jmp_ring[index]._line = line;
 779 }
 780 #endif /* PRODUCT */
 781 
 782 // Called by flat profiler
 783 // Callers have already called wait_for_ext_suspend_completion
 784 // The assertion for that is currently too complex to put here:
 785 bool JavaThread::profile_last_Java_frame(frame* _fr) {
 786   bool gotframe = false;
 787   // self suspension saves needed state.
 788   if (has_last_Java_frame() && _anchor.walkable()) {
 789      *_fr = pd_last_frame();
 790      gotframe = true;


 908 // The flag: potential_vm_operation notifies if this particular safepoint state could potential
 909 // invoke the vm-thread (i.e., and oop allocation). In that case, we also have to make sure that
 910 // no threads which allow_vm_block's are held
 911 void Thread::check_for_valid_safepoint_state(bool potential_vm_operation) {
 912     // Check if current thread is allowed to block at a safepoint
 913     if (!(_allow_safepoint_count == 0))
 914       fatal("Possible safepoint reached by thread that does not allow it");
 915     if (is_Java_thread() && ((JavaThread*)this)->thread_state() != _thread_in_vm) {
 916       fatal("LEAF method calling lock?");
 917     }
 918 
 919 #ifdef ASSERT
 920     if (potential_vm_operation && is_Java_thread()
 921         && !Universe::is_bootstrapping()) {
 922       // Make sure we do not hold any locks that the VM thread also uses.
 923       // This could potentially lead to deadlocks
 924       for(Monitor *cur = _owned_locks; cur; cur = cur->next()) {
 925         // Threads_lock is special, since the safepoint synchronization will not start before this is
 926         // acquired. Hence, a JavaThread cannot be holding it at a safepoint. So is VMOperationRequest_lock,
 927         // since it is used to transfer control between JavaThreads and the VMThread
 928         // Do not *exclude* any locks unless you are absolutly sure it is correct. Ask someone else first!
 929         if ( (cur->allow_vm_block() &&
 930               cur != Threads_lock &&
 931               cur != Compile_lock &&               // Temporary: should not be necessary when we get spearate compilation
 932               cur != VMOperationRequest_lock &&
 933               cur != VMOperationQueue_lock) ||
 934               cur->rank() == Mutex::special) {
 935           warning("Thread holding lock at safepoint that vm can block on: %s", cur->name());
 936         }
 937       }
 938     }
 939 
 940     if (GCALotAtAllSafepoints) {
 941       // We could enter a safepoint here and thus have a gc
 942       InterfaceSupport::check_gc_alot();
 943     }
 944 #endif
 945 }
 946 #endif
 947 
 948 bool Thread::is_in_stack(address adr) const {
 949   assert(Thread::current() == this, "is_in_stack can only be called from current thread");
 950   address end = os::current_stack_pointer();
 951   // Allow non Java threads to call this without stack_base


1254   // enrolled
1255   int remaining = PeriodicTask::time_to_wait();
1256   int time_slept = 0;
1257 
1258   // we expect this to timeout - we only ever get unparked when
1259   // we should terminate or when a new task has been enrolled
1260   OSThreadWaitState osts(this->osthread(), false /* not Object.wait() */);
1261 
1262   jlong time_before_loop = os::javaTimeNanos();
1263 
1264   for (;;) {
1265     bool timedout = PeriodicTask_lock->wait(Mutex::_no_safepoint_check_flag, remaining);
1266     jlong now = os::javaTimeNanos();
1267 
1268     if (remaining == 0) {
1269         // if we didn't have any tasks we could have waited for a long time
1270         // consider the time_slept zero and reset time_before_loop
1271         time_slept = 0;
1272         time_before_loop = now;
1273     } else {
1274         // need to recalulate since we might have new tasks in _tasks
1275         time_slept = (int) ((now - time_before_loop) / 1000000);
1276     }
1277 
1278     // Change to task list or spurious wakeup of some kind
1279     if (timedout || _should_terminate) {
1280         break;
1281     }
1282 
1283     remaining = PeriodicTask::time_to_wait();
1284     if (remaining == 0) {
1285         // Last task was just disenrolled so loop around and wait until
1286         // another task gets enrolled
1287         continue;
1288     }
1289 
1290     remaining -= time_slept;
1291     if (remaining <= 0)
1292       break;
1293   }
1294 


1621       jvmtiDeferredLocalVariableSet* dlv = deferred->at(0);
1622       deferred->remove_at(0);
1623       // individual jvmtiDeferredLocalVariableSet are CHeapObj's
1624       delete dlv;
1625     } while (deferred->length() != 0);
1626     delete deferred;
1627   }
1628 
1629   // All Java related clean up happens in exit
1630   ThreadSafepointState::destroy(this);
1631   if (_thread_profiler != NULL) delete _thread_profiler;
1632   if (_thread_stat != NULL) delete _thread_stat;
1633 }
1634 
1635 
1636 // The first routine called by a new Java thread
1637 void JavaThread::run() {
1638   // initialize thread-local alloc buffer related fields
1639   this->initialize_tlab();
1640 
1641   // used to test validitity of stack trace backs
1642   this->record_base_of_stack_pointer();
1643 
1644   // Record real stack base and size.
1645   this->record_stack_base_and_size();
1646 
1647   // Initialize thread local storage; set before calling MutexLocker
1648   this->initialize_thread_local_storage();
1649 
1650   this->create_stack_guard_pages();
1651 
1652   this->cache_global_variables();
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);


3303 
3304 jint Threads::create_vm(JavaVMInitArgs* args, bool* canTryAgain) {
3305 
3306   extern void JDK_Version_init();
3307 
3308   // Check version
3309   if (!is_supported_jni_version(args->version)) return JNI_EVERSION;
3310 
3311   // Initialize the output stream module
3312   ostream_init();
3313 
3314   // Process java launcher properties.
3315   Arguments::process_sun_java_launcher_properties(args);
3316 
3317   // Initialize the os module before using TLS
3318   os::init();
3319 
3320   // Initialize system properties.
3321   Arguments::init_system_properties();
3322 
3323   // So that JDK version can be used as a discrimintor when parsing arguments
3324   JDK_Version_init();
3325 
3326   // Update/Initialize System properties after JDK version number is known
3327   Arguments::init_version_specific_system_properties();
3328 
3329   // Parse arguments
3330   jint parse_result = Arguments::parse(args);
3331   if (parse_result != JNI_OK) return parse_result;
3332 
3333   os::init_before_ergo();
3334 
3335   jint ergo_result = Arguments::apply_ergo();
3336   if (ergo_result != JNI_OK) return ergo_result;
3337 
3338   if (PauseAtStartup) {
3339     os::pause();
3340   }
3341 
3342 #ifndef USDT2
3343   HS_DTRACE_PROBE(hotspot, vm__init__begin);
3344 #else /* USDT2 */
3345   HOTSPOT_VM_INIT_BEGIN();
3346 #endif /* USDT2 */
3347 
3348   // Record VM creation timing statistics
3349   TraceVmCreationTime create_vm_timer;
3350   create_vm_timer.start();
3351 
3352   // Timing (must come after argument parsing)
3353   TraceTime timer("Create VM", TraceStartupTime);
3354 
3355   // Initialize the os module after parsing the args
3356   jint os_init_2_result = os::init_2();
3357   if (os_init_2_result != JNI_OK) return os_init_2_result;
3358 
3359   jint adjust_after_os_result = Arguments::adjust_after_os();
3360   if (adjust_after_os_result != JNI_OK) return adjust_after_os_result;
3361 
3362   // intialize TLS
3363   ThreadLocalStorage::init();
3364 
3365   // Bootstrap native memory tracking, so it can start recording memory
3366   // activities before worker thread is started. This is the first phase
3367   // of bootstrapping, VM is currently running in single-thread mode.
3368   MemTracker::bootstrap_single_thread();
3369 
3370   // Initialize output stream logging
3371   ostream_init_log();
3372 
3373   // Convert -Xrun to -agentlib: if there is no JVM_OnLoad
3374   // Must be before create_vm_init_agents()
3375   if (Arguments::init_libraries_at_startup()) {
3376     convert_vm_init_libraries_to_agents();
3377   }
3378 
3379   // Launch -agentlib/-agentpath and converted -Xrun agents
3380   if (Arguments::init_agents_at_startup()) {
3381     create_vm_init_agents();
3382   }


4139 
4140   // Since Events::log uses a lock, we grab it outside the Threads_lock
4141   Events::log(p, "Thread exited: " INTPTR_FORMAT, p);
4142 }
4143 
4144 // Threads_lock must be held when this is called (or must be called during a safepoint)
4145 bool Threads::includes(JavaThread* p) {
4146   assert(Threads_lock->is_locked(), "sanity check");
4147   ALL_JAVA_THREADS(q) {
4148     if (q == p ) {
4149       return true;
4150     }
4151   }
4152   return false;
4153 }
4154 
4155 // Operations on the Threads list for GC.  These are not explicitly locked,
4156 // but the garbage collector must provide a safe context for them to run.
4157 // In particular, these things should never be called when the Threads_lock
4158 // is held by some other thread. (Note: the Safepoint abstraction also
4159 // uses the Threads_lock to gurantee this property. It also makes sure that
4160 // all threads gets blocked when exiting or starting).
4161 
4162 void Threads::oops_do(OopClosure* f, CLDToOopClosure* cld_f, CodeBlobClosure* cf) {
4163   ALL_JAVA_THREADS(p) {
4164     p->oops_do(f, cld_f, cf);
4165   }
4166   VMThread::vm_thread()->oops_do(f, cld_f, cf);
4167 }
4168 
4169 void Threads::possibly_parallel_oops_do(OopClosure* f, CLDToOopClosure* cld_f, CodeBlobClosure* cf) {
4170   // Introduce a mechanism allowing parallel threads to claim threads as
4171   // root groups.  Overhead should be small enough to use all the time,
4172   // even in sequential code.
4173   SharedHeap* sh = SharedHeap::heap();
4174   // Cannot yet substitute active_workers for n_par_threads
4175   // because of G1CollectedHeap::verify() use of
4176   // SharedHeap::process_strong_roots().  n_par_threads == 0 will
4177   // turn off parallelism in process_strong_roots while active_workers
4178   // is being used for parallelism elsewhere.
4179   bool is_par = sh->n_par_threads() > 0;




 750       // retry loop.
 751       *bits |= 0x00080000;
 752       return false;
 753     }
 754 
 755     if (is_suspended) {
 756       *bits |= 0x00100000;
 757       return true;
 758     }
 759   } // end retry loop
 760 
 761   // thread did not suspend after all our retries
 762   *bits |= 0x00200000;
 763   return false;
 764 }
 765 
 766 #ifndef PRODUCT
 767 void JavaThread::record_jump(address target, address instr, const char* file, int line) {
 768 
 769   // This should not need to be atomic as the only way for simultaneous
 770   // updates is via interrupts. Even then this should be rare or non-existent
 771   // and we don't care that much anyway.
 772 
 773   int index = _jmp_ring_index;
 774   _jmp_ring_index = (index + 1 ) & (jump_ring_buffer_size - 1);
 775   _jmp_ring[index]._target = (intptr_t) target;
 776   _jmp_ring[index]._instruction = (intptr_t) instr;
 777   _jmp_ring[index]._file = file;
 778   _jmp_ring[index]._line = line;
 779 }
 780 #endif /* PRODUCT */
 781 
 782 // Called by flat profiler
 783 // Callers have already called wait_for_ext_suspend_completion
 784 // The assertion for that is currently too complex to put here:
 785 bool JavaThread::profile_last_Java_frame(frame* _fr) {
 786   bool gotframe = false;
 787   // self suspension saves needed state.
 788   if (has_last_Java_frame() && _anchor.walkable()) {
 789      *_fr = pd_last_frame();
 790      gotframe = true;


 908 // The flag: potential_vm_operation notifies if this particular safepoint state could potential
 909 // invoke the vm-thread (i.e., and oop allocation). In that case, we also have to make sure that
 910 // no threads which allow_vm_block's are held
 911 void Thread::check_for_valid_safepoint_state(bool potential_vm_operation) {
 912     // Check if current thread is allowed to block at a safepoint
 913     if (!(_allow_safepoint_count == 0))
 914       fatal("Possible safepoint reached by thread that does not allow it");
 915     if (is_Java_thread() && ((JavaThread*)this)->thread_state() != _thread_in_vm) {
 916       fatal("LEAF method calling lock?");
 917     }
 918 
 919 #ifdef ASSERT
 920     if (potential_vm_operation && is_Java_thread()
 921         && !Universe::is_bootstrapping()) {
 922       // Make sure we do not hold any locks that the VM thread also uses.
 923       // This could potentially lead to deadlocks
 924       for(Monitor *cur = _owned_locks; cur; cur = cur->next()) {
 925         // Threads_lock is special, since the safepoint synchronization will not start before this is
 926         // acquired. Hence, a JavaThread cannot be holding it at a safepoint. So is VMOperationRequest_lock,
 927         // since it is used to transfer control between JavaThreads and the VMThread
 928         // Do not *exclude* any locks unless you are absolutely sure it is correct. Ask someone else first!
 929         if ( (cur->allow_vm_block() &&
 930               cur != Threads_lock &&
 931               cur != Compile_lock &&               // Temporary: should not be necessary when we get separate compilation
 932               cur != VMOperationRequest_lock &&
 933               cur != VMOperationQueue_lock) ||
 934               cur->rank() == Mutex::special) {
 935           warning("Thread holding lock at safepoint that vm can block on: %s", cur->name());
 936         }
 937       }
 938     }
 939 
 940     if (GCALotAtAllSafepoints) {
 941       // We could enter a safepoint here and thus have a gc
 942       InterfaceSupport::check_gc_alot();
 943     }
 944 #endif
 945 }
 946 #endif
 947 
 948 bool Thread::is_in_stack(address adr) const {
 949   assert(Thread::current() == this, "is_in_stack can only be called from current thread");
 950   address end = os::current_stack_pointer();
 951   // Allow non Java threads to call this without stack_base


1254   // enrolled
1255   int remaining = PeriodicTask::time_to_wait();
1256   int time_slept = 0;
1257 
1258   // we expect this to timeout - we only ever get unparked when
1259   // we should terminate or when a new task has been enrolled
1260   OSThreadWaitState osts(this->osthread(), false /* not Object.wait() */);
1261 
1262   jlong time_before_loop = os::javaTimeNanos();
1263 
1264   for (;;) {
1265     bool timedout = PeriodicTask_lock->wait(Mutex::_no_safepoint_check_flag, remaining);
1266     jlong now = os::javaTimeNanos();
1267 
1268     if (remaining == 0) {
1269         // if we didn't have any tasks we could have waited for a long time
1270         // consider the time_slept zero and reset time_before_loop
1271         time_slept = 0;
1272         time_before_loop = now;
1273     } else {
1274         // need to recalculate since we might have new tasks in _tasks
1275         time_slept = (int) ((now - time_before_loop) / 1000000);
1276     }
1277 
1278     // Change to task list or spurious wakeup of some kind
1279     if (timedout || _should_terminate) {
1280         break;
1281     }
1282 
1283     remaining = PeriodicTask::time_to_wait();
1284     if (remaining == 0) {
1285         // Last task was just disenrolled so loop around and wait until
1286         // another task gets enrolled
1287         continue;
1288     }
1289 
1290     remaining -= time_slept;
1291     if (remaining <= 0)
1292       break;
1293   }
1294 


1621       jvmtiDeferredLocalVariableSet* dlv = deferred->at(0);
1622       deferred->remove_at(0);
1623       // individual jvmtiDeferredLocalVariableSet are CHeapObj's
1624       delete dlv;
1625     } while (deferred->length() != 0);
1626     delete deferred;
1627   }
1628 
1629   // All Java related clean up happens in exit
1630   ThreadSafepointState::destroy(this);
1631   if (_thread_profiler != NULL) delete _thread_profiler;
1632   if (_thread_stat != NULL) delete _thread_stat;
1633 }
1634 
1635 
1636 // The first routine called by a new Java thread
1637 void JavaThread::run() {
1638   // initialize thread-local alloc buffer related fields
1639   this->initialize_tlab();
1640 
1641   // used to test validity of stack trace backs
1642   this->record_base_of_stack_pointer();
1643 
1644   // Record real stack base and size.
1645   this->record_stack_base_and_size();
1646 
1647   // Initialize thread local storage; set before calling MutexLocker
1648   this->initialize_thread_local_storage();
1649 
1650   this->create_stack_guard_pages();
1651 
1652   this->cache_global_variables();
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);


3303 
3304 jint Threads::create_vm(JavaVMInitArgs* args, bool* canTryAgain) {
3305 
3306   extern void JDK_Version_init();
3307 
3308   // Check version
3309   if (!is_supported_jni_version(args->version)) return JNI_EVERSION;
3310 
3311   // Initialize the output stream module
3312   ostream_init();
3313 
3314   // Process java launcher properties.
3315   Arguments::process_sun_java_launcher_properties(args);
3316 
3317   // Initialize the os module before using TLS
3318   os::init();
3319 
3320   // Initialize system properties.
3321   Arguments::init_system_properties();
3322 
3323   // So that JDK version can be used as a discriminator when parsing arguments
3324   JDK_Version_init();
3325 
3326   // Update/Initialize System properties after JDK version number is known
3327   Arguments::init_version_specific_system_properties();
3328 
3329   // Parse arguments
3330   jint parse_result = Arguments::parse(args);
3331   if (parse_result != JNI_OK) return parse_result;
3332 
3333   os::init_before_ergo();
3334 
3335   jint ergo_result = Arguments::apply_ergo();
3336   if (ergo_result != JNI_OK) return ergo_result;
3337 
3338   if (PauseAtStartup) {
3339     os::pause();
3340   }
3341 
3342 #ifndef USDT2
3343   HS_DTRACE_PROBE(hotspot, vm__init__begin);
3344 #else /* USDT2 */
3345   HOTSPOT_VM_INIT_BEGIN();
3346 #endif /* USDT2 */
3347 
3348   // Record VM creation timing statistics
3349   TraceVmCreationTime create_vm_timer;
3350   create_vm_timer.start();
3351 
3352   // Timing (must come after argument parsing)
3353   TraceTime timer("Create VM", TraceStartupTime);
3354 
3355   // Initialize the os module after parsing the args
3356   jint os_init_2_result = os::init_2();
3357   if (os_init_2_result != JNI_OK) return os_init_2_result;
3358 
3359   jint adjust_after_os_result = Arguments::adjust_after_os();
3360   if (adjust_after_os_result != JNI_OK) return adjust_after_os_result;
3361 
3362   // initialize TLS
3363   ThreadLocalStorage::init();
3364 
3365   // Bootstrap native memory tracking, so it can start recording memory
3366   // activities before worker thread is started. This is the first phase
3367   // of bootstrapping, VM is currently running in single-thread mode.
3368   MemTracker::bootstrap_single_thread();
3369 
3370   // Initialize output stream logging
3371   ostream_init_log();
3372 
3373   // Convert -Xrun to -agentlib: if there is no JVM_OnLoad
3374   // Must be before create_vm_init_agents()
3375   if (Arguments::init_libraries_at_startup()) {
3376     convert_vm_init_libraries_to_agents();
3377   }
3378 
3379   // Launch -agentlib/-agentpath and converted -Xrun agents
3380   if (Arguments::init_agents_at_startup()) {
3381     create_vm_init_agents();
3382   }


4139 
4140   // Since Events::log uses a lock, we grab it outside the Threads_lock
4141   Events::log(p, "Thread exited: " INTPTR_FORMAT, p);
4142 }
4143 
4144 // Threads_lock must be held when this is called (or must be called during a safepoint)
4145 bool Threads::includes(JavaThread* p) {
4146   assert(Threads_lock->is_locked(), "sanity check");
4147   ALL_JAVA_THREADS(q) {
4148     if (q == p ) {
4149       return true;
4150     }
4151   }
4152   return false;
4153 }
4154 
4155 // Operations on the Threads list for GC.  These are not explicitly locked,
4156 // but the garbage collector must provide a safe context for them to run.
4157 // In particular, these things should never be called when the Threads_lock
4158 // is held by some other thread. (Note: the Safepoint abstraction also
4159 // uses the Threads_lock to guarantee this property. It also makes sure that
4160 // all threads gets blocked when exiting or starting).
4161 
4162 void Threads::oops_do(OopClosure* f, CLDToOopClosure* cld_f, CodeBlobClosure* cf) {
4163   ALL_JAVA_THREADS(p) {
4164     p->oops_do(f, cld_f, cf);
4165   }
4166   VMThread::vm_thread()->oops_do(f, cld_f, cf);
4167 }
4168 
4169 void Threads::possibly_parallel_oops_do(OopClosure* f, CLDToOopClosure* cld_f, CodeBlobClosure* cf) {
4170   // Introduce a mechanism allowing parallel threads to claim threads as
4171   // root groups.  Overhead should be small enough to use all the time,
4172   // even in sequential code.
4173   SharedHeap* sh = SharedHeap::heap();
4174   // Cannot yet substitute active_workers for n_par_threads
4175   // because of G1CollectedHeap::verify() use of
4176   // SharedHeap::process_strong_roots().  n_par_threads == 0 will
4177   // turn off parallelism in process_strong_roots while active_workers
4178   // is being used for parallelism elsewhere.
4179   bool is_par = sh->n_par_threads() > 0;


src/share/vm/runtime/thread.cpp
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File