1 /*
2  * Copyright (c) 2006, 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_PRIMS_JVMTITHREADSTATE_INLINE_HPP
26 #define SHARE_PRIMS_JVMTITHREADSTATE_INLINE_HPP
27 
28 #include "prims/jvmtiEnvThreadState.hpp"
29 
30 #include "prims/jvmtiThreadState.hpp"
31 #include "runtime/thread.inline.hpp"
32 
33 // JvmtiEnvThreadStateIterator implementation
34 
JvmtiEnvThreadStateIterator(JvmtiThreadState * thread_state)35 inline JvmtiEnvThreadStateIterator::JvmtiEnvThreadStateIterator(JvmtiThreadState* thread_state) {
36   state = thread_state;
37   Thread::current()->entering_jvmti_env_iteration();
38 }
39 
~JvmtiEnvThreadStateIterator()40 inline JvmtiEnvThreadStateIterator::~JvmtiEnvThreadStateIterator() {
41   Thread::current()->leaving_jvmti_env_iteration();
42 }
43 
first()44 inline JvmtiEnvThreadState* JvmtiEnvThreadStateIterator::first() {
45   return state->head_env_thread_state();
46 }
47 
next(JvmtiEnvThreadState * ets)48 inline JvmtiEnvThreadState* JvmtiEnvThreadStateIterator::next(JvmtiEnvThreadState* ets) {
49   return ets->next();
50 }
51 
52 // JvmtiThreadState implementation
53 
env_thread_state(JvmtiEnvBase * env)54 JvmtiEnvThreadState* JvmtiThreadState::env_thread_state(JvmtiEnvBase *env) {
55   JvmtiEnvThreadStateIterator it(this);
56   for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
57     if ((JvmtiEnvBase*)(ets->get_env()) == env) {
58       return ets;
59     }
60   }
61   return NULL;
62 }
63 
head_env_thread_state()64 JvmtiEnvThreadState* JvmtiThreadState::head_env_thread_state() {
65   return _head_env_thread_state;
66 }
67 
set_head_env_thread_state(JvmtiEnvThreadState * ets)68 void JvmtiThreadState::set_head_env_thread_state(JvmtiEnvThreadState* ets) {
69   _head_env_thread_state = ets;
70 }
71 
state_for_while_locked(JavaThread * thread)72 inline JvmtiThreadState* JvmtiThreadState::state_for_while_locked(JavaThread *thread) {
73   assert(JvmtiThreadState_lock->is_locked(), "sanity check");
74 
75   JvmtiThreadState *state = thread->jvmti_thread_state();
76   if (state == NULL) {
77     if (thread->is_exiting()) {
78       // don't add a JvmtiThreadState to a thread that is exiting
79       return NULL;
80     }
81 
82     state = new JvmtiThreadState(thread);
83   }
84   return state;
85 }
86 
state_for(JavaThread * thread)87 inline JvmtiThreadState* JvmtiThreadState::state_for(JavaThread *thread) {
88   JvmtiThreadState *state = thread->jvmti_thread_state();
89   if (state == NULL) {
90     MutexLocker mu(JvmtiThreadState_lock);
91     // check again with the lock held
92     state = state_for_while_locked(thread);
93   } else {
94     // Check possible safepoint even if state is non-null.
95     // (Note: the thread argument isn't the current thread)
96     DEBUG_ONLY(JavaThread::current()->check_possible_safepoint());
97   }
98   return state;
99 }
100 
101 #endif // SHARE_PRIMS_JVMTITHREADSTATE_INLINE_HPP
102