1 /*
  2  * Copyright (c) 2012, 2023, 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_JAVATHREAD_INLINE_HPP
 27 #define SHARE_RUNTIME_JAVATHREAD_INLINE_HPP
 28 
 29 #include "runtime/javaThread.hpp"
 30 
 31 #include "classfile/javaClasses.hpp"
 32 #include "gc/shared/tlab_globals.hpp"
 33 #include "memory/universe.hpp"
 34 #include "oops/instanceKlass.hpp"
 35 #include "oops/oopHandle.inline.hpp"
 36 #include "runtime/atomic.hpp"
 37 #include "runtime/continuation.hpp"
 38 #include "runtime/continuationEntry.inline.hpp"
 39 #include "runtime/lockStack.inline.hpp"
 40 #include "runtime/nonJavaThread.hpp"
 41 #include "runtime/objectMonitor.inline.hpp"
 42 #include "runtime/orderAccess.hpp"
 43 #include "runtime/safepoint.hpp"
 44 
 45 inline void JavaThread::set_suspend_flag(SuspendFlags f) {
 46   uint32_t flags;
 47   do {
 48     flags = _suspend_flags;
 49   }
 50   while (Atomic::cmpxchg(&_suspend_flags, flags, (flags | f)) != flags);
 51 }
 52 inline void JavaThread::clear_suspend_flag(SuspendFlags f) {
 53   uint32_t flags;
 54   do {
 55     flags = _suspend_flags;
 56   }
 57   while (Atomic::cmpxchg(&_suspend_flags, flags, (flags & ~f)) != flags);
 58 }
 59 
 60 inline void JavaThread::set_trace_flag() {
 61   set_suspend_flag(_trace_flag);
 62 }
 63 inline void JavaThread::clear_trace_flag() {
 64   clear_suspend_flag(_trace_flag);
 65 }
 66 inline void JavaThread::set_obj_deopt_flag() {
 67   set_suspend_flag(_obj_deopt);
 68 }
 69 inline void JavaThread::clear_obj_deopt_flag() {
 70   clear_suspend_flag(_obj_deopt);
 71 }
 72 
 73 #if INCLUDE_JVMTI
 74 inline void JavaThread::set_carrier_thread_suspended() {
 75   _carrier_thread_suspended = true;
 76 }
 77 inline void JavaThread::clear_carrier_thread_suspended() {
 78   _carrier_thread_suspended = false;
 79 }
 80 #endif
 81 
 82 class AsyncExceptionHandshake : public AsyncHandshakeClosure {
 83   OopHandle _exception;
 84  public:
 85   AsyncExceptionHandshake(OopHandle& o, const char* name = "AsyncExceptionHandshake")
 86   : AsyncHandshakeClosure(name), _exception(o) { }
 87 
 88   ~AsyncExceptionHandshake() {
 89     Thread* current = Thread::current();
 90     // Can get here from the VMThread via install_async_exception() bail out.
 91     if (current->is_Java_thread()) {
 92       guarantee(JavaThread::cast(current)->is_oop_safe(),
 93                 "JavaThread cannot touch oops after its GC barrier is detached.");
 94     }
 95     assert(!_exception.is_empty(), "invariant");
 96     _exception.release(Universe::vm_global());
 97   }
 98 
 99   void do_thread(Thread* thr) {
100     JavaThread* self = JavaThread::cast(thr);
101     assert(self == JavaThread::current(), "must be");
102 
103     self->handle_async_exception(exception());
104   }
105   oop exception() {
106     assert(!_exception.is_empty(), "invariant");
107     return _exception.resolve();
108   }
109   bool is_async_exception()   { return true; }
110 };
111 
112 class UnsafeAccessErrorHandshake : public AsyncHandshakeClosure {
113  public:
114   UnsafeAccessErrorHandshake() : AsyncHandshakeClosure("UnsafeAccessErrorHandshake") {}
115   void do_thread(Thread* thr) {
116     JavaThread* self = JavaThread::cast(thr);
117     assert(self == JavaThread::current(), "must be");
118 
119     self->handshake_state()->handle_unsafe_access_error();
120   }
121   bool is_async_exception()   { return true; }
122 };
123 
124 inline void JavaThread::set_pending_unsafe_access_error() {
125   if (!has_async_exception_condition()) {
126     Handshake::execute(new UnsafeAccessErrorHandshake(), this);
127   }
128 }
129 
130 inline bool JavaThread::has_async_exception_condition() {
131   return handshake_state()->has_async_exception_operation();
132 }
133 
134 inline JavaThread::NoAsyncExceptionDeliveryMark::NoAsyncExceptionDeliveryMark(JavaThread *t) : _target(t) {
135   assert(!_target->handshake_state()->async_exceptions_blocked(), "Nesting is not supported");
136   _target->handshake_state()->set_async_exceptions_blocked(true);
137 }
138 inline JavaThread::NoAsyncExceptionDeliveryMark::~NoAsyncExceptionDeliveryMark() {
139   _target->handshake_state()->set_async_exceptions_blocked(false);
140 }
141 
142 inline JavaThreadState JavaThread::thread_state() const    {
143 #if defined(PPC64) || defined (AARCH64) || defined(RISCV64)
144   // Use membars when accessing volatile _thread_state. See
145   // Threads::create_vm() for size checks.
146   return Atomic::load_acquire(&_thread_state);
147 #else
148   return Atomic::load(&_thread_state);
149 #endif
150 }
151 
152 inline void JavaThread::set_thread_state(JavaThreadState s) {
153   assert(current_or_null() == nullptr || current_or_null() == this,
154          "state change should only be called by the current thread");
155 #if defined(PPC64) || defined (AARCH64) || defined(RISCV64)
156   // Use membars when accessing volatile _thread_state. See
157   // Threads::create_vm() for size checks.
158   Atomic::release_store(&_thread_state, s);
159 #else
160   Atomic::store(&_thread_state, s);
161 #endif
162 }
163 
164 inline void JavaThread::set_thread_state_fence(JavaThreadState s) {
165   set_thread_state(s);
166   OrderAccess::fence();
167 }
168 
169 ThreadSafepointState* JavaThread::safepoint_state() const  {
170   return _safepoint_state;
171 }
172 
173 void JavaThread::set_safepoint_state(ThreadSafepointState *state) {
174   _safepoint_state = state;
175 }
176 
177 bool JavaThread::is_at_poll_safepoint() {
178   return _safepoint_state->is_at_poll_safepoint();
179 }
180 
181 bool JavaThread::is_vthread_mounted() const {
182   return vthread_continuation() != nullptr;
183 }
184 
185 const ContinuationEntry* JavaThread::vthread_continuation() const {
186   for (ContinuationEntry* c = last_continuation(); c != nullptr; c = c->parent()) {
187     if (c->is_virtual_thread())
188       return c;
189   }
190   return nullptr;
191 }
192 
193 void JavaThread::enter_critical() {
194   assert(Thread::current() == this ||
195          (Thread::current()->is_VM_thread() &&
196          SafepointSynchronize::is_synchronizing()),
197          "this must be current thread or synchronizing");
198   _jni_active_critical++;
199 }
200 
201 inline void JavaThread::set_done_attaching_via_jni() {
202   _jni_attach_state = _attached_via_jni;
203   OrderAccess::fence();
204 }
205 
206 inline bool JavaThread::is_exiting() const {
207   TerminatedTypes l_terminated = Atomic::load_acquire(&_terminated);
208   return l_terminated == _thread_exiting ||
209          l_terminated == _thread_gc_barrier_detached ||
210          check_is_terminated(l_terminated);
211 }
212 
213 inline bool JavaThread::is_oop_safe() const {
214   TerminatedTypes l_terminated = Atomic::load_acquire(&_terminated);
215   return l_terminated != _thread_gc_barrier_detached &&
216          !check_is_terminated(l_terminated);
217 }
218 
219 inline bool JavaThread::is_terminated() const {
220   TerminatedTypes l_terminated = Atomic::load_acquire(&_terminated);
221   return check_is_terminated(l_terminated);
222 }
223 
224 inline void JavaThread::set_terminated(TerminatedTypes t) {
225   Atomic::release_store(&_terminated, t);
226 }
227 
228 inline bool JavaThread::is_active_Java_thread() const {
229   return on_thread_list() && !is_terminated();
230 }
231 
232 // Allow tracking of class initialization monitor use
233 inline void JavaThread::set_class_to_be_initialized(InstanceKlass* k) {
234   assert((k == nullptr && _class_to_be_initialized != nullptr) ||
235          (k != nullptr && _class_to_be_initialized == nullptr), "incorrect usage");
236   assert(this == Thread::current(), "Only the current thread can set this field");
237   _class_to_be_initialized = k;
238 }
239 
240 inline InstanceKlass* JavaThread::class_to_be_initialized() const {
241   return _class_to_be_initialized;
242 }
243 
244 inline void JavaThread::om_set_monitor_cache(ObjectMonitor* monitor) {
245   assert(UseObjectMonitorTable, "must be");
246   assert(monitor != nullptr, "use om_clear_monitor_cache to clear");
247   assert(this == current() || monitor->owner_raw() == this, "only add owned monitors for other threads");
248   assert(this == current() || is_obj_deopt_suspend(), "thread must not run concurrently");
249 
250   _om_cache.set_monitor(monitor);
251 }
252 
253 inline void JavaThread::om_clear_monitor_cache() {
254   if (!UseObjectMonitorTable) {
255     return;
256   }
257 
258   _om_cache.clear();
259 }
260 
261 inline ObjectMonitor* JavaThread::om_get_from_monitor_cache(oop obj) {
262   assert(obj != nullptr, "do not look for null objects");
263   assert(this == current(), "only get own thread locals");
264   return _om_cache.get_monitor(obj);
265 }
266 
267 #endif // SHARE_RUNTIME_JAVATHREAD_INLINE_HPP