blob: 0404ac93941f98aee4cdf35578a5ebc6febab9fe [file] [log] [blame]
Ian Rogers693ff612013-02-01 10:56:12 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_THREAD_INL_H_
18#define ART_RUNTIME_THREAD_INL_H_
Ian Rogers693ff612013-02-01 10:56:12 -080019
20#include "thread.h"
21
Andreas Gampe639b2b12019-01-08 10:32:50 -080022#include "arch/instruction_set.h"
Andreas Gampe39b378c2017-12-07 15:44:13 -080023#include "base/aborting.h"
Ian Rogers1eb512d2013-10-18 15:42:20 -070024#include "base/casts.h"
Ian Rogers693ff612013-02-01 10:56:12 -080025#include "base/mutex-inl.h"
Andreas Gamped4901292017-05-30 18:41:34 -070026#include "base/time_utils.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010027#include "jni/jni_env_ext.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070028#include "managed_stack-inl.h"
Andreas Gampec73cb642017-02-22 10:11:30 -080029#include "obj_ptr.h"
Andreas Gampe0c2313c2019-05-14 09:47:00 -070030#include "suspend_reason.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070031#include "thread-current-inl.h"
Mathieu Chartier3cf22532015-07-09 15:15:09 -070032#include "thread_pool.h"
Ian Rogers693ff612013-02-01 10:56:12 -080033
34namespace art {
35
Ian Rogers1eb512d2013-10-18 15:42:20 -070036// Quickly access the current thread from a JNIEnv.
37static inline Thread* ThreadForEnv(JNIEnv* env) {
38 JNIEnvExt* full_env(down_cast<JNIEnvExt*>(env));
Ian Rogers55256cb2017-12-21 17:07:11 -080039 return full_env->GetSelf();
Ian Rogers1eb512d2013-10-18 15:42:20 -070040}
41
Ian Rogers7b078e82014-09-10 14:44:24 -070042inline void Thread::AllowThreadSuspension() {
43 DCHECK_EQ(Thread::Current(), this);
44 if (UNLIKELY(TestAllFlags())) {
45 CheckSuspend();
46 }
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070047 // Invalidate the current thread's object pointers (ObjPtr) to catch possible moving GC bugs due
48 // to missing handles.
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070049 PoisonObjectPointers();
Ian Rogers7b078e82014-09-10 14:44:24 -070050}
51
52inline void Thread::CheckSuspend() {
53 DCHECK_EQ(Thread::Current(), this);
54 for (;;) {
55 if (ReadFlag(kCheckpointRequest)) {
56 RunCheckpointFunction();
57 } else if (ReadFlag(kSuspendRequest)) {
58 FullSuspendCheck();
Hiroshi Yamauchi30493242016-11-03 13:06:52 -070059 } else if (ReadFlag(kEmptyCheckpointRequest)) {
60 RunEmptyCheckpoint();
61 } else {
62 break;
63 }
64 }
65}
66
Hiroshi Yamauchia2224042017-02-08 16:35:45 -080067inline void Thread::CheckEmptyCheckpointFromWeakRefAccess(BaseMutex* cond_var_mutex) {
68 Thread* self = Thread::Current();
69 DCHECK_EQ(self, this);
70 for (;;) {
71 if (ReadFlag(kEmptyCheckpointRequest)) {
72 RunEmptyCheckpoint();
73 // Check we hold only an expected mutex when accessing weak ref.
74 if (kIsDebugBuild) {
75 for (int i = kLockLevelCount - 1; i >= 0; --i) {
76 BaseMutex* held_mutex = self->GetHeldMutex(static_cast<LockLevel>(i));
77 if (held_mutex != nullptr &&
78 held_mutex != Locks::mutator_lock_ &&
79 held_mutex != cond_var_mutex) {
Hiroshi Yamauchi8a433242017-03-07 14:39:22 -080080 CHECK(Locks::IsExpectedOnWeakRefAccess(held_mutex))
Hiroshi Yamauchia2224042017-02-08 16:35:45 -080081 << "Holding unexpected mutex " << held_mutex->GetName()
82 << " when accessing weak ref";
83 }
84 }
85 }
86 } else {
87 break;
88 }
89 }
90}
91
92inline void Thread::CheckEmptyCheckpointFromMutex() {
Hiroshi Yamauchi30493242016-11-03 13:06:52 -070093 DCHECK_EQ(Thread::Current(), this);
94 for (;;) {
95 if (ReadFlag(kEmptyCheckpointRequest)) {
96 RunEmptyCheckpoint();
Ian Rogers7b078e82014-09-10 14:44:24 -070097 } else {
98 break;
99 }
100 }
101}
102
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800103inline ThreadState Thread::SetState(ThreadState new_state) {
Yu Lieac44242015-06-29 10:50:03 +0800104 // Should only be used to change between suspended states.
105 // Cannot use this code to change into or from Runnable as changing to Runnable should
106 // fail if old_state_and_flags.suspend_request is true and changing from Runnable might
107 // miss passing an active suspend barrier.
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800108 DCHECK_NE(new_state, kRunnable);
Andreas Gampeef048f62014-11-25 22:12:27 -0800109 if (kIsDebugBuild && this != Thread::Current()) {
110 std::string name;
111 GetThreadName(name);
112 LOG(FATAL) << "Thread \"" << name << "\"(" << this << " != Thread::Current()="
113 << Thread::Current() << ") changing state to " << new_state;
114 }
Chris Dearman59cde532013-12-04 18:53:49 -0800115 union StateAndFlags old_state_and_flags;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700116 old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
Yu Lieac44242015-06-29 10:50:03 +0800117 CHECK_NE(old_state_and_flags.as_struct.state, kRunnable);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700118 tls32_.state_and_flags.as_struct.state = new_state;
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800119 return static_cast<ThreadState>(old_state_and_flags.as_struct.state);
120}
121
Mathieu Chartier10b218d2016-07-25 17:48:52 -0700122inline bool Thread::IsThreadSuspensionAllowable() const {
123 if (tls32_.no_thread_suspension != 0) {
124 return false;
125 }
126 for (int i = kLockLevelCount - 1; i >= 0; --i) {
Alex Light88fd7202017-06-30 08:31:59 -0700127 if (i != kMutatorLock &&
128 i != kUserCodeSuspensionLock &&
129 GetHeldMutex(static_cast<LockLevel>(i)) != nullptr) {
Mathieu Chartier10b218d2016-07-25 17:48:52 -0700130 return false;
131 }
132 }
Alex Light88fd7202017-06-30 08:31:59 -0700133 // Thread autoanalysis isn't able to understand that the GetHeldMutex(...) or AssertHeld means we
134 // have the mutex meaning we need to do this hack.
135 auto is_suspending_for_user_code = [this]() NO_THREAD_SAFETY_ANALYSIS {
136 return tls32_.user_code_suspend_count != 0;
137 };
138 if (GetHeldMutex(kUserCodeSuspensionLock) != nullptr && is_suspending_for_user_code()) {
139 return false;
140 }
Mathieu Chartier10b218d2016-07-25 17:48:52 -0700141 return true;
142}
143
Ian Rogers693ff612013-02-01 10:56:12 -0800144inline void Thread::AssertThreadSuspensionIsAllowable(bool check_locks) const {
Ian Rogersf3d874c2014-07-17 18:52:42 -0700145 if (kIsDebugBuild) {
Nicolas Geoffraydb978712014-12-09 13:33:38 +0000146 if (gAborting == 0) {
147 CHECK_EQ(0u, tls32_.no_thread_suspension) << tlsPtr_.last_no_thread_suspension_cause;
148 }
Ian Rogersf3d874c2014-07-17 18:52:42 -0700149 if (check_locks) {
150 bool bad_mutexes_held = false;
151 for (int i = kLockLevelCount - 1; i >= 0; --i) {
Alex Light88fd7202017-06-30 08:31:59 -0700152 // We expect no locks except the mutator_lock_. User code suspension lock is OK as long as
153 // we aren't going to be held suspended due to SuspendReason::kForUserCode.
154 if (i != kMutatorLock && i != kUserCodeSuspensionLock) {
Ian Rogersf3d874c2014-07-17 18:52:42 -0700155 BaseMutex* held_mutex = GetHeldMutex(static_cast<LockLevel>(i));
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700156 if (held_mutex != nullptr) {
Ian Rogersf3d874c2014-07-17 18:52:42 -0700157 LOG(ERROR) << "holding \"" << held_mutex->GetName()
158 << "\" at point where thread suspension is expected";
159 bad_mutexes_held = true;
160 }
Ian Rogers693ff612013-02-01 10:56:12 -0800161 }
162 }
Alex Light88fd7202017-06-30 08:31:59 -0700163 // Make sure that if we hold the user_code_suspension_lock_ we aren't suspending due to
164 // user_code_suspend_count which would prevent the thread from ever waking up. Thread
165 // autoanalysis isn't able to understand that the GetHeldMutex(...) or AssertHeld means we
166 // have the mutex meaning we need to do this hack.
167 auto is_suspending_for_user_code = [this]() NO_THREAD_SAFETY_ANALYSIS {
168 return tls32_.user_code_suspend_count != 0;
169 };
170 if (GetHeldMutex(kUserCodeSuspensionLock) != nullptr && is_suspending_for_user_code()) {
171 LOG(ERROR) << "suspending due to user-code while holding \""
172 << Locks::user_code_suspension_lock_->GetName() << "\"! Thread would never "
173 << "wake up.";
174 bad_mutexes_held = true;
175 }
Nicolas Geoffraydb978712014-12-09 13:33:38 +0000176 if (gAborting == 0) {
177 CHECK(!bad_mutexes_held);
178 }
Ian Rogers693ff612013-02-01 10:56:12 -0800179 }
Ian Rogers693ff612013-02-01 10:56:12 -0800180 }
Ian Rogers693ff612013-02-01 10:56:12 -0800181}
182
Mathieu Chartier8ac9c912015-10-01 15:58:41 -0700183inline void Thread::TransitionToSuspendedAndRunCheckpoints(ThreadState new_state) {
Ian Rogers693ff612013-02-01 10:56:12 -0800184 DCHECK_NE(new_state, kRunnable);
Ian Rogers693ff612013-02-01 10:56:12 -0800185 DCHECK_EQ(GetState(), kRunnable);
186 union StateAndFlags old_state_and_flags;
187 union StateAndFlags new_state_and_flags;
Dave Allison0f5f6bb2013-11-22 17:39:19 -0800188 while (true) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700189 old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700190 if (UNLIKELY((old_state_and_flags.as_struct.flags & kCheckpointRequest) != 0)) {
191 RunCheckpointFunction();
192 continue;
193 }
Hiroshi Yamauchi30493242016-11-03 13:06:52 -0700194 if (UNLIKELY((old_state_and_flags.as_struct.flags & kEmptyCheckpointRequest) != 0)) {
195 RunEmptyCheckpoint();
196 continue;
197 }
Dave Allison0f5f6bb2013-11-22 17:39:19 -0800198 // Change the state but keep the current flags (kCheckpointRequest is clear).
199 DCHECK_EQ((old_state_and_flags.as_struct.flags & kCheckpointRequest), 0);
Hiroshi Yamauchi30493242016-11-03 13:06:52 -0700200 DCHECK_EQ((old_state_and_flags.as_struct.flags & kEmptyCheckpointRequest), 0);
Dave Allison0f5f6bb2013-11-22 17:39:19 -0800201 new_state_and_flags.as_struct.flags = old_state_and_flags.as_struct.flags;
Ian Rogers693ff612013-02-01 10:56:12 -0800202 new_state_and_flags.as_struct.state = new_state;
Ian Rogersb8e087e2014-07-09 21:12:06 -0700203
Yu Lieac44242015-06-29 10:50:03 +0800204 // CAS the value with a memory ordering.
Ian Rogersb8e087e2014-07-09 21:12:06 -0700205 bool done =
Orion Hodson4557b382018-01-03 11:47:54 +0000206 tls32_.state_and_flags.as_atomic_int.CompareAndSetWeakRelease(old_state_and_flags.as_int,
Ian Rogersb8e087e2014-07-09 21:12:06 -0700207 new_state_and_flags.as_int);
208 if (LIKELY(done)) {
Dave Allison0f5f6bb2013-11-22 17:39:19 -0800209 break;
210 }
211 }
Mathieu Chartier8ac9c912015-10-01 15:58:41 -0700212}
Yu Lieac44242015-06-29 10:50:03 +0800213
Mathieu Chartier8ac9c912015-10-01 15:58:41 -0700214inline void Thread::PassActiveSuspendBarriers() {
Yu Lieac44242015-06-29 10:50:03 +0800215 while (true) {
216 uint16_t current_flags = tls32_.state_and_flags.as_struct.flags;
Hiroshi Yamauchi30493242016-11-03 13:06:52 -0700217 if (LIKELY((current_flags &
218 (kCheckpointRequest | kEmptyCheckpointRequest | kActiveSuspendBarrier)) == 0)) {
Yu Lieac44242015-06-29 10:50:03 +0800219 break;
220 } else if ((current_flags & kActiveSuspendBarrier) != 0) {
221 PassActiveSuspendBarriers(this);
222 } else {
223 // Impossible
Mathieu Chartier8ac9c912015-10-01 15:58:41 -0700224 LOG(FATAL) << "Fatal, thread transitioned into suspended without running the checkpoint";
Yu Lieac44242015-06-29 10:50:03 +0800225 }
226 }
Ian Rogers693ff612013-02-01 10:56:12 -0800227}
228
Mathieu Chartier8ac9c912015-10-01 15:58:41 -0700229inline void Thread::TransitionFromRunnableToSuspended(ThreadState new_state) {
230 AssertThreadSuspensionIsAllowable();
Mathieu Chartier3398c782016-09-30 10:27:43 -0700231 PoisonObjectPointersIfDebug();
Mathieu Chartier8ac9c912015-10-01 15:58:41 -0700232 DCHECK_EQ(this, Thread::Current());
233 // Change to non-runnable state, thereby appearing suspended to the system.
234 TransitionToSuspendedAndRunCheckpoints(new_state);
235 // Mark the release of the share of the mutator_lock_.
236 Locks::mutator_lock_->TransitionFromRunnableToSuspended(this);
237 // Once suspended - check the active suspend barrier flag
238 PassActiveSuspendBarriers();
239}
240
Ian Rogers693ff612013-02-01 10:56:12 -0800241inline ThreadState Thread::TransitionFromSuspendedToRunnable() {
Chris Dearman59cde532013-12-04 18:53:49 -0800242 union StateAndFlags old_state_and_flags;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700243 old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
Ian Rogers693ff612013-02-01 10:56:12 -0800244 int16_t old_state = old_state_and_flags.as_struct.state;
245 DCHECK_NE(static_cast<ThreadState>(old_state), kRunnable);
246 do {
247 Locks::mutator_lock_->AssertNotHeld(this); // Otherwise we starve GC..
Ian Rogersdd7624d2014-03-14 17:43:00 -0700248 old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
Ian Rogers693ff612013-02-01 10:56:12 -0800249 DCHECK_EQ(old_state_and_flags.as_struct.state, old_state);
Yu Lieac44242015-06-29 10:50:03 +0800250 if (LIKELY(old_state_and_flags.as_struct.flags == 0)) {
251 // Optimize for the return from native code case - this is the fast path.
252 // Atomically change from suspended to runnable if no suspend request pending.
253 union StateAndFlags new_state_and_flags;
254 new_state_and_flags.as_int = old_state_and_flags.as_int;
255 new_state_and_flags.as_struct.state = kRunnable;
Orion Hodson88591fe2018-03-06 13:35:43 +0000256
Yu Lieac44242015-06-29 10:50:03 +0800257 // CAS the value with a memory barrier.
Orion Hodson4557b382018-01-03 11:47:54 +0000258 if (LIKELY(tls32_.state_and_flags.as_atomic_int.CompareAndSetWeakAcquire(
Yu Lieac44242015-06-29 10:50:03 +0800259 old_state_and_flags.as_int,
260 new_state_and_flags.as_int))) {
261 // Mark the acquisition of a share of the mutator_lock_.
262 Locks::mutator_lock_->TransitionFromSuspendedToRunnable(this);
263 break;
264 }
265 } else if ((old_state_and_flags.as_struct.flags & kActiveSuspendBarrier) != 0) {
266 PassActiveSuspendBarriers(this);
Hiroshi Yamauchi30493242016-11-03 13:06:52 -0700267 } else if ((old_state_and_flags.as_struct.flags &
268 (kCheckpointRequest | kEmptyCheckpointRequest)) != 0) {
Yu Lieac44242015-06-29 10:50:03 +0800269 // Impossible
Mathieu Chartierdabdccc2015-10-01 14:46:29 -0700270 LOG(FATAL) << "Transitioning to runnable with checkpoint flag, "
271 << " flags=" << old_state_and_flags.as_struct.flags
272 << " state=" << old_state_and_flags.as_struct.state;
Yu Lieac44242015-06-29 10:50:03 +0800273 } else if ((old_state_and_flags.as_struct.flags & kSuspendRequest) != 0) {
Ian Rogers693ff612013-02-01 10:56:12 -0800274 // Wait while our suspend count is non-zero.
Nicolas Geoffray9f5f8ac2016-06-29 14:39:59 +0100275
276 // We pass null to the MutexLock as we may be in a situation where the
277 // runtime is shutting down. Guarding ourselves from that situation
278 // requires to take the shutdown lock, which is undesirable here.
279 Thread* thread_to_pass = nullptr;
280 if (kIsDebugBuild && !IsDaemon()) {
281 // We know we can make our debug locking checks on non-daemon threads,
282 // so re-enable them on debug builds.
283 thread_to_pass = this;
284 }
285 MutexLock mu(thread_to_pass, *Locks::thread_suspend_count_lock_);
Hiroshi Yamauchiee235822016-08-19 17:03:27 -0700286 ScopedTransitioningToRunnable scoped_transitioning_to_runnable(this);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700287 old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
Ian Rogers693ff612013-02-01 10:56:12 -0800288 DCHECK_EQ(old_state_and_flags.as_struct.state, old_state);
289 while ((old_state_and_flags.as_struct.flags & kSuspendRequest) != 0) {
290 // Re-check when Thread::resume_cond_ is notified.
Nicolas Geoffray9f5f8ac2016-06-29 14:39:59 +0100291 Thread::resume_cond_->Wait(thread_to_pass);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700292 old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
Ian Rogers693ff612013-02-01 10:56:12 -0800293 DCHECK_EQ(old_state_and_flags.as_struct.state, old_state);
294 }
295 DCHECK_EQ(GetSuspendCount(), 0);
296 }
Ian Rogers719d1a32014-03-06 12:13:39 -0800297 } while (true);
Yu Lieac44242015-06-29 10:50:03 +0800298 // Run the flip function, if set.
299 Closure* flip_func = GetFlipFunction();
300 if (flip_func != nullptr) {
301 flip_func->Run(this);
302 }
303 return static_cast<ThreadState>(old_state);
Ian Rogers693ff612013-02-01 10:56:12 -0800304}
305
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800306inline mirror::Object* Thread::AllocTlab(size_t bytes) {
307 DCHECK_GE(TlabSize(), bytes);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700308 ++tlsPtr_.thread_local_objects;
309 mirror::Object* ret = reinterpret_cast<mirror::Object*>(tlsPtr_.thread_local_pos);
310 tlsPtr_.thread_local_pos += bytes;
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800311 return ret;
312}
313
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800314inline bool Thread::PushOnThreadLocalAllocationStack(mirror::Object* obj) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700315 DCHECK_LE(tlsPtr_.thread_local_alloc_stack_top, tlsPtr_.thread_local_alloc_stack_end);
316 if (tlsPtr_.thread_local_alloc_stack_top < tlsPtr_.thread_local_alloc_stack_end) {
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800317 // There's room.
Ian Rogers13735952014-10-08 12:43:28 -0700318 DCHECK_LE(reinterpret_cast<uint8_t*>(tlsPtr_.thread_local_alloc_stack_top) +
Mathieu Chartiercb535da2015-01-23 13:50:03 -0800319 sizeof(StackReference<mirror::Object>),
Ian Rogers13735952014-10-08 12:43:28 -0700320 reinterpret_cast<uint8_t*>(tlsPtr_.thread_local_alloc_stack_end));
Mathieu Chartiercb535da2015-01-23 13:50:03 -0800321 DCHECK(tlsPtr_.thread_local_alloc_stack_top->AsMirrorPtr() == nullptr);
322 tlsPtr_.thread_local_alloc_stack_top->Assign(obj);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700323 ++tlsPtr_.thread_local_alloc_stack_top;
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800324 return true;
325 }
326 return false;
327}
328
Mathieu Chartiercb535da2015-01-23 13:50:03 -0800329inline void Thread::SetThreadLocalAllocationStack(StackReference<mirror::Object>* start,
330 StackReference<mirror::Object>* end) {
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800331 DCHECK(Thread::Current() == this) << "Should be called by self";
332 DCHECK(start != nullptr);
333 DCHECK(end != nullptr);
Mathieu Chartiercb535da2015-01-23 13:50:03 -0800334 DCHECK_ALIGNED(start, sizeof(StackReference<mirror::Object>));
335 DCHECK_ALIGNED(end, sizeof(StackReference<mirror::Object>));
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800336 DCHECK_LT(start, end);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700337 tlsPtr_.thread_local_alloc_stack_end = end;
338 tlsPtr_.thread_local_alloc_stack_top = start;
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800339}
340
341inline void Thread::RevokeThreadLocalAllocationStack() {
342 if (kIsDebugBuild) {
343 // Note: self is not necessarily equal to this thread since thread may be suspended.
344 Thread* self = Thread::Current();
345 DCHECK(this == self || IsSuspended() || GetState() == kWaitingPerformingGc)
346 << GetState() << " thread " << this << " self " << self;
347 }
Ian Rogersdd7624d2014-03-14 17:43:00 -0700348 tlsPtr_.thread_local_alloc_stack_end = nullptr;
349 tlsPtr_.thread_local_alloc_stack_top = nullptr;
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800350}
351
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700352inline void Thread::PoisonObjectPointersIfDebug() {
Andreas Gampec73cb642017-02-22 10:11:30 -0800353 if (kObjPtrPoisoning) {
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700354 Thread::Current()->PoisonObjectPointers();
355 }
356}
357
Hiroshi Yamauchi02e7f1a2016-10-03 15:32:01 -0700358inline bool Thread::ModifySuspendCount(Thread* self,
359 int delta,
360 AtomicInteger* suspend_barrier,
Alex Light46f93402017-06-29 11:59:50 -0700361 SuspendReason reason) {
Hiroshi Yamauchi02e7f1a2016-10-03 15:32:01 -0700362 if (delta > 0 && ((kUseReadBarrier && this != self) || suspend_barrier != nullptr)) {
363 // When delta > 0 (requesting a suspend), ModifySuspendCountInternal() may fail either if
364 // active_suspend_barriers is full or we are in the middle of a thread flip. Retry in a loop.
365 while (true) {
Alex Light46f93402017-06-29 11:59:50 -0700366 if (LIKELY(ModifySuspendCountInternal(self, delta, suspend_barrier, reason))) {
Hiroshi Yamauchi02e7f1a2016-10-03 15:32:01 -0700367 return true;
368 } else {
369 // Failure means the list of active_suspend_barriers is full or we are in the middle of a
370 // thread flip, we should release the thread_suspend_count_lock_ (to avoid deadlock) and
371 // wait till the target thread has executed or Thread::PassActiveSuspendBarriers() or the
372 // flip function. Note that we could not simply wait for the thread to change to a suspended
373 // state, because it might need to run checkpoint function before the state change or
374 // resumes from the resume_cond_, which also needs thread_suspend_count_lock_.
375 //
376 // The list of active_suspend_barriers is very unlikely to be full since more than
377 // kMaxSuspendBarriers threads need to execute SuspendAllInternal() simultaneously, and
378 // target thread stays in kRunnable in the mean time.
379 Locks::thread_suspend_count_lock_->ExclusiveUnlock(self);
380 NanoSleep(100000);
381 Locks::thread_suspend_count_lock_->ExclusiveLock(self);
382 }
383 }
384 } else {
Alex Light46f93402017-06-29 11:59:50 -0700385 return ModifySuspendCountInternal(self, delta, suspend_barrier, reason);
Hiroshi Yamauchi02e7f1a2016-10-03 15:32:01 -0700386 }
387}
388
Andreas Gampe513061a2017-06-01 09:17:34 -0700389inline ShadowFrame* Thread::PushShadowFrame(ShadowFrame* new_top_frame) {
Nicolas Geoffray893147c2018-10-29 14:49:30 +0000390 new_top_frame->CheckConsistentVRegs();
Andreas Gampe513061a2017-06-01 09:17:34 -0700391 return tlsPtr_.managed_stack.PushShadowFrame(new_top_frame);
392}
393
394inline ShadowFrame* Thread::PopShadowFrame() {
395 return tlsPtr_.managed_stack.PopShadowFrame();
396}
397
Andreas Gampe639b2b12019-01-08 10:32:50 -0800398inline uint8_t* Thread::GetStackEndForInterpreter(bool implicit_overflow_check) const {
399 uint8_t* end = tlsPtr_.stack_end + (implicit_overflow_check
400 ? GetStackOverflowReservedBytes(kRuntimeISA)
401 : 0);
402 if (kIsDebugBuild) {
403 // In a debuggable build, but especially under ASAN, the access-checks interpreter has a
404 // potentially humongous stack size. We don't want to take too much of the stack regularly,
405 // so do not increase the regular reserved size (for compiled code etc) and only report the
406 // virtually smaller stack to the interpreter here.
407 end += GetStackOverflowReservedBytes(kRuntimeISA);
408 }
409 return end;
410}
411
412inline void Thread::ResetDefaultStackEnd() {
413 // Our stacks grow down, so we want stack_end_ to be near there, but reserving enough room
414 // to throw a StackOverflowError.
415 tlsPtr_.stack_end = tlsPtr_.stack_begin + GetStackOverflowReservedBytes(kRuntimeISA);
416}
417
Ian Rogers693ff612013-02-01 10:56:12 -0800418} // namespace art
419
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700420#endif // ART_RUNTIME_THREAD_INL_H_