blob: 02a1e4d8a5bc00323e5d4e33db232bc9c8290e86 [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
Bilyan Borisovbb661c02016-04-04 16:27:32 +010022#ifdef ART_TARGET_ANDROID
Andreas Gampe4382f1e2015-08-05 01:08:53 +000023#include <bionic_tls.h> // Access to our own TLS slot.
24#endif
25
Ian Rogers02ed4c02013-09-06 13:10:04 -070026#include <pthread.h>
27
Ian Rogers1eb512d2013-10-18 15:42:20 -070028#include "base/casts.h"
Ian Rogers693ff612013-02-01 10:56:12 -080029#include "base/mutex-inl.h"
Ian Rogers576ca0c2014-06-06 15:58:22 -070030#include "gc/heap.h"
Ian Rogers68d8b422014-07-17 11:09:10 -070031#include "jni_env_ext.h"
Andreas Gampec73cb642017-02-22 10:11:30 -080032#include "obj_ptr.h"
David Sehrf42eb2c2016-10-19 13:20:45 -070033#include "runtime.h"
Mathieu Chartier3cf22532015-07-09 15:15:09 -070034#include "thread_pool.h"
Ian Rogers693ff612013-02-01 10:56:12 -080035
36namespace art {
37
Ian Rogers1eb512d2013-10-18 15:42:20 -070038// Quickly access the current thread from a JNIEnv.
39static inline Thread* ThreadForEnv(JNIEnv* env) {
40 JNIEnvExt* full_env(down_cast<JNIEnvExt*>(env));
41 return full_env->self;
42}
43
Ian Rogers02ed4c02013-09-06 13:10:04 -070044inline Thread* Thread::Current() {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070045 // We rely on Thread::Current returning null for a detached thread, so it's not obvious
Ian Rogers02ed4c02013-09-06 13:10:04 -070046 // that we can replace this with a direct %fs access on x86.
47 if (!is_started_) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070048 return nullptr;
Ian Rogers02ed4c02013-09-06 13:10:04 -070049 } else {
Bilyan Borisovbb661c02016-04-04 16:27:32 +010050#ifdef ART_TARGET_ANDROID
Andreas Gampe4382f1e2015-08-05 01:08:53 +000051 void* thread = __get_tls()[TLS_SLOT_ART_THREAD_SELF];
52#else
Ian Rogers02ed4c02013-09-06 13:10:04 -070053 void* thread = pthread_getspecific(Thread::pthread_key_self_);
Andreas Gampe4382f1e2015-08-05 01:08:53 +000054#endif
Ian Rogers02ed4c02013-09-06 13:10:04 -070055 return reinterpret_cast<Thread*>(thread);
56 }
57}
58
Ian Rogers7b078e82014-09-10 14:44:24 -070059inline void Thread::AllowThreadSuspension() {
60 DCHECK_EQ(Thread::Current(), this);
61 if (UNLIKELY(TestAllFlags())) {
62 CheckSuspend();
63 }
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070064 // Invalidate the current thread's object pointers (ObjPtr) to catch possible moving GC bugs due
65 // to missing handles.
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070066 PoisonObjectPointers();
Ian Rogers7b078e82014-09-10 14:44:24 -070067}
68
69inline void Thread::CheckSuspend() {
70 DCHECK_EQ(Thread::Current(), this);
71 for (;;) {
72 if (ReadFlag(kCheckpointRequest)) {
73 RunCheckpointFunction();
74 } else if (ReadFlag(kSuspendRequest)) {
75 FullSuspendCheck();
Hiroshi Yamauchi30493242016-11-03 13:06:52 -070076 } else if (ReadFlag(kEmptyCheckpointRequest)) {
77 RunEmptyCheckpoint();
78 } else {
79 break;
80 }
81 }
82}
83
Hiroshi Yamauchia2224042017-02-08 16:35:45 -080084inline void Thread::CheckEmptyCheckpointFromWeakRefAccess(BaseMutex* cond_var_mutex) {
85 Thread* self = Thread::Current();
86 DCHECK_EQ(self, this);
87 for (;;) {
88 if (ReadFlag(kEmptyCheckpointRequest)) {
89 RunEmptyCheckpoint();
90 // Check we hold only an expected mutex when accessing weak ref.
91 if (kIsDebugBuild) {
92 for (int i = kLockLevelCount - 1; i >= 0; --i) {
93 BaseMutex* held_mutex = self->GetHeldMutex(static_cast<LockLevel>(i));
94 if (held_mutex != nullptr &&
95 held_mutex != Locks::mutator_lock_ &&
96 held_mutex != cond_var_mutex) {
Hiroshi Yamauchi8a433242017-03-07 14:39:22 -080097 CHECK(Locks::IsExpectedOnWeakRefAccess(held_mutex))
Hiroshi Yamauchia2224042017-02-08 16:35:45 -080098 << "Holding unexpected mutex " << held_mutex->GetName()
99 << " when accessing weak ref";
100 }
101 }
102 }
103 } else {
104 break;
105 }
106 }
107}
108
109inline void Thread::CheckEmptyCheckpointFromMutex() {
Hiroshi Yamauchi30493242016-11-03 13:06:52 -0700110 DCHECK_EQ(Thread::Current(), this);
111 for (;;) {
112 if (ReadFlag(kEmptyCheckpointRequest)) {
113 RunEmptyCheckpoint();
Ian Rogers7b078e82014-09-10 14:44:24 -0700114 } else {
115 break;
116 }
117 }
118}
119
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800120inline ThreadState Thread::SetState(ThreadState new_state) {
Yu Lieac44242015-06-29 10:50:03 +0800121 // Should only be used to change between suspended states.
122 // Cannot use this code to change into or from Runnable as changing to Runnable should
123 // fail if old_state_and_flags.suspend_request is true and changing from Runnable might
124 // miss passing an active suspend barrier.
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800125 DCHECK_NE(new_state, kRunnable);
Andreas Gampeef048f62014-11-25 22:12:27 -0800126 if (kIsDebugBuild && this != Thread::Current()) {
127 std::string name;
128 GetThreadName(name);
129 LOG(FATAL) << "Thread \"" << name << "\"(" << this << " != Thread::Current()="
130 << Thread::Current() << ") changing state to " << new_state;
131 }
Chris Dearman59cde532013-12-04 18:53:49 -0800132 union StateAndFlags old_state_and_flags;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700133 old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
Yu Lieac44242015-06-29 10:50:03 +0800134 CHECK_NE(old_state_and_flags.as_struct.state, kRunnable);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700135 tls32_.state_and_flags.as_struct.state = new_state;
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800136 return static_cast<ThreadState>(old_state_and_flags.as_struct.state);
137}
138
Mathieu Chartier10b218d2016-07-25 17:48:52 -0700139inline bool Thread::IsThreadSuspensionAllowable() const {
140 if (tls32_.no_thread_suspension != 0) {
141 return false;
142 }
143 for (int i = kLockLevelCount - 1; i >= 0; --i) {
144 if (i != kMutatorLock && GetHeldMutex(static_cast<LockLevel>(i)) != nullptr) {
145 return false;
146 }
147 }
148 return true;
149}
150
Ian Rogers693ff612013-02-01 10:56:12 -0800151inline void Thread::AssertThreadSuspensionIsAllowable(bool check_locks) const {
Ian Rogersf3d874c2014-07-17 18:52:42 -0700152 if (kIsDebugBuild) {
Nicolas Geoffraydb978712014-12-09 13:33:38 +0000153 if (gAborting == 0) {
154 CHECK_EQ(0u, tls32_.no_thread_suspension) << tlsPtr_.last_no_thread_suspension_cause;
155 }
Ian Rogersf3d874c2014-07-17 18:52:42 -0700156 if (check_locks) {
157 bool bad_mutexes_held = false;
158 for (int i = kLockLevelCount - 1; i >= 0; --i) {
159 // We expect no locks except the mutator_lock_ or thread list suspend thread lock.
Ian Rogers4ad5cd32014-11-11 23:08:07 -0800160 if (i != kMutatorLock) {
Ian Rogersf3d874c2014-07-17 18:52:42 -0700161 BaseMutex* held_mutex = GetHeldMutex(static_cast<LockLevel>(i));
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700162 if (held_mutex != nullptr) {
Ian Rogersf3d874c2014-07-17 18:52:42 -0700163 LOG(ERROR) << "holding \"" << held_mutex->GetName()
164 << "\" at point where thread suspension is expected";
165 bad_mutexes_held = true;
166 }
Ian Rogers693ff612013-02-01 10:56:12 -0800167 }
168 }
Nicolas Geoffraydb978712014-12-09 13:33:38 +0000169 if (gAborting == 0) {
170 CHECK(!bad_mutexes_held);
171 }
Ian Rogers693ff612013-02-01 10:56:12 -0800172 }
Ian Rogers693ff612013-02-01 10:56:12 -0800173 }
Ian Rogers693ff612013-02-01 10:56:12 -0800174}
175
Mathieu Chartier8ac9c912015-10-01 15:58:41 -0700176inline void Thread::TransitionToSuspendedAndRunCheckpoints(ThreadState new_state) {
Ian Rogers693ff612013-02-01 10:56:12 -0800177 DCHECK_NE(new_state, kRunnable);
Ian Rogers693ff612013-02-01 10:56:12 -0800178 DCHECK_EQ(GetState(), kRunnable);
179 union StateAndFlags old_state_and_flags;
180 union StateAndFlags new_state_and_flags;
Dave Allison0f5f6bb2013-11-22 17:39:19 -0800181 while (true) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700182 old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700183 if (UNLIKELY((old_state_and_flags.as_struct.flags & kCheckpointRequest) != 0)) {
184 RunCheckpointFunction();
185 continue;
186 }
Hiroshi Yamauchi30493242016-11-03 13:06:52 -0700187 if (UNLIKELY((old_state_and_flags.as_struct.flags & kEmptyCheckpointRequest) != 0)) {
188 RunEmptyCheckpoint();
189 continue;
190 }
Dave Allison0f5f6bb2013-11-22 17:39:19 -0800191 // Change the state but keep the current flags (kCheckpointRequest is clear).
192 DCHECK_EQ((old_state_and_flags.as_struct.flags & kCheckpointRequest), 0);
Hiroshi Yamauchi30493242016-11-03 13:06:52 -0700193 DCHECK_EQ((old_state_and_flags.as_struct.flags & kEmptyCheckpointRequest), 0);
Dave Allison0f5f6bb2013-11-22 17:39:19 -0800194 new_state_and_flags.as_struct.flags = old_state_and_flags.as_struct.flags;
Ian Rogers693ff612013-02-01 10:56:12 -0800195 new_state_and_flags.as_struct.state = new_state;
Ian Rogersb8e087e2014-07-09 21:12:06 -0700196
Yu Lieac44242015-06-29 10:50:03 +0800197 // CAS the value with a memory ordering.
Ian Rogersb8e087e2014-07-09 21:12:06 -0700198 bool done =
Yu Lieac44242015-06-29 10:50:03 +0800199 tls32_.state_and_flags.as_atomic_int.CompareExchangeWeakRelease(old_state_and_flags.as_int,
Ian Rogersb8e087e2014-07-09 21:12:06 -0700200 new_state_and_flags.as_int);
201 if (LIKELY(done)) {
Dave Allison0f5f6bb2013-11-22 17:39:19 -0800202 break;
203 }
204 }
Mathieu Chartier8ac9c912015-10-01 15:58:41 -0700205}
Yu Lieac44242015-06-29 10:50:03 +0800206
Mathieu Chartier8ac9c912015-10-01 15:58:41 -0700207inline void Thread::PassActiveSuspendBarriers() {
Yu Lieac44242015-06-29 10:50:03 +0800208 while (true) {
209 uint16_t current_flags = tls32_.state_and_flags.as_struct.flags;
Hiroshi Yamauchi30493242016-11-03 13:06:52 -0700210 if (LIKELY((current_flags &
211 (kCheckpointRequest | kEmptyCheckpointRequest | kActiveSuspendBarrier)) == 0)) {
Yu Lieac44242015-06-29 10:50:03 +0800212 break;
213 } else if ((current_flags & kActiveSuspendBarrier) != 0) {
214 PassActiveSuspendBarriers(this);
215 } else {
216 // Impossible
Mathieu Chartier8ac9c912015-10-01 15:58:41 -0700217 LOG(FATAL) << "Fatal, thread transitioned into suspended without running the checkpoint";
Yu Lieac44242015-06-29 10:50:03 +0800218 }
219 }
Ian Rogers693ff612013-02-01 10:56:12 -0800220}
221
Mathieu Chartier8ac9c912015-10-01 15:58:41 -0700222inline void Thread::TransitionFromRunnableToSuspended(ThreadState new_state) {
223 AssertThreadSuspensionIsAllowable();
Mathieu Chartier3398c782016-09-30 10:27:43 -0700224 PoisonObjectPointersIfDebug();
Mathieu Chartier8ac9c912015-10-01 15:58:41 -0700225 DCHECK_EQ(this, Thread::Current());
226 // Change to non-runnable state, thereby appearing suspended to the system.
227 TransitionToSuspendedAndRunCheckpoints(new_state);
228 // Mark the release of the share of the mutator_lock_.
229 Locks::mutator_lock_->TransitionFromRunnableToSuspended(this);
230 // Once suspended - check the active suspend barrier flag
231 PassActiveSuspendBarriers();
232}
233
Ian Rogers693ff612013-02-01 10:56:12 -0800234inline ThreadState Thread::TransitionFromSuspendedToRunnable() {
Chris Dearman59cde532013-12-04 18:53:49 -0800235 union StateAndFlags old_state_and_flags;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700236 old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
Ian Rogers693ff612013-02-01 10:56:12 -0800237 int16_t old_state = old_state_and_flags.as_struct.state;
238 DCHECK_NE(static_cast<ThreadState>(old_state), kRunnable);
239 do {
240 Locks::mutator_lock_->AssertNotHeld(this); // Otherwise we starve GC..
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 DCHECK_EQ(old_state_and_flags.as_struct.state, old_state);
Yu Lieac44242015-06-29 10:50:03 +0800243 if (LIKELY(old_state_and_flags.as_struct.flags == 0)) {
244 // Optimize for the return from native code case - this is the fast path.
245 // Atomically change from suspended to runnable if no suspend request pending.
246 union StateAndFlags new_state_and_flags;
247 new_state_and_flags.as_int = old_state_and_flags.as_int;
248 new_state_and_flags.as_struct.state = kRunnable;
249 // CAS the value with a memory barrier.
250 if (LIKELY(tls32_.state_and_flags.as_atomic_int.CompareExchangeWeakAcquire(
251 old_state_and_flags.as_int,
252 new_state_and_flags.as_int))) {
253 // Mark the acquisition of a share of the mutator_lock_.
254 Locks::mutator_lock_->TransitionFromSuspendedToRunnable(this);
255 break;
256 }
257 } else if ((old_state_and_flags.as_struct.flags & kActiveSuspendBarrier) != 0) {
258 PassActiveSuspendBarriers(this);
Hiroshi Yamauchi30493242016-11-03 13:06:52 -0700259 } else if ((old_state_and_flags.as_struct.flags &
260 (kCheckpointRequest | kEmptyCheckpointRequest)) != 0) {
Yu Lieac44242015-06-29 10:50:03 +0800261 // Impossible
Mathieu Chartierdabdccc2015-10-01 14:46:29 -0700262 LOG(FATAL) << "Transitioning to runnable with checkpoint flag, "
263 << " flags=" << old_state_and_flags.as_struct.flags
264 << " state=" << old_state_and_flags.as_struct.state;
Yu Lieac44242015-06-29 10:50:03 +0800265 } else if ((old_state_and_flags.as_struct.flags & kSuspendRequest) != 0) {
Ian Rogers693ff612013-02-01 10:56:12 -0800266 // Wait while our suspend count is non-zero.
Nicolas Geoffray9f5f8ac2016-06-29 14:39:59 +0100267
268 // We pass null to the MutexLock as we may be in a situation where the
269 // runtime is shutting down. Guarding ourselves from that situation
270 // requires to take the shutdown lock, which is undesirable here.
271 Thread* thread_to_pass = nullptr;
272 if (kIsDebugBuild && !IsDaemon()) {
273 // We know we can make our debug locking checks on non-daemon threads,
274 // so re-enable them on debug builds.
275 thread_to_pass = this;
276 }
277 MutexLock mu(thread_to_pass, *Locks::thread_suspend_count_lock_);
Hiroshi Yamauchiee235822016-08-19 17:03:27 -0700278 ScopedTransitioningToRunnable scoped_transitioning_to_runnable(this);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700279 old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
Ian Rogers693ff612013-02-01 10:56:12 -0800280 DCHECK_EQ(old_state_and_flags.as_struct.state, old_state);
281 while ((old_state_and_flags.as_struct.flags & kSuspendRequest) != 0) {
282 // Re-check when Thread::resume_cond_ is notified.
Nicolas Geoffray9f5f8ac2016-06-29 14:39:59 +0100283 Thread::resume_cond_->Wait(thread_to_pass);
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 }
287 DCHECK_EQ(GetSuspendCount(), 0);
288 }
Ian Rogers719d1a32014-03-06 12:13:39 -0800289 } while (true);
Yu Lieac44242015-06-29 10:50:03 +0800290 // Run the flip function, if set.
291 Closure* flip_func = GetFlipFunction();
292 if (flip_func != nullptr) {
293 flip_func->Run(this);
294 }
295 return static_cast<ThreadState>(old_state);
Ian Rogers693ff612013-02-01 10:56:12 -0800296}
297
Ian Rogers04d7aa92013-03-16 14:29:17 -0700298inline void Thread::VerifyStack() {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800299 if (kVerifyStack) {
300 if (Runtime::Current()->GetHeap()->IsObjectValidationEnabled()) {
301 VerifyStackImpl();
302 }
Ian Rogers04d7aa92013-03-16 14:29:17 -0700303 }
304}
305
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800306inline size_t Thread::TlabSize() const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700307 return tlsPtr_.thread_local_end - tlsPtr_.thread_local_pos;
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800308}
309
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800310inline mirror::Object* Thread::AllocTlab(size_t bytes) {
311 DCHECK_GE(TlabSize(), bytes);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700312 ++tlsPtr_.thread_local_objects;
313 mirror::Object* ret = reinterpret_cast<mirror::Object*>(tlsPtr_.thread_local_pos);
314 tlsPtr_.thread_local_pos += bytes;
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800315 return ret;
316}
317
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800318inline bool Thread::PushOnThreadLocalAllocationStack(mirror::Object* obj) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700319 DCHECK_LE(tlsPtr_.thread_local_alloc_stack_top, tlsPtr_.thread_local_alloc_stack_end);
320 if (tlsPtr_.thread_local_alloc_stack_top < tlsPtr_.thread_local_alloc_stack_end) {
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800321 // There's room.
Ian Rogers13735952014-10-08 12:43:28 -0700322 DCHECK_LE(reinterpret_cast<uint8_t*>(tlsPtr_.thread_local_alloc_stack_top) +
Mathieu Chartiercb535da2015-01-23 13:50:03 -0800323 sizeof(StackReference<mirror::Object>),
Ian Rogers13735952014-10-08 12:43:28 -0700324 reinterpret_cast<uint8_t*>(tlsPtr_.thread_local_alloc_stack_end));
Mathieu Chartiercb535da2015-01-23 13:50:03 -0800325 DCHECK(tlsPtr_.thread_local_alloc_stack_top->AsMirrorPtr() == nullptr);
326 tlsPtr_.thread_local_alloc_stack_top->Assign(obj);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700327 ++tlsPtr_.thread_local_alloc_stack_top;
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800328 return true;
329 }
330 return false;
331}
332
Mathieu Chartiercb535da2015-01-23 13:50:03 -0800333inline void Thread::SetThreadLocalAllocationStack(StackReference<mirror::Object>* start,
334 StackReference<mirror::Object>* end) {
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800335 DCHECK(Thread::Current() == this) << "Should be called by self";
336 DCHECK(start != nullptr);
337 DCHECK(end != nullptr);
Mathieu Chartiercb535da2015-01-23 13:50:03 -0800338 DCHECK_ALIGNED(start, sizeof(StackReference<mirror::Object>));
339 DCHECK_ALIGNED(end, sizeof(StackReference<mirror::Object>));
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800340 DCHECK_LT(start, end);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700341 tlsPtr_.thread_local_alloc_stack_end = end;
342 tlsPtr_.thread_local_alloc_stack_top = start;
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800343}
344
345inline void Thread::RevokeThreadLocalAllocationStack() {
346 if (kIsDebugBuild) {
347 // Note: self is not necessarily equal to this thread since thread may be suspended.
348 Thread* self = Thread::Current();
349 DCHECK(this == self || IsSuspended() || GetState() == kWaitingPerformingGc)
350 << GetState() << " thread " << this << " self " << self;
351 }
Ian Rogersdd7624d2014-03-14 17:43:00 -0700352 tlsPtr_.thread_local_alloc_stack_end = nullptr;
353 tlsPtr_.thread_local_alloc_stack_top = nullptr;
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800354}
355
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700356inline void Thread::PoisonObjectPointersIfDebug() {
Andreas Gampec73cb642017-02-22 10:11:30 -0800357 if (kObjPtrPoisoning) {
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700358 Thread::Current()->PoisonObjectPointers();
359 }
360}
361
Hiroshi Yamauchi02e7f1a2016-10-03 15:32:01 -0700362inline bool Thread::ModifySuspendCount(Thread* self,
363 int delta,
364 AtomicInteger* suspend_barrier,
365 bool for_debugger) {
366 if (delta > 0 && ((kUseReadBarrier && this != self) || suspend_barrier != nullptr)) {
367 // When delta > 0 (requesting a suspend), ModifySuspendCountInternal() may fail either if
368 // active_suspend_barriers is full or we are in the middle of a thread flip. Retry in a loop.
369 while (true) {
370 if (LIKELY(ModifySuspendCountInternal(self, delta, suspend_barrier, for_debugger))) {
371 return true;
372 } else {
373 // Failure means the list of active_suspend_barriers is full or we are in the middle of a
374 // thread flip, we should release the thread_suspend_count_lock_ (to avoid deadlock) and
375 // wait till the target thread has executed or Thread::PassActiveSuspendBarriers() or the
376 // flip function. Note that we could not simply wait for the thread to change to a suspended
377 // state, because it might need to run checkpoint function before the state change or
378 // resumes from the resume_cond_, which also needs thread_suspend_count_lock_.
379 //
380 // The list of active_suspend_barriers is very unlikely to be full since more than
381 // kMaxSuspendBarriers threads need to execute SuspendAllInternal() simultaneously, and
382 // target thread stays in kRunnable in the mean time.
383 Locks::thread_suspend_count_lock_->ExclusiveUnlock(self);
384 NanoSleep(100000);
385 Locks::thread_suspend_count_lock_->ExclusiveLock(self);
386 }
387 }
388 } else {
389 return ModifySuspendCountInternal(self, delta, suspend_barrier, for_debugger);
390 }
391}
392
Ian Rogers693ff612013-02-01 10:56:12 -0800393} // namespace art
394
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700395#endif // ART_RUNTIME_THREAD_INL_H_