1 /*
  2  * Copyright (c) 2012, 2025, 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_obj_deopt_flag() {
 61   set_suspend_flag(_obj_deopt);
 62 }
 63 inline void JavaThread::clear_obj_deopt_flag() {
 64   clear_suspend_flag(_obj_deopt);
 65 }
 66 
 67 #if INCLUDE_JVMTI
 68 inline bool JavaThread::set_carrier_thread_suspended() {
 69   return Atomic::cmpxchg(&_carrier_thread_suspended, false, true) == false;
 70 }
 71 inline bool JavaThread::clear_carrier_thread_suspended() {
 72   return Atomic::cmpxchg(&_carrier_thread_suspended, true, false) == true;
 73 }
 74 #endif
 75 
 76 class AsyncExceptionHandshake : public AsyncHandshakeClosure {
 77   OopHandle _exception;
 78  public:
 79   AsyncExceptionHandshake(OopHandle& o, const char* name = "AsyncExceptionHandshake")
 80   : AsyncHandshakeClosure(name), _exception(o) { }
 81 
 82   ~AsyncExceptionHandshake() {
 83     Thread* current = Thread::current();
 84     // Can get here from the VMThread via install_async_exception() bail out.
 85     if (current->is_Java_thread()) {
 86       guarantee(JavaThread::cast(current)->is_oop_safe(),
 87                 "JavaThread cannot touch oops after its GC barrier is detached.");
 88     }
 89     assert(!_exception.is_empty(), "invariant");
 90     _exception.release(Universe::vm_global());
 91   }
 92 
 93   void do_thread(Thread* thr) {
 94     JavaThread* self = JavaThread::cast(thr);
 95     assert(self == JavaThread::current(), "must be");
 96 
 97     self->handle_async_exception(exception());
 98   }
 99   oop exception() {
100     assert(!_exception.is_empty(), "invariant");
101     return _exception.resolve();
102   }
103   bool is_async_exception()   { return true; }
104 };
105 
106 class UnsafeAccessErrorHandshake : public AsyncHandshakeClosure {
107  public:
108   UnsafeAccessErrorHandshake() : AsyncHandshakeClosure("UnsafeAccessErrorHandshake") {}
109   void do_thread(Thread* thr) {
110     JavaThread* self = JavaThread::cast(thr);
111     assert(self == JavaThread::current(), "must be");
112 
113     self->handshake_state()->handle_unsafe_access_error();
114   }
115   bool is_async_exception()   { return true; }
116 };
117 
118 inline void JavaThread::set_pending_unsafe_access_error() {
119   if (!has_async_exception_condition()) {
120     Handshake::execute(new UnsafeAccessErrorHandshake(), this);
121   }
122 }
123 
124 inline bool JavaThread::has_async_exception_condition() {
125   return handshake_state()->has_async_exception_operation();
126 }
127 
128 inline JavaThread::NoAsyncExceptionDeliveryMark::NoAsyncExceptionDeliveryMark(JavaThread *t) : _target(t) {
129   assert(!_target->handshake_state()->async_exceptions_blocked(), "Nesting is not supported");
130   _target->handshake_state()->set_async_exceptions_blocked(true);
131 }
132 inline JavaThread::NoAsyncExceptionDeliveryMark::~NoAsyncExceptionDeliveryMark() {
133   _target->handshake_state()->set_async_exceptions_blocked(false);
134 }
135 
136 inline JavaThreadState JavaThread::thread_state() const    {
137 #if defined(PPC64) || defined (AARCH64) || defined(RISCV64)
138   // Use membars when accessing volatile _thread_state. See
139   // Threads::create_vm() for size checks.
140   return Atomic::load_acquire(&_thread_state);
141 #else
142   return Atomic::load(&_thread_state);
143 #endif
144 }
145 
146 inline void JavaThread::set_thread_state(JavaThreadState s) {
147   assert(current_or_null() == nullptr || current_or_null() == this,
148          "state change should only be called by the current thread");
149 #if defined(PPC64) || defined (AARCH64) || defined(RISCV64)
150   // Use membars when accessing volatile _thread_state. See
151   // Threads::create_vm() for size checks.
152   Atomic::release_store(&_thread_state, s);
153 #else
154   Atomic::store(&_thread_state, s);
155 #endif
156 }
157 
158 inline void JavaThread::set_thread_state_fence(JavaThreadState s) {
159   set_thread_state(s);
160   OrderAccess::fence();
161 }
162 
163 ThreadSafepointState* JavaThread::safepoint_state() const  {
164   return _safepoint_state;
165 }
166 
167 void JavaThread::set_safepoint_state(ThreadSafepointState *state) {
168   _safepoint_state = state;
169 }
170 
171 bool JavaThread::is_at_poll_safepoint() {
172   return _safepoint_state->is_at_poll_safepoint();
173 }
174 
175 bool JavaThread::is_vthread_mounted() const {
176   return vthread_continuation() != nullptr;
177 }
178 
179 const ContinuationEntry* JavaThread::vthread_continuation() const {
180   for (ContinuationEntry* c = last_continuation(); c != nullptr; c = c->parent()) {
181     if (c->is_virtual_thread())
182       return c;
183   }
184   return nullptr;
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   TerminatedTypes l_terminated = Atomic::load_acquire(&_terminated);
202   return l_terminated == _thread_exiting ||
203          l_terminated == _thread_gc_barrier_detached ||
204          check_is_terminated(l_terminated);
205 }
206 
207 inline bool JavaThread::is_oop_safe() const {
208   TerminatedTypes l_terminated = Atomic::load_acquire(&_terminated);
209   return l_terminated != _thread_gc_barrier_detached &&
210          !check_is_terminated(l_terminated);
211 }
212 
213 inline bool JavaThread::is_terminated() const {
214   TerminatedTypes l_terminated = Atomic::load_acquire(&_terminated);
215   return check_is_terminated(l_terminated);
216 }
217 
218 inline void JavaThread::set_terminated(TerminatedTypes t) {
219   Atomic::release_store(&_terminated, t);
220 }
221 
222 inline bool JavaThread::is_active_Java_thread() const {
223   return on_thread_list() && !is_terminated();
224 }
225 
226 // Allow tracking of class initialization monitor use
227 inline void JavaThread::set_class_to_be_initialized(InstanceKlass* k) {
228   assert((k == nullptr && _class_to_be_initialized != nullptr) ||
229          (k != nullptr && _class_to_be_initialized == nullptr), "incorrect usage");
230   assert(this == Thread::current(), "Only the current thread can set this field");
231   _class_to_be_initialized = k;
232 }
233 
234 inline InstanceKlass* JavaThread::class_to_be_initialized() const {
235   return _class_to_be_initialized;
236 }
237 
238 inline InstanceKlass* JavaThread::set_class_being_initialized(InstanceKlass* k) {
239   assert(this == Thread::current(), "Only the current thread can set this field");
240   InstanceKlass* prev = _class_being_initialized;
241   _class_being_initialized = k;
242   return prev;
243 }
244 
245 inline InstanceKlass* JavaThread::class_being_initialized() const {
246   assert(this == Thread::current(), "Only the current thread can get this field");
247   return _class_being_initialized;
248 }
249 
250 inline void JavaThread::om_set_monitor_cache(ObjectMonitor* monitor) {
251   assert(UseObjectMonitorTable, "must be");
252   assert(monitor != nullptr, "use om_clear_monitor_cache to clear");
253   assert(this == current() || monitor->has_owner(this), "only add owned monitors for other threads");
254   assert(this == current() || is_obj_deopt_suspend(), "thread must not run concurrently");
255 
256   _om_cache.set_monitor(monitor);
257 }
258 
259 inline void JavaThread::om_clear_monitor_cache() {
260   if (UseObjectMonitorTable) {
261     _om_cache.clear();
262   }
263 }
264 
265 inline ObjectMonitor* JavaThread::om_get_from_monitor_cache(oop obj) {
266   assert(obj != nullptr, "do not look for null objects");
267   assert(this == current(), "only get own thread locals");
268   return _om_cache.get_monitor(obj);
269 }
270 
271 #endif // SHARE_RUNTIME_JAVATHREAD_INLINE_HPP