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 
thread_inclusion_predicate(Thread * t)30 static bool thread_inclusion_predicate(Thread* t) {
31   assert(t != NULL, "invariant");
32   return !t->jfr_thread_local()->is_dead();
33 }
34 
java_thread_inclusion_predicate(JavaThread * jt,bool live_only)35 static bool java_thread_inclusion_predicate(JavaThread* jt, bool live_only) {
36   assert(jt != NULL, "invariant");
37   if (live_only && jt->thread_state() == _thread_new) {
38     return false;
39   }
40   return thread_inclusion_predicate(jt);
41 }
42 
next_java_thread(JavaThreadIteratorWithHandle & iter,bool live_only)43 static JavaThread* next_java_thread(JavaThreadIteratorWithHandle& iter, bool live_only) {
44   JavaThread* next = iter.next();
45   while (next != NULL && !java_thread_inclusion_predicate(next, live_only)) {
46     next = iter.next();
47   }
48   return next;
49 }
50 
next_non_java_thread(NonJavaThread::Iterator & iter)51 static NonJavaThread* next_non_java_thread(NonJavaThread::Iterator& iter) {
52   while (!iter.end()) {
53     NonJavaThread* next = iter.current();
54     iter.step();
55     assert(next != NULL, "invariant");
56     if (thread_inclusion_predicate(next)) {
57       return next;
58     }
59   }
60   return NULL;
61 }
62 
JfrJavaThreadIteratorAdapter(bool live_only)63 JfrJavaThreadIteratorAdapter::JfrJavaThreadIteratorAdapter(bool live_only /* true */) : _iter(),
64                                                                                         _next(next_java_thread(_iter, live_only)),
65                                                                                         _live_only(live_only) {}
66 
next()67 JavaThread* JfrJavaThreadIteratorAdapter::next() {
68   assert(has_next(), "invariant");
69   Type* const temp = _next;
70   _next = next_java_thread(_iter, _live_only);
71   assert(temp != _next, "invariant");
72   return temp;
73 }
74 
JfrNonJavaThreadIteratorAdapter(bool live_only)75 JfrNonJavaThreadIteratorAdapter::JfrNonJavaThreadIteratorAdapter(bool live_only /* true */) : _iter(), _next(next_non_java_thread(_iter)) {}
76 
has_next() const77 bool JfrNonJavaThreadIteratorAdapter::has_next() const {
78   return _next != NULL;
79 }
80 
next()81 NonJavaThread* JfrNonJavaThreadIteratorAdapter::next() {
82   assert(has_next(), "invariant");
83   Type* const temp = _next;
84   _next = next_non_java_thread(_iter);
85   assert(temp != _next, "invariant");
86   return temp;
87 }
88 
89 // explicit instantiations
90 template class JfrThreadIterator<JfrJavaThreadIteratorAdapter, StackObj>;
91 template class JfrThreadIterator<JfrNonJavaThreadIteratorAdapter, StackObj>;
92