< prev index next >

src/hotspot/share/runtime/threadSMR.cpp

Print this page




   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/vmOperations.hpp"
  32 #include "services/threadService.hpp"

  33 #include "utilities/copy.hpp"
  34 #include "utilities/globalDefinitions.hpp"
  35 #include "utilities/ostream.hpp"
  36 #include "utilities/resourceHash.hpp"
  37 #include "utilities/vmError.hpp"
  38 
  39 // The '_cnt', '_max' and '_times" fields are enabled via
  40 // -XX:+EnableThreadSMRStatistics:
  41 
  42 // # of parallel threads in _delete_lock->wait().
  43 // Impl note: Hard to imagine > 64K waiting threads so this could be 16-bit,
  44 // but there is no nice 16-bit _FORMAT support.
  45 uint                  ThreadsSMRSupport::_delete_lock_wait_cnt = 0;
  46 
  47 // Max # of parallel threads in _delete_lock->wait().
  48 // Impl note: See _delete_lock_wait_cnt note.
  49 uint                  ThreadsSMRSupport::_delete_lock_wait_max = 0;
  50 
  51 // Flag to indicate when an _delete_lock->notify() is needed.
  52 // Impl note: See _delete_lock_wait_cnt note.


 590 }
 591 
 592 void ThreadsList::dec_nested_handle_cnt() {
 593   // The decrement only needs to be MO_ACQ_REL since the reference
 594   // counter is volatile (and the hazard ptr is already NULL).
 595   Atomic::dec(&_nested_handle_cnt);
 596 }
 597 
 598 int ThreadsList::find_index_of_JavaThread(JavaThread *target) {
 599   if (target == NULL) {
 600     return -1;
 601   }
 602   for (uint i = 0; i < length(); i++) {
 603     if (target == thread_at(i)) {
 604       return (int)i;
 605     }
 606   }
 607   return -1;
 608 }
 609 


 610 JavaThread* ThreadsList::find_JavaThread_from_java_tid(jlong java_tid) const {





 611   for (uint i = 0; i < length(); i++) {
 612     JavaThread* thread = thread_at(i);
 613     oop tobj = thread->threadObj();
 614     // Ignore the thread if it hasn't run yet, has exited
 615     // or is starting to exit.
 616     if (tobj != NULL && !thread->is_exiting() &&
 617         java_tid == java_lang_Thread::thread_id(tobj)) {
 618       // found a match
 619       return thread;
 620     }
 621   }



 622   return NULL;
 623 }







 624 
 625 void ThreadsList::inc_nested_handle_cnt() {
 626   // The increment needs to be MO_SEQ_CST so that the reference counter
 627   // update is seen before the subsequent hazard ptr update.
 628   Atomic::inc(&_nested_handle_cnt);
 629 }
 630 
 631 bool ThreadsList::includes(const JavaThread * const p) const {
 632   if (p == NULL) {
 633     return false;
 634   }
 635   for (uint i = 0; i < length(); i++) {
 636     if (thread_at(i) == p) {
 637       return true;
 638     }
 639   }
 640   return false;
 641 }
 642 
 643 // Remove a JavaThread from a ThreadsList. The returned ThreadsList is a


 725     }
 726   }
 727 
 728   // Return a live JavaThread that is "protected" by the
 729   // ThreadsListHandle in the caller.
 730   *jt_pp = java_thread;
 731   return true;
 732 }
 733 
 734 void ThreadsSMRSupport::add_thread(JavaThread *thread){
 735   ThreadsList *new_list = ThreadsList::add_thread(get_java_thread_list(), thread);
 736   if (EnableThreadSMRStatistics) {
 737     inc_java_thread_list_alloc_cnt();
 738     update_java_thread_list_max(new_list->length());
 739   }
 740   // Initial _java_thread_list will not generate a "Threads::add" mesg.
 741   log_debug(thread, smr)("tid=" UINTX_FORMAT ": Threads::add: new ThreadsList=" INTPTR_FORMAT, os::current_thread_id(), p2i(new_list));
 742 
 743   ThreadsList *old_list = xchg_java_thread_list(new_list);
 744   free_list(old_list);


 745 }
 746 
 747 // set_delete_notify() and clear_delete_notify() are called
 748 // under the protection of the delete_lock, but we also use an
 749 // Atomic operation to ensure the memory update is seen earlier than
 750 // when the delete_lock is dropped.
 751 //
 752 void ThreadsSMRSupport::clear_delete_notify() {
 753   Atomic::dec(&_delete_notify);
 754 }
 755 
 756 bool ThreadsSMRSupport::delete_notify() {
 757   // Use load_acquire() in order to see any updates to _delete_notify
 758   // earlier than when delete_lock is grabbed.
 759   return (OrderAccess::load_acquire(&_delete_notify) != 0);
 760 }
 761 
 762 // Safely free a ThreadsList after a Threads::add() or Threads::remove().
 763 // The specified ThreadsList may not get deleted during this call if it
 764 // is still in-use (referenced by a hazard ptr). Other ThreadsLists


 892   const char* log_str = is_nested ? "nested hazard ptr" : "regular hazard ptr";
 893 
 894   // Note: delete_lock is held in smr_delete() for the entire
 895   // hazard ptr search so that we do not lose this notify() if
 896   // the exiting thread has to wait. That code path also holds
 897   // Threads_lock (which was grabbed before delete_lock) so that
 898   // threads_do() can be called. This means the system can't start a
 899   // safepoint which means this thread can't take too long to get to
 900   // a safepoint because of being blocked on delete_lock.
 901   //
 902   MonitorLocker ml(ThreadsSMRSupport::delete_lock(), Monitor::_no_safepoint_check_flag);
 903   if (ThreadsSMRSupport::delete_notify()) {
 904     // Notify any exiting JavaThreads that are waiting in smr_delete()
 905     // that we've released a ThreadsList.
 906     ml.notify_all();
 907     log_debug(thread, smr)("tid=" UINTX_FORMAT ": ThreadsSMRSupport::release_stable_list notified %s", os::current_thread_id(), log_str);
 908   }
 909 }
 910 
 911 void ThreadsSMRSupport::remove_thread(JavaThread *thread) {


 912   ThreadsList *new_list = ThreadsList::remove_thread(ThreadsSMRSupport::get_java_thread_list(), thread);
 913   if (EnableThreadSMRStatistics) {
 914     ThreadsSMRSupport::inc_java_thread_list_alloc_cnt();
 915     // This list is smaller so no need to check for a "longest" update.
 916   }
 917 
 918   // Final _java_thread_list will not generate a "Threads::remove" mesg.
 919   log_debug(thread, smr)("tid=" UINTX_FORMAT ": Threads::remove: new ThreadsList=" INTPTR_FORMAT, os::current_thread_id(), p2i(new_list));
 920 
 921   ThreadsList *old_list = ThreadsSMRSupport::xchg_java_thread_list(new_list);
 922   ThreadsSMRSupport::free_list(old_list);
 923 }
 924 
 925 // See note for clear_delete_notify().
 926 //
 927 void ThreadsSMRSupport::set_delete_notify() {
 928   Atomic::inc(&_delete_notify);
 929 }
 930 
 931 // Safely delete a JavaThread when it is no longer in use by a




   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/threadService.hpp"
  34 #include "services/threadTable.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.


 592 }
 593 
 594 void ThreadsList::dec_nested_handle_cnt() {
 595   // The decrement only needs to be MO_ACQ_REL since the reference
 596   // counter is volatile (and the hazard ptr is already NULL).
 597   Atomic::dec(&_nested_handle_cnt);
 598 }
 599 
 600 int ThreadsList::find_index_of_JavaThread(JavaThread *target) {
 601   if (target == NULL) {
 602     return -1;
 603   }
 604   for (uint i = 0; i < length(); i++) {
 605     if (target == thread_at(i)) {
 606       return (int)i;
 607     }
 608   }
 609   return -1;
 610 }
 611 
 612 #define PMIMORDIAL_JAVA_TID 1
 613 
 614 JavaThread* ThreadsList::find_JavaThread_from_java_tid(jlong java_tid) const {
 615     JavaThread* java_thread = ThreadTable::find_thread(java_tid);
 616     if (java_thread == NULL && java_tid == PMIMORDIAL_JAVA_TID) {
 617         // ThreadsSMRSupport::add_thread() is not called for the primordial
 618         // thread. Thus, we find this thread with a linear search and add it
 619         // to the thread table.
 620         for (uint i = 0; i < length(); i++) {
 621             JavaThread* thread = thread_at(i);
 622             if (is_valid_java_thread(java_tid,thread)) {
 623                 ThreadTable::add_thread(java_tid, thread);




 624                 return thread;
 625             }
 626         }
 627     } else if (java_thread != NULL && is_valid_java_thread(java_tid, java_thread)) {
 628         return java_thread;
 629     }
 630     return NULL;
 631 }
 632 bool ThreadsList::is_valid_java_thread(jlong java_tid, JavaThread* java_thread) {
 633     oop tobj = java_thread->threadObj();
 634     // Ignore the thread if it hasn't run yet, has exited
 635     // or is starting to exit.
 636     return (tobj != NULL && !java_thread->is_exiting() &&
 637             java_tid == java_lang_Thread::thread_id(tobj));
 638 }
 639     
 640 void ThreadsList::inc_nested_handle_cnt() {
 641   // The increment needs to be MO_SEQ_CST so that the reference counter
 642   // update is seen before the subsequent hazard ptr update.
 643   Atomic::inc(&_nested_handle_cnt);
 644 }
 645 
 646 bool ThreadsList::includes(const JavaThread * const p) const {
 647   if (p == NULL) {
 648     return false;
 649   }
 650   for (uint i = 0; i < length(); i++) {
 651     if (thread_at(i) == p) {
 652       return true;
 653     }
 654   }
 655   return false;
 656 }
 657 
 658 // Remove a JavaThread from a ThreadsList. The returned ThreadsList is a


 740     }
 741   }
 742 
 743   // Return a live JavaThread that is "protected" by the
 744   // ThreadsListHandle in the caller.
 745   *jt_pp = java_thread;
 746   return true;
 747 }
 748 
 749 void ThreadsSMRSupport::add_thread(JavaThread *thread){
 750   ThreadsList *new_list = ThreadsList::add_thread(get_java_thread_list(), thread);
 751   if (EnableThreadSMRStatistics) {
 752     inc_java_thread_list_alloc_cnt();
 753     update_java_thread_list_max(new_list->length());
 754   }
 755   // Initial _java_thread_list will not generate a "Threads::add" mesg.
 756   log_debug(thread, smr)("tid=" UINTX_FORMAT ": Threads::add: new ThreadsList=" INTPTR_FORMAT, os::current_thread_id(), p2i(new_list));
 757 
 758   ThreadsList *old_list = xchg_java_thread_list(new_list);
 759   free_list(old_list);
 760   jlong tid = SharedRuntime::get_java_tid(thread);
 761   ThreadTable::add_thread(tid, thread);
 762 }
 763 
 764 // set_delete_notify() and clear_delete_notify() are called
 765 // under the protection of the delete_lock, but we also use an
 766 // Atomic operation to ensure the memory update is seen earlier than
 767 // when the delete_lock is dropped.
 768 //
 769 void ThreadsSMRSupport::clear_delete_notify() {
 770   Atomic::dec(&_delete_notify);
 771 }
 772 
 773 bool ThreadsSMRSupport::delete_notify() {
 774   // Use load_acquire() in order to see any updates to _delete_notify
 775   // earlier than when delete_lock is grabbed.
 776   return (OrderAccess::load_acquire(&_delete_notify) != 0);
 777 }
 778 
 779 // Safely free a ThreadsList after a Threads::add() or Threads::remove().
 780 // The specified ThreadsList may not get deleted during this call if it
 781 // is still in-use (referenced by a hazard ptr). Other ThreadsLists


 909   const char* log_str = is_nested ? "nested hazard ptr" : "regular hazard ptr";
 910 
 911   // Note: delete_lock is held in smr_delete() for the entire
 912   // hazard ptr search so that we do not lose this notify() if
 913   // the exiting thread has to wait. That code path also holds
 914   // Threads_lock (which was grabbed before delete_lock) so that
 915   // threads_do() can be called. This means the system can't start a
 916   // safepoint which means this thread can't take too long to get to
 917   // a safepoint because of being blocked on delete_lock.
 918   //
 919   MonitorLocker ml(ThreadsSMRSupport::delete_lock(), Monitor::_no_safepoint_check_flag);
 920   if (ThreadsSMRSupport::delete_notify()) {
 921     // Notify any exiting JavaThreads that are waiting in smr_delete()
 922     // that we've released a ThreadsList.
 923     ml.notify_all();
 924     log_debug(thread, smr)("tid=" UINTX_FORMAT ": ThreadsSMRSupport::release_stable_list notified %s", os::current_thread_id(), log_str);
 925   }
 926 }
 927 
 928 void ThreadsSMRSupport::remove_thread(JavaThread *thread) {
 929   jlong tid = SharedRuntime::get_java_tid(thread);
 930   ThreadTable::remove_thread(tid);
 931   ThreadsList *new_list = ThreadsList::remove_thread(ThreadsSMRSupport::get_java_thread_list(), thread);
 932   if (EnableThreadSMRStatistics) {
 933     ThreadsSMRSupport::inc_java_thread_list_alloc_cnt();
 934     // This list is smaller so no need to check for a "longest" update.
 935   }
 936 
 937   // Final _java_thread_list will not generate a "Threads::remove" mesg.
 938   log_debug(thread, smr)("tid=" UINTX_FORMAT ": Threads::remove: new ThreadsList=" INTPTR_FORMAT, os::current_thread_id(), p2i(new_list));
 939 
 940   ThreadsList *old_list = ThreadsSMRSupport::xchg_java_thread_list(new_list);
 941   ThreadsSMRSupport::free_list(old_list);
 942 }
 943 
 944 // See note for clear_delete_notify().
 945 //
 946 void ThreadsSMRSupport::set_delete_notify() {
 947   Atomic::inc(&_delete_notify);
 948 }
 949 
 950 // Safely delete a JavaThread when it is no longer in use by a


< prev index next >