blob: 2f6f50e31eecbdcae37a81acffba1a6710b8b113 [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 Gampe39b378c2017-12-07 15:44:13 -080022#include "base/aborting.h"
Ian Rogers1eb512d2013-10-18 15:42:20 -070023#include "base/casts.h"
Ian Rogers693ff612013-02-01 10:56:12 -080024#include "base/mutex-inl.h"
Andreas Gamped4901292017-05-30 18:41:34 -070025#include "base/time_utils.h"
Ian Rogers68d8b422014-07-17 11:09:10 -070026#include "jni_env_ext.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070027#include "managed_stack-inl.h"
Andreas Gampec73cb642017-02-22 10:11:30 -080028#include "obj_ptr.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070029#include "thread-current-inl.h"
Mathieu Chartier3cf22532015-07-09 15:15:09 -070030#include "thread_pool.h"
Ian Rogers693ff612013-02-01 10:56:12 -080031
32namespace art {
33
Ian Rogers1eb512d2013-10-18 15:42:20 -070034// Quickly access the current thread from a JNIEnv.
35static inline Thread* ThreadForEnv(JNIEnv* env) {
36 JNIEnvExt* full_env(down_cast<JNIEnvExt*>(env));
Ian Rogers55256cb2017-12-21 17:07:11 -080037 return full_env->GetSelf();
Ian Rogers1eb512d2013-10-18 15:42:20 -070038}
39
Ian Rogers7b078e82014-09-10 14:44:24 -070040inline void Thread::AllowThreadSuspension() {
41 DCHECK_EQ(Thread::Current(), this);
42 if (UNLIKELY(TestAllFlags())) {
43 CheckSuspend();
44 }
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070045 // Invalidate the current thread's object pointers (ObjPtr) to catch possible moving GC bugs due
46 // to missing handles.
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070047 PoisonObjectPointers();
Ian Rogers7b078e82014-09-10 14:44:24 -070048}
49
50inline void Thread::CheckSuspend() {
51 DCHECK_EQ(Thread::Current(), this);
52 for (;;) {
53 if (ReadFlag(kCheckpointRequest)) {
54 RunCheckpointFunction();
55 } else if (ReadFlag(kSuspendRequest)) {
56 FullSuspendCheck();
Hiroshi Yamauchi30493242016-11-03 13:06:52 -070057 } else if (ReadFlag(kEmptyCheckpointRequest)) {
58 RunEmptyCheckpoint();
59 } else {
60 break;
61 }
62 }
63}
64
Hiroshi Yamauchia2224042017-02-08 16:35:45 -080065inline void Thread::CheckEmptyCheckpointFromWeakRefAccess(BaseMutex* cond_var_mutex) {
66 Thread* self = Thread::Current();
67 DCHECK_EQ(self, this);
68 for (;;) {
69 if (ReadFlag(kEmptyCheckpointRequest)) {
70 RunEmptyCheckpoint();
71 // Check we hold only an expected mutex when accessing weak ref.
72 if (kIsDebugBuild) {
73 for (int i = kLockLevelCount - 1; i >= 0; --i) {
74 BaseMutex* held_mutex = self->GetHeldMutex(static_cast<LockLevel>(i));
75 if (held_mutex != nullptr &&
76 held_mutex != Locks::mutator_lock_ &&
77 held_mutex != cond_var_mutex) {
Hiroshi Yamauchi8a433242017-03-07 14:39:22 -080078 CHECK(Locks::IsExpectedOnWeakRefAccess(held_mutex))
Hiroshi Yamauchia2224042017-02-08 16:35:45 -080079 << "Holding unexpected mutex " << held_mutex->GetName()
80 << " when accessing weak ref";
81 }
82 }
83 }
84 } else {
85 break;
86 }
87 }
88}
89
90inline void Thread::CheckEmptyCheckpointFromMutex() {
Hiroshi Yamauchi30493242016-11-03 13:06:52 -070091 DCHECK_EQ(Thread::Current(), this);
92 for (;;) {
93 if (ReadFlag(kEmptyCheckpointRequest)) {
94 RunEmptyCheckpoint();
Ian Rogers7b078e82014-09-10 14:44:24 -070095 } else {
96 break;
97 }
98 }
99}
100
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800101inline ThreadState Thread::SetState(ThreadState new_state) {
Yu Lieac44242015-06-29 10:50:03 +0800102 // Should only be used to change between suspended states.
103 // Cannot use this code to change into or from Runnable as changing to Runnable should
104 // fail if old_state_and_flags.suspend_request is true and changing from Runnable might
105 // miss passing an active suspend barrier.
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800106 DCHECK_NE(new_state, kRunnable);
Andreas Gampeef048f62014-11-25 22:12:27 -0800107 if (kIsDebugBuild && this != Thread::Current()) {
108 std::string name;
109 GetThreadName(name);
110 LOG(FATAL) << "Thread \"" << name << "\"(" << this << " != Thread::Current()="
111 << Thread::Current() << ") changing state to " << new_state;
112 }
Chris Dearman59cde532013-12-04 18:53:49 -0800113 union StateAndFlags old_state_and_flags;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700114 old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
Yu Lieac44242015-06-29 10:50:03 +0800115 CHECK_NE(old_state_and_flags.as_struct.state, kRunnable);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700116 tls32_.state_and_flags.as_struct.state = new_state;
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800117 return static_cast<ThreadState>(old_state_and_flags.as_struct.state);
118}
119
Mathieu Chartier10b218d2016-07-25 17:48:52 -0700120inline bool Thread::IsThreadSuspensionAllowable() const {
121 if (tls32_.no_thread_suspension != 0) {
122 return false;
123 }
124 for (int i = kLockLevelCount - 1; i >= 0; --i) {
Alex Light88fd7202017-06-30 08:31:59 -0700125 if (i != kMutatorLock &&
126 i != kUserCodeSuspensionLock &&
127 GetHeldMutex(static_cast<LockLevel>(i)) != nullptr) {
Mathieu Chartier10b218d2016-07-25 17:48:52 -0700128 return false;
129 }
130 }
Alex Light88fd7202017-06-30 08:31:59 -0700131 // Thread autoanalysis isn't able to understand that the GetHeldMutex(...) or AssertHeld means we
132 // have the mutex meaning we need to do this hack.
133 auto is_suspending_for_user_code = [this]() NO_THREAD_SAFETY_ANALYSIS {
134 return tls32_.user_code_suspend_count != 0;
135 };
136 if (GetHeldMutex(kUserCodeSuspensionLock) != nullptr && is_suspending_for_user_code()) {
137 return false;
138 }
Mathieu Chartier10b218d2016-07-25 17:48:52 -0700139 return true;
140}
141
Ian Rogers693ff612013-02-01 10:56:12 -0800142inline void Thread::AssertThreadSuspensionIsAllowable(bool check_locks) const {
Ian Rogersf3d874c2014-07-17 18:52:42 -0700143 if (kIsDebugBuild) {
Nicolas Geoffraydb978712014-12-09 13:33:38 +0000144 if (gAborting == 0) {
145 CHECK_EQ(0u, tls32_.no_thread_suspension) << tlsPtr_.last_no_thread_suspension_cause;
146 }
Ian Rogersf3d874c2014-07-17 18:52:42 -0700147 if (check_locks) {
148 bool bad_mutexes_held = false;
149 for (int i = kLockLevelCount - 1; i >= 0; --i) {
Alex Light88fd7202017-06-30 08:31:59 -0700150 // We expect no locks except the mutator_lock_. User code suspension lock is OK as long as
151 // we aren't going to be held suspended due to SuspendReason::kForUserCode.
152 if (i != kMutatorLock && i != kUserCodeSuspensionLock) {
Ian Rogersf3d874c2014-07-17 18:52:42 -0700153 BaseMutex* held_mutex = GetHeldMutex(static_cast<LockLevel>(i));
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700154 if (held_mutex != nullptr) {
Ian Rogersf3d874c2014-07-17 18:52:42 -0700155 LOG(ERROR) << "holding \"" << held_mutex->GetName()
156 << "\" at point where thread suspension is expected";
157 bad_mutexes_held = true;
158 }
Ian Rogers693ff612013-02-01 10:56:12 -0800159 }
160 }
Alex Light88fd7202017-06-30 08:31:59 -0700161 // Make sure that if we hold the user_code_suspension_lock_ we aren't suspending due to
162 // user_code_suspend_count which would prevent the thread from ever waking up. Thread
163 // autoanalysis isn't able to understand that the GetHeldMutex(...) or AssertHeld means we
164 // have the mutex meaning we need to do this hack.
165 auto is_suspending_for_user_code = [this]() NO_THREAD_SAFETY_ANALYSIS {
166 return tls32_.user_code_suspend_count != 0;
167 };
168 if (GetHeldMutex(kUserCodeSuspensionLock) != nullptr && is_suspending_for_user_code()) {
169 LOG(ERROR) << "suspending due to user-code while holding \""
170 << Locks::user_code_suspension_lock_->GetName() << "\"! Thread would never "
171 << "wake up.";
172 bad_mutexes_held = true;
173 }
Nicolas Geoffraydb978712014-12-09 13:33:38 +0000174 if (gAborting == 0) {
175 CHECK(!bad_mutexes_held);
176 }
Ian Rogers693ff612013-02-01 10:56:12 -0800177 }
Ian Rogers693ff612013-02-01 10:56:12 -0800178 }
Ian Rogers693ff612013-02-01 10:56:12 -0800179}
180
Mathieu Chartier8ac9c912015-10-01 15:58:41 -0700181inline void Thread::TransitionToSuspendedAndRunCheckpoints(ThreadState new_state) {
Ian Rogers693ff612013-02-01 10:56:12 -0800182 DCHECK_NE(new_state, kRunnable);
Ian Rogers693ff612013-02-01 10:56:12 -0800183 DCHECK_EQ(GetState(), kRunnable);
184 union StateAndFlags old_state_and_flags;
185 union StateAndFlags new_state_and_flags;
Dave Allison0f5f6bb2013-11-22 17:39:19 -0800186 while (true) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700187 old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700188 if (UNLIKELY((old_state_and_flags.as_struct.flags & kCheckpointRequest) != 0)) {
189 RunCheckpointFunction();
190 continue;
191 }
Hiroshi Yamauchi30493242016-11-03 13:06:52 -0700192 if (UNLIKELY((old_state_and_flags.as_struct.flags & kEmptyCheckpointRequest) != 0)) {
193 RunEmptyCheckpoint();
194 continue;
195 }
Dave Allison0f5f6bb2013-11-22 17:39:19 -0800196 // Change the state but keep the current flags (kCheckpointRequest is clear).
197 DCHECK_EQ((old_state_and_flags.as_struct.flags & kCheckpointRequest), 0);
Hiroshi Yamauchi30493242016-11-03 13:06:52 -0700198 DCHECK_EQ((old_state_and_flags.as_struct.flags & kEmptyCheckpointRequest), 0);
Dave Allison0f5f6bb2013-11-22 17:39:19 -0800199 new_state_and_flags.as_struct.flags = old_state_and_flags.as_struct.flags;
Ian Rogers693ff612013-02-01 10:56:12 -0800200 new_state_and_flags.as_struct.state = new_state;
Ian Rogersb8e087e2014-07-09 21:12:06 -0700201
Yu Lieac44242015-06-29 10:50:03 +0800202 // CAS the value with a memory ordering.
Ian Rogersb8e087e2014-07-09 21:12:06 -0700203 bool done =
Orion Hodson4557b382018-01-03 11:47:54 +0000204 tls32_.state_and_flags.as_atomic_int.CompareAndSetWeakRelease(old_state_and_flags.as_int,
Ian Rogersb8e087e2014-07-09 21:12:06 -0700205 new_state_and_flags.as_int);
206 if (LIKELY(done)) {
Dave Allison0f5f6bb2013-11-22 17:39:19 -0800207 break;
208 }
209 }
Mathieu Chartier8ac9c912015-10-01 15:58:41 -0700210}
Yu Lieac44242015-06-29 10:50:03 +0800211
Mathieu Chartier8ac9c912015-10-01 15:58:41 -0700212inline void Thread::PassActiveSuspendBarriers() {
Yu Lieac44242015-06-29 10:50:03 +0800213 while (true) {
214 uint16_t current_flags = tls32_.state_and_flags.as_struct.flags;
Hiroshi Yamauchi30493242016-11-03 13:06:52 -0700215 if (LIKELY((current_flags &
216 (kCheckpointRequest | kEmptyCheckpointRequest | kActiveSuspendBarrier)) == 0)) {
Yu Lieac44242015-06-29 10:50:03 +0800217 break;
218 } else if ((current_flags & kActiveSuspendBarrier) != 0) {
219 PassActiveSuspendBarriers(this);
220 } else {
221 // Impossible
Mathieu Chartier8ac9c912015-10-01 15:58:41 -0700222 LOG(FATAL) << "Fatal, thread transitioned into suspended without running the checkpoint";
Yu Lieac44242015-06-29 10:50:03 +0800223 }
224 }
Ian Rogers693ff612013-02-01 10:56:12 -0800225}
226
Mathieu Chartier8ac9c912015-10-01 15:58:41 -0700227inline void Thread::TransitionFromRunnableToSuspended(ThreadState new_state) {
228 AssertThreadSuspensionIsAllowable();
Mathieu Chartier3398c782016-09-30 10:27:43 -0700229 PoisonObjectPointersIfDebug();
Mathieu Chartier8ac9c912015-10-01 15:58:41 -0700230 DCHECK_EQ(this, Thread::Current());
231 // Change to non-runnable state, thereby appearing suspended to the system.
232 TransitionToSuspendedAndRunCheckpoints(new_state);
233 // Mark the release of the share of the mutator_lock_.
234 Locks::mutator_lock_->TransitionFromRunnableToSuspended(this);
235 // Once suspended - check the active suspend barrier flag
236 PassActiveSuspendBarriers();
237}
238
Ian Rogers693ff612013-02-01 10:56:12 -0800239inline ThreadState Thread::TransitionFromSuspendedToRunnable() {
Chris Dearman59cde532013-12-04 18:53:49 -0800240 union StateAndFlags old_state_and_flags;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700241 old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
Ian Rogers693ff612013-02-01 10:56:12 -0800242 int16_t old_state = old_state_and_flags.as_struct.state;
243 DCHECK_NE(static_cast<ThreadState>(old_state), kRunnable);
244 do {
245 Locks::mutator_lock_->AssertNotHeld(this); // Otherwise we starve GC..
Ian Rogersdd7624d2014-03-14 17:43:00 -0700246 old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
Ian Rogers693ff612013-02-01 10:56:12 -0800247 DCHECK_EQ(old_state_and_flags.as_struct.state, old_state);
Yu Lieac44242015-06-29 10:50:03 +0800248 if (LIKELY(old_state_and_flags.as_struct.flags == 0)) {
249 // Optimize for the return from native code case - this is the fast path.
250 // Atomically change from suspended to runnable if no suspend request pending.
251 union StateAndFlags new_state_and_flags;
252 new_state_and_flags.as_int = old_state_and_flags.as_int;
253 new_state_and_flags.as_struct.state = kRunnable;
254 // CAS the value with a memory barrier.
Orion Hodson4557b382018-01-03 11:47:54 +0000255 if (LIKELY(tls32_.state_and_flags.as_atomic_int.CompareAndSetWeakAcquire(
Yu Lieac44242015-06-29 10:50:03 +0800256 old_state_and_flags.as_int,
257 new_state_and_flags.as_int))) {
258 // Mark the acquisition of a share of the mutator_lock_.
259 Locks::mutator_lock_->TransitionFromSuspendedToRunnable(this);
260 break;
261 }
262 } else if ((old_state_and_flags.as_struct.flags & kActiveSuspendBarrier) != 0) {
263 PassActiveSuspendBarriers(this);
Hiroshi Yamauchi30493242016-11-03 13:06:52 -0700264 } else if ((old_state_and_flags.as_struct.flags &
265 (kCheckpointRequest | kEmptyCheckpointRequest)) != 0) {
Yu Lieac44242015-06-29 10:50:03 +0800266 // Impossible
Mathieu Chartierdabdccc2015-10-01 14:46:29 -0700267 LOG(FATAL) << "Transitioning to runnable with checkpoint flag, "
268 << " flags=" << old_state_and_flags.as_struct.flags
269 << " state=" << old_state_and_flags.as_struct.state;
Yu Lieac44242015-06-29 10:50:03 +0800270 } else if ((old_state_and_flags.as_struct.flags & kSuspendRequest) != 0) {
Ian Rogers693ff612013-02-01 10:56:12 -0800271 // Wait while our suspend count is non-zero.
Nicolas Geoffray9f5f8ac2016-06-29 14:39:59 +0100272
273 // We pass null to the MutexLock as we may be in a situation where the
274 // runtime is shutting down. Guarding ourselves from that situation
275 // requires to take the shutdown lock, which is undesirable here.
276 Thread* thread_to_pass = nullptr;
277 if (kIsDebugBuild && !IsDaemon()) {
278 // We know we can make our debug locking checks on non-daemon threads,
279 // so re-enable them on debug builds.
280 thread_to_pass = this;
281 }
282 MutexLock mu(thread_to_pass, *Locks::thread_suspend_count_lock_);
Hiroshi Yamauchiee235822016-08-19 17:03:27 -0700283 ScopedTransitioningToRunnable scoped_transitioning_to_runnable(this);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700284 old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
Ian Rogers693ff612013-02-01 10:56:12 -0800285 DCHECK_EQ(old_state_and_flags.as_struct.state, old_state);
286 while ((old_state_and_flags.as_struct.flags & kSuspendRequest) != 0) {
287 // Re-check when Thread::resume_cond_ is notified.
Nicolas Geoffray9f5f8ac2016-06-29 14:39:59 +0100288 Thread::resume_cond_->Wait(thread_to_pass);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700289 old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
Ian Rogers693ff612013-02-01 10:56:12 -0800290 DCHECK_EQ(old_state_and_flags.as_struct.state, old_state);
291 }
292 DCHECK_EQ(GetSuspendCount(), 0);
293 }
Ian Rogers719d1a32014-03-06 12:13:39 -0800294 } while (true);
Yu Lieac44242015-06-29 10:50:03 +0800295 // Run the flip function, if set.
296 Closure* flip_func = GetFlipFunction();
297 if (flip_func != nullptr) {
298 flip_func->Run(this);
299 }
300 return static_cast<ThreadState>(old_state);
Ian Rogers693ff612013-02-01 10:56:12 -0800301}
302
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800303inline mirror::Object* Thread::AllocTlab(size_t bytes) {
304 DCHECK_GE(TlabSize(), bytes);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700305 ++tlsPtr_.thread_local_objects;
306 mirror::Object* ret = reinterpret_cast<mirror::Object*>(tlsPtr_.thread_local_pos);
307 tlsPtr_.thread_local_pos += bytes;
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800308 return ret;
309}
310
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800311inline bool Thread::PushOnThreadLocalAllocationStack(mirror::Object* obj) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700312 DCHECK_LE(tlsPtr_.thread_local_alloc_stack_top, tlsPtr_.thread_local_alloc_stack_end);
313 if (tlsPtr_.thread_local_alloc_stack_top < tlsPtr_.thread_local_alloc_stack_end) {
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800314 // There's room.
Ian Rogers13735952014-10-08 12:43:28 -0700315 DCHECK_LE(reinterpret_cast<uint8_t*>(tlsPtr_.thread_local_alloc_stack_top) +
Mathieu Chartiercb535da2015-01-23 13:50:03 -0800316 sizeof(StackReference<mirror::Object>),
Ian Rogers13735952014-10-08 12:43:28 -0700317 reinterpret_cast<uint8_t*>(tlsPtr_.thread_local_alloc_stack_end));
Mathieu Chartiercb535da2015-01-23 13:50:03 -0800318 DCHECK(tlsPtr_.thread_local_alloc_stack_top->AsMirrorPtr() == nullptr);
319 tlsPtr_.thread_local_alloc_stack_top->Assign(obj);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700320 ++tlsPtr_.thread_local_alloc_stack_top;
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800321 return true;
322 }
323 return false;
324}
325
Mathieu Chartiercb535da2015-01-23 13:50:03 -0800326inline void Thread::SetThreadLocalAllocationStack(StackReference<mirror::Object>* start,
327 StackReference<mirror::Object>* end) {
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800328 DCHECK(Thread::Current() == this) << "Should be called by self";
329 DCHECK(start != nullptr);
330 DCHECK(end != nullptr);
Mathieu Chartiercb535da2015-01-23 13:50:03 -0800331 DCHECK_ALIGNED(start, sizeof(StackReference<mirror::Object>));
332 DCHECK_ALIGNED(end, sizeof(StackReference<mirror::Object>));
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800333 DCHECK_LT(start, end);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700334 tlsPtr_.thread_local_alloc_stack_end = end;
335 tlsPtr_.thread_local_alloc_stack_top = start;
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800336}
337
338inline void Thread::RevokeThreadLocalAllocationStack() {
339 if (kIsDebugBuild) {
340 // Note: self is not necessarily equal to this thread since thread may be suspended.
341 Thread* self = Thread::Current();
342 DCHECK(this == self || IsSuspended() || GetState() == kWaitingPerformingGc)
343 << GetState() << " thread " << this << " self " << self;
344 }
Ian Rogersdd7624d2014-03-14 17:43:00 -0700345 tlsPtr_.thread_local_alloc_stack_end = nullptr;
346 tlsPtr_.thread_local_alloc_stack_top = nullptr;
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800347}
348
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700349inline void Thread::PoisonObjectPointersIfDebug() {
Andreas Gampec73cb642017-02-22 10:11:30 -0800350 if (kObjPtrPoisoning) {
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700351 Thread::Current()->PoisonObjectPointers();
352 }
353}
354
Hiroshi Yamauchi02e7f1a2016-10-03 15:32:01 -0700355inline bool Thread::ModifySuspendCount(Thread* self,
356 int delta,
357 AtomicInteger* suspend_barrier,
Alex Light46f93402017-06-29 11:59:50 -0700358 SuspendReason reason) {
Hiroshi Yamauchi02e7f1a2016-10-03 15:32:01 -0700359 if (delta > 0 && ((kUseReadBarrier && this != self) || suspend_barrier != nullptr)) {
360 // When delta > 0 (requesting a suspend), ModifySuspendCountInternal() may fail either if
361 // active_suspend_barriers is full or we are in the middle of a thread flip. Retry in a loop.
362 while (true) {
Alex Light46f93402017-06-29 11:59:50 -0700363 if (LIKELY(ModifySuspendCountInternal(self, delta, suspend_barrier, reason))) {
Hiroshi Yamauchi02e7f1a2016-10-03 15:32:01 -0700364 return true;
365 } else {
366 // Failure means the list of active_suspend_barriers is full or we are in the middle of a
367 // thread flip, we should release the thread_suspend_count_lock_ (to avoid deadlock) and
368 // wait till the target thread has executed or Thread::PassActiveSuspendBarriers() or the
369 // flip function. Note that we could not simply wait for the thread to change to a suspended
370 // state, because it might need to run checkpoint function before the state change or
371 // resumes from the resume_cond_, which also needs thread_suspend_count_lock_.
372 //
373 // The list of active_suspend_barriers is very unlikely to be full since more than
374 // kMaxSuspendBarriers threads need to execute SuspendAllInternal() simultaneously, and
375 // target thread stays in kRunnable in the mean time.
376 Locks::thread_suspend_count_lock_->ExclusiveUnlock(self);
377 NanoSleep(100000);
378 Locks::thread_suspend_count_lock_->ExclusiveLock(self);
379 }
380 }
381 } else {
Alex Light46f93402017-06-29 11:59:50 -0700382 return ModifySuspendCountInternal(self, delta, suspend_barrier, reason);
Hiroshi Yamauchi02e7f1a2016-10-03 15:32:01 -0700383 }
384}
385
Andreas Gampe513061a2017-06-01 09:17:34 -0700386inline ShadowFrame* Thread::PushShadowFrame(ShadowFrame* new_top_frame) {
387 return tlsPtr_.managed_stack.PushShadowFrame(new_top_frame);
388}
389
390inline ShadowFrame* Thread::PopShadowFrame() {
391 return tlsPtr_.managed_stack.PopShadowFrame();
392}
393
Ian Rogers693ff612013-02-01 10:56:12 -0800394} // namespace art
395
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700396#endif // ART_RUNTIME_THREAD_INL_H_