src/hotspot/share/jfr/utilities/jfrThreadIterator.cpp
branchJEP-349-branch
changeset 57361 53dccc90a5be
child 57878 bffba8d6611a
equal deleted inserted replaced
57360:5d043a159d5c 57361:53dccc90a5be
       
     1 /*
       
     2  * Copyright (c) 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 "jfr/support/jfrThreadLocal.hpp"
       
    27 #include "jfr/utilities/jfrThreadIterator.hpp"
       
    28 #include "runtime/thread.inline.hpp"
       
    29 
       
    30 static bool thread_inclusion_predicate(Thread* t) {
       
    31   assert(t != NULL, "invariant");
       
    32   return !(t->jfr_thread_local()->is_excluded() || t->jfr_thread_local()->is_dead());
       
    33 }
       
    34 
       
    35 static bool java_thread_inclusion_predicate(JavaThread* jt) {
       
    36   assert(jt != NULL, "invariant");
       
    37   return thread_inclusion_predicate(jt) && jt->thread_state() != _thread_new;
       
    38 }
       
    39 
       
    40 static JavaThread* next_java_thread(JavaThreadIteratorWithHandle& iter) {
       
    41   JavaThread* next = iter.next();
       
    42   while (next != NULL && !java_thread_inclusion_predicate(next)) {
       
    43     next = iter.next();
       
    44   }
       
    45   return next;
       
    46 }
       
    47 
       
    48 static NonJavaThread* next_non_java_thread(NonJavaThread::Iterator& iter) {
       
    49   NonJavaThread* next = NULL;
       
    50   while (!iter.end()) {
       
    51     next = iter.current();
       
    52     iter.step();
       
    53     assert(next != NULL, "invariant");
       
    54     if (!thread_inclusion_predicate(next)) {
       
    55       continue;
       
    56     }
       
    57   }
       
    58   return next;
       
    59 }
       
    60 
       
    61 JfrJavaThreadIteratorAdapter::JfrJavaThreadIteratorAdapter() : _iter(), _next(next_java_thread(_iter)) {}
       
    62 
       
    63 JavaThread* JfrJavaThreadIteratorAdapter::next() {
       
    64   assert(has_next(), "invariant");
       
    65   Type* const temp = _next;
       
    66   assert(java_thread_inclusion_predicate(temp), "invariant");
       
    67   _next = next_java_thread(_iter);
       
    68   assert(temp != _next, "invariant");
       
    69   return temp;
       
    70 }
       
    71 
       
    72 JfrNonJavaThreadIteratorAdapter::JfrNonJavaThreadIteratorAdapter() : _iter(), _next(next_non_java_thread(_iter)) {}
       
    73 
       
    74 bool JfrNonJavaThreadIteratorAdapter::has_next() const {
       
    75   return _next != NULL;
       
    76 }
       
    77 
       
    78 NonJavaThread* JfrNonJavaThreadIteratorAdapter::next() {
       
    79   assert(has_next(), "invariant");
       
    80   Type* const temp = _next;
       
    81   assert(thread_inclusion_predicate(temp), "invariant");
       
    82   _next = next_non_java_thread(_iter);
       
    83   assert(temp != _next, "invariant");
       
    84   return temp;
       
    85 }
       
    86 
       
    87 // explicit instantiations
       
    88 template class JfrThreadIterator<JfrJavaThreadIteratorAdapter, StackObj>;
       
    89 template class JfrThreadIterator<JfrNonJavaThreadIteratorAdapter, StackObj>;