--- old/src/hotspot/os/linux/os_linux.cpp Wed Nov 8 08:12:22 2017 +++ new/src/hotspot/os/linux/os_linux.cpp Wed Nov 8 08:12:21 2017 @@ -1835,9 +1835,7 @@ // may have been queued at the same time. if (!_stack_is_executable) { - ThreadsListHandle tlh; - JavaThreadIterator jti(tlh.list()); - for (JavaThread *jt = jti.first(); jt != NULL; jt = jti.next()) { + for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) { if (!jt->stack_guard_zone_unused() && // Stack not yet fully initialized jt->stack_guards_enabled()) { // No pending stack overflow exceptions if (!os::guard_memory((char *)jt->stack_end(), jt->stack_guard_zone_size())) { --- old/src/hotspot/share/gc/g1/dirtyCardQueue.cpp Wed Nov 8 08:12:25 2017 +++ new/src/hotspot/share/gc/g1/dirtyCardQueue.cpp Wed Nov 8 08:12:25 2017 @@ -320,12 +320,8 @@ clear(); // Since abandon is done only at safepoints, we can safely manipulate // these queues. - { - ThreadsListHandle tlh; - JavaThreadIterator jti(tlh.list()); - for (JavaThread* t = jti.first(); t != NULL; t = jti.next()) { - t->dirty_card_queue().reset(); - } + for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) { + t->dirty_card_queue().reset(); } shared_dirty_card_queue()->reset(); } @@ -343,12 +339,8 @@ int save_max_completed_queue = _max_completed_queue; _max_completed_queue = max_jint; assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint."); - { - ThreadsListHandle tlh; - JavaThreadIterator jti(tlh.list()); - for (JavaThread* t = jti.first(); t != NULL; t = jti.next()) { - concatenate_log(t->dirty_card_queue()); - } + for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) { + concatenate_log(t->dirty_card_queue()); } concatenate_log(_shared_dirty_card_queue); // Restore the completed buffer queue limit. --- old/src/hotspot/share/gc/g1/g1CollectedHeap.cpp Wed Nov 8 08:12:28 2017 +++ new/src/hotspot/share/gc/g1/g1CollectedHeap.cpp Wed Nov 8 08:12:27 2017 @@ -2630,13 +2630,9 @@ size_t G1CollectedHeap::pending_card_num() { size_t extra_cards = 0; - { - ThreadsListHandle tlh; - JavaThreadIterator jti(tlh.list()); - for (JavaThread *curr = jti.first(); curr != NULL; curr = jti.next()) { - DirtyCardQueue& dcq = curr->dirty_card_queue(); - extra_cards += dcq.size(); - } + for (JavaThreadIteratorWithHandle jtiwh; JavaThread *curr = jtiwh.next(); ) { + DirtyCardQueue& dcq = curr->dirty_card_queue(); + extra_cards += dcq.size(); } DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set(); size_t buffer_size = dcqs.buffer_size(); --- old/src/hotspot/share/gc/g1/satbMarkQueue.cpp Wed Nov 8 08:12:31 2017 +++ new/src/hotspot/share/gc/g1/satbMarkQueue.cpp Wed Nov 8 08:12:31 2017 @@ -215,12 +215,8 @@ log_error(gc, verify)("Expected SATB active state: %s", expected_active ? "ACTIVE" : "INACTIVE"); log_error(gc, verify)("Actual SATB active states:"); log_error(gc, verify)(" Queue set: %s", is_active() ? "ACTIVE" : "INACTIVE"); - { - ThreadsListHandle tlh; - JavaThreadIterator jti(tlh.list()); - for (JavaThread* t = jti.first(); t != NULL; t = jti.next()) { - log_error(gc, verify)(" Thread \"%s\" queue: %s", t->name(), t->satb_mark_queue().is_active() ? "ACTIVE" : "INACTIVE"); - } + for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) { + log_error(gc, verify)(" Thread \"%s\" queue: %s", t->name(), t->satb_mark_queue().is_active() ? "ACTIVE" : "INACTIVE"); } log_error(gc, verify)(" Shared queue: %s", shared_satb_queue()->is_active() ? "ACTIVE" : "INACTIVE"); } @@ -233,14 +229,10 @@ } // Verify thread queue states - { - ThreadsListHandle tlh; - JavaThreadIterator jti(tlh.list()); - for (JavaThread* t = jti.first(); t != NULL; t = jti.next()) { - if (t->satb_mark_queue().is_active() != expected_active) { - dump_active_states(expected_active); - guarantee(false, "Thread SATB queue has an unexpected active state"); - } + for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) { + if (t->satb_mark_queue().is_active() != expected_active) { + dump_active_states(expected_active); + guarantee(false, "Thread SATB queue has an unexpected active state"); } } @@ -258,23 +250,15 @@ verify_active_states(expected_active); #endif // ASSERT _all_active = active; - { - ThreadsListHandle tlh; - JavaThreadIterator jti(tlh.list()); - for (JavaThread* t = jti.first(); t != NULL; t = jti.next()) { - t->satb_mark_queue().set_active(active); - } + for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) { + t->satb_mark_queue().set_active(active); } shared_satb_queue()->set_active(active); } void SATBMarkQueueSet::filter_thread_buffers() { - { - ThreadsListHandle tlh; - JavaThreadIterator jti(tlh.list()); - for (JavaThread* t = jti.first(); t != NULL; t = jti.next()) { - t->satb_mark_queue().filter(); - } + for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) { + t->satb_mark_queue().filter(); } shared_satb_queue()->filter(); } @@ -326,13 +310,9 @@ i += 1; } - { - ThreadsListHandle tlh; - JavaThreadIterator jti(tlh.list()); - for (JavaThread* t = jti.first(); t != NULL; t = jti.next()) { - jio_snprintf(buffer, SATB_PRINTER_BUFFER_SIZE, "Thread: %s", t->name()); - t->satb_mark_queue().print(buffer); - } + for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) { + jio_snprintf(buffer, SATB_PRINTER_BUFFER_SIZE, "Thread: %s", t->name()); + t->satb_mark_queue().print(buffer); } shared_satb_queue()->print("Shared"); @@ -362,12 +342,8 @@ } assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint."); // So we can safely manipulate these queues. - { - ThreadsListHandle tlh; - JavaThreadIterator jti(tlh.list()); - for (JavaThread* t = jti.first(); t != NULL; t = jti.next()) { - t->satb_mark_queue().reset(); - } + for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) { + t->satb_mark_queue().reset(); } shared_satb_queue()->reset(); } --- old/src/hotspot/share/gc/parallel/mutableNUMASpace.cpp Wed Nov 8 08:12:34 2017 +++ new/src/hotspot/share/gc/parallel/mutableNUMASpace.cpp Wed Nov 8 08:12:33 2017 @@ -288,9 +288,7 @@ FREE_C_HEAP_ARRAY(int, lgrp_ids); if (changed) { - ThreadsListHandle tlh; - JavaThreadIterator jti(tlh.list()); - for (JavaThread *thread = jti.first(); thread != NULL; thread = jti.next()) { + for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thread = jtiwh.next(); ) { thread->set_lgrp_id(-1); } } --- old/src/hotspot/share/gc/shared/collectedHeap.cpp Wed Nov 8 08:12:37 2017 +++ new/src/hotspot/share/gc/shared/collectedHeap.cpp Wed Nov 8 08:12:36 2017 @@ -551,12 +551,11 @@ const bool deferred = _defer_initial_card_mark; // The main thread starts allocating via a TLAB even before it // has added itself to the threads list at vm boot-up. - ThreadsListHandle tlh; - assert(!use_tlab || tlh.length() > 0, + JavaThreadIteratorWithHandle jtiwh; + assert(!use_tlab || jtiwh.length() > 0, "Attempt to fill tlabs before main thread has been added" " to threads list is doomed to failure!"); - JavaThreadIterator jti(tlh.list()); - for (JavaThread *thread = jti.first(); thread != NULL; thread = jti.next()) { + for (; JavaThread *thread = jtiwh.next(); ) { if (use_tlab) thread->tlab().make_parsable(retire_tlabs); #if defined(COMPILER2) || INCLUDE_JVMCI // The deferred store barriers must all have been flushed to the --- old/src/hotspot/share/gc/shared/gcLocker.cpp Wed Nov 8 08:12:40 2017 +++ new/src/hotspot/share/gc/shared/gcLocker.cpp Wed Nov 8 08:12:39 2017 @@ -46,9 +46,8 @@ assert(!needs_gc() || _debug_jni_lock_count == _jni_lock_count, "must agree"); int count = 0; // Count the number of threads with critical operations in progress - ThreadsListHandle tlh; - JavaThreadIterator jti(tlh.list()); - for (JavaThread* thr = jti.first(); thr != NULL; thr = jti.next()) { + JavaThreadIteratorWithHandle jtiwh; + for (; JavaThread *thr = jtiwh.next(); ) { if (thr->in_critical()) { count++; } @@ -55,7 +54,8 @@ } if (_jni_lock_count != count) { log_error(gc, verify)("critical counts don't match: %d != %d", _jni_lock_count, count); - for (JavaThread* thr = jti.first(); thr != NULL; thr = jti.next()) { + jtiwh.rewind(); + for (; JavaThread *thr = jtiwh.next(); ) { if (thr->in_critical()) { log_error(gc, verify)(INTPTR_FORMAT " in_critical %d", p2i(thr), thr->in_critical()); } --- old/src/hotspot/share/gc/shared/threadLocalAllocBuffer.cpp Wed Nov 8 08:12:43 2017 +++ new/src/hotspot/share/gc/shared/threadLocalAllocBuffer.cpp Wed Nov 8 08:12:42 2017 @@ -49,13 +49,9 @@ void ThreadLocalAllocBuffer::accumulate_statistics_before_gc() { global_stats()->initialize(); - { - ThreadsListHandle tlh; - JavaThreadIterator jti(tlh.list()); - for (JavaThread *thread = jti.first(); thread != NULL; thread = jti.next()) { - thread->tlab().accumulate_statistics(); - thread->tlab().initialize_statistics(); - } + for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thread = jtiwh.next(); ) { + thread->tlab().accumulate_statistics(); + thread->tlab().initialize_statistics(); } // Publish new stats if some allocation occurred. @@ -135,9 +131,7 @@ void ThreadLocalAllocBuffer::resize_all_tlabs() { if (ResizeTLAB) { - ThreadsListHandle tlh; - JavaThreadIterator jti(tlh.list()); - for (JavaThread *thread = jti.first(); thread != NULL; thread = jti.next()) { + for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thread = jtiwh.next(); ) { thread->tlab().resize(); } } --- old/src/hotspot/share/opto/idealGraphPrinter.cpp Wed Nov 8 08:12:46 2017 +++ new/src/hotspot/share/opto/idealGraphPrinter.cpp Wed Nov 8 08:12:45 2017 @@ -92,9 +92,7 @@ } void IdealGraphPrinter::clean_up() { - ThreadsListHandle tlh; - JavaThreadIterator jti(tlh.list()); - for (JavaThread *p = jti.first(); p != NULL; p = jti.next()) { + for (JavaThreadIteratorWithHandle jtiwh; JavaThread *p = jtiwh.next(); ) { if (p->is_Compiler_thread()) { CompilerThread *c = (CompilerThread *)p; IdealGraphPrinter *printer = c->ideal_graph_printer(); --- old/src/hotspot/share/prims/jvmtiEnvBase.cpp Wed Nov 8 08:12:48 2017 +++ new/src/hotspot/share/prims/jvmtiEnvBase.cpp Wed Nov 8 08:12:48 2017 @@ -1277,20 +1277,16 @@ ResourceMark rm; _final_thread_count = 0; - { - ThreadsListHandle tlh; - JavaThreadIterator jti(tlh.list()); - for (JavaThread *jt = jti.first(); jt != NULL; jt = jti.next()) { - oop thread_oop = jt->threadObj(); - if (thread_oop != NULL && - !jt->is_exiting() && - java_lang_Thread::is_alive(thread_oop) && - !jt->is_hidden_from_external_view()) { - ++_final_thread_count; - // Handle block of the calling thread is used to create local refs. - fill_frames((jthread)JNIHandles::make_local(_calling_thread, thread_oop), - jt, thread_oop); - } + for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) { + oop thread_oop = jt->threadObj(); + if (thread_oop != NULL && + !jt->is_exiting() && + java_lang_Thread::is_alive(thread_oop) && + !jt->is_hidden_from_external_view()) { + ++_final_thread_count; + // Handle block of the calling thread is used to create local refs. + fill_frames((jthread)JNIHandles::make_local(_calling_thread, thread_oop), + jt, thread_oop); } } allocate_and_fill_stacks(_final_thread_count); --- old/src/hotspot/share/prims/jvmtiEventController.cpp Wed Nov 8 08:12:51 2017 +++ new/src/hotspot/share/prims/jvmtiEventController.cpp Wed Nov 8 08:12:51 2017 @@ -581,9 +581,7 @@ // filtered events and there weren't last time if ( (any_env_thread_enabled & THREAD_FILTERED_EVENT_BITS) != 0 && (was_any_env_thread_enabled & THREAD_FILTERED_EVENT_BITS) == 0) { - ThreadsListHandle tlh; - JavaThreadIterator jti(tlh.list()); - for (JavaThread *tp = jti.first(); tp != NULL; tp = jti.next()) { + for (JavaThreadIteratorWithHandle jtiwh; JavaThread *tp = jtiwh.next(); ) { // state_for_while_locked() makes tp->is_exiting() check JvmtiThreadState::state_for_while_locked(tp); // create the thread state if missing } --- old/src/hotspot/share/prims/jvmtiExport.cpp Wed Nov 8 08:12:55 2017 +++ new/src/hotspot/share/prims/jvmtiExport.cpp Wed Nov 8 08:12:54 2017 @@ -2788,9 +2788,7 @@ return; } - ThreadsListHandle tlh; - JavaThreadIterator jti(tlh.list()); - for (JavaThread *jthr = jti.first(); jthr != NULL; jthr = jti.next()) { + for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jthr = jtiwh.next(); ) { JvmtiThreadState *state = jthr->jvmti_thread_state(); if (state != NULL) { JvmtiVMObjectAllocEventCollector *collector; --- old/src/hotspot/share/prims/jvmtiImpl.cpp Wed Nov 8 08:12:58 2017 +++ new/src/hotspot/share/prims/jvmtiImpl.cpp Wed Nov 8 08:12:57 2017 @@ -881,9 +881,7 @@ #ifndef PRODUCT LogStreamHandle(Trace, jvmti) log_stream; log_stream.print("Suspended Threads: ["); - ThreadsListHandle tlh; - JavaThreadIterator jti(tlh.list()); - for (JavaThread *thread = jti.first(); thread != NULL; thread = jti.next()) { + for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thread = jtiwh.next(); ) { #ifdef JVMTI_TRACE const char *name = JvmtiTrace::safe_get_thread_name(thread); #else --- old/src/hotspot/share/prims/jvmtiTagMap.cpp Wed Nov 8 08:13:01 2017 +++ new/src/hotspot/share/prims/jvmtiTagMap.cpp Wed Nov 8 08:13:00 2017 @@ -3177,9 +3177,7 @@ // stack to find all references and local JNI refs. inline bool VM_HeapWalkOperation::collect_stack_roots() { JNILocalRootsClosure blk; - ThreadsListHandle tlh; - JavaThreadIterator jti(tlh.list()); - for (JavaThread* thread = jti.first(); thread != NULL; thread = jti.next()) { + for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thread = jtiwh.next(); ) { oop threadObj = thread->threadObj(); if (threadObj != NULL && !thread->is_exiting() && !thread->is_hidden_from_external_view()) { // Collect the simple root for this thread before we --- old/src/hotspot/share/prims/whitebox.cpp Wed Nov 8 08:13:04 2017 +++ new/src/hotspot/share/prims/whitebox.cpp Wed Nov 8 08:13:03 2017 @@ -661,9 +661,7 @@ int result() const { return _result; } void doit() { - ThreadsListHandle tlh; - JavaThreadIterator jti(tlh.list()); - for (JavaThread* t = jti.first(); t != NULL; t = jti.next()) { + for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) { if (t->has_last_Java_frame()) { for (StackFrameStream fst(t, UseBiasedLocking); !fst.is_done(); fst.next()) { frame* f = fst.current(); --- old/src/hotspot/share/runtime/biasedLocking.cpp Wed Nov 8 08:13:07 2017 +++ new/src/hotspot/share/runtime/biasedLocking.cpp Wed Nov 8 08:13:06 2017 @@ -212,9 +212,7 @@ if (requesting_thread == biased_thread) { thread_is_alive = true; } else { - ThreadsListHandle tlh; - JavaThreadIterator jti(tlh.list()); - for (JavaThread* cur_thread = jti.first(); cur_thread != NULL; cur_thread = jti.next()) { + for (JavaThreadIteratorWithHandle jtiwh; JavaThread *cur_thread = jtiwh.next(); ) { if (cur_thread == biased_thread) { thread_is_alive = true; break; @@ -386,8 +384,7 @@ Klass* klass = k_o; { - ThreadsListHandle tlh; - JavaThreadIterator jti(tlh.list()); + JavaThreadIteratorWithHandle jtiwh; if (bulk_rebias) { // Use the epoch in the klass of the object to implicitly revoke @@ -407,7 +404,7 @@ // Now walk all threads' stacks and adjust epochs of any biased // and locked objects of this data type we encounter - for (JavaThread* thr = jti.first(); thr != NULL; thr = jti.next()) { + for (; JavaThread *thr = jtiwh.next(); ) { GrowableArray* cached_monitor_info = get_or_compute_monitor_info(thr); for (int i = 0; i < cached_monitor_info->length(); i++) { MonitorInfo* mon_info = cached_monitor_info->at(i); @@ -439,7 +436,7 @@ // Now walk all threads' stacks and forcibly revoke the biases of // any locked and biased objects of this data type we encounter. - for (JavaThread* thr = jti.first(); thr != NULL; thr = jti.next()) { + for (; JavaThread *thr = jtiwh.next(); ) { GrowableArray* cached_monitor_info = get_or_compute_monitor_info(thr); for (int i = 0; i < cached_monitor_info->length(); i++) { MonitorInfo* mon_info = cached_monitor_info->at(i); @@ -481,9 +478,7 @@ static void clean_up_cached_monitor_info() { // Walk the thread list clearing out the cached monitors - ThreadsListHandle tlh; - JavaThreadIterator jti(tlh.list()); - for (JavaThread* thr = jti.first(); thr != NULL; thr = jti.next()) { + for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thr = jtiwh.next(); ) { thr->set_cached_monitor_info(NULL); } } @@ -738,9 +733,7 @@ ResourceMark rm; Thread* cur = Thread::current(); - ThreadsListHandle tlh; - JavaThreadIterator jti(tlh.list()); - for (JavaThread* thread = jti.first(); thread != NULL; thread = jti.next()) { + for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thread = jtiwh.next(); ) { if (thread->has_last_Java_frame()) { RegisterMap rm(thread); for (javaVFrame* vf = thread->last_java_vframe(&rm); vf != NULL; vf = vf->java_sender()) { --- old/src/hotspot/share/runtime/deoptimization.cpp Wed Nov 8 08:13:10 2017 +++ new/src/hotspot/share/runtime/deoptimization.cpp Wed Nov 8 08:13:09 2017 @@ -1304,26 +1304,22 @@ assert(SafepointSynchronize::is_at_safepoint(), "must only be called from safepoint"); GrowableArray* objects_to_revoke = new GrowableArray(); - { - ThreadsListHandle tlh; - JavaThreadIterator jti(tlh.list()); - for (JavaThread* jt = jti.first(); jt != NULL; jt = jti.next()) { - if (jt->has_last_Java_frame()) { - StackFrameStream sfs(jt, true); - while (!sfs.is_done()) { - frame* cur = sfs.current(); - if (cb->contains(cur->pc())) { - vframe* vf = vframe::new_vframe(cur, sfs.register_map(), jt); - compiledVFrame* cvf = compiledVFrame::cast(vf); - // Revoke monitors' biases in all scopes - while (!cvf->is_top()) { - collect_monitors(cvf, objects_to_revoke); - cvf = compiledVFrame::cast(cvf->sender()); - } + for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) { + if (jt->has_last_Java_frame()) { + StackFrameStream sfs(jt, true); + while (!sfs.is_done()) { + frame* cur = sfs.current(); + if (cb->contains(cur->pc())) { + vframe* vf = vframe::new_vframe(cur, sfs.register_map(), jt); + compiledVFrame* cvf = compiledVFrame::cast(vf); + // Revoke monitors' biases in all scopes + while (!cvf->is_top()) { collect_monitors(cvf, objects_to_revoke); + cvf = compiledVFrame::cast(cvf->sender()); } - sfs.next(); + collect_monitors(cvf, objects_to_revoke); } + sfs.next(); } } } --- old/src/hotspot/share/runtime/memprofiler.cpp Wed Nov 8 08:13:13 2017 +++ new/src/hotspot/share/runtime/memprofiler.cpp Wed Nov 8 08:13:12 2017 @@ -111,9 +111,8 @@ size_t handles_memory_usage = VMThread::vm_thread()->handle_area()->size_in_bytes(); size_t resource_memory_usage = VMThread::vm_thread()->resource_area()->size_in_bytes(); { - ThreadsListHandle tlh; - JavaThreadIterator jti(tlh.list()); - for (JavaThread *cur = jti.first(); cur != NULL; cur = jti.next()) { + JavaThreadIteratorWithHandle jtiwh; + for (; JavaThread *cur = jtiwh.next(); ) { handles_memory_usage += cur->handle_area()->size_in_bytes(); resource_memory_usage += cur->resource_area()->size_in_bytes(); } @@ -121,7 +120,7 @@ // Print trace line in log fprintf(_log_fp, "%6.1f,%5d,%5d," UINTX_FORMAT_W(6) "," UINTX_FORMAT_W(6) ",", os::elapsedTime(), - tlh.length(), + jtiwh.length(), InstanceKlass::number_of_instance_classes(), Universe::heap()->used() / K, Universe::heap()->capacity() / K); --- old/src/hotspot/share/runtime/os.cpp Wed Nov 8 08:13:16 2017 +++ new/src/hotspot/share/runtime/os.cpp Wed Nov 8 08:13:15 2017 @@ -1093,37 +1093,33 @@ } #endif - { - ThreadsListHandle tlh; - JavaThreadIterator jti(tlh.list()); - for (JavaThread *thread = jti.first(); thread != NULL; thread = jti.next()) { - // Check for privilege stack - if (thread->privileged_stack_top() != NULL && - thread->privileged_stack_top()->contains(addr)) { - st->print_cr(INTPTR_FORMAT " is pointing into the privilege stack " - "for thread: " INTPTR_FORMAT, p2i(addr), p2i(thread)); - if (verbose) thread->print_on(st); - return; + for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thread = jtiwh.next(); ) { + // Check for privilege stack + if (thread->privileged_stack_top() != NULL && + thread->privileged_stack_top()->contains(addr)) { + st->print_cr(INTPTR_FORMAT " is pointing into the privilege stack " + "for thread: " INTPTR_FORMAT, p2i(addr), p2i(thread)); + if (verbose) thread->print_on(st); + return; + } + // If the addr is a java thread print information about that. + if (addr == (address)thread) { + if (verbose) { + thread->print_on(st); + } else { + st->print_cr(INTPTR_FORMAT " is a thread", p2i(addr)); } - // If the addr is a java thread print information about that. - if (addr == (address)thread) { - if (verbose) { - thread->print_on(st); - } else { - st->print_cr(INTPTR_FORMAT " is a thread", p2i(addr)); - } - return; - } - // If the addr is in the stack region for this thread then report that - // and print thread info - if (thread->on_local_stack(addr)) { - st->print_cr(INTPTR_FORMAT " is pointing into the stack for thread: " - INTPTR_FORMAT, p2i(addr), p2i(thread)); - if (verbose) thread->print_on(st); - return; - } + return; } - } // ThreadsListHandle destroyed here. + // If the addr is in the stack region for this thread then report that + // and print thread info + if (thread->on_local_stack(addr)) { + st->print_cr(INTPTR_FORMAT " is pointing into the stack for thread: " + INTPTR_FORMAT, p2i(addr), p2i(thread)); + if (verbose) thread->print_on(st); + return; + } + } // Check if in metaspace and print types that have vptrs (only method now) if (Metaspace::contains(addr)) { --- old/src/hotspot/share/runtime/safepoint.cpp Wed Nov 8 08:13:19 2017 +++ new/src/hotspot/share/runtime/safepoint.cpp Wed Nov 8 08:13:18 2017 @@ -203,10 +203,9 @@ unsigned int iterations = 0; { - ThreadsListHandle tlh; - JavaThreadIterator jti(tlh.list()); + JavaThreadIteratorWithHandle jtiwh; #ifdef ASSERT - for (JavaThread *cur = jti.first(); cur != NULL; cur = jti.next()) { + for (; JavaThread *cur = jtiwh.next(); ) { assert(cur->safepoint_state()->is_running(), "Illegal initial state"); // Clear the visited flag to ensure that the critical counts are collected properly. cur->set_visited_for_critical_count(false); @@ -219,7 +218,8 @@ // Iterate through all threads until it have been determined how to stop them all at a safepoint int steps = 0 ; while(still_running > 0) { - for (JavaThread *cur = jti.first(); cur != NULL; cur = jti.next()) { + jtiwh.rewind(); + for (; JavaThread *cur = jtiwh.next(); ) { assert(!cur->is_ConcurrentGC_thread(), "A concurrent GC thread is unexpectly being suspended"); ThreadSafepointState *cur_state = cur->safepoint_state(); if (cur_state->is_running()) { @@ -396,13 +396,9 @@ } // EventSafepointWaitBlocked #ifdef ASSERT - { - // Make sure all the threads were visited. - ThreadsListHandle tlh; - JavaThreadIterator jti(tlh.list()); - for (JavaThread *cur = jti.first(); cur != NULL; cur = jti.next()) { - assert(cur->was_visited_for_critical_count(), "missed a thread"); - } + // Make sure all the threads were visited. + for (JavaThreadIteratorWithHandle jtiwh; JavaThread *cur = jtiwh.next(); ) { + assert(cur->was_visited_for_critical_count(), "missed a thread"); } #endif // ASSERT @@ -460,13 +456,12 @@ } { - ThreadsListHandle tlh; - JavaThreadIterator jti(tlh.list()); + JavaThreadIteratorWithHandle jtiwh; #ifdef ASSERT // A pending_exception cannot be installed during a safepoint. The threads // may install an async exception after they come back from a safepoint into // pending_exception after they unblock. But that should happen later. - for (JavaThread *cur = jti.first(); cur != NULL; cur = jti.next()) { + for (; JavaThread *cur = jtiwh.next(); ) { assert (!(cur->has_pending_exception() && cur->safepoint_state()->is_at_poll_safepoint()), "safepoint installed a pending exception"); @@ -495,7 +490,8 @@ log_debug(safepoint)("Leaving safepoint region"); // Start suspended threads - for (JavaThread *current = jti.first(); current != NULL; current = jti.next()) { + jtiwh.rewind(); + for (; JavaThread *current = jtiwh.next(); ) { // A problem occurring on Solaris is when attempting to restart threads // the first #cpus - 1 go well, but then the VMThread is preempted when we get // to the next one (since it has been running the longest). We then have @@ -909,19 +905,15 @@ tty->print_cr("# SafepointSynchronize::begin: Threads which did not reach the safepoint:"); ThreadSafepointState *cur_state; ResourceMark rm; - { - ThreadsListHandle tlh; - JavaThreadIterator jti(tlh.list()); - for (JavaThread *cur_thread = jti.first(); cur_thread != NULL; cur_thread = jti.next()) { - cur_state = cur_thread->safepoint_state(); + for (JavaThreadIteratorWithHandle jtiwh; JavaThread *cur_thread = jtiwh.next(); ) { + cur_state = cur_thread->safepoint_state(); - if (cur_thread->thread_state() != _thread_blocked && - ((reason == _spinning_timeout && cur_state->is_running()) || - (reason == _blocking_timeout && !cur_state->has_called_back()))) { - tty->print("# "); - cur_thread->print(); - tty->cr(); - } + if (cur_thread->thread_state() != _thread_blocked && + ((reason == _spinning_timeout && cur_state->is_running()) || + (reason == _blocking_timeout && !cur_state->has_called_back()))) { + tty->print("# "); + cur_thread->print(); + tty->cr(); } } tty->print_cr("# SafepointSynchronize::begin: (End of list)"); @@ -1423,9 +1415,7 @@ tty->print_cr("State: %s", (_state == _synchronizing) ? "synchronizing" : "synchronized"); - ThreadsListHandle tlh; - JavaThreadIterator jti(tlh.list()); - for (JavaThread *cur = jti.first(); cur != NULL; cur = jti.next()) { + for (JavaThreadIteratorWithHandle jtiwh; JavaThread *cur = jtiwh.next(); ) { cur->safepoint_state()->print(); } } --- old/src/hotspot/share/runtime/thread.cpp Wed Nov 8 08:13:22 2017 +++ new/src/hotspot/share/runtime/thread.cpp Wed Nov 8 08:13:21 2017 @@ -1480,12 +1480,11 @@ // dcubed - Looks like the Threads_lock is for stable access // to _jvmci_old_thread_counters and _jvmci_counters. MutexLocker tl(Threads_lock); - ThreadsListHandle tlh; + JavaThreadIteratorWithHandle jtiwh; for (int i = 0; i < array->length(); i++) { array->long_at_put(i, _jvmci_old_thread_counters[i]); } - JavaThreadIterator jti(tlh.list()); - for (JavaThread* tp = jti.first(); tp != NULL; tp = jti.next()) { + for (; JavaThread *tp = jtiwh.next(); ) { if (jvmci_counters_include(tp)) { for (int i = 0; i < array->length(); i++) { array->long_at_put(i, array->long_at(i) + tp->_jvmci_counters[i]); --- old/src/hotspot/share/runtime/threadSMR.hpp Wed Nov 8 08:13:25 2017 +++ new/src/hotspot/share/runtime/threadSMR.hpp Wed Nov 8 08:13:24 2017 @@ -133,8 +133,13 @@ }; // This stack allocated JavaThreadIterator is used to walk the -// specified ThreadsList. +// specified ThreadsList using the following style: // +// JavaThreadIterator jti(t_list); +// for (JavaThread *jt = jti.first(); jt != NULL; jt = jti.next()) { +// ... +// } +// class JavaThreadIterator : public StackObj { ThreadsList * _list; uint _index; @@ -164,5 +169,40 @@ return _list->thread_at(_index); } }; + +// This stack allocated ThreadsListHandle and JavaThreadIterator combo +// is used to walk the ThreadsList in the included ThreadsListHandle +// using the following style: +// +// for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) { +// ... +// } +// +class JavaThreadIteratorWithHandle : public StackObj { + ThreadsListHandle _tlh; + uint _index; + +public: + JavaThreadIteratorWithHandle() : _tlh(), _index(0) {} + + uint length() const { + return _tlh.length(); + } + + ThreadsList *list() const { + return _tlh.list(); + } + + JavaThread *next() { + if (_index >= length()) { + return NULL; + } + return _tlh.list()->thread_at(_index++); + } + + void rewind() { + _index = 0; + } +}; #endif // SHARE_VM_RUNTIME_THREADSMR_HPP --- old/src/hotspot/share/runtime/vm_operations.cpp Wed Nov 8 08:13:28 2017 +++ new/src/hotspot/share/runtime/vm_operations.cpp Wed Nov 8 08:13:27 2017 @@ -148,11 +148,10 @@ void VM_DeoptimizeAll::doit() { DeoptimizationMarker dm; - ThreadsListHandle tlh; - JavaThreadIterator jti(tlh.list()); + JavaThreadIteratorWithHandle jtiwh; // deoptimize all java threads in the system if (DeoptimizeALot) { - for (JavaThread* thread = jti.first(); thread != NULL; thread = jti.next()) { + for (; JavaThread *thread = jtiwh.next(); ) { if (thread->has_last_Java_frame()) { thread->deoptimize(); } @@ -163,7 +162,7 @@ int tnum = os::random() & 0x3; int fnum = os::random() & 0x3; int tcount = 0; - for (JavaThread* thread = jti.first(); thread != NULL; thread = jti.next()) { + for (; JavaThread *thread = jtiwh.next(); ) { if (thread->has_last_Java_frame()) { if (tcount++ == tnum) { tcount = 0; @@ -425,9 +424,7 @@ _shutdown_thread = thr_cur; _vm_exited = true; // global flag - ThreadsListHandle tlh; - JavaThreadIterator jti(tlh.list()); - for (JavaThread *thr = jti.first(); thr != NULL; thr = jti.next()) { + for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thr = jtiwh.next(); ) { if (thr!=thr_cur && thr->thread_state() == _thread_in_native) { ++num_active; thr->set_terminated(JavaThread::_vm_exited); // per-thread flag @@ -460,13 +457,13 @@ int max_wait = max_wait_compiler_thread; int attempts = 0; - ThreadsListHandle tlh; - JavaThreadIterator jti(tlh.list()); + JavaThreadIteratorWithHandle jtiwh; while (true) { int num_active = 0; int num_active_compiler_thread = 0; - for (JavaThread *thr = jti.first(); thr != NULL; thr = jti.next()) { + jtiwh.rewind(); + for (; JavaThread *thr = jtiwh.next(); ) { if (thr!=thr_cur && thr->thread_state() == _thread_in_native) { num_active++; if (thr->is_Compiler_thread()) { --- old/src/hotspot/share/services/heapDumper.cpp Wed Nov 8 08:13:31 2017 +++ new/src/hotspot/share/services/heapDumper.cpp Wed Nov 8 08:13:30 2017 @@ -1897,9 +1897,7 @@ _stack_traces = NEW_C_HEAP_ARRAY(ThreadStackTrace*, Threads::number_of_threads(), mtInternal); int frame_serial_num = 0; - ThreadsListHandle tlh; - JavaThreadIterator jti(tlh.list()); - for (JavaThread* thread = jti.first(); thread != NULL; thread = jti.next()) { + for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thread = jtiwh.next(); ) { oop threadObj = thread->threadObj(); if (threadObj != NULL && !thread->is_exiting() && !thread->is_hidden_from_external_view()) { // dump thread stack trace --- old/src/hotspot/share/services/management.cpp Wed Nov 8 08:13:34 2017 +++ new/src/hotspot/share/services/management.cpp Wed Nov 8 08:13:33 2017 @@ -1324,11 +1324,10 @@ } // Look for the JavaThread of this given tid - ThreadsListHandle tlh; + JavaThreadIteratorWithHandle jtiwh; if (tid == 0) { - JavaThreadIterator jti(tlh.list()); // reset contention statistics for all threads if tid == 0 - for (JavaThread* java_thread = jti.first(); java_thread != NULL; java_thread = jti.next()) { + for (; JavaThread *java_thread = jtiwh.next(); ) { if (type == JMM_STAT_THREAD_CONTENTION_COUNT) { ThreadService::reset_contention_count_stat(java_thread); } else { @@ -1337,7 +1336,7 @@ } } else { // reset contention statistics for a given thread - JavaThread* java_thread = tlh.list()->find_JavaThread_from_java_tid(tid); + JavaThread* java_thread = jtiwh.list()->find_JavaThread_from_java_tid(tid); if (java_thread == NULL) { return false; } --- old/src/hotspot/share/services/threadService.cpp Wed Nov 8 08:13:37 2017 +++ new/src/hotspot/share/services/threadService.cpp Wed Nov 8 08:13:36 2017 @@ -954,9 +954,7 @@ int init_size = ThreadService::get_live_thread_count(); _threads_array = new GrowableArray(init_size); - ThreadsListHandle tlh; - JavaThreadIterator jti(tlh.list()); - for (JavaThread* jt = jti.first(); jt != NULL; jt = jti.next()) { + for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) { // skips JavaThreads in the process of exiting // and also skips VM internal JavaThreads // Threads in _thread_new or _thread_new_trans state are included.