41 #include "memory/oopFactory.hpp"
42 #include "memory/universe.inline.hpp"
43 #include "oops/instanceKlass.hpp"
44 #include "oops/objArrayOop.hpp"
45 #include "oops/oop.inline.hpp"
46 #include "oops/symbol.hpp"
47 #include "oops/verifyOopClosure.hpp"
48 #include "prims/jvm_misc.hpp"
49 #include "prims/jvmtiExport.hpp"
50 #include "prims/jvmtiThreadState.hpp"
51 #include "prims/privilegedStack.hpp"
52 #include "runtime/arguments.hpp"
53 #include "runtime/atomic.inline.hpp"
54 #include "runtime/biasedLocking.hpp"
55 #include "runtime/commandLineFlagConstraintList.hpp"
56 #include "runtime/commandLineFlagRangeList.hpp"
57 #include "runtime/deoptimization.hpp"
58 #include "runtime/fprofiler.hpp"
59 #include "runtime/frame.inline.hpp"
60 #include "runtime/globals.hpp"
61 #include "runtime/init.hpp"
62 #include "runtime/interfaceSupport.hpp"
63 #include "runtime/java.hpp"
64 #include "runtime/javaCalls.hpp"
65 #include "runtime/jniPeriodicChecker.hpp"
66 #include "runtime/memprofiler.hpp"
67 #include "runtime/mutexLocker.hpp"
68 #include "runtime/objectMonitor.hpp"
69 #include "runtime/orderAccess.inline.hpp"
70 #include "runtime/osThread.hpp"
71 #include "runtime/safepoint.hpp"
72 #include "runtime/sharedRuntime.hpp"
73 #include "runtime/statSampler.hpp"
74 #include "runtime/stubRoutines.hpp"
75 #include "runtime/sweeper.hpp"
76 #include "runtime/task.hpp"
77 #include "runtime/thread.inline.hpp"
78 #include "runtime/threadCritical.hpp"
79 #include "runtime/threadLocalStorage.hpp"
80 #include "runtime/vframe.hpp"
81 #include "runtime/vframeArray.hpp"
82 #include "runtime/vframe_hp.hpp"
83 #include "runtime/vmThread.hpp"
84 #include "runtime/vm_operations.hpp"
85 #include "runtime/vm_version.hpp"
86 #include "services/attachListener.hpp"
87 #include "services/management.hpp"
88 #include "services/memTracker.hpp"
89 #include "services/threadService.hpp"
90 #include "trace/traceMacros.hpp"
91 #include "trace/tracing.hpp"
92 #include "utilities/defaultStream.hpp"
93 #include "utilities/dtrace.hpp"
94 #include "utilities/events.hpp"
95 #include "utilities/macros.hpp"
96 #include "utilities/preserveException.hpp"
97 #if INCLUDE_ALL_GCS
98 #include "gc/cms/concurrentMarkSweepThread.hpp"
99 #include "gc/g1/concurrentMarkThread.inline.hpp"
100 #include "gc/parallel/pcTasks.hpp"
101 #endif // INCLUDE_ALL_GCS
102 #ifdef COMPILER1
103 #include "c1/c1_Compiler.hpp"
104 #endif
105 #ifdef COMPILER2
106 #include "opto/c2compiler.hpp"
107 #include "opto/idealGraphPrinter.hpp"
108 #endif
109 #if INCLUDE_RTM_OPT
110 #include "runtime/rtmLocking.hpp"
111 #endif
112
113 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
114
192 // stack and get_thread
193 set_stack_base(NULL);
194 set_stack_size(0);
195 set_self_raw_id(0);
196 set_lgrp_id(-1);
197 DEBUG_ONLY(clear_suspendible_thread();)
198
199 // allocated data structures
200 set_osthread(NULL);
201 set_resource_area(new (mtThread)ResourceArea());
202 DEBUG_ONLY(_current_resource_mark = NULL;)
203 set_handle_area(new (mtThread) HandleArea(NULL));
204 set_metadata_handles(new (ResourceObj::C_HEAP, mtClass) GrowableArray<Metadata*>(30, true));
205 set_active_handles(NULL);
206 set_free_handle_block(NULL);
207 set_last_handle_mark(NULL);
208
209 // This initial value ==> never claimed.
210 _oops_do_parity = 0;
211
212 // the handle mark links itself to last_handle_mark
213 new HandleMark(this);
214
215 // plain initialization
216 debug_only(_owned_locks = NULL;)
217 debug_only(_allow_allocation_count = 0;)
218 NOT_PRODUCT(_allow_safepoint_count = 0;)
219 NOT_PRODUCT(_skip_gcalot = false;)
220 _jvmti_env_iteration_count = 0;
221 set_allocated_bytes(0);
222 _vm_operation_started_count = 0;
223 _vm_operation_completed_count = 0;
224 _current_pending_monitor = NULL;
225 _current_pending_monitor_is_from_java = true;
226 _current_waiting_monitor = NULL;
227 _num_nested_signal = 0;
228 omFreeList = NULL;
229 omFreeCount = 0;
230 omFreeProvision = 32;
231 omInUseList = NULL;
1377 assert(PeriodicTask_lock->owned_by_self(), "PeriodicTask_lock required");
1378 PeriodicTask_lock->notify();
1379 }
1380
1381 void WatcherThread::print_on(outputStream* st) const {
1382 st->print("\"%s\" ", name());
1383 Thread::print_on(st);
1384 st->cr();
1385 }
1386
1387 // ======= JavaThread ========
1388
1389 // A JavaThread is a normal Java thread
1390
1391 void JavaThread::initialize() {
1392 // Initialize fields
1393
1394 // Set the claimed par_id to UINT_MAX (ie not claiming any par_ids)
1395 set_claimed_par_id(UINT_MAX);
1396
1397 set_saved_exception_pc(NULL);
1398 set_threadObj(NULL);
1399 _anchor.clear();
1400 set_entry_point(NULL);
1401 set_jni_functions(jni_functions());
1402 set_callee_target(NULL);
1403 set_vm_result(NULL);
1404 set_vm_result_2(NULL);
1405 set_vframe_array_head(NULL);
1406 set_vframe_array_last(NULL);
1407 set_deferred_locals(NULL);
1408 set_deopt_mark(NULL);
1409 set_deopt_nmethod(NULL);
1410 clear_must_deopt_id();
1411 set_monitor_chunks(NULL);
1412 set_next(NULL);
1413 set_thread_state(_thread_new);
1414 _terminated = _not_terminated;
1415 _privileged_stack_top = NULL;
1416 _array_for_gc = NULL;
1472 SATBMarkQueueSet JavaThread::_satb_mark_queue_set;
1473 DirtyCardQueueSet JavaThread::_dirty_card_queue_set;
1474 #endif // INCLUDE_ALL_GCS
1475
1476 JavaThread::JavaThread(bool is_attaching_via_jni) :
1477 Thread()
1478 #if INCLUDE_ALL_GCS
1479 , _satb_mark_queue(&_satb_mark_queue_set),
1480 _dirty_card_queue(&_dirty_card_queue_set)
1481 #endif // INCLUDE_ALL_GCS
1482 {
1483 initialize();
1484 if (is_attaching_via_jni) {
1485 _jni_attach_state = _attaching_via_jni;
1486 } else {
1487 _jni_attach_state = _not_attaching_via_jni;
1488 }
1489 assert(deferred_card_mark().is_empty(), "Default MemRegion ctor");
1490 }
1491
1492 bool JavaThread::reguard_stack(address cur_sp) {
1493 if (_stack_guard_state != stack_guard_yellow_disabled) {
1494 return true; // Stack already guarded or guard pages not needed.
1495 }
1496
1497 if (register_stack_overflow()) {
1498 // For those architectures which have separate register and
1499 // memory stacks, we must check the register stack to see if
1500 // it has overflowed.
1501 return false;
1502 }
1503
1504 // Java code never executes within the yellow zone: the latter is only
1505 // there to provoke an exception during stack banging. If java code
1506 // is executing there, either StackShadowPages should be larger, or
1507 // some exception code in c1, c2 or the interpreter isn't unwinding
1508 // when it should.
1509 guarantee(cur_sp > stack_yellow_zone_base(), "not enough space to reguard - increase StackShadowPages");
1510
1511 enable_stack_yellow_zone();
1512 return true;
1513 }
1514
1515 bool JavaThread::reguard_stack(void) {
1516 return reguard_stack(os::current_stack_pointer());
1517 }
1518
1519
1520 void JavaThread::block_if_vm_exited() {
1521 if (_terminated == _vm_exited) {
1522 // _vm_exited is set at safepoint, and Threads_lock is never released
1523 // we will block here forever
1524 Threads_lock->lock_without_safepoint_check();
1525 ShouldNotReachHere();
1526 }
1527 }
1528
1529
1530 // Remove this ifdef when C1 is ported to the compiler interface.
1531 static void compiler_thread_entry(JavaThread* thread, TRAPS);
1532 static void sweeper_thread_entry(JavaThread* thread, TRAPS);
1533
1534 JavaThread::JavaThread(ThreadFunction entry_point, size_t stack_sz) :
1535 Thread()
1536 #if INCLUDE_ALL_GCS
1537 , _satb_mark_queue(&_satb_mark_queue_set),
1538 _dirty_card_queue(&_dirty_card_queue_set)
1539 #endif // INCLUDE_ALL_GCS
1540 {
1541 initialize();
1542 _jni_attach_state = _not_attaching_via_jni;
1543 set_entry_point(entry_point);
1544 // Create the native thread itself.
1545 // %note runtime_23
1546 os::ThreadType thr_type = os::java_thread;
1547 thr_type = entry_point == &compiler_thread_entry ? os::compiler_thread :
1548 os::java_thread;
1647 void JavaThread::thread_main_inner() {
1648 assert(JavaThread::current() == this, "sanity check");
1649 assert(this->threadObj() != NULL, "just checking");
1650
1651 // Execute thread entry point unless this thread has a pending exception
1652 // or has been stopped before starting.
1653 // Note: Due to JVM_StopThread we can have pending exceptions already!
1654 if (!this->has_pending_exception() &&
1655 !java_lang_Thread::is_stillborn(this->threadObj())) {
1656 {
1657 ResourceMark rm(this);
1658 this->set_native_thread_name(this->get_thread_name());
1659 }
1660 HandleMark hm(this);
1661 this->entry_point()(this, this);
1662 }
1663
1664 DTRACE_THREAD_PROBE(stop, this);
1665
1666 this->exit(false);
1667 delete this;
1668 }
1669
1670
1671 static void ensure_join(JavaThread* thread) {
1672 // We do not need to grap the Threads_lock, since we are operating on ourself.
1673 Handle threadObj(thread, thread->threadObj());
1674 assert(threadObj.not_null(), "java thread object must exist");
1675 ObjectLocker lock(threadObj, thread);
1676 // Ignore pending exception (ThreadDeath), since we are exiting anyway
1677 thread->clear_pending_exception();
1678 // Thread is exiting. So set thread_status field in java.lang.Thread class to TERMINATED.
1679 java_lang_Thread::set_thread_status(threadObj(), java_lang_Thread::TERMINATED);
1680 // Clear the native thread instance - this makes isAlive return false and allows the join()
1681 // to complete once we've done the notify_all below
1682 java_lang_Thread::set_thread(threadObj(), NULL);
1683 lock.notify_all(thread);
1684 // Ignore pending exception (ThreadDeath), since we are exiting anyway
1685 thread->clear_pending_exception();
1686 }
1687
1919 if (free_handle_block() != NULL) {
1920 JNIHandleBlock* block = free_handle_block();
1921 set_free_handle_block(NULL);
1922 JNIHandleBlock::release_block(block);
1923 }
1924
1925 // These have to be removed while this is still a valid thread.
1926 remove_stack_guard_pages();
1927
1928 if (UseTLAB) {
1929 tlab().make_parsable(true); // retire TLAB, if any
1930 }
1931
1932 #if INCLUDE_ALL_GCS
1933 if (UseG1GC) {
1934 flush_barrier_queues();
1935 }
1936 #endif // INCLUDE_ALL_GCS
1937
1938 Threads::remove(this);
1939 delete this;
1940 }
1941
1942
1943
1944
1945 JavaThread* JavaThread::active() {
1946 Thread* thread = ThreadLocalStorage::thread();
1947 assert(thread != NULL, "just checking");
1948 if (thread->is_Java_thread()) {
1949 return (JavaThread*) thread;
1950 } else {
1951 assert(thread->is_VM_thread(), "this must be a vm thread");
1952 VM_Operation* op = ((VMThread*) thread)->vm_operation();
1953 JavaThread *ret=op == NULL ? NULL : (JavaThread *)op->calling_thread();
1954 assert(ret->is_Java_thread(), "must be a Java thread");
1955 return ret;
1956 }
1957 }
1958
1959 bool JavaThread::is_lock_owned(address adr) const {
3183 void CodeCacheSweeperThread::oops_do(OopClosure* f, CLDClosure* cld_f, CodeBlobClosure* cf) {
3184 JavaThread::oops_do(f, cld_f, cf);
3185 if (_scanned_nmethod != NULL && cf != NULL) {
3186 // Safepoints can occur when the sweeper is scanning an nmethod so
3187 // process it here to make sure it isn't unloaded in the middle of
3188 // a scan.
3189 cf->do_code_blob(_scanned_nmethod);
3190 }
3191 }
3192
3193
3194 // ======= Threads ========
3195
3196 // The Threads class links together all active threads, and provides
3197 // operations over all threads. It is protected by its own Mutex
3198 // lock, which is also used in other contexts to protect thread
3199 // operations from having the thread being operated on from exiting
3200 // and going away unexpectedly (e.g., safepoint synchronization)
3201
3202 JavaThread* Threads::_thread_list = NULL;
3203 int Threads::_number_of_threads = 0;
3204 int Threads::_number_of_non_daemon_threads = 0;
3205 int Threads::_return_code = 0;
3206 int Threads::_thread_claim_parity = 0;
3207 size_t JavaThread::_stack_size_at_create = 0;
3208 #ifdef ASSERT
3209 bool Threads::_vm_complete = false;
3210 #endif
3211
3212 // All JavaThreads
3213 #define ALL_JAVA_THREADS(X) for (JavaThread* X = _thread_list; X; X = X->next())
3214
3215 // All JavaThreads + all non-JavaThreads (i.e., every thread in the system)
3216 void Threads::threads_do(ThreadClosure* tc) {
3217 assert_locked_or_safepoint(Threads_lock);
3218 // ALL_JAVA_THREADS iterates through all JavaThreads
3219 ALL_JAVA_THREADS(p) {
3220 tc->do_thread(p);
3221 }
3222 // Someday we could have a table or list of all non-JavaThreads.
3223 // For now, just manually iterate through them.
3224 tc->do_thread(VMThread::vm_thread());
3225 Universe::heap()->gc_threads_do(tc);
3226 WatcherThread *wt = WatcherThread::watcher_thread();
3227 // Strictly speaking, the following NULL check isn't sufficient to make sure
3228 // the data for WatcherThread is still valid upon being examined. However,
3229 // considering that WatchThread terminates when the VM is on the way to
3230 // exit at safepoint, the chance of the above is extremely small. The right
3231 // way to prevent termination of WatcherThread would be to acquire
3232 // Terminator_lock, but we can't do that without violating the lock rank
3233 // checking in some cases.
3234 if (wt != NULL) {
3235 tc->do_thread(wt);
3236 }
3237
3238 // If CompilerThreads ever become non-JavaThreads, add them here
3239 }
3240
3241 void Threads::initialize_java_lang_classes(JavaThread* main_thread, TRAPS) {
3242 TraceTime timer("Initialize java.lang classes", TraceStartupTime);
3243
3244 if (EagerXrunInit && Arguments::init_libraries_at_startup()) {
3245 create_vm_init_libraries();
3246 }
3247
3248 initialize_class(vmSymbols::java_lang_String(), CHECK);
3249
3250 // Initialize java_lang.System (needed before creating the thread)
3251 initialize_class(vmSymbols::java_lang_System(), CHECK);
3252 // The VM creates & returns objects of this class. Make sure it's initialized.
3253 initialize_class(vmSymbols::java_lang_Class(), CHECK);
3254 initialize_class(vmSymbols::java_lang_ThreadGroup(), CHECK);
3255 Handle thread_group = create_initial_thread_group(CHECK);
3256 Universe::set_main_thread_group(thread_group());
3257 initialize_class(vmSymbols::java_lang_Thread(), CHECK);
3258 oop thread_object = create_initial_thread(thread_group, main_thread, CHECK);
3259 main_thread->set_threadObj(thread_object);
3260 // Set thread status to running since main thread has
3380
3381 // Initialize global data structures and create system classes in heap
3382 vm_init_globals();
3383
3384 // Attach the main thread to this os thread
3385 JavaThread* main_thread = new JavaThread();
3386 main_thread->set_thread_state(_thread_in_vm);
3387 // must do this before set_active_handles and initialize_thread_local_storage
3388 // Note: on solaris initialize_thread_local_storage() will (indirectly)
3389 // change the stack size recorded here to one based on the java thread
3390 // stacksize. This adjusted size is what is used to figure the placement
3391 // of the guard pages.
3392 main_thread->record_stack_base_and_size();
3393 main_thread->initialize_thread_local_storage();
3394
3395 main_thread->set_active_handles(JNIHandleBlock::allocate_block());
3396
3397 if (!main_thread->set_as_starting_thread()) {
3398 vm_shutdown_during_initialization(
3399 "Failed necessary internal allocation. Out of swap space");
3400 delete main_thread;
3401 *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again
3402 return JNI_ENOMEM;
3403 }
3404
3405 // Enable guard page *after* os::create_main_thread(), otherwise it would
3406 // crash Linux VM, see notes in os_linux.cpp.
3407 main_thread->create_stack_guard_pages();
3408
3409 // Initialize Java-Level synchronization subsystem
3410 ObjectMonitor::Initialize();
3411
3412 // Initialize global modules
3413 jint status = init_globals();
3414 if (status != JNI_OK) {
3415 delete main_thread;
3416 *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again
3417 return status;
3418 }
3419
3420 // Should be done after the heap is fully created
3421 main_thread->cache_global_variables();
3422
3423 HandleMark hm;
3424
3425 { MutexLocker mu(Threads_lock);
3426 Threads::add(main_thread);
3427 }
3428
3429 // Any JVMTI raw monitors entered in onload will transition into
3430 // real raw monitor. VM is setup enough here for raw monitor enter.
3431 JvmtiExport::transition_pending_onload_raw_monitors();
3432
3433 // Create the VMThread
3434 { TraceTime timer("Start VMThread", TraceStartupTime);
3435 VMThread::create();
3945 IdealGraphPrinter::clean_up();
3946 #endif
3947
3948 // Now, all Java threads are gone except daemon threads. Daemon threads
3949 // running Java code or in VM are stopped by the Safepoint. However,
3950 // daemon threads executing native code are still running. But they
3951 // will be stopped at native=>Java/VM barriers. Note that we can't
3952 // simply kill or suspend them, as it is inherently deadlock-prone.
3953
3954 #ifndef PRODUCT
3955 // disable function tracing at JNI/JVM barriers
3956 TraceJNICalls = false;
3957 TraceJVMCalls = false;
3958 TraceRuntimeCalls = false;
3959 #endif
3960
3961 VM_Exit::set_vm_exited();
3962
3963 notify_vm_shutdown();
3964
3965 delete thread;
3966
3967 // exit_globals() will delete tty
3968 exit_globals();
3969
3970 return true;
3971 }
3972
3973
3974 jboolean Threads::is_supported_jni_version_including_1_1(jint version) {
3975 if (version == JNI_VERSION_1_1) return JNI_TRUE;
3976 return is_supported_jni_version(version);
3977 }
3978
3979
3980 jboolean Threads::is_supported_jni_version(jint version) {
3981 if (version == JNI_VERSION_1_2) return JNI_TRUE;
3982 if (version == JNI_VERSION_1_4) return JNI_TRUE;
3983 if (version == JNI_VERSION_1_6) return JNI_TRUE;
3984 if (version == JNI_VERSION_1_8) return JNI_TRUE;
3985 return JNI_FALSE;
3986 }
3987
3988
3989 void Threads::add(JavaThread* p, bool force_daemon) {
3990 // The threads lock must be owned at this point
3991 assert_locked_or_safepoint(Threads_lock);
3992
3993 // See the comment for this method in thread.hpp for its purpose and
3994 // why it is called here.
3995 p->initialize_queues();
3996 p->set_next(_thread_list);
3997 _thread_list = p;
3998 _number_of_threads++;
3999 oop threadObj = p->threadObj();
4000 bool daemon = true;
4001 // Bootstrapping problem: threadObj can be null for initial
4002 // JavaThread (or for threads attached via JNI)
4003 if ((!force_daemon) && (threadObj == NULL || !java_lang_Thread::is_daemon(threadObj))) {
4004 _number_of_non_daemon_threads++;
4005 daemon = false;
4006 }
4007
4008 ThreadService::add_thread(p, daemon);
4009
4010 // Possible GC point.
4011 Events::log(p, "Thread added: " INTPTR_FORMAT, p);
4012 }
4013
4014 void Threads::remove(JavaThread* p) {
4015 // Extra scope needed for Thread_lock, so we can check
4016 // that we do not remove thread without safepoint code notice
4017 { MutexLocker ml(Threads_lock);
4018
4019 assert(includes(p), "p must be present");
4020
4021 JavaThread* current = _thread_list;
4022 JavaThread* prev = NULL;
4023
4024 while (current != p) {
4025 prev = current;
4026 current = current->next();
4027 }
4028
4029 if (prev) {
4030 prev->set_next(current->next());
4031 } else {
4032 _thread_list = p->next();
4033 }
4034 _number_of_threads--;
4035 oop threadObj = p->threadObj();
4036 bool daemon = true;
4037 if (threadObj == NULL || !java_lang_Thread::is_daemon(threadObj)) {
4038 _number_of_non_daemon_threads--;
4039 daemon = false;
4040
4041 // Only one thread left, do a notify on the Threads_lock so a thread waiting
4042 // on destroy_vm will wake up.
4043 if (number_of_non_daemon_threads() == 1) {
4044 Threads_lock->notify_all();
4045 }
4046 }
4047 ThreadService::remove_thread(p, daemon);
4048
4049 // Make sure that safepoint code disregard this thread. This is needed since
4050 // the thread might mess around with locks after this point. This can cause it
4051 // to do callbacks into the safepoint code. However, the safepoint code is not aware
4052 // of this thread since it is removed from the queue.
4053 p->set_terminated_value();
|
41 #include "memory/oopFactory.hpp"
42 #include "memory/universe.inline.hpp"
43 #include "oops/instanceKlass.hpp"
44 #include "oops/objArrayOop.hpp"
45 #include "oops/oop.inline.hpp"
46 #include "oops/symbol.hpp"
47 #include "oops/verifyOopClosure.hpp"
48 #include "prims/jvm_misc.hpp"
49 #include "prims/jvmtiExport.hpp"
50 #include "prims/jvmtiThreadState.hpp"
51 #include "prims/privilegedStack.hpp"
52 #include "runtime/arguments.hpp"
53 #include "runtime/atomic.inline.hpp"
54 #include "runtime/biasedLocking.hpp"
55 #include "runtime/commandLineFlagConstraintList.hpp"
56 #include "runtime/commandLineFlagRangeList.hpp"
57 #include "runtime/deoptimization.hpp"
58 #include "runtime/fprofiler.hpp"
59 #include "runtime/frame.inline.hpp"
60 #include "runtime/globals.hpp"
61 #include "runtime/globalSynchronizer.hpp"
62 #include "runtime/init.hpp"
63 #include "runtime/interfaceSupport.hpp"
64 #include "runtime/java.hpp"
65 #include "runtime/javaCalls.hpp"
66 #include "runtime/jniPeriodicChecker.hpp"
67 #include "runtime/memprofiler.hpp"
68 #include "runtime/mutexLocker.hpp"
69 #include "runtime/objectMonitor.hpp"
70 #include "runtime/orderAccess.inline.hpp"
71 #include "runtime/osThread.hpp"
72 #include "runtime/safepoint.hpp"
73 #include "runtime/sharedRuntime.hpp"
74 #include "runtime/statSampler.hpp"
75 #include "runtime/stubRoutines.hpp"
76 #include "runtime/sweeper.hpp"
77 #include "runtime/task.hpp"
78 #include "runtime/thread.inline.hpp"
79 #include "runtime/threadCritical.hpp"
80 #include "runtime/threadLocalStorage.hpp"
81 #include "runtime/vframe.hpp"
82 #include "runtime/vframeArray.hpp"
83 #include "runtime/vframe_hp.hpp"
84 #include "runtime/vmThread.hpp"
85 #include "runtime/vm_operations.hpp"
86 #include "runtime/vm_version.hpp"
87 #include "services/attachListener.hpp"
88 #include "services/management.hpp"
89 #include "services/memTracker.hpp"
90 #include "services/threadService.hpp"
91 #include "trace/traceMacros.hpp"
92 #include "trace/tracing.hpp"
93 #include "utilities/defaultStream.hpp"
94 #include "utilities/dtrace.hpp"
95 #include "utilities/events.hpp"
96 #include "utilities/hashtable.hpp"
97 #include "utilities/macros.hpp"
98 #include "utilities/preserveException.hpp"
99 #if INCLUDE_ALL_GCS
100 #include "gc/cms/concurrentMarkSweepThread.hpp"
101 #include "gc/g1/concurrentMarkThread.inline.hpp"
102 #include "gc/parallel/pcTasks.hpp"
103 #endif // INCLUDE_ALL_GCS
104 #ifdef COMPILER1
105 #include "c1/c1_Compiler.hpp"
106 #endif
107 #ifdef COMPILER2
108 #include "opto/c2compiler.hpp"
109 #include "opto/idealGraphPrinter.hpp"
110 #endif
111 #if INCLUDE_RTM_OPT
112 #include "runtime/rtmLocking.hpp"
113 #endif
114
115 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
116
194 // stack and get_thread
195 set_stack_base(NULL);
196 set_stack_size(0);
197 set_self_raw_id(0);
198 set_lgrp_id(-1);
199 DEBUG_ONLY(clear_suspendible_thread();)
200
201 // allocated data structures
202 set_osthread(NULL);
203 set_resource_area(new (mtThread)ResourceArea());
204 DEBUG_ONLY(_current_resource_mark = NULL;)
205 set_handle_area(new (mtThread) HandleArea(NULL));
206 set_metadata_handles(new (ResourceObj::C_HEAP, mtClass) GrowableArray<Metadata*>(30, true));
207 set_active_handles(NULL);
208 set_free_handle_block(NULL);
209 set_last_handle_mark(NULL);
210
211 // This initial value ==> never claimed.
212 _oops_do_parity = 0;
213
214 _java_threads_do_hp = NULL;
215
216 // the handle mark links itself to last_handle_mark
217 new HandleMark(this);
218
219 // plain initialization
220 debug_only(_owned_locks = NULL;)
221 debug_only(_allow_allocation_count = 0;)
222 NOT_PRODUCT(_allow_safepoint_count = 0;)
223 NOT_PRODUCT(_skip_gcalot = false;)
224 _jvmti_env_iteration_count = 0;
225 set_allocated_bytes(0);
226 _vm_operation_started_count = 0;
227 _vm_operation_completed_count = 0;
228 _current_pending_monitor = NULL;
229 _current_pending_monitor_is_from_java = true;
230 _current_waiting_monitor = NULL;
231 _num_nested_signal = 0;
232 omFreeList = NULL;
233 omFreeCount = 0;
234 omFreeProvision = 32;
235 omInUseList = NULL;
1381 assert(PeriodicTask_lock->owned_by_self(), "PeriodicTask_lock required");
1382 PeriodicTask_lock->notify();
1383 }
1384
1385 void WatcherThread::print_on(outputStream* st) const {
1386 st->print("\"%s\" ", name());
1387 Thread::print_on(st);
1388 st->cr();
1389 }
1390
1391 // ======= JavaThread ========
1392
1393 // A JavaThread is a normal Java thread
1394
1395 void JavaThread::initialize() {
1396 // Initialize fields
1397
1398 // Set the claimed par_id to UINT_MAX (ie not claiming any par_ids)
1399 set_claimed_par_id(UINT_MAX);
1400
1401 set_yieldpoint(false);
1402 _serialized_memory_version = GlobalSynchronizer::global_serialized_memory_version();
1403 _force_yield = false;
1404
1405 set_saved_exception_pc(NULL);
1406 set_threadObj(NULL);
1407 _anchor.clear();
1408 set_entry_point(NULL);
1409 set_jni_functions(jni_functions());
1410 set_callee_target(NULL);
1411 set_vm_result(NULL);
1412 set_vm_result_2(NULL);
1413 set_vframe_array_head(NULL);
1414 set_vframe_array_last(NULL);
1415 set_deferred_locals(NULL);
1416 set_deopt_mark(NULL);
1417 set_deopt_nmethod(NULL);
1418 clear_must_deopt_id();
1419 set_monitor_chunks(NULL);
1420 set_next(NULL);
1421 set_thread_state(_thread_new);
1422 _terminated = _not_terminated;
1423 _privileged_stack_top = NULL;
1424 _array_for_gc = NULL;
1480 SATBMarkQueueSet JavaThread::_satb_mark_queue_set;
1481 DirtyCardQueueSet JavaThread::_dirty_card_queue_set;
1482 #endif // INCLUDE_ALL_GCS
1483
1484 JavaThread::JavaThread(bool is_attaching_via_jni) :
1485 Thread()
1486 #if INCLUDE_ALL_GCS
1487 , _satb_mark_queue(&_satb_mark_queue_set),
1488 _dirty_card_queue(&_dirty_card_queue_set)
1489 #endif // INCLUDE_ALL_GCS
1490 {
1491 initialize();
1492 if (is_attaching_via_jni) {
1493 _jni_attach_state = _attaching_via_jni;
1494 } else {
1495 _jni_attach_state = _not_attaching_via_jni;
1496 }
1497 assert(deferred_card_mark().is_empty(), "Default MemRegion ctor");
1498 }
1499
1500 void JavaThread::update_serialized_memory_version() {
1501 int global_version = GlobalSynchronizer::global_serialized_memory_version();
1502 int local_version = OrderAccess::load_acquire(&_serialized_memory_version);
1503 if (local_version != global_version) {
1504 assert(local_version < global_version, "sanity");
1505 OrderAccess::release_store(&_serialized_memory_version, global_version);
1506 }
1507 }
1508
1509 bool JavaThread::reguard_stack(address cur_sp) {
1510 if (_stack_guard_state != stack_guard_yellow_disabled) {
1511 return true; // Stack already guarded or guard pages not needed.
1512 }
1513
1514 if (register_stack_overflow()) {
1515 // For those architectures which have separate register and
1516 // memory stacks, we must check the register stack to see if
1517 // it has overflowed.
1518 return false;
1519 }
1520
1521 // Java code never executes within the yellow zone: the latter is only
1522 // there to provoke an exception during stack banging. If java code
1523 // is executing there, either StackShadowPages should be larger, or
1524 // some exception code in c1, c2 or the interpreter isn't unwinding
1525 // when it should.
1526 guarantee(cur_sp > stack_yellow_zone_base(), "not enough space to reguard - increase StackShadowPages");
1527
1528 enable_stack_yellow_zone();
1529 return true;
1530 }
1531
1532 bool JavaThread::reguard_stack(void) {
1533 return reguard_stack(os::current_stack_pointer());
1534 }
1535
1536
1537 void JavaThread::block_if_vm_exited() {
1538 if (_terminated == _vm_exited) {
1539 // _vm_exited is set at safepoint, and Threads_lock is never released
1540 // we will block here forever
1541 Threads_lock->lock_without_safepoint_check();
1542 ShouldNotReachHere();
1543 }
1544 }
1545
1546 bool JavaThread::is_online_vm() {
1547 return thread_state() == _thread_in_Java;
1548 }
1549
1550 bool JavaThread::is_online_os() {
1551 return _osthread->is_online();
1552 }
1553
1554 // Remove this ifdef when C1 is ported to the compiler interface.
1555 static void compiler_thread_entry(JavaThread* thread, TRAPS);
1556 static void sweeper_thread_entry(JavaThread* thread, TRAPS);
1557
1558 JavaThread::JavaThread(ThreadFunction entry_point, size_t stack_sz) :
1559 Thread()
1560 #if INCLUDE_ALL_GCS
1561 , _satb_mark_queue(&_satb_mark_queue_set),
1562 _dirty_card_queue(&_dirty_card_queue_set)
1563 #endif // INCLUDE_ALL_GCS
1564 {
1565 initialize();
1566 _jni_attach_state = _not_attaching_via_jni;
1567 set_entry_point(entry_point);
1568 // Create the native thread itself.
1569 // %note runtime_23
1570 os::ThreadType thr_type = os::java_thread;
1571 thr_type = entry_point == &compiler_thread_entry ? os::compiler_thread :
1572 os::java_thread;
1671 void JavaThread::thread_main_inner() {
1672 assert(JavaThread::current() == this, "sanity check");
1673 assert(this->threadObj() != NULL, "just checking");
1674
1675 // Execute thread entry point unless this thread has a pending exception
1676 // or has been stopped before starting.
1677 // Note: Due to JVM_StopThread we can have pending exceptions already!
1678 if (!this->has_pending_exception() &&
1679 !java_lang_Thread::is_stillborn(this->threadObj())) {
1680 {
1681 ResourceMark rm(this);
1682 this->set_native_thread_name(this->get_thread_name());
1683 }
1684 HandleMark hm(this);
1685 this->entry_point()(this, this);
1686 }
1687
1688 DTRACE_THREAD_PROBE(stop, this);
1689
1690 this->exit(false);
1691 Threads::smr_free(this, false);
1692 }
1693
1694
1695 static void ensure_join(JavaThread* thread) {
1696 // We do not need to grap the Threads_lock, since we are operating on ourself.
1697 Handle threadObj(thread, thread->threadObj());
1698 assert(threadObj.not_null(), "java thread object must exist");
1699 ObjectLocker lock(threadObj, thread);
1700 // Ignore pending exception (ThreadDeath), since we are exiting anyway
1701 thread->clear_pending_exception();
1702 // Thread is exiting. So set thread_status field in java.lang.Thread class to TERMINATED.
1703 java_lang_Thread::set_thread_status(threadObj(), java_lang_Thread::TERMINATED);
1704 // Clear the native thread instance - this makes isAlive return false and allows the join()
1705 // to complete once we've done the notify_all below
1706 java_lang_Thread::set_thread(threadObj(), NULL);
1707 lock.notify_all(thread);
1708 // Ignore pending exception (ThreadDeath), since we are exiting anyway
1709 thread->clear_pending_exception();
1710 }
1711
1943 if (free_handle_block() != NULL) {
1944 JNIHandleBlock* block = free_handle_block();
1945 set_free_handle_block(NULL);
1946 JNIHandleBlock::release_block(block);
1947 }
1948
1949 // These have to be removed while this is still a valid thread.
1950 remove_stack_guard_pages();
1951
1952 if (UseTLAB) {
1953 tlab().make_parsable(true); // retire TLAB, if any
1954 }
1955
1956 #if INCLUDE_ALL_GCS
1957 if (UseG1GC) {
1958 flush_barrier_queues();
1959 }
1960 #endif // INCLUDE_ALL_GCS
1961
1962 Threads::remove(this);
1963 Threads::smr_free(this, false);
1964 }
1965
1966
1967
1968
1969 JavaThread* JavaThread::active() {
1970 Thread* thread = ThreadLocalStorage::thread();
1971 assert(thread != NULL, "just checking");
1972 if (thread->is_Java_thread()) {
1973 return (JavaThread*) thread;
1974 } else {
1975 assert(thread->is_VM_thread(), "this must be a vm thread");
1976 VM_Operation* op = ((VMThread*) thread)->vm_operation();
1977 JavaThread *ret=op == NULL ? NULL : (JavaThread *)op->calling_thread();
1978 assert(ret->is_Java_thread(), "must be a Java thread");
1979 return ret;
1980 }
1981 }
1982
1983 bool JavaThread::is_lock_owned(address adr) const {
3207 void CodeCacheSweeperThread::oops_do(OopClosure* f, CLDClosure* cld_f, CodeBlobClosure* cf) {
3208 JavaThread::oops_do(f, cld_f, cf);
3209 if (_scanned_nmethod != NULL && cf != NULL) {
3210 // Safepoints can occur when the sweeper is scanning an nmethod so
3211 // process it here to make sure it isn't unloaded in the middle of
3212 // a scan.
3213 cf->do_code_blob(_scanned_nmethod);
3214 }
3215 }
3216
3217
3218 // ======= Threads ========
3219
3220 // The Threads class links together all active threads, and provides
3221 // operations over all threads. It is protected by its own Mutex
3222 // lock, which is also used in other contexts to protect thread
3223 // operations from having the thread being operated on from exiting
3224 // and going away unexpectedly (e.g., safepoint synchronization)
3225
3226 JavaThread* Threads::_thread_list = NULL;
3227 JavaThread* Threads::_thread_smr_list = NULL;
3228 JavaThread** Threads::_thread_smr_list_list = NULL;
3229 int Threads::_number_of_threads = 0;
3230 int Threads::_number_of_non_daemon_threads = 0;
3231 int Threads::_return_code = 0;
3232 int Threads::_thread_claim_parity = 0;
3233 JavaThread **volatile Threads::_fast_java_thread_list = NULL;
3234 size_t JavaThread::_stack_size_at_create = 0;
3235 #ifdef ASSERT
3236 bool Threads::_vm_complete = false;
3237 #endif
3238
3239 // All JavaThreads
3240 #define ALL_JAVA_THREADS(X) for (JavaThread* X = _thread_list; X; X = X->next())
3241
3242 // All JavaThreads + all non-JavaThreads (i.e., every thread in the system)
3243 void Threads::threads_do(ThreadClosure* tc) {
3244 assert_locked_or_safepoint(Threads_lock);
3245 // ALL_JAVA_THREADS iterates through all JavaThreads
3246 ALL_JAVA_THREADS(p) {
3247 tc->do_thread(p);
3248 }
3249 // Someday we could have a table or list of all non-JavaThreads.
3250 // For now, just manually iterate through them.
3251 tc->do_thread(VMThread::vm_thread());
3252 Universe::heap()->gc_threads_do(tc);
3253 WatcherThread *wt = WatcherThread::watcher_thread();
3254 // Strictly speaking, the following NULL check isn't sufficient to make sure
3255 // the data for WatcherThread is still valid upon being examined. However,
3256 // considering that WatchThread terminates when the VM is on the way to
3257 // exit at safepoint, the chance of the above is extremely small. The right
3258 // way to prevent termination of WatcherThread would be to acquire
3259 // Terminator_lock, but we can't do that without violating the lock rank
3260 // checking in some cases.
3261 if (wt != NULL) {
3262 tc->do_thread(wt);
3263 }
3264
3265 // If CompilerThreads ever become non-JavaThreads, add them here
3266 }
3267
3268 void Threads::java_threads_do_fast(ThreadClosure *tc, Thread *self) {
3269 JavaThread **threads;
3270
3271 // Stable load of thread list w.r.t. hazard pointer for SMR
3272 do {
3273 threads = (JavaThread**)OrderAccess::load_ptr_acquire((volatile void*)&_fast_java_thread_list);
3274 OrderAccess::release_store_ptr_fence((volatile void*)&self->_java_threads_do_hp, (void*)threads);
3275 } while ((JavaThread**)OrderAccess::load_ptr_acquire((volatile void*)&_fast_java_thread_list) != threads);
3276 if (threads == NULL) return;
3277
3278 for (JavaThread **current = threads; *current != NULL; current++) {
3279 tc->do_thread(*current);
3280 }
3281 OrderAccess::release_store_ptr_fence((volatile void*)&self->_java_threads_do_hp, NULL);
3282 }
3283
3284 void Threads::initialize_java_lang_classes(JavaThread* main_thread, TRAPS) {
3285 TraceTime timer("Initialize java.lang classes", TraceStartupTime);
3286
3287 if (EagerXrunInit && Arguments::init_libraries_at_startup()) {
3288 create_vm_init_libraries();
3289 }
3290
3291 initialize_class(vmSymbols::java_lang_String(), CHECK);
3292
3293 // Initialize java_lang.System (needed before creating the thread)
3294 initialize_class(vmSymbols::java_lang_System(), CHECK);
3295 // The VM creates & returns objects of this class. Make sure it's initialized.
3296 initialize_class(vmSymbols::java_lang_Class(), CHECK);
3297 initialize_class(vmSymbols::java_lang_ThreadGroup(), CHECK);
3298 Handle thread_group = create_initial_thread_group(CHECK);
3299 Universe::set_main_thread_group(thread_group());
3300 initialize_class(vmSymbols::java_lang_Thread(), CHECK);
3301 oop thread_object = create_initial_thread(thread_group, main_thread, CHECK);
3302 main_thread->set_threadObj(thread_object);
3303 // Set thread status to running since main thread has
3423
3424 // Initialize global data structures and create system classes in heap
3425 vm_init_globals();
3426
3427 // Attach the main thread to this os thread
3428 JavaThread* main_thread = new JavaThread();
3429 main_thread->set_thread_state(_thread_in_vm);
3430 // must do this before set_active_handles and initialize_thread_local_storage
3431 // Note: on solaris initialize_thread_local_storage() will (indirectly)
3432 // change the stack size recorded here to one based on the java thread
3433 // stacksize. This adjusted size is what is used to figure the placement
3434 // of the guard pages.
3435 main_thread->record_stack_base_and_size();
3436 main_thread->initialize_thread_local_storage();
3437
3438 main_thread->set_active_handles(JNIHandleBlock::allocate_block());
3439
3440 if (!main_thread->set_as_starting_thread()) {
3441 vm_shutdown_during_initialization(
3442 "Failed necessary internal allocation. Out of swap space");
3443 smr_free(main_thread, false);
3444 *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again
3445 return JNI_ENOMEM;
3446 }
3447
3448 // Enable guard page *after* os::create_main_thread(), otherwise it would
3449 // crash Linux VM, see notes in os_linux.cpp.
3450 main_thread->create_stack_guard_pages();
3451
3452 // Initialize Java-Level synchronization subsystem
3453 ObjectMonitor::Initialize();
3454
3455 // Initialize global modules
3456 jint status = init_globals();
3457 if (status != JNI_OK) {
3458 smr_free(main_thread, false);
3459 *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again
3460 return status;
3461 }
3462
3463 // Should be done after the heap is fully created
3464 main_thread->cache_global_variables();
3465
3466 HandleMark hm;
3467
3468 { MutexLocker mu(Threads_lock);
3469 Threads::add(main_thread);
3470 }
3471
3472 // Any JVMTI raw monitors entered in onload will transition into
3473 // real raw monitor. VM is setup enough here for raw monitor enter.
3474 JvmtiExport::transition_pending_onload_raw_monitors();
3475
3476 // Create the VMThread
3477 { TraceTime timer("Start VMThread", TraceStartupTime);
3478 VMThread::create();
3988 IdealGraphPrinter::clean_up();
3989 #endif
3990
3991 // Now, all Java threads are gone except daemon threads. Daemon threads
3992 // running Java code or in VM are stopped by the Safepoint. However,
3993 // daemon threads executing native code are still running. But they
3994 // will be stopped at native=>Java/VM barriers. Note that we can't
3995 // simply kill or suspend them, as it is inherently deadlock-prone.
3996
3997 #ifndef PRODUCT
3998 // disable function tracing at JNI/JVM barriers
3999 TraceJNICalls = false;
4000 TraceJVMCalls = false;
4001 TraceRuntimeCalls = false;
4002 #endif
4003
4004 VM_Exit::set_vm_exited();
4005
4006 notify_vm_shutdown();
4007
4008 smr_free(thread, true);
4009
4010 // exit_globals() will delete tty
4011 exit_globals();
4012
4013 return true;
4014 }
4015
4016
4017 jboolean Threads::is_supported_jni_version_including_1_1(jint version) {
4018 if (version == JNI_VERSION_1_1) return JNI_TRUE;
4019 return is_supported_jni_version(version);
4020 }
4021
4022
4023 jboolean Threads::is_supported_jni_version(jint version) {
4024 if (version == JNI_VERSION_1_2) return JNI_TRUE;
4025 if (version == JNI_VERSION_1_4) return JNI_TRUE;
4026 if (version == JNI_VERSION_1_6) return JNI_TRUE;
4027 if (version == JNI_VERSION_1_8) return JNI_TRUE;
4028 return JNI_FALSE;
4029 }
4030
4031 class ThreadScanEntry: public BasicHashtableEntry<mtThread> {
4032 public:
4033 void *_pointer;
4034
4035 ThreadScanEntry* next() {
4036 return (ThreadScanEntry*)BasicHashtableEntry<mtThread>::next();
4037 }
4038
4039 const void* pointer() { return _pointer; }
4040 void set_pointer(void* pointer) { _pointer = pointer; }
4041 };
4042
4043 class ThreadScanHashtable : public BasicHashtable<mtThread> {
4044 private:
4045 inline unsigned int compute_hash(void* pointer) {
4046 return (unsigned int)(((uint32_t)(uintptr_t)pointer) * 2654435761u);
4047 }
4048
4049 ThreadScanEntry* bucket(int index) {
4050 return (ThreadScanEntry*)BasicHashtable<mtThread>::bucket(index);
4051 }
4052
4053 ThreadScanEntry* get_entry(int index, unsigned int hash, void *pointer) {
4054 for (ThreadScanEntry* pp = bucket(index); pp != NULL; pp = pp->next()) {
4055 if (pp->hash() == hash &&
4056 pp->pointer() == pointer) {
4057 return pp;
4058 }
4059 }
4060 return NULL;
4061 }
4062
4063 public:
4064 ThreadScanHashtable(int table_size)
4065 : BasicHashtable<mtThread>(table_size, sizeof(ThreadScanEntry)) {}
4066
4067 ThreadScanEntry* get_entry(void *pointer) {
4068 unsigned int hash = compute_hash(pointer);
4069 return get_entry(hash_to_index(hash), hash, pointer);
4070 }
4071
4072 ThreadScanEntry* new_entry(void *pointer) {
4073 unsigned int hash = compute_hash(pointer);
4074 ThreadScanEntry* pp;
4075 pp = (ThreadScanEntry*)BasicHashtable<mtThread>::new_entry(hash);
4076 pp->set_pointer(pointer);
4077 return pp;
4078 }
4079
4080 void add_entry(ThreadScanEntry* pp) {
4081 int index = hash_to_index(pp->hash());
4082 BasicHashtable<mtThread>::add_entry(index, pp);
4083 }
4084 };
4085
4086 class ScanHazardPointerThreadClosure: public ThreadClosure {
4087 private:
4088 ThreadScanHashtable *_table;
4089 public:
4090 ScanHazardPointerThreadClosure(ThreadScanHashtable *table) : _table(table) {}
4091
4092 virtual void do_thread(Thread *thread) {
4093 assert_locked_or_safepoint(Threads_lock);
4094 assert(thread->is_Java_thread(), "sanity");
4095 JavaThread *const jthread = reinterpret_cast<JavaThread*>(thread);
4096 JavaThread **threads = (JavaThread**)OrderAccess::load_ptr_acquire((volatile void*)&jthread->_java_threads_do_hp);
4097 if (threads == NULL) return;
4098 for (JavaThread** current = threads; *current != NULL; current++) {
4099 JavaThread *p = *current;
4100 if (_table->get_entry((void*)p) == NULL) {
4101 _table->add_entry(_table->new_entry((void*)p));
4102 }
4103 }
4104 }
4105 };
4106
4107 void Threads::smr_free(JavaThread *thread, bool have_lock) {
4108 assert(!have_lock || Threads_lock->is_locked(), "Threads_lock inconsistency");
4109 JavaThread *delete_head;
4110 if (!have_lock) {
4111 MutexLocker ml(Threads_lock);
4112 delete_head = smr_free_work(thread);
4113 } else {
4114 delete_head = smr_free_work(thread);
4115 }
4116
4117 while (delete_head != NULL) {
4118 JavaThread *next = delete_head->next();
4119 delete delete_head;
4120 delete_head = next;
4121 }
4122 }
4123
4124 JavaThread *Threads::smr_free_work(JavaThread *thread) {
4125 assert(Threads_lock->is_locked(), "Threads_lock should be locked");
4126
4127 thread->set_next(_thread_smr_list);
4128 _thread_smr_list = thread;
4129
4130 JavaThread *current = _thread_smr_list;
4131 JavaThread *prev = NULL;
4132 JavaThread *next = NULL;
4133 JavaThread *delete_head = NULL;
4134
4135 ThreadScanHashtable *scan_table = new ThreadScanHashtable(32);
4136 ScanHazardPointerThreadClosure scan_cl(scan_table);
4137 ALL_JAVA_THREADS(q) {
4138 scan_cl.do_thread(q);
4139 }
4140
4141 while (current != NULL) {
4142 next = current->next();
4143 if (!scan_table->get_entry((void*)current)) {
4144 if (prev != NULL) {
4145 prev->set_next(next);
4146 }
4147 if (_thread_smr_list == current) _thread_smr_list = next;
4148
4149 current->set_next(delete_head);
4150 delete_head = current;
4151 } else {
4152 prev = current;
4153 }
4154
4155 current = next;
4156 }
4157
4158 delete scan_table;
4159
4160 return delete_head;
4161 }
4162
4163 class ScanHazardPointerThreadsClosure: public ThreadClosure {
4164 private:
4165 ThreadScanHashtable *_table;
4166 public:
4167 ScanHazardPointerThreadsClosure(ThreadScanHashtable *table) : _table(table) {}
4168
4169 virtual void do_thread(Thread *thread) {
4170 assert_locked_or_safepoint(Threads_lock);
4171 assert(thread->is_Java_thread(), "sanity");
4172 JavaThread *const jthread = reinterpret_cast<JavaThread*>(thread);
4173 JavaThread **threads = (JavaThread**)OrderAccess::load_ptr_acquire((volatile void*)&jthread->_java_threads_do_hp);
4174 if (threads == NULL) return;
4175 if (_table->get_entry((void*)threads) == NULL) {
4176 _table->add_entry(_table->new_entry((void*)threads));
4177 }
4178 }
4179 };
4180
4181 void Threads::smr_free_list(JavaThread **threads) {
4182 assert(Threads_lock->is_locked(), "Threads_lock should be locked");
4183
4184 JavaThread ***threads_header_addr = (JavaThread***)(threads - 1);
4185
4186 *threads_header_addr = _thread_smr_list_list;
4187 _thread_smr_list_list = threads;
4188
4189 JavaThread **current = _thread_smr_list_list;
4190 JavaThread **prev = NULL;
4191 JavaThread **next = NULL;
4192
4193 ThreadScanHashtable *scan_table = new ThreadScanHashtable(32);
4194 ScanHazardPointerThreadsClosure scan_cl(scan_table);
4195 ALL_JAVA_THREADS(q) {
4196 scan_cl.do_thread(q);
4197 }
4198
4199 while (current != NULL) {
4200 JavaThread ***current_header_addr = (JavaThread***)(current - 1);
4201 next = *current_header_addr;
4202 if (!scan_table->get_entry((void*)current)) {
4203 if (prev != NULL) {
4204 JavaThread ***prev_header_addr = (JavaThread***)(prev - 1);
4205 // prev->next = current->next
4206 *prev_header_addr = *current_header_addr;
4207 }
4208 if (_thread_smr_list_list == current) _thread_smr_list_list = *current_header_addr;
4209 FREE_C_HEAP_ARRAY(JavaThread*, (JavaThread**)current_header_addr);
4210 } else {
4211 prev = current;
4212 }
4213
4214 current = next;
4215 }
4216 }
4217
4218 void Threads::add(JavaThread* p, bool force_daemon) {
4219 // The threads lock must be owned at this point
4220 assert_locked_or_safepoint(Threads_lock);
4221
4222 // See the comment for this method in thread.hpp for its purpose and
4223 // why it is called here.
4224 p->initialize_queues();
4225 p->set_next(_thread_list);
4226 _thread_list = p;
4227 _number_of_threads++;
4228 oop threadObj = p->threadObj();
4229 bool daemon = true;
4230 // Bootstrapping problem: threadObj can be null for initial
4231 // JavaThread (or for threads attached via JNI)
4232 if ((!force_daemon) && (threadObj == NULL || !java_lang_Thread::is_daemon(threadObj))) {
4233 _number_of_non_daemon_threads++;
4234 daemon = false;
4235 }
4236
4237 ThreadService::add_thread(p, daemon);
4238
4239 JavaThread **new_thread_list = NEW_C_HEAP_ARRAY(JavaThread*, _number_of_threads + 2, mtThread) + 1;
4240 int i = 0;
4241 ALL_JAVA_THREADS(q) {
4242 new_thread_list[i++] = q;
4243 }
4244 new_thread_list[i] = NULL;
4245 JavaThread **old_list = (JavaThread**)Atomic::xchg_ptr((void*)new_thread_list, (volatile void*)&_fast_java_thread_list);
4246 if (old_list != NULL) smr_free_list(old_list);
4247
4248 // Possible GC point.
4249 Events::log(p, "Thread added: " INTPTR_FORMAT, p);
4250 }
4251
4252 void Threads::remove(JavaThread* p) {
4253 // Extra scope needed for Thread_lock, so we can check
4254 // that we do not remove thread without safepoint code notice
4255 { MutexLocker ml(Threads_lock);
4256
4257 assert(includes(p), "p must be present");
4258
4259 JavaThread* current = _thread_list;
4260 JavaThread* prev = NULL;
4261
4262 JavaThread **new_thread_list = NEW_C_HEAP_ARRAY(JavaThread*, _number_of_threads + 1, mtThread) + 1;
4263 int i = 0;
4264
4265 while (current != p) {
4266 new_thread_list[i++] = current;
4267 prev = current;
4268 current = current->next();
4269 }
4270
4271 if (prev) {
4272 prev->set_next(current->next());
4273 } else {
4274 _thread_list = p->next();
4275 }
4276
4277 current = current->next();
4278 while (current != NULL) {
4279 new_thread_list[i++] = current;
4280 current = current->next();
4281 }
4282 new_thread_list[i] = NULL;
4283 JavaThread **old_list = (JavaThread**)Atomic::xchg_ptr((void*)new_thread_list, (volatile void*)&_fast_java_thread_list);
4284 if (old_list != NULL) smr_free_list(old_list);
4285
4286 _number_of_threads--;
4287 oop threadObj = p->threadObj();
4288 bool daemon = true;
4289 if (threadObj == NULL || !java_lang_Thread::is_daemon(threadObj)) {
4290 _number_of_non_daemon_threads--;
4291 daemon = false;
4292
4293 // Only one thread left, do a notify on the Threads_lock so a thread waiting
4294 // on destroy_vm will wake up.
4295 if (number_of_non_daemon_threads() == 1) {
4296 Threads_lock->notify_all();
4297 }
4298 }
4299 ThreadService::remove_thread(p, daemon);
4300
4301 // Make sure that safepoint code disregard this thread. This is needed since
4302 // the thread might mess around with locks after this point. This can cause it
4303 // to do callbacks into the safepoint code. However, the safepoint code is not aware
4304 // of this thread since it is removed from the queue.
4305 p->set_terminated_value();
|