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 #ifndef SHARE_RUNTIME_THREADSMR_INLINE_HPP
  26 #define SHARE_RUNTIME_THREADSMR_INLINE_HPP
  27 
  28 #include "runtime/atomic.hpp"
  29 #include "memory/iterator.hpp"
  30 #include "runtime/prefetch.inline.hpp"
  31 #include "runtime/thread.inline.hpp"
  32 #include "runtime/threadSMR.hpp"
  33 
  34 // Devirtualize known thread closure types.
  35 template <class T>
  36 inline void ThreadsList::threads_do_dispatch(T *cl, JavaThread *const thread) const {
  37   cl->T::do_thread(thread);
  38 }
  39 
  40 template <>
  41 inline void ThreadsList::threads_do_dispatch<ThreadClosure>(ThreadClosure *cl, JavaThread *const thread) const {
  42   cl->do_thread(thread);
  43 }
  44 
  45 template <class T>
  46 inline void ThreadsList::threads_do(T *cl) const {
  47   const intx scan_interval = PrefetchScanIntervalInBytes;
  48   JavaThread *const *const end = _threads + _length;
  49   for (JavaThread *const *current_p = _threads; current_p != end; current_p++) {
  50     Prefetch::read((void*)current_p, scan_interval);
  51     JavaThread *const current = *current_p;
  52     threads_do_dispatch(cl, current);
  53   }
  54 }
  55 
  56 // These three inlines are private to ThreadsSMRSupport, but
  57 // they are called by public inline update_tlh_stats() below:
  58 
  59 inline void ThreadsSMRSupport::add_tlh_times(uint add_value) {
  60   Atomic::add(&_tlh_times, add_value);
  61 }
  62 
  63 inline void ThreadsSMRSupport::inc_tlh_cnt() {
  64   Atomic::inc(&_tlh_cnt);
  65 }
  66 
  67 inline void ThreadsSMRSupport::update_tlh_time_max(uint new_value) {
  68   while (true) {
  69     uint cur_value = _tlh_time_max;
  70     if (new_value <= cur_value) {
  71       // No need to update max value so we're done.
  72       break;
  73     }
  74     if (Atomic::cmpxchg(&_tlh_time_max, cur_value, new_value) == cur_value) {
  75       // Updated max value so we're done. Otherwise try it all again.
  76       break;
  77     }
  78   }
  79 }
  80 
  81 inline ThreadsList* ThreadsSMRSupport::get_java_thread_list() {
  82   return (ThreadsList*)Atomic::load_acquire(&_java_thread_list);
  83 }
  84 
  85 inline bool ThreadsSMRSupport::is_a_protected_JavaThread_with_lock(JavaThread *thread, bool skiplock) {
  86   MutexLocker ml(Threads_lock->owned_by_self() || skiplock ? NULL : Threads_lock);
  87   return is_a_protected_JavaThread(thread);
  88 }
  89 
  90 inline void ThreadsSMRSupport::update_tlh_stats(uint millis) {
  91   ThreadsSMRSupport::inc_tlh_cnt();
  92   ThreadsSMRSupport::add_tlh_times(millis);
  93   ThreadsSMRSupport::update_tlh_time_max(millis);
  94 }
  95 
  96 #endif // SHARE_RUNTIME_THREADSMR_INLINE_HPP