312 // to BarrierSet::set_barrier_set(). |
316 // to BarrierSet::set_barrier_set(). |
313 BarrierSet* const barrier_set = BarrierSet::barrier_set(); |
317 BarrierSet* const barrier_set = BarrierSet::barrier_set(); |
314 if (barrier_set != NULL) { |
318 if (barrier_set != NULL) { |
315 barrier_set->on_thread_create(this); |
319 barrier_set->on_thread_create(this); |
316 } else { |
320 } else { |
317 #ifdef ASSERT |
321 // Only the main thread should be created before the barrier set |
318 static bool initial_thread_created = false; |
322 // and that happens just before Thread::current is set. No other thread |
319 assert(!initial_thread_created, "creating thread before barrier set"); |
323 // can attach as the VM is not created yet, so they can't execute this code. |
320 initial_thread_created = true; |
324 // If the main thread creates other threads before the barrier set that is an error. |
321 #endif // ASSERT |
325 assert(Thread::current_or_null() == NULL, "creating thread before barrier set"); |
322 } |
326 } |
323 } |
327 } |
324 |
328 |
325 void Thread::initialize_thread_current() { |
329 void Thread::initialize_thread_current() { |
326 #ifndef USE_LIBRARY_BASED_TLS_ONLY |
330 #ifndef USE_LIBRARY_BASED_TLS_ONLY |
366 MemTracker::record_thread_stack(stack_end(), stack_size()); |
370 MemTracker::record_thread_stack(stack_end(), stack_size()); |
367 } |
371 } |
368 #endif // INCLUDE_NMT |
372 #endif // INCLUDE_NMT |
369 |
373 |
370 void Thread::call_run() { |
374 void Thread::call_run() { |
|
375 DEBUG_ONLY(_run_state = CALL_RUN;) |
|
376 |
371 // At this point, Thread object should be fully initialized and |
377 // At this point, Thread object should be fully initialized and |
372 // Thread::current() should be set. |
378 // Thread::current() should be set. |
|
379 |
|
380 assert(Thread::current_or_null() != NULL, "current thread is unset"); |
|
381 assert(Thread::current_or_null() == this, "current thread is wrong"); |
|
382 |
|
383 // Perform common initialization actions |
373 |
384 |
374 register_thread_stack_with_NMT(); |
385 register_thread_stack_with_NMT(); |
375 |
386 |
376 JFR_ONLY(Jfr::on_thread_start(this);) |
387 JFR_ONLY(Jfr::on_thread_start(this);) |
377 |
388 |
378 log_debug(os, thread)("Thread " UINTX_FORMAT " stack dimensions: " |
389 log_debug(os, thread)("Thread " UINTX_FORMAT " stack dimensions: " |
379 PTR_FORMAT "-" PTR_FORMAT " (" SIZE_FORMAT "k).", |
390 PTR_FORMAT "-" PTR_FORMAT " (" SIZE_FORMAT "k).", |
380 os::current_thread_id(), p2i(stack_base() - stack_size()), |
391 os::current_thread_id(), p2i(stack_base() - stack_size()), |
381 p2i(stack_base()), stack_size()/1024); |
392 p2i(stack_base()), stack_size()/1024); |
382 |
393 |
|
394 // Perform <ChildClass> initialization actions |
|
395 DEBUG_ONLY(_run_state = PRE_RUN;) |
|
396 this->pre_run(); |
|
397 |
383 // Invoke <ChildClass>::run() |
398 // Invoke <ChildClass>::run() |
|
399 DEBUG_ONLY(_run_state = RUN;) |
384 this->run(); |
400 this->run(); |
385 // Returned from <ChildClass>::run(). Thread finished. |
401 // Returned from <ChildClass>::run(). Thread finished. |
386 |
402 |
387 // Note: at this point the thread object may already have deleted itself. |
403 // Perform common tear-down actions |
388 // So from here on do not dereference *this*. |
404 |
389 |
405 assert(Thread::current_or_null() != NULL, "current thread is unset"); |
390 // If a thread has not deleted itself ("delete this") as part of its |
406 assert(Thread::current_or_null() == this, "current thread is wrong"); |
391 // termination sequence, we have to ensure thread-local-storage is |
407 |
392 // cleared before we actually terminate. No threads should ever be |
408 // Perform <ChildClass> tear-down actions |
393 // deleted asynchronously with respect to their termination. |
409 DEBUG_ONLY(_run_state = POST_RUN;) |
394 if (Thread::current_or_null_safe() != NULL) { |
410 this->post_run(); |
395 assert(Thread::current_or_null_safe() == this, "current thread is wrong"); |
411 |
396 Thread::clear_thread_current(); |
412 // Note: at this point the thread object may already have deleted itself, |
397 } |
413 // so from here on do not dereference *this*. Not all thread types currently |
398 |
414 // delete themselves when they terminate. But no thread should ever be deleted |
|
415 // asynchronously with respect to its termination - that is what _run_state can |
|
416 // be used to check. |
|
417 |
|
418 assert(Thread::current_or_null() == NULL, "current thread still present"); |
399 } |
419 } |
400 |
420 |
401 Thread::~Thread() { |
421 Thread::~Thread() { |
|
422 |
|
423 // Attached threads will remain in PRE_CALL_RUN, as will threads that don't actually |
|
424 // get started due to errors etc. Any active thread should at least reach post_run |
|
425 // before it is deleted (usually in post_run()). |
|
426 assert(_run_state == PRE_CALL_RUN || |
|
427 _run_state == POST_RUN, "Active Thread deleted before post_run(): " |
|
428 "_run_state=%d", (int)_run_state); |
|
429 |
402 // Notify the barrier set that a thread is being destroyed. Note that a barrier |
430 // Notify the barrier set that a thread is being destroyed. Note that a barrier |
403 // set might not be available if we encountered errors during bootstrapping. |
431 // set might not be available if we encountered errors during bootstrapping. |
404 BarrierSet* const barrier_set = BarrierSet::barrier_set(); |
432 BarrierSet* const barrier_set = BarrierSet::barrier_set(); |
405 if (barrier_set != NULL) { |
433 if (barrier_set != NULL) { |
406 barrier_set->on_thread_destroy(this); |
434 barrier_set->on_thread_destroy(this); |
443 _SR_lock = NULL; |
471 _SR_lock = NULL; |
444 |
472 |
445 // osthread() can be NULL, if creation of thread failed. |
473 // osthread() can be NULL, if creation of thread failed. |
446 if (osthread() != NULL) os::free_thread(osthread()); |
474 if (osthread() != NULL) os::free_thread(osthread()); |
447 |
475 |
448 // clear Thread::current if thread is deleting itself. |
476 // Clear Thread::current if thread is deleting itself and it has not |
|
477 // already been done. This must be done before the memory is deallocated. |
449 // Needed to ensure JNI correctly detects non-attached threads. |
478 // Needed to ensure JNI correctly detects non-attached threads. |
450 if (this == Thread::current()) { |
479 if (this == Thread::current_or_null()) { |
451 Thread::clear_thread_current(); |
480 Thread::clear_thread_current(); |
452 } |
481 } |
453 |
482 |
454 CHECK_UNHANDLED_OOPS_ONLY(if (CheckUnhandledOops) delete unhandled_oops();) |
483 CHECK_UNHANDLED_OOPS_ONLY(if (CheckUnhandledOops) delete unhandled_oops();) |
455 } |
484 } |
1049 bool Thread::is_lock_owned(address adr) const { |
1078 bool Thread::is_lock_owned(address adr) const { |
1050 return on_local_stack(adr); |
1079 return on_local_stack(adr); |
1051 } |
1080 } |
1052 |
1081 |
1053 bool Thread::set_as_starting_thread() { |
1082 bool Thread::set_as_starting_thread() { |
|
1083 assert(_starting_thread == NULL, "already initialized: " |
|
1084 "_starting_thread=" INTPTR_FORMAT, p2i(_starting_thread)); |
1054 // NOTE: this must be called inside the main thread. |
1085 // NOTE: this must be called inside the main thread. |
|
1086 DEBUG_ONLY(_starting_thread = this;) |
1055 return os::create_main_thread((JavaThread*)this); |
1087 return os::create_main_thread((JavaThread*)this); |
1056 } |
1088 } |
1057 |
1089 |
1058 static void initialize_class(Symbol* class_name, TRAPS) { |
1090 static void initialize_class(Symbol* class_name, TRAPS) { |
1059 Klass* klass = SystemDictionary::resolve_or_fail(class_name, true, CHECK); |
1091 Klass* klass = SystemDictionary::resolve_or_fail(class_name, true, CHECK); |
1252 assert(!end(), "precondition"); |
1284 assert(!end(), "precondition"); |
1253 _current = OrderAccess::load_acquire(&_current->_next); |
1285 _current = OrderAccess::load_acquire(&_current->_next); |
1254 } |
1286 } |
1255 |
1287 |
1256 NonJavaThread::NonJavaThread() : Thread(), _next(NULL) { |
1288 NonJavaThread::NonJavaThread() : Thread(), _next(NULL) { |
1257 // Add this thread to _the_list. |
1289 assert(BarrierSet::barrier_set() != NULL, "NonJavaThread created too soon!"); |
|
1290 } |
|
1291 |
|
1292 NonJavaThread::~NonJavaThread() { } |
|
1293 |
|
1294 void NonJavaThread::add_to_the_list() { |
1258 MutexLockerEx lock(NonJavaThreadsList_lock, Mutex::_no_safepoint_check_flag); |
1295 MutexLockerEx lock(NonJavaThreadsList_lock, Mutex::_no_safepoint_check_flag); |
1259 _next = _the_list._head; |
1296 _next = _the_list._head; |
1260 OrderAccess::release_store(&_the_list._head, this); |
1297 OrderAccess::release_store(&_the_list._head, this); |
1261 } |
1298 } |
1262 |
1299 |
1263 NonJavaThread::~NonJavaThread() { |
1300 void NonJavaThread::remove_from_the_list() { |
1264 JFR_ONLY(Jfr::on_thread_exit(this);) |
|
1265 // Remove this thread from _the_list. |
|
1266 MutexLockerEx lock(NonJavaThreadsList_lock, Mutex::_no_safepoint_check_flag); |
1301 MutexLockerEx lock(NonJavaThreadsList_lock, Mutex::_no_safepoint_check_flag); |
1267 NonJavaThread* volatile* p = &_the_list._head; |
1302 NonJavaThread* volatile* p = &_the_list._head; |
1268 for (NonJavaThread* t = *p; t != NULL; p = &t->_next, t = *p) { |
1303 for (NonJavaThread* t = *p; t != NULL; p = &t->_next, t = *p) { |
1269 if (t == this) { |
1304 if (t == this) { |
1270 *p = this->_next; |
1305 *p = this->_next; |
1271 // Wait for any in-progress iterators. |
1306 // Wait for any in-progress iterators. Concurrent synchronize is |
|
1307 // not allowed, so do it while holding the list lock. |
1272 _the_list._protect.synchronize(); |
1308 _the_list._protect.synchronize(); |
1273 break; |
1309 break; |
1274 } |
1310 } |
1275 } |
1311 } |
|
1312 } |
|
1313 |
|
1314 void NonJavaThread::pre_run() { |
|
1315 assert(BarrierSet::barrier_set() != NULL, "invariant"); |
|
1316 add_to_the_list(); |
|
1317 |
|
1318 // This is slightly odd in that NamedThread is a subclass, but |
|
1319 // in fact name() is defined in Thread |
|
1320 assert(this->name() != NULL, "thread name was not set before it was started"); |
|
1321 this->set_native_thread_name(this->name()); |
|
1322 } |
|
1323 |
|
1324 void NonJavaThread::post_run() { |
|
1325 JFR_ONLY(Jfr::on_thread_exit(this);) |
|
1326 remove_from_the_list(); |
|
1327 // Ensure thread-local-storage is cleared before termination. |
|
1328 Thread::clear_thread_current(); |
1276 } |
1329 } |
1277 |
1330 |
1278 // NamedThread -- non-JavaThread subclasses with multiple |
1331 // NamedThread -- non-JavaThread subclasses with multiple |
1279 // uniquely named instances should derive from this. |
1332 // uniquely named instances should derive from this. |
1280 NamedThread::NamedThread() : |
1333 NamedThread::NamedThread() : |
1297 guarantee(_name != NULL, "alloc failure"); |
1350 guarantee(_name != NULL, "alloc failure"); |
1298 va_list ap; |
1351 va_list ap; |
1299 va_start(ap, format); |
1352 va_start(ap, format); |
1300 jio_vsnprintf(_name, max_name_len, format, ap); |
1353 jio_vsnprintf(_name, max_name_len, format, ap); |
1301 va_end(ap); |
1354 va_end(ap); |
1302 } |
|
1303 |
|
1304 void NamedThread::initialize_named_thread() { |
|
1305 set_native_thread_name(name()); |
|
1306 } |
1355 } |
1307 |
1356 |
1308 void NamedThread::print_on(outputStream* st) const { |
1357 void NamedThread::print_on(outputStream* st) const { |
1309 st->print("\"%s\" ", name()); |
1358 st->print("\"%s\" ", name()); |
1310 Thread::print_on(st); |
1359 Thread::print_on(st); |
1755 } |
1803 } |
1756 #endif // INCLUDE_JVMCI |
1804 #endif // INCLUDE_JVMCI |
1757 } |
1805 } |
1758 |
1806 |
1759 |
1807 |
1760 // The first routine called by a new Java thread |
1808 // First JavaThread specific code executed by a new Java thread. |
|
1809 void JavaThread::pre_run() { |
|
1810 // empty - see comments in run() |
|
1811 } |
|
1812 |
|
1813 // The main routine called by a new Java thread. This isn't overridden |
|
1814 // by subclasses, instead different subclasses define a different "entry_point" |
|
1815 // which defines the actual logic for that kind of thread. |
1761 void JavaThread::run() { |
1816 void JavaThread::run() { |
1762 // initialize thread-local alloc buffer related fields |
1817 // initialize thread-local alloc buffer related fields |
1763 this->initialize_tlab(); |
1818 this->initialize_tlab(); |
1764 |
1819 |
1765 // used to test validity of stack trace backs |
1820 // Used to test validity of stack trace backs. |
|
1821 // This can't be moved into pre_run() else we invalidate |
|
1822 // the requirement that thread_main_inner is lower on |
|
1823 // the stack. Consequently all the initialization logic |
|
1824 // stays here in run() rather than pre_run(). |
1766 this->record_base_of_stack_pointer(); |
1825 this->record_base_of_stack_pointer(); |
1767 |
1826 |
1768 this->create_stack_guard_pages(); |
1827 this->create_stack_guard_pages(); |
1769 |
1828 |
1770 this->cache_global_variables(); |
1829 this->cache_global_variables(); |
1771 |
1830 |
1772 // Thread is now sufficient initialized to be handled by the safepoint code as being |
1831 // Thread is now sufficiently initialized to be handled by the safepoint code as being |
1773 // in the VM. Change thread state from _thread_new to _thread_in_vm |
1832 // in the VM. Change thread state from _thread_new to _thread_in_vm |
1774 ThreadStateTransition::transition_and_fence(this, _thread_new, _thread_in_vm); |
1833 ThreadStateTransition::transition_and_fence(this, _thread_new, _thread_in_vm); |
1775 |
1834 |
1776 assert(JavaThread::current() == this, "sanity check"); |
1835 assert(JavaThread::current() == this, "sanity check"); |
1777 assert(!Thread::current()->owns_locks(), "sanity check"); |
1836 assert(!Thread::current()->owns_locks(), "sanity check"); |
1786 JvmtiExport::post_thread_start(this); |
1845 JvmtiExport::post_thread_start(this); |
1787 |
1846 |
1788 } |
1847 } |
1789 |
1848 |
1790 // We call another function to do the rest so we are sure that the stack addresses used |
1849 // We call another function to do the rest so we are sure that the stack addresses used |
1791 // from there will be lower than the stack base just computed |
1850 // from there will be lower than the stack base just computed. |
1792 thread_main_inner(); |
1851 thread_main_inner(); |
1793 |
1852 } |
1794 // Note, thread is no longer valid at this point! |
|
1795 } |
|
1796 |
|
1797 |
1853 |
1798 void JavaThread::thread_main_inner() { |
1854 void JavaThread::thread_main_inner() { |
1799 assert(JavaThread::current() == this, "sanity check"); |
1855 assert(JavaThread::current() == this, "sanity check"); |
1800 assert(this->threadObj() != NULL, "just checking"); |
1856 assert(this->threadObj() != NULL, "just checking"); |
1801 |
1857 |
1812 this->entry_point()(this, this); |
1868 this->entry_point()(this, this); |
1813 } |
1869 } |
1814 |
1870 |
1815 DTRACE_THREAD_PROBE(stop, this); |
1871 DTRACE_THREAD_PROBE(stop, this); |
1816 |
1872 |
|
1873 // Cleanup is handled in post_run() |
|
1874 } |
|
1875 |
|
1876 // Shared teardown for all JavaThreads |
|
1877 void JavaThread::post_run() { |
1817 this->exit(false); |
1878 this->exit(false); |
|
1879 // Defer deletion to here to ensure 'this' is still referenceable in call_run |
|
1880 // for any shared tear-down. |
1818 this->smr_delete(); |
1881 this->smr_delete(); |
1819 } |
1882 } |
1820 |
|
1821 |
1883 |
1822 static void ensure_join(JavaThread* thread) { |
1884 static void ensure_join(JavaThread* thread) { |
1823 // We do not need to grab the Threads_lock, since we are operating on ourself. |
1885 // We do not need to grab the Threads_lock, since we are operating on ourself. |
1824 Handle threadObj(thread, thread->threadObj()); |
1886 Handle threadObj(thread, thread->threadObj()); |
1825 assert(threadObj.not_null(), "java thread object must exist"); |
1887 assert(threadObj.not_null(), "java thread object must exist"); |