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_pending_async_exception(oop e) {
126   _pending_async_exception = e;
127   set_async_exception_condition(_async_exception);
128   // Set _suspend_flags too so we save a comparison in the transition from native to Java
129   // in the native wrappers. It will be cleared in check_and_handle_async_exceptions()
130   // when we actually install the exception.
131   set_suspend_flag(_has_async_exception);
132 }
133 
134 inline JavaThreadState JavaThread::thread_state() const    {
135 #if defined(PPC64) || defined (AARCH64)
136   // Use membars when accessing volatile _thread_state. See
137   // Threads::create_vm() for size checks.
138   return (JavaThreadState) Atomic::load_acquire((volatile jint*)&_thread_state);
139 #else
140   return _thread_state;
141 #endif
142 }
143 
144 inline void JavaThread::set_thread_state(JavaThreadState s) {
145   assert(current_or_null() == NULL || current_or_null() == this,
146          "state change should only be called by the current thread");
147 #if defined(PPC64) || defined (AARCH64)
148   // Use membars when accessing volatile _thread_state. See
149   // Threads::create_vm() for size checks.
150   Atomic::release_store((volatile jint*)&_thread_state, (jint)s);
151 #else
152   _thread_state = s;
153 #endif
154 }
155 
156 inline void JavaThread::set_thread_state_fence(JavaThreadState s) {
157   set_thread_state(s);
158   OrderAccess::fence();
159 }
160 
161 ThreadSafepointState* JavaThread::safepoint_state() const  {
162   return _safepoint_state;
163 }
164 
165 void JavaThread::set_safepoint_state(ThreadSafepointState *state) {
166   _safepoint_state = state;
167 }
168 
169 bool JavaThread::is_at_poll_safepoint() {
170   return _safepoint_state->is_at_poll_safepoint();
171 }
172 
173 void JavaThread::enter_critical() {
174   assert(Thread::current() == this ||
175          (Thread::current()->is_VM_thread() &&
176          SafepointSynchronize::is_synchronizing()),
177          "this must be current thread or synchronizing");
178   _jni_active_critical++;
179 }
180 
181 inline void JavaThread::set_done_attaching_via_jni() {
182   _jni_attach_state = _attached_via_jni;
183   OrderAccess::fence();
184 }
185 
186 inline bool JavaThread::is_exiting() const {
187   // Use load-acquire so that setting of _terminated by
188   // JavaThread::exit() is seen more quickly.
189   TerminatedTypes l_terminated = Atomic::load_acquire(&_terminated);
190   return l_terminated == _thread_exiting || check_is_terminated(l_terminated);
191 }
192 
193 inline bool JavaThread::is_terminated() const {
194   // Use load-acquire so that setting of _terminated by
195   // JavaThread::exit() is seen more quickly.
196   TerminatedTypes l_terminated = Atomic::load_acquire(&_terminated);
197   return check_is_terminated(l_terminated);
198 }
199 
200 inline void JavaThread::set_terminated(TerminatedTypes t) {
201   // use release-store so the setting of _terminated is seen more quickly
202   Atomic::release_store(&_terminated, t);
203 }
204 
205 // Allow tracking of class initialization monitor use
206 inline void JavaThread::set_class_to_be_initialized(InstanceKlass* k) {
207   assert((k == NULL && _class_to_be_initialized != NULL) ||
208          (k != NULL && _class_to_be_initialized == NULL), "incorrect usage");
209   assert(this == Thread::current(), "Only the current thread can set this field");
210   _class_to_be_initialized = k;
211 }
212 
213 inline InstanceKlass* JavaThread::class_to_be_initialized() const {
214   return _class_to_be_initialized;
215 }
216 
217 #endif // SHARE_RUNTIME_THREAD_INLINE_HPP