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