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