1 /*
  2  * Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2021, Azul Systems, Inc. All rights reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 #ifndef SHARE_RUNTIME_THREAD_INLINE_HPP
 27 #define SHARE_RUNTIME_THREAD_INLINE_HPP
 28 
 29 #include "runtime/thread.hpp"
 30 
 31 #include "gc/shared/tlab_globals.hpp"
 32 #include "runtime/atomic.hpp"
 33 #include "runtime/nonJavaThread.hpp"
 34 #include "runtime/orderAccess.hpp"
 35 #include "runtime/safepoint.hpp"
 36 
 37 #if defined(__APPLE__) && defined(AARCH64)
 38 #include "runtime/os.hpp"
 39 #endif
 40 
 41 inline jlong Thread::cooked_allocated_bytes() {
 42   jlong allocated_bytes = Atomic::load_acquire(&_allocated_bytes);
 43   if (UseTLAB) {
 44     // These reads are unsynchronized and unordered with the thread updating its tlab pointers.
 45     // Use only if top > start && used_bytes <= max_tlab_size_bytes.
 46     const HeapWord* const top = tlab().top_relaxed();
 47     const HeapWord* const start = tlab().start_relaxed();
 48     if (top <= start) {
 49       return allocated_bytes;
 50     }
 51     const size_t used_bytes = pointer_delta(top, start, 1);
 52     if (used_bytes <= ThreadLocalAllocBuffer::max_size_in_bytes()) {
 53       // Comparing used_bytes with the maximum allowed size will ensure
 54       // that we don't add the used bytes from a semi-initialized TLAB
 55       // ending up with incorrect values. There is still a race between
 56       // incrementing _allocated_bytes and clearing the TLAB, that might
 57       // cause double counting in rare cases.
 58       return allocated_bytes + used_bytes;
 59     }
 60   }
 61   return allocated_bytes;
 62 }
 63 
 64 inline ThreadsList* Thread::cmpxchg_threads_hazard_ptr(ThreadsList* exchange_value, ThreadsList* compare_value) {
 65   return (ThreadsList*)Atomic::cmpxchg(&_threads_hazard_ptr, compare_value, exchange_value);
 66 }
 67 
 68 inline ThreadsList* Thread::get_threads_hazard_ptr() const {
 69   return (ThreadsList*)Atomic::load_acquire(&_threads_hazard_ptr);
 70 }
 71 
 72 inline void Thread::set_threads_hazard_ptr(ThreadsList* new_list) {
 73   Atomic::release_store_fence(&_threads_hazard_ptr, new_list);
 74 }
 75 
 76 #if defined(__APPLE__) && defined(AARCH64)
 77 inline void Thread::init_wx() {
 78   assert(this == Thread::current(), "should only be called for current thread");
 79   assert(!_wx_init, "second init");
 80   _wx_state = WXWrite;
 81   os::current_thread_enable_wx(_wx_state);
 82   DEBUG_ONLY(_wx_init = true);
 83 }
 84 
 85 inline WXMode Thread::enable_wx(WXMode new_state) {
 86   assert(this == Thread::current(), "should only be called for current thread");
 87   assert(_wx_init, "should be inited");
 88   WXMode old = _wx_state;
 89   if (_wx_state != new_state) {
 90     _wx_state = new_state;
 91     os::current_thread_enable_wx(new_state);
 92   }
 93   return old;
 94 }
 95 #endif // __APPLE__ && AARCH64
 96 
 97 inline void JavaThread::set_suspend_flag(SuspendFlags f) {
 98   uint32_t flags;
 99   do {
100     flags = _suspend_flags;
101   }
102   while (Atomic::cmpxchg(&_suspend_flags, flags, (flags | f)) != flags);
103 }
104 inline void JavaThread::clear_suspend_flag(SuspendFlags f) {
105   uint32_t flags;
106   do {
107     flags = _suspend_flags;
108   }
109   while (Atomic::cmpxchg(&_suspend_flags, flags, (flags & ~f)) != flags);
110 }
111 
112 inline void JavaThread::set_trace_flag() {
113   set_suspend_flag(_trace_flag);
114 }
115 inline void JavaThread::clear_trace_flag() {
116   clear_suspend_flag(_trace_flag);
117 }
118 inline void JavaThread::set_obj_deopt_flag() {
119   set_suspend_flag(_obj_deopt);
120 }
121 inline void JavaThread::clear_obj_deopt_flag() {
122   clear_suspend_flag(_obj_deopt);
123 }
124 
125 inline void JavaThread::set_thread_suspended() {
126   set_suspend_flag(_thread_suspended);
127 }
128 inline void JavaThread::clear_thread_suspended() {
129   clear_suspend_flag(_thread_suspended);
130 }
131 
132 inline bool JavaThread::clear_async_exception_condition() {
133   bool ret = has_async_exception_condition();
134   clear_suspend_flag(_has_async_exception);
135   return ret;
136 }
137 
138 inline void JavaThread::set_pending_async_exception(oop e) {
139   _pending_async_exception = e;
140   set_suspend_flag(_has_async_exception);
141 }
142 
143 inline void JavaThread::set_pending_unsafe_access_error() {
144   set_suspend_flag(_has_async_exception);
145   DEBUG_ONLY(_is_unsafe_access_error = true);
146 }
147 
148 inline JavaThreadState JavaThread::thread_state() const    {
149 #if defined(PPC64) || defined (AARCH64)
150   // Use membars when accessing volatile _thread_state. See
151   // Threads::create_vm() for size checks.
152   return (JavaThreadState) Atomic::load_acquire((volatile jint*)&_thread_state);
153 #else
154   return _thread_state;
155 #endif
156 }
157 
158 inline void JavaThread::set_thread_state(JavaThreadState s) {
159   assert(current_or_null() == NULL || current_or_null() == this,
160          "state change should only be called by the current thread");
161 #if defined(PPC64) || defined (AARCH64)
162   // Use membars when accessing volatile _thread_state. See
163   // Threads::create_vm() for size checks.
164   Atomic::release_store((volatile jint*)&_thread_state, (jint)s);
165 #else
166   _thread_state = s;
167 #endif
168 }
169 
170 inline void JavaThread::set_thread_state_fence(JavaThreadState s) {
171   set_thread_state(s);
172   OrderAccess::fence();
173 }
174 
175 ThreadSafepointState* JavaThread::safepoint_state() const  {
176   return _safepoint_state;
177 }
178 
179 void JavaThread::set_safepoint_state(ThreadSafepointState *state) {
180   _safepoint_state = state;
181 }
182 
183 bool JavaThread::is_at_poll_safepoint() {
184   return _safepoint_state->is_at_poll_safepoint();
185 }
186 
187 void JavaThread::enter_critical() {
188   assert(Thread::current() == this ||
189          (Thread::current()->is_VM_thread() &&
190          SafepointSynchronize::is_synchronizing()),
191          "this must be current thread or synchronizing");
192   _jni_active_critical++;
193 }
194 
195 inline void JavaThread::set_done_attaching_via_jni() {
196   _jni_attach_state = _attached_via_jni;
197   OrderAccess::fence();
198 }
199 
200 inline bool JavaThread::is_exiting() const {
201   // Use load-acquire so that setting of _terminated by
202   // JavaThread::exit() is seen more quickly.
203   TerminatedTypes l_terminated = Atomic::load_acquire(&_terminated);
204   return l_terminated == _thread_exiting || check_is_terminated(l_terminated);
205 }
206 
207 inline bool JavaThread::is_terminated() const {
208   // Use load-acquire so that setting of _terminated by
209   // JavaThread::exit() is seen more quickly.
210   TerminatedTypes l_terminated = Atomic::load_acquire(&_terminated);
211   return check_is_terminated(l_terminated);
212 }
213 
214 inline void JavaThread::set_terminated(TerminatedTypes t) {
215   // use release-store so the setting of _terminated is seen more quickly
216   Atomic::release_store(&_terminated, t);
217 }
218 
219 // Allow tracking of class initialization monitor use
220 inline void JavaThread::set_class_to_be_initialized(InstanceKlass* k) {
221   assert((k == NULL && _class_to_be_initialized != NULL) ||
222          (k != NULL && _class_to_be_initialized == NULL), "incorrect usage");
223   assert(this == Thread::current(), "Only the current thread can set this field");
224   _class_to_be_initialized = k;
225 }
226 
227 inline InstanceKlass* JavaThread::class_to_be_initialized() const {
228   return _class_to_be_initialized;
229 }
230 
231 #endif // SHARE_RUNTIME_THREAD_INLINE_HPP
--- EOF ---