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