< prev index next >

src/hotspot/share/runtime/threadSMR.cpp

Print this page
rev 49501 : 8200374: Add ThreadsSMRSupport::verify_hazard_pointer_scanned() to verify threads_do().
rev 49502 : Add missing NULL check; use proper lock-free get/set with TracingExport::_sampler_thread.


 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   // Threads::threads_do() is used by the Thread-SMR protocol to visit all
 474   // Threads in the system which ensures the safety of the ThreadsList
 475   // managed by this ThreadsListHandle, but JavaThreads that are not on
 476   // the Threads list cannot be included in that visit. The JavaThread that
 477   // calls Threads::destroy_vm() is exempt from this check because it has
 478   // to logically exit as part of the shutdown procedure. This is safe
 479   // because VM_Exit::_shutdown_thread is not set until after the VMThread
 480   // has started the final safepoint which holds the Threads_lock for the
 481   // remainder of the VM's life.
 482   assert(!self->is_Java_thread() || self == VM_Exit::shutdown_thread() || (((JavaThread*)self)->on_thread_list() && !((JavaThread*)self)->is_terminated()), "JavaThread must be on the Threads list to use a ThreadsListHandle");
 483   if (EnableThreadSMRStatistics) {
 484     _timer.start();
 485   }
 486 }
 487 
 488 ThreadsListHandle::~ThreadsListHandle() {
 489   ThreadsSMRSupport::release_stable_list(_self);
 490   if (EnableThreadSMRStatistics) {
 491     _timer.stop();
 492     uint millis = (uint)_timer.milliseconds();
 493     ThreadsSMRSupport::update_tlh_stats(millis);
 494   }
 495 }
 496 
 497 // Convert an internal thread reference to a JavaThread found on the
 498 // associated ThreadsList. This ThreadsListHandle "protects" the
 499 // returned JavaThread *.
 500 //
 501 // If thread_oop_p is not NULL, then the caller wants to use the oop
 502 // after this call so the oop is returned. On success, *jt_pp is set


 536     // the JavaThread * against the ThreadsList.
 537     if (EnableThreadSMRExtraValidityChecks && !includes(java_thread)) {
 538       // Not on the JavaThreads list so it is not alive.
 539       return false;
 540     }
 541   }
 542 
 543   // Return a live JavaThread that is "protected" by the
 544   // ThreadsListHandle in the caller.
 545   *jt_pp = java_thread;
 546   return true;
 547 }
 548 
 549 ThreadsListSetter::~ThreadsListSetter() {
 550   if (_target_needs_release) {
 551     // The hazard ptr in the target needs to be released.
 552     ThreadsSMRSupport::release_stable_list(_target);
 553   }
 554 }
 555 






































































 556 void ThreadsListSetter::set() {
 557   assert(_target->get_threads_hazard_ptr() == NULL, "hazard ptr should not already be set");
 558   (void) ThreadsSMRSupport::acquire_stable_list(_target, /* is_ThreadsListSetter */ true);
 559   _target_needs_release = true;
 560 }
 561 
 562 // Acquire a stable ThreadsList.
 563 //
 564 ThreadsList *ThreadsSMRSupport::acquire_stable_list(Thread *self, bool is_ThreadsListSetter) {
 565   assert(self != NULL, "sanity check");
 566   // acquire_stable_list_nested_path() will grab the Threads_lock
 567   // so let's make sure the ThreadsListHandle is in a safe place.
 568   // ThreadsListSetter cannot make this check on this code path.
 569   debug_only(if (!is_ThreadsListSetter && StrictSafepointChecks) self->check_for_valid_safepoint_state(/* potential_vm_operation */ false);)
 570 
 571   if (self->get_threads_hazard_ptr() == NULL) {
 572     // The typical case is first.
 573     return acquire_stable_list_fast_path(self);
 574   }
 575 


 609     if (get_java_thread_list() != threads) {
 610       continue;
 611     }
 612 
 613     // We try to remove the tag which will verify the hazard ptr as
 614     // being stable. This exchange can race with a scanning thread
 615     // which might invalidate the tagged hazard ptr to keep it from
 616     // being followed to access JavaThread ptrs. If we lose the race,
 617     // we simply retry. If we win the race, then the stable hazard
 618     // ptr is officially published.
 619     if (self->cmpxchg_threads_hazard_ptr(threads, unverified_threads) == unverified_threads) {
 620       break;
 621     }
 622   }
 623 
 624   // A stable hazard ptr has been published letting other threads know
 625   // that the ThreadsList and the JavaThreads reachable from this list
 626   // are protected and hence they should not be deleted until everyone
 627   // agrees it is safe to do so.
 628 


 629   return threads;
 630 }
 631 
 632 // Acquire a nested stable ThreadsList; this is rare so it uses
 633 // Threads_lock.
 634 //
 635 ThreadsList *ThreadsSMRSupport::acquire_stable_list_nested_path(Thread *self) {
 636   assert(self != NULL, "sanity check");
 637   assert(self->get_threads_hazard_ptr() != NULL,
 638          "cannot have a NULL regular hazard ptr when acquiring a nested hazard ptr");
 639 
 640   // The thread already has a hazard ptr (ThreadsList ref) so we need
 641   // to create a nested ThreadsListHandle with the current ThreadsList
 642   // since it might be different than our current hazard ptr. The need
 643   // for a nested ThreadsListHandle is rare so we do this while holding
 644   // the Threads_lock so we don't race with the scanning code; the code
 645   // is so much simpler this way.
 646 
 647   NestedThreadsList* node;
 648   {
 649     // Only grab the Threads_lock if we don't already own it.
 650     MutexLockerEx ml(Threads_lock->owned_by_self() ? NULL : Threads_lock);
 651     node = new NestedThreadsList(get_java_thread_list());
 652     // We insert at the front of the list to match up with the delete
 653     // in release_stable_list().
 654     node->set_next(self->get_nested_threads_hazard_ptr());
 655     self->set_nested_threads_hazard_ptr(node);
 656     if (EnableThreadSMRStatistics) {
 657       self->inc_nested_threads_hazard_ptr_cnt();
 658       if (self->nested_threads_hazard_ptr_cnt() > _nested_thread_list_max) {
 659         _nested_thread_list_max = self->nested_threads_hazard_ptr_cnt();
 660       }
 661     }
 662   }
 663   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()));
 664 


 665   return node->t_list();
 666 }
 667 
 668 void ThreadsSMRSupport::add_thread(JavaThread *thread){
 669   ThreadsList *new_list = ThreadsList::add_thread(ThreadsSMRSupport::get_java_thread_list(), thread);
 670   if (EnableThreadSMRStatistics) {
 671     ThreadsSMRSupport::inc_java_thread_list_alloc_cnt();
 672     ThreadsSMRSupport::update_java_thread_list_max(new_list->length());
 673   }
 674   // Initial _java_thread_list will not generate a "Threads::add" mesg.
 675   log_debug(thread, smr)("tid=" UINTX_FORMAT ": Threads::add: new ThreadsList=" INTPTR_FORMAT, os::current_thread_id(), p2i(new_list));
 676 
 677   ThreadsList *old_list = ThreadsSMRSupport::xchg_java_thread_list(new_list);
 678   ThreadsSMRSupport::free_list(old_list);
 679 }
 680 
 681 // set_delete_notify() and clear_delete_notify() are called
 682 // under the protection of the delete_lock, but we also use an
 683 // Atomic operation to ensure the memory update is seen earlier than
 684 // when the delete_lock is dropped.


 705   if (EnableThreadSMRStatistics) {
 706     _to_delete_list_cnt++;
 707     if (_to_delete_list_cnt > _to_delete_list_max) {
 708       _to_delete_list_max = _to_delete_list_cnt;
 709     }
 710   }
 711 
 712   // Hash table size should be first power of two higher than twice the length of the ThreadsList
 713   int hash_table_size = MIN2((int)get_java_thread_list()->length(), 32) << 1;
 714   hash_table_size--;
 715   hash_table_size |= hash_table_size >> 1;
 716   hash_table_size |= hash_table_size >> 2;
 717   hash_table_size |= hash_table_size >> 4;
 718   hash_table_size |= hash_table_size >> 8;
 719   hash_table_size |= hash_table_size >> 16;
 720   hash_table_size++;
 721 
 722   // Gather a hash table of the current hazard ptrs:
 723   ThreadScanHashtable *scan_table = new ThreadScanHashtable(hash_table_size);
 724   ScanHazardPtrGatherThreadsListClosure scan_cl(scan_table);
 725   Threads::threads_do(&scan_cl);
 726 
 727   // Walk through the linked list of pending freeable ThreadsLists
 728   // and free the ones that are not referenced from hazard ptrs.
 729   ThreadsList* current = _to_delete_list;
 730   ThreadsList* prev = NULL;
 731   ThreadsList* next = NULL;
 732   bool threads_is_freed = false;
 733   while (current != NULL) {
 734     next = current->next_list();
 735     if (!scan_table->has_entry((void*)current)) {
 736       // This ThreadsList is not referenced by a hazard ptr.
 737       if (prev != NULL) {
 738         prev->set_next_list(next);
 739       }
 740       if (_to_delete_list == current) {
 741         _to_delete_list = next;
 742       }
 743 
 744       log_debug(thread, smr)("tid=" UINTX_FORMAT ": ThreadsSMRSupport::free_list: threads=" INTPTR_FORMAT " is freed.", os::current_thread_id(), p2i(current));
 745       if (current == threads) threads_is_freed = true;


 767 // pointer (ThreadsList reference). Otherwise, returns false.
 768 //
 769 bool ThreadsSMRSupport::is_a_protected_JavaThread(JavaThread *thread) {
 770   assert_locked_or_safepoint(Threads_lock);
 771 
 772   // Hash table size should be first power of two higher than twice
 773   // the length of the Threads list.
 774   int hash_table_size = MIN2((int)get_java_thread_list()->length(), 32) << 1;
 775   hash_table_size--;
 776   hash_table_size |= hash_table_size >> 1;
 777   hash_table_size |= hash_table_size >> 2;
 778   hash_table_size |= hash_table_size >> 4;
 779   hash_table_size |= hash_table_size >> 8;
 780   hash_table_size |= hash_table_size >> 16;
 781   hash_table_size++;
 782 
 783   // Gather a hash table of the JavaThreads indirectly referenced by
 784   // hazard ptrs.
 785   ThreadScanHashtable *scan_table = new ThreadScanHashtable(hash_table_size);
 786   ScanHazardPtrGatherProtectedThreadsClosure scan_cl(scan_table);
 787   Threads::threads_do(&scan_cl);
 788 
 789   bool thread_is_protected = false;
 790   if (scan_table->has_entry((void*)thread)) {
 791     thread_is_protected = true;
 792   }
 793   delete scan_table;
 794   return thread_is_protected;
 795 }
 796 
 797 // Release a stable ThreadsList.
 798 //
 799 void ThreadsSMRSupport::release_stable_list(Thread *self) {
 800   assert(self != NULL, "sanity check");
 801   // release_stable_list_nested_path() will grab the Threads_lock
 802   // so let's make sure the ThreadsListHandle is in a safe place.
 803   debug_only(if (StrictSafepointChecks) self->check_for_valid_safepoint_state(/* potential_vm_operation */ false);)
 804 
 805   if (self->get_nested_threads_hazard_ptr() == NULL) {
 806     // The typical case is first.
 807     release_stable_list_fast_path(self);


 932       MutexLockerEx ml(Threads_lock, Mutex::_no_safepoint_check_flag);
 933       // Cannot use a MonitorLockerEx helper here because we have
 934       // to drop the Threads_lock first if we wait.
 935       ThreadsSMRSupport::delete_lock()->lock_without_safepoint_check();
 936       // Set the delete_notify flag after we grab delete_lock
 937       // and before we scan hazard ptrs because we're doing
 938       // double-check locking in release_stable_list().
 939       ThreadsSMRSupport::set_delete_notify();
 940 
 941       if (!is_a_protected_JavaThread(thread)) {
 942         // This is the common case.
 943         ThreadsSMRSupport::clear_delete_notify();
 944         ThreadsSMRSupport::delete_lock()->unlock();
 945         break;
 946       }
 947       if (!has_logged_once) {
 948         has_logged_once = true;
 949         log_debug(thread, smr)("tid=" UINTX_FORMAT ": ThreadsSMRSupport::smr_delete: thread=" INTPTR_FORMAT " is not deleted.", os::current_thread_id(), p2i(thread));
 950         if (log_is_enabled(Debug, os, thread)) {
 951           ScanHazardPtrPrintMatchingThreadsClosure scan_cl(thread);
 952           Threads::threads_do(&scan_cl);
 953         }
 954       }
 955     } // We have to drop the Threads_lock to wait or delete the thread
 956 
 957     if (EnableThreadSMRStatistics) {
 958       _delete_lock_wait_cnt++;
 959       if (_delete_lock_wait_cnt > _delete_lock_wait_max) {
 960         _delete_lock_wait_max = _delete_lock_wait_cnt;
 961       }
 962     }
 963     // Wait for a release_stable_list() call before we check again. No
 964     // safepoint check, no timeout, and not as suspend equivalent flag
 965     // because this JavaThread is not on the Threads list.
 966     ThreadsSMRSupport::delete_lock()->wait(Mutex::_no_safepoint_check_flag, 0,
 967                                      !Mutex::_as_suspend_equivalent_flag);
 968     if (EnableThreadSMRStatistics) {
 969       _delete_lock_wait_cnt--;
 970     }
 971 
 972     ThreadsSMRSupport::clear_delete_notify();




 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


 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   assert(threads != NULL, "threads must not be NULL");
 584 
 585   // The closure will attempt to verify that the calling thread can
 586   // be found by threads_do() on the specified ThreadsList. If it
 587   // is successful, then the specified ThreadsList was acquired as
 588   // a stable hazard pointer by the calling thread in a way that
 589   // honored the Thread-SMR protocol.
 590   //
 591   // If the calling thread cannot be found by threads_do() and if
 592   // it is not the shutdown thread, then the calling thread is not
 593   // honoring the Thread-SMR ptotocol. This means that the specified
 594   // ThreadsList is not a stable hazard pointer and can be freed
 595   // by another thread from the to-be-deleted list at any time.
 596   //
 597   // Note: The shutdown thread has removed itself from the Threads
 598   // list and is safe to have a waiver from this check because
 599   // VM_Exit::_shutdown_thread is not set until after the VMThread
 600   // has started the final safepoint which holds the Threads_lock
 601   // for the remainder of the VM's life.
 602   //
 603   VerifyHazardPointerThreadClosure cl(self);
 604   threads_do(&cl, threads);
 605 
 606   // If the calling thread is not honoring the Thread-SMR protocol,
 607   // then we will either crash in threads_do() above because 'threads'
 608   // was freed by another thread or we will fail the assert() below.
 609   // In either case, we won't get past this point with a badly placed
 610   // ThreadsListHandle.
 611 
 612   assert(cl.found() || self == VM_Exit::shutdown_thread(), "Acquired a ThreadsList snapshot from a thread not recognized by the Thread-SMR protocol.");
 613 #endif
 614 }
 615 
 616 void ThreadsListSetter::set() {
 617   assert(_target->get_threads_hazard_ptr() == NULL, "hazard ptr should not already be set");
 618   (void) ThreadsSMRSupport::acquire_stable_list(_target, /* is_ThreadsListSetter */ true);
 619   _target_needs_release = true;
 620 }
 621 
 622 // Acquire a stable ThreadsList.
 623 //
 624 ThreadsList *ThreadsSMRSupport::acquire_stable_list(Thread *self, bool is_ThreadsListSetter) {
 625   assert(self != NULL, "sanity check");
 626   // acquire_stable_list_nested_path() will grab the Threads_lock
 627   // so let's make sure the ThreadsListHandle is in a safe place.
 628   // ThreadsListSetter cannot make this check on this code path.
 629   debug_only(if (!is_ThreadsListSetter && StrictSafepointChecks) self->check_for_valid_safepoint_state(/* potential_vm_operation */ false);)
 630 
 631   if (self->get_threads_hazard_ptr() == NULL) {
 632     // The typical case is first.
 633     return acquire_stable_list_fast_path(self);
 634   }
 635 


 669     if (get_java_thread_list() != threads) {
 670       continue;
 671     }
 672 
 673     // We try to remove the tag which will verify the hazard ptr as
 674     // being stable. This exchange can race with a scanning thread
 675     // which might invalidate the tagged hazard ptr to keep it from
 676     // being followed to access JavaThread ptrs. If we lose the race,
 677     // we simply retry. If we win the race, then the stable hazard
 678     // ptr is officially published.
 679     if (self->cmpxchg_threads_hazard_ptr(threads, unverified_threads) == unverified_threads) {
 680       break;
 681     }
 682   }
 683 
 684   // A stable hazard ptr has been published letting other threads know
 685   // that the ThreadsList and the JavaThreads reachable from this list
 686   // are protected and hence they should not be deleted until everyone
 687   // agrees it is safe to do so.
 688 
 689   verify_hazard_pointer_scanned(self, threads);
 690 
 691   return threads;
 692 }
 693 
 694 // Acquire a nested stable ThreadsList; this is rare so it uses
 695 // Threads_lock.
 696 //
 697 ThreadsList *ThreadsSMRSupport::acquire_stable_list_nested_path(Thread *self) {
 698   assert(self != NULL, "sanity check");
 699   assert(self->get_threads_hazard_ptr() != NULL,
 700          "cannot have a NULL regular hazard ptr when acquiring a nested hazard ptr");
 701 
 702   // The thread already has a hazard ptr (ThreadsList ref) so we need
 703   // to create a nested ThreadsListHandle with the current ThreadsList
 704   // since it might be different than our current hazard ptr. The need
 705   // for a nested ThreadsListHandle is rare so we do this while holding
 706   // the Threads_lock so we don't race with the scanning code; the code
 707   // is so much simpler this way.
 708 
 709   NestedThreadsList* node;
 710   {
 711     // Only grab the Threads_lock if we don't already own it.
 712     MutexLockerEx ml(Threads_lock->owned_by_self() ? NULL : Threads_lock);
 713     node = new NestedThreadsList(get_java_thread_list());
 714     // We insert at the front of the list to match up with the delete
 715     // in release_stable_list().
 716     node->set_next(self->get_nested_threads_hazard_ptr());
 717     self->set_nested_threads_hazard_ptr(node);
 718     if (EnableThreadSMRStatistics) {
 719       self->inc_nested_threads_hazard_ptr_cnt();
 720       if (self->nested_threads_hazard_ptr_cnt() > _nested_thread_list_max) {
 721         _nested_thread_list_max = self->nested_threads_hazard_ptr_cnt();
 722       }
 723     }
 724   }
 725   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()));
 726 
 727   verify_hazard_pointer_scanned(self, node->t_list());
 728 
 729   return node->t_list();
 730 }
 731 
 732 void ThreadsSMRSupport::add_thread(JavaThread *thread){
 733   ThreadsList *new_list = ThreadsList::add_thread(ThreadsSMRSupport::get_java_thread_list(), thread);
 734   if (EnableThreadSMRStatistics) {
 735     ThreadsSMRSupport::inc_java_thread_list_alloc_cnt();
 736     ThreadsSMRSupport::update_java_thread_list_max(new_list->length());
 737   }
 738   // Initial _java_thread_list will not generate a "Threads::add" mesg.
 739   log_debug(thread, smr)("tid=" UINTX_FORMAT ": Threads::add: new ThreadsList=" INTPTR_FORMAT, os::current_thread_id(), p2i(new_list));
 740 
 741   ThreadsList *old_list = ThreadsSMRSupport::xchg_java_thread_list(new_list);
 742   ThreadsSMRSupport::free_list(old_list);
 743 }
 744 
 745 // set_delete_notify() and clear_delete_notify() are called
 746 // under the protection of the delete_lock, but we also use an
 747 // Atomic operation to ensure the memory update is seen earlier than
 748 // when the delete_lock is dropped.


 769   if (EnableThreadSMRStatistics) {
 770     _to_delete_list_cnt++;
 771     if (_to_delete_list_cnt > _to_delete_list_max) {
 772       _to_delete_list_max = _to_delete_list_cnt;
 773     }
 774   }
 775 
 776   // Hash table size should be first power of two higher than twice the length of the ThreadsList
 777   int hash_table_size = MIN2((int)get_java_thread_list()->length(), 32) << 1;
 778   hash_table_size--;
 779   hash_table_size |= hash_table_size >> 1;
 780   hash_table_size |= hash_table_size >> 2;
 781   hash_table_size |= hash_table_size >> 4;
 782   hash_table_size |= hash_table_size >> 8;
 783   hash_table_size |= hash_table_size >> 16;
 784   hash_table_size++;
 785 
 786   // Gather a hash table of the current hazard ptrs:
 787   ThreadScanHashtable *scan_table = new ThreadScanHashtable(hash_table_size);
 788   ScanHazardPtrGatherThreadsListClosure scan_cl(scan_table);
 789   threads_do(&scan_cl);
 790 
 791   // Walk through the linked list of pending freeable ThreadsLists
 792   // and free the ones that are not referenced from hazard ptrs.
 793   ThreadsList* current = _to_delete_list;
 794   ThreadsList* prev = NULL;
 795   ThreadsList* next = NULL;
 796   bool threads_is_freed = false;
 797   while (current != NULL) {
 798     next = current->next_list();
 799     if (!scan_table->has_entry((void*)current)) {
 800       // This ThreadsList is not referenced by a hazard ptr.
 801       if (prev != NULL) {
 802         prev->set_next_list(next);
 803       }
 804       if (_to_delete_list == current) {
 805         _to_delete_list = next;
 806       }
 807 
 808       log_debug(thread, smr)("tid=" UINTX_FORMAT ": ThreadsSMRSupport::free_list: threads=" INTPTR_FORMAT " is freed.", os::current_thread_id(), p2i(current));
 809       if (current == threads) threads_is_freed = true;


 831 // pointer (ThreadsList reference). Otherwise, returns false.
 832 //
 833 bool ThreadsSMRSupport::is_a_protected_JavaThread(JavaThread *thread) {
 834   assert_locked_or_safepoint(Threads_lock);
 835 
 836   // Hash table size should be first power of two higher than twice
 837   // the length of the Threads list.
 838   int hash_table_size = MIN2((int)get_java_thread_list()->length(), 32) << 1;
 839   hash_table_size--;
 840   hash_table_size |= hash_table_size >> 1;
 841   hash_table_size |= hash_table_size >> 2;
 842   hash_table_size |= hash_table_size >> 4;
 843   hash_table_size |= hash_table_size >> 8;
 844   hash_table_size |= hash_table_size >> 16;
 845   hash_table_size++;
 846 
 847   // Gather a hash table of the JavaThreads indirectly referenced by
 848   // hazard ptrs.
 849   ThreadScanHashtable *scan_table = new ThreadScanHashtable(hash_table_size);
 850   ScanHazardPtrGatherProtectedThreadsClosure scan_cl(scan_table);
 851   threads_do(&scan_cl);
 852 
 853   bool thread_is_protected = false;
 854   if (scan_table->has_entry((void*)thread)) {
 855     thread_is_protected = true;
 856   }
 857   delete scan_table;
 858   return thread_is_protected;
 859 }
 860 
 861 // Release a stable ThreadsList.
 862 //
 863 void ThreadsSMRSupport::release_stable_list(Thread *self) {
 864   assert(self != NULL, "sanity check");
 865   // release_stable_list_nested_path() will grab the Threads_lock
 866   // so let's make sure the ThreadsListHandle is in a safe place.
 867   debug_only(if (StrictSafepointChecks) self->check_for_valid_safepoint_state(/* potential_vm_operation */ false);)
 868 
 869   if (self->get_nested_threads_hazard_ptr() == NULL) {
 870     // The typical case is first.
 871     release_stable_list_fast_path(self);


 996       MutexLockerEx ml(Threads_lock, Mutex::_no_safepoint_check_flag);
 997       // Cannot use a MonitorLockerEx helper here because we have
 998       // to drop the Threads_lock first if we wait.
 999       ThreadsSMRSupport::delete_lock()->lock_without_safepoint_check();
1000       // Set the delete_notify flag after we grab delete_lock
1001       // and before we scan hazard ptrs because we're doing
1002       // double-check locking in release_stable_list().
1003       ThreadsSMRSupport::set_delete_notify();
1004 
1005       if (!is_a_protected_JavaThread(thread)) {
1006         // This is the common case.
1007         ThreadsSMRSupport::clear_delete_notify();
1008         ThreadsSMRSupport::delete_lock()->unlock();
1009         break;
1010       }
1011       if (!has_logged_once) {
1012         has_logged_once = true;
1013         log_debug(thread, smr)("tid=" UINTX_FORMAT ": ThreadsSMRSupport::smr_delete: thread=" INTPTR_FORMAT " is not deleted.", os::current_thread_id(), p2i(thread));
1014         if (log_is_enabled(Debug, os, thread)) {
1015           ScanHazardPtrPrintMatchingThreadsClosure scan_cl(thread);
1016           threads_do(&scan_cl);
1017         }
1018       }
1019     } // We have to drop the Threads_lock to wait or delete the thread
1020 
1021     if (EnableThreadSMRStatistics) {
1022       _delete_lock_wait_cnt++;
1023       if (_delete_lock_wait_cnt > _delete_lock_wait_max) {
1024         _delete_lock_wait_max = _delete_lock_wait_cnt;
1025       }
1026     }
1027     // Wait for a release_stable_list() call before we check again. No
1028     // safepoint check, no timeout, and not as suspend equivalent flag
1029     // because this JavaThread is not on the Threads list.
1030     ThreadsSMRSupport::delete_lock()->wait(Mutex::_no_safepoint_check_flag, 0,
1031                                      !Mutex::_as_suspend_equivalent_flag);
1032     if (EnableThreadSMRStatistics) {
1033       _delete_lock_wait_cnt--;
1034     }
1035 
1036     ThreadsSMRSupport::clear_delete_notify();


< prev index next >