1 /*
2  * Copyright (c) 2012, 2020, 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_THREAD_INLINE_HPP
26 #define SHARE_RUNTIME_THREAD_INLINE_HPP
27 
28 #include "runtime/atomic.hpp"
29 #include "runtime/globals.hpp"
30 #include "runtime/orderAccess.hpp"
31 #include "runtime/os.inline.hpp"
32 #include "runtime/safepoint.hpp"
33 #include "runtime/thread.hpp"
34 
set_suspend_flag(SuspendFlags f)35 inline void Thread::set_suspend_flag(SuspendFlags f) {
36   uint32_t flags;
37   do {
38     flags = _suspend_flags;
39   }
40   while (Atomic::cmpxchg(&_suspend_flags, flags, (flags | f)) != flags);
41 }
clear_suspend_flag(SuspendFlags f)42 inline void Thread::clear_suspend_flag(SuspendFlags f) {
43   uint32_t flags;
44   do {
45     flags = _suspend_flags;
46   }
47   while (Atomic::cmpxchg(&_suspend_flags, flags, (flags & ~f)) != flags);
48 }
49 
set_has_async_exception()50 inline void Thread::set_has_async_exception() {
51   set_suspend_flag(_has_async_exception);
52 }
clear_has_async_exception()53 inline void Thread::clear_has_async_exception() {
54   clear_suspend_flag(_has_async_exception);
55 }
set_trace_flag()56 inline void Thread::set_trace_flag() {
57   set_suspend_flag(_trace_flag);
58 }
clear_trace_flag()59 inline void Thread::clear_trace_flag() {
60   clear_suspend_flag(_trace_flag);
61 }
set_obj_deopt_flag()62 inline void Thread::set_obj_deopt_flag() {
63   set_suspend_flag(_obj_deopt);
64 }
clear_obj_deopt_flag()65 inline void Thread::clear_obj_deopt_flag() {
66   clear_suspend_flag(_obj_deopt);
67 }
68 
cooked_allocated_bytes()69 inline jlong Thread::cooked_allocated_bytes() {
70   jlong allocated_bytes = Atomic::load_acquire(&_allocated_bytes);
71   if (UseTLAB) {
72     size_t used_bytes = tlab().used_bytes();
73     if (used_bytes <= ThreadLocalAllocBuffer::max_size_in_bytes()) {
74       // Comparing used_bytes with the maximum allowed size will ensure
75       // that we don't add the used bytes from a semi-initialized TLAB
76       // ending up with incorrect values. There is still a race between
77       // incrementing _allocated_bytes and clearing the TLAB, that might
78       // cause double counting in rare cases.
79       return allocated_bytes + used_bytes;
80     }
81   }
82   return allocated_bytes;
83 }
84 
cmpxchg_threads_hazard_ptr(ThreadsList * exchange_value,ThreadsList * compare_value)85 inline ThreadsList* Thread::cmpxchg_threads_hazard_ptr(ThreadsList* exchange_value, ThreadsList* compare_value) {
86   return (ThreadsList*)Atomic::cmpxchg(&_threads_hazard_ptr, compare_value, exchange_value);
87 }
88 
get_threads_hazard_ptr()89 inline ThreadsList* Thread::get_threads_hazard_ptr() {
90   return (ThreadsList*)Atomic::load_acquire(&_threads_hazard_ptr);
91 }
92 
set_threads_hazard_ptr(ThreadsList * new_list)93 inline void Thread::set_threads_hazard_ptr(ThreadsList* new_list) {
94   Atomic::release_store_fence(&_threads_hazard_ptr, new_list);
95 }
96 
set_ext_suspended()97 inline void JavaThread::set_ext_suspended() {
98   set_suspend_flag (_ext_suspended);
99 }
clear_ext_suspended()100 inline void JavaThread::clear_ext_suspended() {
101   clear_suspend_flag(_ext_suspended);
102 }
103 
set_external_suspend()104 inline void JavaThread::set_external_suspend() {
105   set_suspend_flag(_external_suspend);
106 }
clear_external_suspend()107 inline void JavaThread::clear_external_suspend() {
108   clear_suspend_flag(_external_suspend);
109 }
110 
set_pending_async_exception(oop e)111 inline void JavaThread::set_pending_async_exception(oop e) {
112   _pending_async_exception = e;
113   _special_runtime_exit_condition = _async_exception;
114   set_has_async_exception();
115 }
116 
thread_state() const117 inline JavaThreadState JavaThread::thread_state() const    {
118 #if defined(PPC64) || defined (AARCH64)
119   // Use membars when accessing volatile _thread_state. See
120   // Threads::create_vm() for size checks.
121   return (JavaThreadState) Atomic::load_acquire((volatile jint*)&_thread_state);
122 #else
123   return _thread_state;
124 #endif
125 }
126 
set_thread_state(JavaThreadState s)127 inline void JavaThread::set_thread_state(JavaThreadState s) {
128   assert(current_or_null() == NULL || current_or_null() == this,
129          "state change should only be called by the current thread");
130 #if defined(PPC64) || defined (AARCH64)
131   // Use membars when accessing volatile _thread_state. See
132   // Threads::create_vm() for size checks.
133   Atomic::release_store((volatile jint*)&_thread_state, (jint)s);
134 #else
135   _thread_state = s;
136 #endif
137 }
138 
set_thread_state_fence(JavaThreadState s)139 inline void JavaThread::set_thread_state_fence(JavaThreadState s) {
140   set_thread_state(s);
141   OrderAccess::fence();
142 }
143 
safepoint_state() const144 ThreadSafepointState* JavaThread::safepoint_state() const  {
145   return _safepoint_state;
146 }
147 
set_safepoint_state(ThreadSafepointState * state)148 void JavaThread::set_safepoint_state(ThreadSafepointState *state) {
149   _safepoint_state = state;
150 }
151 
is_at_poll_safepoint()152 bool JavaThread::is_at_poll_safepoint() {
153   return _safepoint_state->is_at_poll_safepoint();
154 }
155 
enter_critical()156 void JavaThread::enter_critical() {
157   assert(Thread::current() == this ||
158          (Thread::current()->is_VM_thread() &&
159          SafepointSynchronize::is_synchronizing()),
160          "this must be current thread or synchronizing");
161   _jni_active_critical++;
162 }
163 
set_done_attaching_via_jni()164 inline void JavaThread::set_done_attaching_via_jni() {
165   _jni_attach_state = _attached_via_jni;
166   OrderAccess::fence();
167 }
168 
is_exiting() const169 inline bool JavaThread::is_exiting() const {
170   // Use load-acquire so that setting of _terminated by
171   // JavaThread::exit() is seen more quickly.
172   TerminatedTypes l_terminated = (TerminatedTypes)
173       Atomic::load_acquire((volatile jint *) &_terminated);
174   return l_terminated == _thread_exiting || check_is_terminated(l_terminated);
175 }
176 
is_terminated() const177 inline bool JavaThread::is_terminated() const {
178   // Use load-acquire so that setting of _terminated by
179   // JavaThread::exit() is seen more quickly.
180   TerminatedTypes l_terminated = (TerminatedTypes)
181       Atomic::load_acquire((volatile jint *) &_terminated);
182   return check_is_terminated(l_terminated);
183 }
184 
set_terminated(TerminatedTypes t)185 inline void JavaThread::set_terminated(TerminatedTypes t) {
186   // use release-store so the setting of _terminated is seen more quickly
187   Atomic::release_store((volatile jint *) &_terminated, (jint) t);
188 }
189 
190 // special for Threads::remove() which is static:
set_terminated_value()191 inline void JavaThread::set_terminated_value() {
192   // use release-store so the setting of _terminated is seen more quickly
193   Atomic::release_store((volatile jint *) &_terminated, (jint) _thread_terminated);
194 }
195 
196 // Allow tracking of class initialization monitor use
set_class_to_be_initialized(InstanceKlass * k)197 inline void JavaThread::set_class_to_be_initialized(InstanceKlass* k) {
198   assert((k == NULL && _class_to_be_initialized != NULL) ||
199          (k != NULL && _class_to_be_initialized == NULL), "incorrect usage");
200   assert(this == Thread::current(), "Only the current thread can set this field");
201   _class_to_be_initialized = k;
202 }
203 
class_to_be_initialized() const204 inline InstanceKlass* JavaThread::class_to_be_initialized() const {
205   return _class_to_be_initialized;
206 }
207 
208 #endif // SHARE_RUNTIME_THREAD_INLINE_HPP
209