1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "logging/logStream.hpp"
  27 #include "memory/allocation.inline.hpp"
  28 #include "runtime/jniHandles.inline.hpp"
  29 #include "runtime/thread.inline.hpp"
  30 #include "runtime/threadSMR.inline.hpp"
  31 #include "runtime/vm_operations.hpp"
  32 #include "services/threadService.hpp"
  33 #include "utilities/copy.hpp"
  34 #include "utilities/globalDefinitions.hpp"
  35 #include "utilities/resourceHash.hpp"
  36 #include "utilities/vmError.hpp"
  37 
  38 Monitor*              ThreadsSMRSupport::_delete_lock =
  39                           new Monitor(Monitor::special, "Thread_SMR_delete_lock",
  40                                       false /* allow_vm_block */,
  41                                       Monitor::_safepoint_check_never);
  42 // The '_cnt', '_max' and '_times" fields are enabled via
  43 // -XX:+EnableThreadSMRStatistics:
  44 
  45 // # of parallel threads in _delete_lock->wait().
  46 // Impl note: Hard to imagine > 64K waiting threads so this could be 16-bit,
  47 // but there is no nice 16-bit _FORMAT support.
  48 uint                  ThreadsSMRSupport::_delete_lock_wait_cnt = 0;
  49 
  50 // Max # of parallel threads in _delete_lock->wait().
  51 // Impl note: See _delete_lock_wait_cnt note.
  52 uint                  ThreadsSMRSupport::_delete_lock_wait_max = 0;
  53 
  54 // Flag to indicate when an _delete_lock->notify() is needed.
  55 // Impl note: See _delete_lock_wait_cnt note.
  56 volatile uint         ThreadsSMRSupport::_delete_notify = 0;
  57 
  58 // # of threads deleted over VM lifetime.
  59 // Impl note: Atomically incremented over VM lifetime so use unsigned for more
  60 // range. Unsigned 64-bit would be more future proof, but 64-bit atomic inc
  61 // isn't available everywhere (or is it?).
  62 volatile uint         ThreadsSMRSupport::_deleted_thread_cnt = 0;
  63 
  64 // Max time in millis to delete a thread.
  65 // Impl note: 16-bit might be too small on an overloaded machine. Use
  66 // unsigned since this is a time value. Set via Atomic::cmpxchg() in a
  67 // loop for correctness.
  68 volatile uint         ThreadsSMRSupport::_deleted_thread_time_max = 0;
  69 
  70 // Cumulative time in millis to delete threads.
  71 // Impl note: Atomically added to over VM lifetime so use unsigned for more
  72 // range. Unsigned 64-bit would be more future proof, but 64-bit atomic inc
  73 // isn't available everywhere (or is it?).
  74 volatile uint         ThreadsSMRSupport::_deleted_thread_times = 0;
  75 
  76 ThreadsList* volatile ThreadsSMRSupport::_java_thread_list = new ThreadsList(0);
  77 
  78 // # of ThreadsLists allocated over VM lifetime.
  79 // Impl note: We allocate a new ThreadsList for every thread create and
  80 // every thread delete so we need a bigger type than the
  81 // _deleted_thread_cnt field.
  82 uint64_t              ThreadsSMRSupport::_java_thread_list_alloc_cnt = 1;
  83 
  84 // # of ThreadsLists freed over VM lifetime.
  85 // Impl note: See _java_thread_list_alloc_cnt note.
  86 uint64_t              ThreadsSMRSupport::_java_thread_list_free_cnt = 0;
  87 
  88 // Max size ThreadsList allocated.
  89 // Impl note: Max # of threads alive at one time should fit in unsigned 32-bit.
  90 uint                  ThreadsSMRSupport::_java_thread_list_max = 0;
  91 
  92 // Max # of nested ThreadsLists for a thread.
  93 // Impl note: Hard to imagine > 64K nested ThreadsLists so this could be
  94 // 16-bit, but there is no nice 16-bit _FORMAT support.
  95 uint                  ThreadsSMRSupport::_nested_thread_list_max = 0;
  96 
  97 // # of ThreadsListHandles deleted over VM lifetime.
  98 // Impl note: Atomically incremented over VM lifetime so use unsigned for
  99 // more range. There will be fewer ThreadsListHandles than threads so
 100 // unsigned 32-bit should be fine.
 101 volatile uint         ThreadsSMRSupport::_tlh_cnt = 0;
 102 
 103 // Max time in millis to delete a ThreadsListHandle.
 104 // Impl note: 16-bit might be too small on an overloaded machine. Use
 105 // unsigned since this is a time value. Set via Atomic::cmpxchg() in a
 106 // loop for correctness.
 107 volatile uint         ThreadsSMRSupport::_tlh_time_max = 0;
 108 
 109 // Cumulative time in millis to delete ThreadsListHandles.
 110 // Impl note: Atomically added to over VM lifetime so use unsigned for more
 111 // range. Unsigned 64-bit would be more future proof, but 64-bit atomic inc
 112 // isn't available everywhere (or is it?).
 113 volatile uint         ThreadsSMRSupport::_tlh_times = 0;
 114 
 115 ThreadsList*          ThreadsSMRSupport::_to_delete_list = NULL;
 116 
 117 // # of parallel ThreadsLists on the to-delete list.
 118 // Impl note: Hard to imagine > 64K ThreadsLists needing to be deleted so
 119 // this could be 16-bit, but there is no nice 16-bit _FORMAT support.
 120 uint                  ThreadsSMRSupport::_to_delete_list_cnt = 0;
 121 
 122 // Max # of parallel ThreadsLists on the to-delete list.
 123 // Impl note: See _to_delete_list_cnt note.
 124 uint                  ThreadsSMRSupport::_to_delete_list_max = 0;
 125 
 126 
 127 // 'inline' functions first so the definitions are before first use:
 128 
 129 inline void ThreadsSMRSupport::add_deleted_thread_times(uint add_value) {
 130   Atomic::add(add_value, &_deleted_thread_times);
 131 }
 132 
 133 inline void ThreadsSMRSupport::inc_deleted_thread_cnt() {
 134   Atomic::inc(&_deleted_thread_cnt);
 135 }
 136 
 137 inline void ThreadsSMRSupport::inc_java_thread_list_alloc_cnt() {
 138   _java_thread_list_alloc_cnt++;
 139 }
 140 
 141 inline void ThreadsSMRSupport::update_deleted_thread_time_max(uint new_value) {
 142   while (true) {
 143     uint cur_value = _deleted_thread_time_max;
 144     if (new_value <= cur_value) {
 145       // No need to update max value so we're done.
 146       break;
 147     }
 148     if (Atomic::cmpxchg(new_value, &_deleted_thread_time_max, cur_value) == cur_value) {
 149       // Updated max value so we're done. Otherwise try it all again.
 150       break;
 151     }
 152   }
 153 }
 154 
 155 inline void ThreadsSMRSupport::update_java_thread_list_max(uint new_value) {
 156   if (new_value > _java_thread_list_max) {
 157     _java_thread_list_max = new_value;
 158   }
 159 }
 160 
 161 inline ThreadsList* ThreadsSMRSupport::xchg_java_thread_list(ThreadsList* new_list) {
 162   return (ThreadsList*)Atomic::xchg(new_list, &_java_thread_list);
 163 }
 164 
 165 
 166 // Hash table of pointers found by a scan. Used for collecting hazard
 167 // pointers (ThreadsList references). Also used for collecting JavaThreads
 168 // that are indirectly referenced by hazard ptrs. An instance of this
 169 // class only contains one type of pointer.
 170 //
 171 class ThreadScanHashtable : public CHeapObj<mtThread> {
 172  private:
 173   static bool ptr_equals(void * const& s1, void * const& s2) {
 174     return s1 == s2;
 175   }
 176 
 177   static unsigned int ptr_hash(void * const& s1) {
 178     // 2654435761 = 2^32 * Phi (golden ratio)
 179     return (unsigned int)(((uint32_t)(uintptr_t)s1) * 2654435761u);
 180   }
 181 
 182   int _table_size;
 183   // ResourceHashtable SIZE is specified at compile time so our
 184   // dynamic _table_size is unused for now; 1031 is the first prime
 185   // after 1024.
 186   typedef ResourceHashtable<void *, int, &ThreadScanHashtable::ptr_hash,
 187                             &ThreadScanHashtable::ptr_equals, 1031,
 188                             ResourceObj::C_HEAP, mtThread> PtrTable;
 189   PtrTable * _ptrs;
 190 
 191  public:
 192   // ResourceHashtable is passed to various functions and populated in
 193   // different places so we allocate it using C_HEAP to make it immune
 194   // from any ResourceMarks that happen to be in the code paths.
 195   ThreadScanHashtable(int table_size) : _table_size(table_size), _ptrs(new (ResourceObj::C_HEAP, mtThread) PtrTable()) {}
 196 
 197   ~ThreadScanHashtable() { delete _ptrs; }
 198 
 199   bool has_entry(void *pointer) {
 200     int *val_ptr = _ptrs->get(pointer);
 201     return val_ptr != NULL && *val_ptr == 1;
 202   }
 203 
 204   void add_entry(void *pointer) {
 205     _ptrs->put(pointer, 1);
 206   }
 207 };
 208 
 209 // Closure to gather JavaThreads indirectly referenced by hazard ptrs
 210 // (ThreadsList references) into a hash table. This closure handles part 2
 211 // of the dance - adding all the JavaThreads referenced by the hazard
 212 // pointer (ThreadsList reference) to the hash table.
 213 //
 214 class AddThreadHazardPointerThreadClosure : public ThreadClosure {
 215  private:
 216   ThreadScanHashtable *_table;
 217 
 218  public:
 219   AddThreadHazardPointerThreadClosure(ThreadScanHashtable *table) : _table(table) {}
 220 
 221   virtual void do_thread(Thread *thread) {
 222     if (!_table->has_entry((void*)thread)) {
 223       // The same JavaThread might be on more than one ThreadsList or
 224       // more than one thread might be using the same ThreadsList. In
 225       // either case, we only need a single entry for a JavaThread.
 226       _table->add_entry((void*)thread);
 227     }
 228   }
 229 };
 230 
 231 // Closure to gather JavaThreads indirectly referenced by hazard ptrs
 232 // (ThreadsList references) into a hash table. This closure handles part 1
 233 // of the dance - hazard ptr chain walking and dispatch to another
 234 // closure.
 235 //
 236 class ScanHazardPtrGatherProtectedThreadsClosure : public ThreadClosure {
 237  private:
 238   ThreadScanHashtable *_table;
 239  public:
 240   ScanHazardPtrGatherProtectedThreadsClosure(ThreadScanHashtable *table) : _table(table) {}
 241 
 242   virtual void do_thread(Thread *thread) {
 243     assert_locked_or_safepoint(Threads_lock);
 244 
 245     if (thread == NULL) return;
 246 
 247     // This code races with ThreadsSMRSupport::acquire_stable_list() which
 248     // is lock-free so we have to handle some special situations.
 249     //
 250     ThreadsList *current_list = NULL;
 251     while (true) {
 252       current_list = thread->get_threads_hazard_ptr();
 253       // No hazard ptr so nothing more to do.
 254       if (current_list == NULL) {
 255         assert(thread->get_nested_threads_hazard_ptr() == NULL,
 256                "cannot have a nested hazard ptr with a NULL regular hazard ptr");
 257         return;
 258       }
 259 
 260       // If the hazard ptr is verified as stable (since it is not tagged),
 261       // then it is safe to use.
 262       if (!Thread::is_hazard_ptr_tagged(current_list)) break;
 263 
 264       // The hazard ptr is tagged as not yet verified as being stable
 265       // so we are racing with acquire_stable_list(). This exchange
 266       // attempts to invalidate the hazard ptr. If we win the race,
 267       // then we can ignore this unstable hazard ptr and the other
 268       // thread will retry the attempt to publish a stable hazard ptr.
 269       // If we lose the race, then we retry our attempt to look at the
 270       // hazard ptr.
 271       if (thread->cmpxchg_threads_hazard_ptr(NULL, current_list) == current_list) return;
 272     }
 273 
 274     // The current JavaThread has a hazard ptr (ThreadsList reference)
 275     // which might be _java_thread_list or it might be an older
 276     // ThreadsList that has been removed but not freed. In either case,
 277     // the hazard ptr is protecting all the JavaThreads on that
 278     // ThreadsList.
 279     AddThreadHazardPointerThreadClosure add_cl(_table);
 280     current_list->threads_do(&add_cl);
 281 
 282     // Any NestedThreadsLists are also protecting JavaThreads so
 283     // gather those also; the ThreadsLists may be different.
 284     for (NestedThreadsList* node = thread->get_nested_threads_hazard_ptr();
 285          node != NULL; node = node->next()) {
 286       node->t_list()->threads_do(&add_cl);
 287     }
 288   }
 289 };
 290 
 291 // Closure to gather hazard ptrs (ThreadsList references) into a hash table.
 292 //
 293 class ScanHazardPtrGatherThreadsListClosure : public ThreadClosure {
 294  private:
 295   ThreadScanHashtable *_table;
 296  public:
 297   ScanHazardPtrGatherThreadsListClosure(ThreadScanHashtable *table) : _table(table) {}
 298 
 299   virtual void do_thread(Thread* thread) {
 300     assert_locked_or_safepoint(Threads_lock);
 301 
 302     if (thread == NULL) return;
 303     ThreadsList *threads = thread->get_threads_hazard_ptr();
 304     if (threads == NULL) {
 305       assert(thread->get_nested_threads_hazard_ptr() == NULL,
 306              "cannot have a nested hazard ptr with a NULL regular hazard ptr");
 307       return;
 308     }
 309     // In this closure we always ignore the tag that might mark this
 310     // hazard ptr as not yet verified. If we happen to catch an
 311     // unverified hazard ptr that is subsequently discarded (not
 312     // published), then the only side effect is that we might keep a
 313     // to-be-deleted ThreadsList alive a little longer.
 314     threads = Thread::untag_hazard_ptr(threads);
 315     if (!_table->has_entry((void*)threads)) {
 316       _table->add_entry((void*)threads);
 317     }
 318 
 319     // Any NestedThreadsLists are also protecting JavaThreads so
 320     // gather those also; the ThreadsLists may be different.
 321     for (NestedThreadsList* node = thread->get_nested_threads_hazard_ptr();
 322          node != NULL; node = node->next()) {
 323       threads = node->t_list();
 324       if (!_table->has_entry((void*)threads)) {
 325         _table->add_entry((void*)threads);
 326       }
 327     }
 328   }
 329 };
 330 
 331 // Closure to print JavaThreads that have a hazard ptr (ThreadsList
 332 // reference) that contains an indirect reference to a specific JavaThread.
 333 //
 334 class ScanHazardPtrPrintMatchingThreadsClosure : public ThreadClosure {
 335  private:
 336   JavaThread *_thread;
 337  public:
 338   ScanHazardPtrPrintMatchingThreadsClosure(JavaThread *thread) : _thread(thread) {}
 339 
 340   virtual void do_thread(Thread *thread) {
 341     assert_locked_or_safepoint(Threads_lock);
 342 
 343     if (thread == NULL) return;
 344     ThreadsList *current_list = thread->get_threads_hazard_ptr();
 345     if (current_list == NULL) {
 346       assert(thread->get_nested_threads_hazard_ptr() == NULL,
 347              "cannot have a nested hazard ptr with a NULL regular hazard ptr");
 348       return;
 349     }
 350     // If the hazard ptr is unverified, then ignore it.
 351     if (Thread::is_hazard_ptr_tagged(current_list)) return;
 352 
 353     // The current JavaThread has a hazard ptr (ThreadsList reference)
 354     // which might be _java_thread_list or it might be an older
 355     // ThreadsList that has been removed but not freed. In either case,
 356     // the hazard ptr is protecting all the JavaThreads on that
 357     // ThreadsList, but we only care about matching a specific JavaThread.
 358     JavaThreadIterator jti(current_list);
 359     for (JavaThread *p = jti.first(); p != NULL; p = jti.next()) {
 360       if (p == _thread) {
 361         log_debug(thread, smr)("tid=" UINTX_FORMAT ": ThreadsSMRSupport::smr_delete: thread1=" INTPTR_FORMAT " has a hazard pointer for thread2=" INTPTR_FORMAT, os::current_thread_id(), p2i(thread), p2i(_thread));
 362         break;
 363       }
 364     }
 365 
 366     // Any NestedThreadsLists are also protecting JavaThreads so
 367     // check those also; the ThreadsLists may be different.
 368     for (NestedThreadsList* node = thread->get_nested_threads_hazard_ptr();
 369          node != NULL; node = node->next()) {
 370       JavaThreadIterator jti(node->t_list());
 371       for (JavaThread *p = jti.first(); p != NULL; p = jti.next()) {
 372         if (p == _thread) {
 373           log_debug(thread, smr)("tid=" UINTX_FORMAT ": ThreadsSMRSupport::smr_delete: thread1=" INTPTR_FORMAT " has a nested hazard pointer for thread2=" INTPTR_FORMAT, os::current_thread_id(), p2i(thread), p2i(_thread));
 374           return;
 375         }
 376       }
 377     }
 378   }
 379 };
 380 
 381 
 382 // 'entries + 1' so we always have at least one entry.
 383 ThreadsList::ThreadsList(int entries) : _length(entries), _threads(NEW_C_HEAP_ARRAY(JavaThread*, entries + 1, mtThread)), _next_list(NULL) {
 384   *(JavaThread**)(_threads + entries) = NULL;  // Make sure the extra entry is NULL.
 385 }
 386 
 387 ThreadsList::~ThreadsList() {
 388   FREE_C_HEAP_ARRAY(JavaThread*, _threads);
 389 }
 390 
 391 // Add a JavaThread to a ThreadsList. The returned ThreadsList is a
 392 // new copy of the specified ThreadsList with the specified JavaThread
 393 // appended to the end.
 394 ThreadsList *ThreadsList::add_thread(ThreadsList *list, JavaThread *java_thread) {
 395   const uint index = list->_length;
 396   const uint new_length = index + 1;
 397   const uint head_length = index;
 398   ThreadsList *const new_list = new ThreadsList(new_length);
 399 
 400   if (head_length > 0) {
 401     Copy::disjoint_words((HeapWord*)list->_threads, (HeapWord*)new_list->_threads, head_length);
 402   }
 403   *(JavaThread**)(new_list->_threads + index) = java_thread;
 404 
 405   return new_list;
 406 }
 407 
 408 int ThreadsList::find_index_of_JavaThread(JavaThread *target) {
 409   if (target == NULL) {
 410     return -1;
 411   }
 412   for (uint i = 0; i < length(); i++) {
 413     if (target == thread_at(i)) {
 414       return (int)i;
 415     }
 416   }
 417   return -1;
 418 }
 419 
 420 JavaThread* ThreadsList::find_JavaThread_from_java_tid(jlong java_tid) const {
 421   for (uint i = 0; i < length(); i++) {
 422     JavaThread* thread = thread_at(i);
 423     oop tobj = thread->threadObj();
 424     // Ignore the thread if it hasn't run yet, has exited
 425     // or is starting to exit.
 426     if (tobj != NULL && !thread->is_exiting() &&
 427         java_tid == java_lang_Thread::thread_id(tobj)) {
 428       // found a match
 429       return thread;
 430     }
 431   }
 432   return NULL;
 433 }
 434 
 435 bool ThreadsList::includes(const JavaThread * const p) const {
 436   if (p == NULL) {
 437     return false;
 438   }
 439   for (uint i = 0; i < length(); i++) {
 440     if (thread_at(i) == p) {
 441       return true;
 442     }
 443   }
 444   return false;
 445 }
 446 
 447 // Remove a JavaThread from a ThreadsList. The returned ThreadsList is a
 448 // new copy of the specified ThreadsList with the specified JavaThread
 449 // removed.
 450 ThreadsList *ThreadsList::remove_thread(ThreadsList* list, JavaThread* java_thread) {
 451   assert(list->_length > 0, "sanity");
 452 
 453   uint i = (uint)list->find_index_of_JavaThread(java_thread);
 454   assert(i < list->_length, "did not find JavaThread on the list");
 455   const uint index = i;
 456   const uint new_length = list->_length - 1;
 457   const uint head_length = index;
 458   const uint tail_length = (new_length >= index) ? (new_length - index) : 0;
 459   ThreadsList *const new_list = new ThreadsList(new_length);
 460 
 461   if (head_length > 0) {
 462     Copy::disjoint_words((HeapWord*)list->_threads, (HeapWord*)new_list->_threads, head_length);
 463   }
 464   if (tail_length > 0) {
 465     Copy::disjoint_words((HeapWord*)list->_threads + index + 1, (HeapWord*)new_list->_threads + index, tail_length);
 466   }
 467 
 468   return new_list;
 469 }
 470 
 471 ThreadsListHandle::ThreadsListHandle(Thread *self) : _list(ThreadsSMRSupport::acquire_stable_list(self, /* is_ThreadsListSetter */ false)), _self(self) {
 472   assert(self == Thread::current(), "sanity check");
 473   if (EnableThreadSMRStatistics) {
 474     _timer.start();
 475   }
 476 }
 477 
 478 ThreadsListHandle::~ThreadsListHandle() {
 479   ThreadsSMRSupport::release_stable_list(_self);
 480   if (EnableThreadSMRStatistics) {
 481     _timer.stop();
 482     uint millis = (uint)_timer.milliseconds();
 483     ThreadsSMRSupport::update_tlh_stats(millis);
 484   }
 485 }
 486 
 487 // Convert an internal thread reference to a JavaThread found on the
 488 // associated ThreadsList. This ThreadsListHandle "protects" the
 489 // returned JavaThread *.
 490 //
 491 // If thread_oop_p is not NULL, then the caller wants to use the oop
 492 // after this call so the oop is returned. On success, *jt_pp is set
 493 // to the converted JavaThread * and true is returned. On error,
 494 // returns false.
 495 //
 496 bool ThreadsListHandle::cv_internal_thread_to_JavaThread(jobject jthread,
 497                                                          JavaThread ** jt_pp,
 498                                                          oop * thread_oop_p) {
 499   assert(this->list() != NULL, "must have a ThreadsList");
 500   assert(jt_pp != NULL, "must have a return JavaThread pointer");
 501   // thread_oop_p is optional so no assert()
 502 
 503   // The JVM_* interfaces don't allow a NULL thread parameter; JVM/TI
 504   // allows a NULL thread parameter to signify "current thread" which
 505   // allows us to avoid calling cv_external_thread_to_JavaThread().
 506   // The JVM_* interfaces have no such leeway.
 507 
 508   oop thread_oop = JNIHandles::resolve_non_null(jthread);
 509   // Looks like an oop at this point.
 510   if (thread_oop_p != NULL) {
 511     // Return the oop to the caller; the caller may still want
 512     // the oop even if this function returns false.
 513     *thread_oop_p = thread_oop;
 514   }
 515 
 516   JavaThread *java_thread = java_lang_Thread::thread(thread_oop);
 517   if (java_thread == NULL) {
 518     // The java.lang.Thread does not contain a JavaThread * so it has
 519     // not yet run or it has died.
 520     return false;
 521   }
 522   // Looks like a live JavaThread at this point.
 523 
 524   if (java_thread != JavaThread::current()) {
 525     // jthread is not for the current JavaThread so have to verify
 526     // the JavaThread * against the ThreadsList.
 527     if (EnableThreadSMRExtraValidityChecks && !includes(java_thread)) {
 528       // Not on the JavaThreads list so it is not alive.
 529       return false;
 530     }
 531   }
 532 
 533   // Return a live JavaThread that is "protected" by the
 534   // ThreadsListHandle in the caller.
 535   *jt_pp = java_thread;
 536   return true;
 537 }
 538 
 539 ThreadsListSetter::~ThreadsListSetter() {
 540   if (_target_needs_release) {
 541     // The hazard ptr in the target needs to be released.
 542     ThreadsSMRSupport::release_stable_list(_target);
 543   }
 544 }
 545 
 546 // Closure to determine if the specified JavaThread is found by
 547 // threads_do().
 548 //
 549 class VerifyHazardPointerThreadClosure : public ThreadClosure {
 550  private:
 551   bool _found;
 552   Thread *_self;
 553 
 554  public:
 555   VerifyHazardPointerThreadClosure(Thread *self) : _found(false), _self(self) {}
 556 
 557   bool found() const { return _found; }
 558 
 559   virtual void do_thread(Thread *thread) {
 560     if (thread == _self) {
 561       _found = true;
 562     }
 563   }
 564 };
 565 
 566 // Apply the closure to all threads in the system, with a snapshot of
 567 // all JavaThreads provided by the list parameter.
 568 void ThreadsSMRSupport::threads_do(ThreadClosure *tc, ThreadsList *list) {
 569   list->threads_do(tc);
 570   Threads::non_java_threads_do(tc);
 571 }
 572 
 573 // Apply the closure to all threads in the system.
 574 void ThreadsSMRSupport::threads_do(ThreadClosure *tc) {
 575   threads_do(tc, _java_thread_list);
 576 }
 577 
 578 // Verify that the stable hazard pointer used to safely keep threads
 579 // alive is scanned by threads_do() which is a key piece of honoring
 580 // the Thread-SMR protocol.
 581 void ThreadsSMRSupport::verify_hazard_pointer_scanned(Thread *self, ThreadsList *threads) {
 582 #ifdef ASSERT
 583   // The closure will attempt to verify that the calling thread can
 584   // be found by threads_do() on the specified ThreadsList. If it
 585   // is successful, then the specified ThreadsList was acquired as
 586   // a stable hazard pointer by the calling thread in a way that
 587   // honored the Thread-SMR protocol.
 588   //
 589   // If the calling thread cannot be found by threads_do() and if
 590   // it is not the shutdown thread, then the calling thread is not
 591   // honoring the Thread-SMR ptotocol. This means that the specified
 592   // ThreadsList is not a stable hazard pointer and can be freed
 593   // by another thread from the to-be-deleted list at any time.
 594   //
 595   // Note: The shutdown thread has removed itself from the Threads
 596   // list and is safe to have a waiver from this check because
 597   // VM_Exit::_shutdown_thread is not set until after the VMThread
 598   // has started the final safepoint which holds the Threads_lock
 599   // for the remainder of the VM's life.
 600   //
 601   VerifyHazardPointerThreadClosure cl(self);
 602   threads_do(&cl, threads);
 603 
 604   // If the calling thread is not honoring the Thread-SMR protocol,
 605   // then we will either crash in threads_do() above because 'threads'
 606   // was freed by another thread or we will fail the assert() below.
 607   // In either case, we won't get past this point with a badly placed
 608   // ThreadsListHandle.
 609 
 610   assert(cl.found() || self == VM_Exit::shutdown_thread(), "Acquired a ThreadsList snapshot from a thread not recognized by the Thread-SMR protocol.");
 611 #endif
 612 }
 613 
 614 void ThreadsListSetter::set() {
 615   assert(_target->get_threads_hazard_ptr() == NULL, "hazard ptr should not already be set");
 616   (void) ThreadsSMRSupport::acquire_stable_list(_target, /* is_ThreadsListSetter */ true);
 617   _target_needs_release = true;
 618 }
 619 
 620 // Acquire a stable ThreadsList.
 621 //
 622 ThreadsList *ThreadsSMRSupport::acquire_stable_list(Thread *self, bool is_ThreadsListSetter) {
 623   assert(self != NULL, "sanity check");
 624   // acquire_stable_list_nested_path() will grab the Threads_lock
 625   // so let's make sure the ThreadsListHandle is in a safe place.
 626   // ThreadsListSetter cannot make this check on this code path.
 627   debug_only(if (!is_ThreadsListSetter && StrictSafepointChecks) self->check_for_valid_safepoint_state(/* potential_vm_operation */ false);)
 628 
 629   if (self->get_threads_hazard_ptr() == NULL) {
 630     // The typical case is first.
 631     return acquire_stable_list_fast_path(self);
 632   }
 633 
 634   // The nested case is rare.
 635   return acquire_stable_list_nested_path(self);
 636 }
 637 
 638 // Fast path (and lock free) way to acquire a stable ThreadsList.
 639 //
 640 ThreadsList *ThreadsSMRSupport::acquire_stable_list_fast_path(Thread *self) {
 641   assert(self != NULL, "sanity check");
 642   assert(self->get_threads_hazard_ptr() == NULL, "sanity check");
 643   assert(self->get_nested_threads_hazard_ptr() == NULL,
 644          "cannot have a nested hazard ptr with a NULL regular hazard ptr");
 645 
 646   ThreadsList* threads;
 647 
 648   // Stable recording of a hazard ptr for SMR. This code does not use
 649   // locks so its use of the _java_thread_list & _threads_hazard_ptr
 650   // fields is racy relative to code that uses those fields with locks.
 651   // OrderAccess and Atomic functions are used to deal with those races.
 652   //
 653   while (true) {
 654     threads = get_java_thread_list();
 655 
 656     // Publish a tagged hazard ptr to denote that the hazard ptr is not
 657     // yet verified as being stable. Due to the fence after the hazard
 658     // ptr write, it will be sequentially consistent w.r.t. the
 659     // sequentially consistent writes of the ThreadsList, even on
 660     // non-multiple copy atomic machines where stores can be observed
 661     // in different order from different observer threads.
 662     ThreadsList* unverified_threads = Thread::tag_hazard_ptr(threads);
 663     self->set_threads_hazard_ptr(unverified_threads);
 664 
 665     // If _java_thread_list has changed, we have lost a race with
 666     // Threads::add() or Threads::remove() and have to try again.
 667     if (get_java_thread_list() != threads) {
 668       continue;
 669     }
 670 
 671     // We try to remove the tag which will verify the hazard ptr as
 672     // being stable. This exchange can race with a scanning thread
 673     // which might invalidate the tagged hazard ptr to keep it from
 674     // being followed to access JavaThread ptrs. If we lose the race,
 675     // we simply retry. If we win the race, then the stable hazard
 676     // ptr is officially published.
 677     if (self->cmpxchg_threads_hazard_ptr(threads, unverified_threads) == unverified_threads) {
 678       break;
 679     }
 680   }
 681 
 682   // A stable hazard ptr has been published letting other threads know
 683   // that the ThreadsList and the JavaThreads reachable from this list
 684   // are protected and hence they should not be deleted until everyone
 685   // agrees it is safe to do so.
 686 
 687   verify_hazard_pointer_scanned(self, threads);
 688 
 689   return threads;
 690 }
 691 
 692 // Acquire a nested stable ThreadsList; this is rare so it uses
 693 // Threads_lock.
 694 //
 695 ThreadsList *ThreadsSMRSupport::acquire_stable_list_nested_path(Thread *self) {
 696   assert(self != NULL, "sanity check");
 697   assert(self->get_threads_hazard_ptr() != NULL,
 698          "cannot have a NULL regular hazard ptr when acquiring a nested hazard ptr");
 699 
 700   // The thread already has a hazard ptr (ThreadsList ref) so we need
 701   // to create a nested ThreadsListHandle with the current ThreadsList
 702   // since it might be different than our current hazard ptr. The need
 703   // for a nested ThreadsListHandle is rare so we do this while holding
 704   // the Threads_lock so we don't race with the scanning code; the code
 705   // is so much simpler this way.
 706 
 707   NestedThreadsList* node;
 708   {
 709     // Only grab the Threads_lock if we don't already own it.
 710     MutexLockerEx ml(Threads_lock->owned_by_self() ? NULL : Threads_lock);
 711     node = new NestedThreadsList(get_java_thread_list());
 712     // We insert at the front of the list to match up with the delete
 713     // in release_stable_list().
 714     node->set_next(self->get_nested_threads_hazard_ptr());
 715     self->set_nested_threads_hazard_ptr(node);
 716     if (EnableThreadSMRStatistics) {
 717       self->inc_nested_threads_hazard_ptr_cnt();
 718       if (self->nested_threads_hazard_ptr_cnt() > _nested_thread_list_max) {
 719         _nested_thread_list_max = self->nested_threads_hazard_ptr_cnt();
 720       }
 721     }
 722   }
 723   log_debug(thread, smr)("tid=" UINTX_FORMAT ": ThreadsSMRSupport::acquire_stable_list: add NestedThreadsList node containing ThreadsList=" INTPTR_FORMAT, os::current_thread_id(), p2i(node->t_list()));
 724 
 725   verify_hazard_pointer_scanned(self, node->t_list());
 726 
 727   return node->t_list();
 728 }
 729 
 730 void ThreadsSMRSupport::add_thread(JavaThread *thread){
 731   ThreadsList *new_list = ThreadsList::add_thread(ThreadsSMRSupport::get_java_thread_list(), thread);
 732   if (EnableThreadSMRStatistics) {
 733     ThreadsSMRSupport::inc_java_thread_list_alloc_cnt();
 734     ThreadsSMRSupport::update_java_thread_list_max(new_list->length());
 735   }
 736   // Initial _java_thread_list will not generate a "Threads::add" mesg.
 737   log_debug(thread, smr)("tid=" UINTX_FORMAT ": Threads::add: new ThreadsList=" INTPTR_FORMAT, os::current_thread_id(), p2i(new_list));
 738 
 739   ThreadsList *old_list = ThreadsSMRSupport::xchg_java_thread_list(new_list);
 740   ThreadsSMRSupport::free_list(old_list);
 741 }
 742 
 743 // set_delete_notify() and clear_delete_notify() are called
 744 // under the protection of the delete_lock, but we also use an
 745 // Atomic operation to ensure the memory update is seen earlier than
 746 // when the delete_lock is dropped.
 747 //
 748 void ThreadsSMRSupport::clear_delete_notify() {
 749   Atomic::dec(&_delete_notify);
 750 }
 751 
 752 bool ThreadsSMRSupport::delete_notify() {
 753   // Use load_acquire() in order to see any updates to _delete_notify
 754   // earlier than when delete_lock is grabbed.
 755   return (OrderAccess::load_acquire(&_delete_notify) != 0);
 756 }
 757 
 758 // Safely free a ThreadsList after a Threads::add() or Threads::remove().
 759 // The specified ThreadsList may not get deleted during this call if it
 760 // is still in-use (referenced by a hazard ptr). Other ThreadsLists
 761 // in the chain may get deleted by this call if they are no longer in-use.
 762 void ThreadsSMRSupport::free_list(ThreadsList* threads) {
 763   assert_locked_or_safepoint(Threads_lock);
 764 
 765   threads->set_next_list(_to_delete_list);
 766   _to_delete_list = threads;
 767   if (EnableThreadSMRStatistics) {
 768     _to_delete_list_cnt++;
 769     if (_to_delete_list_cnt > _to_delete_list_max) {
 770       _to_delete_list_max = _to_delete_list_cnt;
 771     }
 772   }
 773 
 774   // Hash table size should be first power of two higher than twice the length of the ThreadsList
 775   int hash_table_size = MIN2((int)get_java_thread_list()->length(), 32) << 1;
 776   hash_table_size--;
 777   hash_table_size |= hash_table_size >> 1;
 778   hash_table_size |= hash_table_size >> 2;
 779   hash_table_size |= hash_table_size >> 4;
 780   hash_table_size |= hash_table_size >> 8;
 781   hash_table_size |= hash_table_size >> 16;
 782   hash_table_size++;
 783 
 784   // Gather a hash table of the current hazard ptrs:
 785   ThreadScanHashtable *scan_table = new ThreadScanHashtable(hash_table_size);
 786   ScanHazardPtrGatherThreadsListClosure scan_cl(scan_table);
 787   threads_do(&scan_cl);
 788 
 789   // Walk through the linked list of pending freeable ThreadsLists
 790   // and free the ones that are not referenced from hazard ptrs.
 791   ThreadsList* current = _to_delete_list;
 792   ThreadsList* prev = NULL;
 793   ThreadsList* next = NULL;
 794   bool threads_is_freed = false;
 795   while (current != NULL) {
 796     next = current->next_list();
 797     if (!scan_table->has_entry((void*)current)) {
 798       // This ThreadsList is not referenced by a hazard ptr.
 799       if (prev != NULL) {
 800         prev->set_next_list(next);
 801       }
 802       if (_to_delete_list == current) {
 803         _to_delete_list = next;
 804       }
 805 
 806       log_debug(thread, smr)("tid=" UINTX_FORMAT ": ThreadsSMRSupport::free_list: threads=" INTPTR_FORMAT " is freed.", os::current_thread_id(), p2i(current));
 807       if (current == threads) threads_is_freed = true;
 808       delete current;
 809       if (EnableThreadSMRStatistics) {
 810         _java_thread_list_free_cnt++;
 811         _to_delete_list_cnt--;
 812       }
 813     } else {
 814       prev = current;
 815     }
 816     current = next;
 817   }
 818 
 819   if (!threads_is_freed) {
 820     // Only report "is not freed" on the original call to
 821     // free_list() for this ThreadsList.
 822     log_debug(thread, smr)("tid=" UINTX_FORMAT ": ThreadsSMRSupport::free_list: threads=" INTPTR_FORMAT " is not freed.", os::current_thread_id(), p2i(threads));
 823   }
 824 
 825   delete scan_table;
 826 }
 827 
 828 // Return true if the specified JavaThread is protected by a hazard
 829 // pointer (ThreadsList reference). Otherwise, returns false.
 830 //
 831 bool ThreadsSMRSupport::is_a_protected_JavaThread(JavaThread *thread) {
 832   assert_locked_or_safepoint(Threads_lock);
 833 
 834   // Hash table size should be first power of two higher than twice
 835   // the length of the Threads list.
 836   int hash_table_size = MIN2((int)get_java_thread_list()->length(), 32) << 1;
 837   hash_table_size--;
 838   hash_table_size |= hash_table_size >> 1;
 839   hash_table_size |= hash_table_size >> 2;
 840   hash_table_size |= hash_table_size >> 4;
 841   hash_table_size |= hash_table_size >> 8;
 842   hash_table_size |= hash_table_size >> 16;
 843   hash_table_size++;
 844 
 845   // Gather a hash table of the JavaThreads indirectly referenced by
 846   // hazard ptrs.
 847   ThreadScanHashtable *scan_table = new ThreadScanHashtable(hash_table_size);
 848   ScanHazardPtrGatherProtectedThreadsClosure scan_cl(scan_table);
 849   threads_do(&scan_cl);
 850 
 851   bool thread_is_protected = false;
 852   if (scan_table->has_entry((void*)thread)) {
 853     thread_is_protected = true;
 854   }
 855   delete scan_table;
 856   return thread_is_protected;
 857 }
 858 
 859 // Release a stable ThreadsList.
 860 //
 861 void ThreadsSMRSupport::release_stable_list(Thread *self) {
 862   assert(self != NULL, "sanity check");
 863   // release_stable_list_nested_path() will grab the Threads_lock
 864   // so let's make sure the ThreadsListHandle is in a safe place.
 865   debug_only(if (StrictSafepointChecks) self->check_for_valid_safepoint_state(/* potential_vm_operation */ false);)
 866 
 867   if (self->get_nested_threads_hazard_ptr() == NULL) {
 868     // The typical case is first.
 869     release_stable_list_fast_path(self);
 870     return;
 871   }
 872 
 873   // The nested case is rare.
 874   release_stable_list_nested_path(self);
 875 }
 876 
 877 // Fast path way to release a stable ThreadsList. The release portion
 878 // is lock-free, but the wake up portion is not.
 879 //
 880 void ThreadsSMRSupport::release_stable_list_fast_path(Thread *self) {
 881   assert(self != NULL, "sanity check");
 882   assert(self->get_threads_hazard_ptr() != NULL, "sanity check");
 883   assert(self->get_nested_threads_hazard_ptr() == NULL,
 884          "cannot have a nested hazard ptr when releasing a regular hazard ptr");
 885 
 886   // After releasing the hazard ptr, other threads may go ahead and
 887   // free up some memory temporarily used by a ThreadsList snapshot.
 888   self->set_threads_hazard_ptr(NULL);
 889 
 890   // We use double-check locking to reduce traffic on the system
 891   // wide Thread-SMR delete_lock.
 892   if (ThreadsSMRSupport::delete_notify()) {
 893     // An exiting thread might be waiting in smr_delete(); we need to
 894     // check with delete_lock to be sure.
 895     release_stable_list_wake_up((char *) "regular hazard ptr");
 896   }
 897 }
 898 
 899 // Release a nested stable ThreadsList; this is rare so it uses
 900 // Threads_lock.
 901 //
 902 void ThreadsSMRSupport::release_stable_list_nested_path(Thread *self) {
 903   assert(self != NULL, "sanity check");
 904   assert(self->get_nested_threads_hazard_ptr() != NULL, "sanity check");
 905   assert(self->get_threads_hazard_ptr() != NULL,
 906          "must have a regular hazard ptr to have nested hazard ptrs");
 907 
 908   // We have a nested ThreadsListHandle so we have to release it first.
 909   // The need for a nested ThreadsListHandle is rare so we do this while
 910   // holding the Threads_lock so we don't race with the scanning code;
 911   // the code is so much simpler this way.
 912 
 913   NestedThreadsList *node;
 914   {
 915     // Only grab the Threads_lock if we don't already own it.
 916     MutexLockerEx ml(Threads_lock->owned_by_self() ? NULL : Threads_lock);
 917     // We remove from the front of the list to match up with the insert
 918     // in acquire_stable_list().
 919     node = self->get_nested_threads_hazard_ptr();
 920     self->set_nested_threads_hazard_ptr(node->next());
 921     if (EnableThreadSMRStatistics) {
 922       self->dec_nested_threads_hazard_ptr_cnt();
 923     }
 924   }
 925 
 926   // An exiting thread might be waiting in smr_delete(); we need to
 927   // check with delete_lock to be sure.
 928   release_stable_list_wake_up((char *) "nested hazard ptr");
 929 
 930   log_debug(thread, smr)("tid=" UINTX_FORMAT ": ThreadsSMRSupport::release_stable_list: delete NestedThreadsList node containing ThreadsList=" INTPTR_FORMAT, os::current_thread_id(), p2i(node->t_list()));
 931 
 932   delete node;
 933 }
 934 
 935 // Wake up portion of the release stable ThreadsList protocol;
 936 // uses the delete_lock().
 937 //
 938 void ThreadsSMRSupport::release_stable_list_wake_up(char *log_str) {
 939   assert(log_str != NULL, "sanity check");
 940 
 941   // Note: delete_lock is held in smr_delete() for the entire
 942   // hazard ptr search so that we do not lose this notify() if
 943   // the exiting thread has to wait. That code path also holds
 944   // Threads_lock (which was grabbed before delete_lock) so that
 945   // threads_do() can be called. This means the system can't start a
 946   // safepoint which means this thread can't take too long to get to
 947   // a safepoint because of being blocked on delete_lock.
 948   //
 949   MonitorLockerEx ml(ThreadsSMRSupport::delete_lock(), Monitor::_no_safepoint_check_flag);
 950   if (ThreadsSMRSupport::delete_notify()) {
 951     // Notify any exiting JavaThreads that are waiting in smr_delete()
 952     // that we've released a ThreadsList.
 953     ml.notify_all();
 954     log_debug(thread, smr)("tid=" UINTX_FORMAT ": ThreadsSMRSupport::release_stable_list notified %s", os::current_thread_id(), log_str);
 955   }
 956 }
 957 
 958 void ThreadsSMRSupport::remove_thread(JavaThread *thread) {
 959   ThreadsList *new_list = ThreadsList::remove_thread(ThreadsSMRSupport::get_java_thread_list(), thread);
 960   if (EnableThreadSMRStatistics) {
 961     ThreadsSMRSupport::inc_java_thread_list_alloc_cnt();
 962     // This list is smaller so no need to check for a "longest" update.
 963   }
 964 
 965   // Final _java_thread_list will not generate a "Threads::remove" mesg.
 966   log_debug(thread, smr)("tid=" UINTX_FORMAT ": Threads::remove: new ThreadsList=" INTPTR_FORMAT, os::current_thread_id(), p2i(new_list));
 967 
 968   ThreadsList *old_list = ThreadsSMRSupport::xchg_java_thread_list(new_list);
 969   ThreadsSMRSupport::free_list(old_list);
 970 }
 971 
 972 // See note for clear_delete_notify().
 973 //
 974 void ThreadsSMRSupport::set_delete_notify() {
 975   Atomic::inc(&_delete_notify);
 976 }
 977 
 978 // Safely delete a JavaThread when it is no longer in use by a
 979 // ThreadsListHandle.
 980 //
 981 void ThreadsSMRSupport::smr_delete(JavaThread *thread) {
 982   assert(!Threads_lock->owned_by_self(), "sanity");
 983 
 984   bool has_logged_once = false;
 985   elapsedTimer timer;
 986   if (EnableThreadSMRStatistics) {
 987     timer.start();
 988   }
 989 
 990   while (true) {
 991     {
 992       // No safepoint check because this JavaThread is not on the
 993       // Threads list.
 994       MutexLockerEx ml(Threads_lock, Mutex::_no_safepoint_check_flag);
 995       // Cannot use a MonitorLockerEx helper here because we have
 996       // to drop the Threads_lock first if we wait.
 997       ThreadsSMRSupport::delete_lock()->lock_without_safepoint_check();
 998       // Set the delete_notify flag after we grab delete_lock
 999       // and before we scan hazard ptrs because we're doing
1000       // double-check locking in release_stable_list().
1001       ThreadsSMRSupport::set_delete_notify();
1002 
1003       if (!is_a_protected_JavaThread(thread)) {
1004         // This is the common case.
1005         ThreadsSMRSupport::clear_delete_notify();
1006         ThreadsSMRSupport::delete_lock()->unlock();
1007         break;
1008       }
1009       if (!has_logged_once) {
1010         has_logged_once = true;
1011         log_debug(thread, smr)("tid=" UINTX_FORMAT ": ThreadsSMRSupport::smr_delete: thread=" INTPTR_FORMAT " is not deleted.", os::current_thread_id(), p2i(thread));
1012         if (log_is_enabled(Debug, os, thread)) {
1013           ScanHazardPtrPrintMatchingThreadsClosure scan_cl(thread);
1014           threads_do(&scan_cl);
1015         }
1016       }
1017     } // We have to drop the Threads_lock to wait or delete the thread
1018 
1019     if (EnableThreadSMRStatistics) {
1020       _delete_lock_wait_cnt++;
1021       if (_delete_lock_wait_cnt > _delete_lock_wait_max) {
1022         _delete_lock_wait_max = _delete_lock_wait_cnt;
1023       }
1024     }
1025     // Wait for a release_stable_list() call before we check again. No
1026     // safepoint check, no timeout, and not as suspend equivalent flag
1027     // because this JavaThread is not on the Threads list.
1028     ThreadsSMRSupport::delete_lock()->wait(Mutex::_no_safepoint_check_flag, 0,
1029                                      !Mutex::_as_suspend_equivalent_flag);
1030     if (EnableThreadSMRStatistics) {
1031       _delete_lock_wait_cnt--;
1032     }
1033 
1034     ThreadsSMRSupport::clear_delete_notify();
1035     ThreadsSMRSupport::delete_lock()->unlock();
1036     // Retry the whole scenario.
1037   }
1038 
1039   if (ThreadLocalHandshakes) {
1040     // The thread is about to be deleted so cancel any handshake.
1041     thread->cancel_handshake();
1042   }
1043 
1044   delete thread;
1045   if (EnableThreadSMRStatistics) {
1046     timer.stop();
1047     uint millis = (uint)timer.milliseconds();
1048     ThreadsSMRSupport::inc_deleted_thread_cnt();
1049     ThreadsSMRSupport::add_deleted_thread_times(millis);
1050     ThreadsSMRSupport::update_deleted_thread_time_max(millis);
1051   }
1052 
1053   log_debug(thread, smr)("tid=" UINTX_FORMAT ": ThreadsSMRSupport::smr_delete: thread=" INTPTR_FORMAT " is deleted.", os::current_thread_id(), p2i(thread));
1054 }
1055 
1056 
1057 // Debug, logging, and printing stuff at the end:
1058 
1059 // Log Threads class SMR info.
1060 void ThreadsSMRSupport::log_statistics() {
1061   LogTarget(Info, thread, smr) log;
1062   if (log.is_enabled()) {
1063     LogStream out(log);
1064     print_info_on(&out);
1065   }
1066 }
1067 
1068 // Print Threads class SMR info.
1069 void ThreadsSMRSupport::print_info_on(outputStream* st) {
1070   // Only grab the Threads_lock if we don't already own it
1071   // and if we are not reporting an error.
1072   MutexLockerEx ml((Threads_lock->owned_by_self() || VMError::is_error_reported()) ? NULL : Threads_lock);
1073 
1074   st->print_cr("Threads class SMR info:");
1075   st->print_cr("_java_thread_list=" INTPTR_FORMAT ", length=%u, "
1076                "elements={", p2i(_java_thread_list),
1077                _java_thread_list->length());
1078   print_info_elements_on(st, _java_thread_list);
1079   st->print_cr("}");
1080   if (_to_delete_list != NULL) {
1081     st->print_cr("_to_delete_list=" INTPTR_FORMAT ", length=%u, "
1082                  "elements={", p2i(_to_delete_list),
1083                  _to_delete_list->length());
1084     print_info_elements_on(st, _to_delete_list);
1085     st->print_cr("}");
1086     for (ThreadsList *t_list = _to_delete_list->next_list();
1087          t_list != NULL; t_list = t_list->next_list()) {
1088       st->print("next-> " INTPTR_FORMAT ", length=%u, "
1089                 "elements={", p2i(t_list), t_list->length());
1090       print_info_elements_on(st, t_list);
1091       st->print_cr("}");
1092     }
1093   }
1094   if (!EnableThreadSMRStatistics) {
1095     return;
1096   }
1097   st->print_cr("_java_thread_list_alloc_cnt=" UINT64_FORMAT ","
1098                "_java_thread_list_free_cnt=" UINT64_FORMAT ","
1099                "_java_thread_list_max=%u, "
1100                "_nested_thread_list_max=%u",
1101                _java_thread_list_alloc_cnt,
1102                _java_thread_list_free_cnt,
1103                _java_thread_list_max,
1104                _nested_thread_list_max);
1105   if (_tlh_cnt > 0) {
1106     st->print_cr("_tlh_cnt=%u"
1107                  ", _tlh_times=%u"
1108                  ", avg_tlh_time=%0.2f"
1109                  ", _tlh_time_max=%u",
1110                  _tlh_cnt, _tlh_times,
1111                  ((double) _tlh_times / _tlh_cnt),
1112                  _tlh_time_max);
1113   }
1114   if (_deleted_thread_cnt > 0) {
1115     st->print_cr("_deleted_thread_cnt=%u"
1116                  ", _deleted_thread_times=%u"
1117                  ", avg_deleted_thread_time=%0.2f"
1118                  ", _deleted_thread_time_max=%u",
1119                  _deleted_thread_cnt, _deleted_thread_times,
1120                  ((double) _deleted_thread_times / _deleted_thread_cnt),
1121                  _deleted_thread_time_max);
1122   }
1123   st->print_cr("_delete_lock_wait_cnt=%u, _delete_lock_wait_max=%u",
1124                _delete_lock_wait_cnt, _delete_lock_wait_max);
1125   st->print_cr("_to_delete_list_cnt=%u, _to_delete_list_max=%u",
1126                _to_delete_list_cnt, _to_delete_list_max);
1127 }
1128 
1129 // Print ThreadsList elements (4 per line).
1130 void ThreadsSMRSupport::print_info_elements_on(outputStream* st, ThreadsList* t_list) {
1131   uint cnt = 0;
1132   JavaThreadIterator jti(t_list);
1133   for (JavaThread *jt = jti.first(); jt != NULL; jt = jti.next()) {
1134     st->print(INTPTR_FORMAT, p2i(jt));
1135     if (cnt < t_list->length() - 1) {
1136       // Separate with comma or comma-space except for the last one.
1137       if (((cnt + 1) % 4) == 0) {
1138         // Four INTPTR_FORMAT fit on an 80 column line so end the
1139         // current line with just a comma.
1140         st->print_cr(",");
1141       } else {
1142         // Not the last one on the current line so use comma-space:
1143         st->print(", ");
1144       }
1145     } else {
1146       // Last one so just end the current line.
1147       st->cr();
1148     }
1149     cnt++;
1150   }
1151 }