32 #include "code/scopeDesc.hpp"
33 #include "compiler/compileBroker.hpp"
34 #include "compiler/compileTask.hpp"
35 #include "gc/shared/gcId.hpp"
36 #include "gc/shared/gcLocker.inline.hpp"
37 #include "gc/shared/workgroup.hpp"
38 #include "interpreter/interpreter.hpp"
39 #include "interpreter/linkResolver.hpp"
40 #include "interpreter/oopMapCache.hpp"
41 #include "jvmtifiles/jvmtiEnv.hpp"
42 #include "logging/log.hpp"
43 #include "logging/logConfiguration.hpp"
44 #include "memory/metaspaceShared.hpp"
45 #include "memory/oopFactory.hpp"
46 #include "memory/resourceArea.hpp"
47 #include "memory/universe.inline.hpp"
48 #include "oops/instanceKlass.hpp"
49 #include "oops/objArrayOop.hpp"
50 #include "oops/oop.inline.hpp"
51 #include "oops/symbol.hpp"
52 #include "oops/verifyOopClosure.hpp"
53 #include "prims/jvm_misc.hpp"
54 #include "prims/jvmtiExport.hpp"
55 #include "prims/jvmtiThreadState.hpp"
56 #include "prims/privilegedStack.hpp"
57 #include "runtime/arguments.hpp"
58 #include "runtime/atomic.hpp"
59 #include "runtime/biasedLocking.hpp"
60 #include "runtime/commandLineFlagConstraintList.hpp"
61 #include "runtime/commandLineFlagWriteableList.hpp"
62 #include "runtime/commandLineFlagRangeList.hpp"
63 #include "runtime/deoptimization.hpp"
64 #include "runtime/fprofiler.hpp"
65 #include "runtime/frame.inline.hpp"
66 #include "runtime/globals.hpp"
67 #include "runtime/init.hpp"
68 #include "runtime/interfaceSupport.hpp"
69 #include "runtime/java.hpp"
70 #include "runtime/javaCalls.hpp"
71 #include "runtime/jniPeriodicChecker.hpp"
1497 // or up to some count of the number of profiled threads, etc.
1498 ThreadProfiler* pp = new ThreadProfiler();
1499 pp->engage();
1500 set_thread_profiler(pp);
1501 }
1502
1503 // Setup safepoint state info for this thread
1504 ThreadSafepointState::create(this);
1505
1506 debug_only(_java_call_counter = 0);
1507
1508 // JVMTI PopFrame support
1509 _popframe_condition = popframe_inactive;
1510 _popframe_preserved_args = NULL;
1511 _popframe_preserved_args_size = 0;
1512 _frames_to_pop_failed_realloc = 0;
1513
1514 pd_initialize();
1515 }
1516
1517 #if INCLUDE_ALL_GCS
1518 SATBMarkQueueSet JavaThread::_satb_mark_queue_set;
1519 DirtyCardQueueSet JavaThread::_dirty_card_queue_set;
1520 #endif // INCLUDE_ALL_GCS
1521
1522 JavaThread::JavaThread(bool is_attaching_via_jni) :
1523 Thread()
1524 #if INCLUDE_ALL_GCS
1525 , _satb_mark_queue(&_satb_mark_queue_set),
1526 _dirty_card_queue(&_dirty_card_queue_set)
1527 #endif // INCLUDE_ALL_GCS
1528 {
1529 initialize();
1530 if (is_attaching_via_jni) {
1531 _jni_attach_state = _attaching_via_jni;
1532 } else {
1533 _jni_attach_state = _not_attaching_via_jni;
1534 }
1535 assert(deferred_card_mark().is_empty(), "Default MemRegion ctor");
1536 }
1537
1538 bool JavaThread::reguard_stack(address cur_sp) {
1539 if (_stack_guard_state != stack_guard_yellow_reserved_disabled
1540 && _stack_guard_state != stack_guard_reserved_disabled) {
1541 return true; // Stack already guarded or guard pages not needed.
1542 }
1543
1544 if (register_stack_overflow()) {
1545 // For those architectures which have separate register and
1546 // memory stacks, we must check the register stack to see if
1547 // it has overflowed.
1571 return reguard_stack(os::current_stack_pointer());
1572 }
1573
1574
1575 void JavaThread::block_if_vm_exited() {
1576 if (_terminated == _vm_exited) {
1577 // _vm_exited is set at safepoint, and Threads_lock is never released
1578 // we will block here forever
1579 Threads_lock->lock_without_safepoint_check();
1580 ShouldNotReachHere();
1581 }
1582 }
1583
1584
1585 // Remove this ifdef when C1 is ported to the compiler interface.
1586 static void compiler_thread_entry(JavaThread* thread, TRAPS);
1587 static void sweeper_thread_entry(JavaThread* thread, TRAPS);
1588
1589 JavaThread::JavaThread(ThreadFunction entry_point, size_t stack_sz) :
1590 Thread()
1591 #if INCLUDE_ALL_GCS
1592 , _satb_mark_queue(&_satb_mark_queue_set),
1593 _dirty_card_queue(&_dirty_card_queue_set)
1594 #endif // INCLUDE_ALL_GCS
1595 {
1596 initialize();
1597 _jni_attach_state = _not_attaching_via_jni;
1598 set_entry_point(entry_point);
1599 // Create the native thread itself.
1600 // %note runtime_23
1601 os::ThreadType thr_type = os::java_thread;
1602 thr_type = entry_point == &compiler_thread_entry ? os::compiler_thread :
1603 os::java_thread;
1604 os::create_thread(this, thr_type, stack_sz);
1605 // The _osthread may be NULL here because we ran out of memory (too many threads active).
1606 // We need to throw and OutOfMemoryError - however we cannot do this here because the caller
1607 // may hold a lock and all locks must be unlocked before throwing the exception (throwing
1608 // the exception consists of creating the exception object & initializing it, initialization
1609 // will leave the VM via a JavaCall and then all locks must be unlocked).
1610 //
1611 // The thread is still suspended when we reach here. Thread must be explicit started
1612 // by creator! Furthermore, the thread must also explicitly be added to the Threads list
1613 // by calling Threads:add. The reason why this is not done here, is because the thread
1614 // object must be fully initialized (take a look at JVM_Start)
1897 JNIHandleBlock::release_block(block);
1898 }
1899
1900 if (free_handle_block() != NULL) {
1901 JNIHandleBlock* block = free_handle_block();
1902 set_free_handle_block(NULL);
1903 JNIHandleBlock::release_block(block);
1904 }
1905
1906 // These have to be removed while this is still a valid thread.
1907 remove_stack_guard_pages();
1908
1909 if (UseTLAB) {
1910 tlab().make_parsable(true); // retire TLAB
1911 }
1912
1913 if (JvmtiEnv::environments_might_exist()) {
1914 JvmtiExport::cleanup_thread(this);
1915 }
1916
1917 // We must flush any deferred card marks before removing a thread from
1918 // the list of active threads.
1919 Universe::heap()->flush_deferred_store_barrier(this);
1920 assert(deferred_card_mark().is_empty(), "Should have been flushed");
1921
1922 #if INCLUDE_ALL_GCS
1923 // We must flush the G1-related buffers before removing a thread
1924 // from the list of active threads. We must do this after any deferred
1925 // card marks have been flushed (above) so that any entries that are
1926 // added to the thread's dirty card queue as a result are not lost.
1927 if (UseG1GC) {
1928 flush_barrier_queues();
1929 }
1930 #endif // INCLUDE_ALL_GCS
1931
1932 log_info(os, thread)("JavaThread %s (tid: " UINTX_FORMAT ").",
1933 exit_type == JavaThread::normal_exit ? "exiting" : "detaching",
1934 os::current_thread_id());
1935
1936 // Remove from list of active threads list, and notify VM thread if we are the last non-daemon thread
1937 Threads::remove(this);
1938 }
1939
1940 #if INCLUDE_ALL_GCS
1941 // Flush G1-related queues.
1942 void JavaThread::flush_barrier_queues() {
1943 satb_mark_queue().flush();
1944 dirty_card_queue().flush();
1945 }
1946
1947 void JavaThread::initialize_queues() {
1948 assert(!SafepointSynchronize::is_at_safepoint(),
1949 "we should not be at a safepoint");
1950
1951 SATBMarkQueue& satb_queue = satb_mark_queue();
1952 SATBMarkQueueSet& satb_queue_set = satb_mark_queue_set();
1953 // The SATB queue should have been constructed with its active
1954 // field set to false.
1955 assert(!satb_queue.is_active(), "SATB queue should not be active");
1956 assert(satb_queue.is_empty(), "SATB queue should be empty");
1957 // If we are creating the thread during a marking cycle, we should
1958 // set the active field of the SATB queue to true.
1959 if (satb_queue_set.is_active()) {
1960 satb_queue.set_active(true);
1961 }
1962
1963 DirtyCardQueue& dirty_queue = dirty_card_queue();
1964 // The dirty card queue should have been constructed with its
1965 // active field set to true.
1966 assert(dirty_queue.is_active(), "dirty card queue should be active");
1967 }
1968 #endif // INCLUDE_ALL_GCS
1969
1970 void JavaThread::cleanup_failed_attach_current_thread() {
1971 if (get_thread_profiler() != NULL) {
1972 get_thread_profiler()->disengage();
1973 ResourceMark rm;
1974 get_thread_profiler()->print(get_thread_name());
1975 }
1976
1977 if (active_handles() != NULL) {
1978 JNIHandleBlock* block = active_handles();
1979 set_active_handles(NULL);
1980 JNIHandleBlock::release_block(block);
1981 }
1982
1983 if (free_handle_block() != NULL) {
1984 JNIHandleBlock* block = free_handle_block();
1985 set_free_handle_block(NULL);
1986 JNIHandleBlock::release_block(block);
1987 }
1988
1989 // These have to be removed while this is still a valid thread.
1990 remove_stack_guard_pages();
1991
1992 if (UseTLAB) {
1993 tlab().make_parsable(true); // retire TLAB, if any
1994 }
1995
1996 #if INCLUDE_ALL_GCS
1997 if (UseG1GC) {
1998 flush_barrier_queues();
1999 }
2000 #endif // INCLUDE_ALL_GCS
2001
2002 Threads::remove(this);
2003 delete this;
2004 }
2005
2006
2007
2008
2009 JavaThread* JavaThread::active() {
2010 Thread* thread = Thread::current();
2011 if (thread->is_Java_thread()) {
2012 return (JavaThread*) thread;
2013 } else {
2014 assert(thread->is_VM_thread(), "this must be a vm thread");
2015 VM_Operation* op = ((VMThread*) thread)->vm_operation();
2016 JavaThread *ret=op == NULL ? NULL : (JavaThread *)op->calling_thread();
2017 assert(ret->is_Java_thread(), "must be a Java thread");
2018 return ret;
2019 }
2020 }
4208 return is_supported_jni_version(version);
4209 }
4210
4211
4212 jboolean Threads::is_supported_jni_version(jint version) {
4213 if (version == JNI_VERSION_1_2) return JNI_TRUE;
4214 if (version == JNI_VERSION_1_4) return JNI_TRUE;
4215 if (version == JNI_VERSION_1_6) return JNI_TRUE;
4216 if (version == JNI_VERSION_1_8) return JNI_TRUE;
4217 if (version == JNI_VERSION_9) return JNI_TRUE;
4218 return JNI_FALSE;
4219 }
4220
4221
4222 void Threads::add(JavaThread* p, bool force_daemon) {
4223 // The threads lock must be owned at this point
4224 assert_locked_or_safepoint(Threads_lock);
4225
4226 // See the comment for this method in thread.hpp for its purpose and
4227 // why it is called here.
4228 p->initialize_queues();
4229 p->set_next(_thread_list);
4230 _thread_list = p;
4231 _number_of_threads++;
4232 oop threadObj = p->threadObj();
4233 bool daemon = true;
4234 // Bootstrapping problem: threadObj can be null for initial
4235 // JavaThread (or for threads attached via JNI)
4236 if ((!force_daemon) && (threadObj == NULL || !java_lang_Thread::is_daemon(threadObj))) {
4237 _number_of_non_daemon_threads++;
4238 daemon = false;
4239 }
4240
4241 ThreadService::add_thread(p, daemon);
4242
4243 // Possible GC point.
4244 Events::log(p, "Thread added: " INTPTR_FORMAT, p2i(p));
4245 }
4246
4247 void Threads::remove(JavaThread* p) {
4248 // Extra scope needed for Thread_lock, so we can check
|
32 #include "code/scopeDesc.hpp"
33 #include "compiler/compileBroker.hpp"
34 #include "compiler/compileTask.hpp"
35 #include "gc/shared/gcId.hpp"
36 #include "gc/shared/gcLocker.inline.hpp"
37 #include "gc/shared/workgroup.hpp"
38 #include "interpreter/interpreter.hpp"
39 #include "interpreter/linkResolver.hpp"
40 #include "interpreter/oopMapCache.hpp"
41 #include "jvmtifiles/jvmtiEnv.hpp"
42 #include "logging/log.hpp"
43 #include "logging/logConfiguration.hpp"
44 #include "memory/metaspaceShared.hpp"
45 #include "memory/oopFactory.hpp"
46 #include "memory/resourceArea.hpp"
47 #include "memory/universe.inline.hpp"
48 #include "oops/instanceKlass.hpp"
49 #include "oops/objArrayOop.hpp"
50 #include "oops/oop.inline.hpp"
51 #include "oops/symbol.hpp"
52 #include "oops/typeArrayOop.inline.hpp"
53 #include "oops/verifyOopClosure.hpp"
54 #include "prims/jvm_misc.hpp"
55 #include "prims/jvmtiExport.hpp"
56 #include "prims/jvmtiThreadState.hpp"
57 #include "prims/privilegedStack.hpp"
58 #include "runtime/arguments.hpp"
59 #include "runtime/atomic.hpp"
60 #include "runtime/biasedLocking.hpp"
61 #include "runtime/commandLineFlagConstraintList.hpp"
62 #include "runtime/commandLineFlagWriteableList.hpp"
63 #include "runtime/commandLineFlagRangeList.hpp"
64 #include "runtime/deoptimization.hpp"
65 #include "runtime/fprofiler.hpp"
66 #include "runtime/frame.inline.hpp"
67 #include "runtime/globals.hpp"
68 #include "runtime/init.hpp"
69 #include "runtime/interfaceSupport.hpp"
70 #include "runtime/java.hpp"
71 #include "runtime/javaCalls.hpp"
72 #include "runtime/jniPeriodicChecker.hpp"
1498 // or up to some count of the number of profiled threads, etc.
1499 ThreadProfiler* pp = new ThreadProfiler();
1500 pp->engage();
1501 set_thread_profiler(pp);
1502 }
1503
1504 // Setup safepoint state info for this thread
1505 ThreadSafepointState::create(this);
1506
1507 debug_only(_java_call_counter = 0);
1508
1509 // JVMTI PopFrame support
1510 _popframe_condition = popframe_inactive;
1511 _popframe_preserved_args = NULL;
1512 _popframe_preserved_args_size = 0;
1513 _frames_to_pop_failed_realloc = 0;
1514
1515 pd_initialize();
1516 }
1517
1518 JavaThread::JavaThread(bool is_attaching_via_jni) :
1519 Thread()
1520 {
1521 initialize();
1522 if (is_attaching_via_jni) {
1523 _jni_attach_state = _attaching_via_jni;
1524 } else {
1525 _jni_attach_state = _not_attaching_via_jni;
1526 }
1527 assert(deferred_card_mark().is_empty(), "Default MemRegion ctor");
1528 }
1529
1530 bool JavaThread::reguard_stack(address cur_sp) {
1531 if (_stack_guard_state != stack_guard_yellow_reserved_disabled
1532 && _stack_guard_state != stack_guard_reserved_disabled) {
1533 return true; // Stack already guarded or guard pages not needed.
1534 }
1535
1536 if (register_stack_overflow()) {
1537 // For those architectures which have separate register and
1538 // memory stacks, we must check the register stack to see if
1539 // it has overflowed.
1563 return reguard_stack(os::current_stack_pointer());
1564 }
1565
1566
1567 void JavaThread::block_if_vm_exited() {
1568 if (_terminated == _vm_exited) {
1569 // _vm_exited is set at safepoint, and Threads_lock is never released
1570 // we will block here forever
1571 Threads_lock->lock_without_safepoint_check();
1572 ShouldNotReachHere();
1573 }
1574 }
1575
1576
1577 // Remove this ifdef when C1 is ported to the compiler interface.
1578 static void compiler_thread_entry(JavaThread* thread, TRAPS);
1579 static void sweeper_thread_entry(JavaThread* thread, TRAPS);
1580
1581 JavaThread::JavaThread(ThreadFunction entry_point, size_t stack_sz) :
1582 Thread()
1583 {
1584 initialize();
1585 _jni_attach_state = _not_attaching_via_jni;
1586 set_entry_point(entry_point);
1587 // Create the native thread itself.
1588 // %note runtime_23
1589 os::ThreadType thr_type = os::java_thread;
1590 thr_type = entry_point == &compiler_thread_entry ? os::compiler_thread :
1591 os::java_thread;
1592 os::create_thread(this, thr_type, stack_sz);
1593 // The _osthread may be NULL here because we ran out of memory (too many threads active).
1594 // We need to throw and OutOfMemoryError - however we cannot do this here because the caller
1595 // may hold a lock and all locks must be unlocked before throwing the exception (throwing
1596 // the exception consists of creating the exception object & initializing it, initialization
1597 // will leave the VM via a JavaCall and then all locks must be unlocked).
1598 //
1599 // The thread is still suspended when we reach here. Thread must be explicit started
1600 // by creator! Furthermore, the thread must also explicitly be added to the Threads list
1601 // by calling Threads:add. The reason why this is not done here, is because the thread
1602 // object must be fully initialized (take a look at JVM_Start)
1885 JNIHandleBlock::release_block(block);
1886 }
1887
1888 if (free_handle_block() != NULL) {
1889 JNIHandleBlock* block = free_handle_block();
1890 set_free_handle_block(NULL);
1891 JNIHandleBlock::release_block(block);
1892 }
1893
1894 // These have to be removed while this is still a valid thread.
1895 remove_stack_guard_pages();
1896
1897 if (UseTLAB) {
1898 tlab().make_parsable(true); // retire TLAB
1899 }
1900
1901 if (JvmtiEnv::environments_might_exist()) {
1902 JvmtiExport::cleanup_thread(this);
1903 }
1904
1905 BarrierSet *bs = Universe::heap()->barrier_set();
1906 bs->on_destroy_thread(this);
1907
1908 log_info(os, thread)("JavaThread %s (tid: " UINTX_FORMAT ").",
1909 exit_type == JavaThread::normal_exit ? "exiting" : "detaching",
1910 os::current_thread_id());
1911
1912 // Remove from list of active threads list, and notify VM thread if we are the last non-daemon thread
1913 Threads::remove(this);
1914 }
1915
1916 void JavaThread::cleanup_failed_attach_current_thread() {
1917 if (get_thread_profiler() != NULL) {
1918 get_thread_profiler()->disengage();
1919 ResourceMark rm;
1920 get_thread_profiler()->print(get_thread_name());
1921 }
1922
1923 if (active_handles() != NULL) {
1924 JNIHandleBlock* block = active_handles();
1925 set_active_handles(NULL);
1926 JNIHandleBlock::release_block(block);
1927 }
1928
1929 if (free_handle_block() != NULL) {
1930 JNIHandleBlock* block = free_handle_block();
1931 set_free_handle_block(NULL);
1932 JNIHandleBlock::release_block(block);
1933 }
1934
1935 // These have to be removed while this is still a valid thread.
1936 remove_stack_guard_pages();
1937
1938 if (UseTLAB) {
1939 tlab().make_parsable(true); // retire TLAB, if any
1940 }
1941
1942 BarrierSet *bs = Universe::heap()->barrier_set();
1943 bs->on_destroy_thread(this);
1944
1945 Threads::remove(this);
1946 delete this;
1947 }
1948
1949
1950
1951
1952 JavaThread* JavaThread::active() {
1953 Thread* thread = Thread::current();
1954 if (thread->is_Java_thread()) {
1955 return (JavaThread*) thread;
1956 } else {
1957 assert(thread->is_VM_thread(), "this must be a vm thread");
1958 VM_Operation* op = ((VMThread*) thread)->vm_operation();
1959 JavaThread *ret=op == NULL ? NULL : (JavaThread *)op->calling_thread();
1960 assert(ret->is_Java_thread(), "must be a Java thread");
1961 return ret;
1962 }
1963 }
4151 return is_supported_jni_version(version);
4152 }
4153
4154
4155 jboolean Threads::is_supported_jni_version(jint version) {
4156 if (version == JNI_VERSION_1_2) return JNI_TRUE;
4157 if (version == JNI_VERSION_1_4) return JNI_TRUE;
4158 if (version == JNI_VERSION_1_6) return JNI_TRUE;
4159 if (version == JNI_VERSION_1_8) return JNI_TRUE;
4160 if (version == JNI_VERSION_9) return JNI_TRUE;
4161 return JNI_FALSE;
4162 }
4163
4164
4165 void Threads::add(JavaThread* p, bool force_daemon) {
4166 // The threads lock must be owned at this point
4167 assert_locked_or_safepoint(Threads_lock);
4168
4169 // See the comment for this method in thread.hpp for its purpose and
4170 // why it is called here.
4171 BarrierSet* bs = Universe::heap()->barrier_set();
4172 bs->on_add_thread(p);
4173
4174 p->set_next(_thread_list);
4175 _thread_list = p;
4176 _number_of_threads++;
4177 oop threadObj = p->threadObj();
4178 bool daemon = true;
4179 // Bootstrapping problem: threadObj can be null for initial
4180 // JavaThread (or for threads attached via JNI)
4181 if ((!force_daemon) && (threadObj == NULL || !java_lang_Thread::is_daemon(threadObj))) {
4182 _number_of_non_daemon_threads++;
4183 daemon = false;
4184 }
4185
4186 ThreadService::add_thread(p, daemon);
4187
4188 // Possible GC point.
4189 Events::log(p, "Thread added: " INTPTR_FORMAT, p2i(p));
4190 }
4191
4192 void Threads::remove(JavaThread* p) {
4193 // Extra scope needed for Thread_lock, so we can check
|