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 if (ret) {
128 clear_suspend_flag(_has_async_exception);
129 }
130 return ret;
131 }
132
133 inline void JavaThread::set_pending_async_exception(oop e) {
134 _pending_async_exception = e;
135 set_suspend_flag(_has_async_exception);
136 }
137
138 inline void JavaThread::set_pending_unsafe_access_error() {
139 set_suspend_flag(_has_async_exception);
140 DEBUG_ONLY(_is_unsafe_access_error = true);
141 }
142
143
144 inline JavaThread::NoAsyncExceptionDeliveryMark::NoAsyncExceptionDeliveryMark(JavaThread *t) : _target(t) {
145 assert((_target->_suspend_flags & _async_delivery_disabled) == 0, "Nesting is not supported");
146 _target->set_suspend_flag(_async_delivery_disabled);
147 }
148 inline JavaThread::NoAsyncExceptionDeliveryMark::~NoAsyncExceptionDeliveryMark() {
149 _target->clear_suspend_flag(_async_delivery_disabled);
150 }
151
152 inline JavaThreadState JavaThread::thread_state() const {
153 #if defined(PPC64) || defined (AARCH64)
154 // Use membars when accessing volatile _thread_state. See
155 // Threads::create_vm() for size checks.
156 return (JavaThreadState) Atomic::load_acquire((volatile jint*)&_thread_state);
157 #else
158 return _thread_state;
159 #endif
160 }
161
162 inline void JavaThread::set_thread_state(JavaThreadState s) {
163 assert(current_or_null() == NULL || current_or_null() == this,
164 "state change should only be called by the current thread");
165 #if defined(PPC64) || defined (AARCH64)
166 // Use membars when accessing volatile _thread_state. See
167 // Threads::create_vm() for size checks.
168 Atomic::release_store((volatile jint*)&_thread_state, (jint)s);
169 #else
170 _thread_state = s;
171 #endif
172 }
173
174 inline void JavaThread::set_thread_state_fence(JavaThreadState s) {
175 set_thread_state(s);
176 OrderAccess::fence();
177 }
178
179 ThreadSafepointState* JavaThread::safepoint_state() const {
180 return _safepoint_state;
181 }
182
183 void JavaThread::set_safepoint_state(ThreadSafepointState *state) {
184 _safepoint_state = state;
185 }
186
187 bool JavaThread::is_at_poll_safepoint() {
188 return _safepoint_state->is_at_poll_safepoint();
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 // Use load-acquire so that setting of _terminated by
206 // JavaThread::exit() is seen more quickly.
207 TerminatedTypes l_terminated = Atomic::load_acquire(&_terminated);
208 return l_terminated == _thread_exiting || check_is_terminated(l_terminated);
209 }
210
211 inline bool JavaThread::is_terminated() const {
212 // Use load-acquire so that setting of _terminated by
213 // JavaThread::exit() is seen more quickly.
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 // use release-store so the setting of _terminated is seen more quickly
220 Atomic::release_store(&_terminated, t);
221 }
222
223 // Allow tracking of class initialization monitor use
224 inline void JavaThread::set_class_to_be_initialized(InstanceKlass* k) {
225 assert((k == NULL && _class_to_be_initialized != NULL) ||
226 (k != NULL && _class_to_be_initialized == NULL), "incorrect usage");
227 assert(this == Thread::current(), "Only the current thread can set this field");
228 _class_to_be_initialized = k;
229 }
230
231 inline InstanceKlass* JavaThread::class_to_be_initialized() const {
232 return _class_to_be_initialized;
233 }
234
235 #endif // SHARE_RUNTIME_THREAD_INLINE_HPP
--- EOF ---