blob: b3dd2ebd80aa312c1d305f570d7df1a2a9b24672 [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 Gampe4382f1e2015-08-05 01:08:53 +000022#ifdef __ANDROID__
23#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"
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));
39 return full_env->self;
40}
41
Ian Rogers02ed4c02013-09-06 13:10:04 -070042inline Thread* Thread::Current() {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070043 // We rely on Thread::Current returning null for a detached thread, so it's not obvious
Ian Rogers02ed4c02013-09-06 13:10:04 -070044 // that we can replace this with a direct %fs access on x86.
45 if (!is_started_) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070046 return nullptr;
Ian Rogers02ed4c02013-09-06 13:10:04 -070047 } else {
Andreas Gampe4382f1e2015-08-05 01:08:53 +000048#ifdef __ANDROID__
49 void* thread = __get_tls()[TLS_SLOT_ART_THREAD_SELF];
50#else
Ian Rogers02ed4c02013-09-06 13:10:04 -070051 void* thread = pthread_getspecific(Thread::pthread_key_self_);
Andreas Gampe4382f1e2015-08-05 01:08:53 +000052#endif
Ian Rogers02ed4c02013-09-06 13:10:04 -070053 return reinterpret_cast<Thread*>(thread);
54 }
55}
56
Ian Rogers7b078e82014-09-10 14:44:24 -070057inline void Thread::AllowThreadSuspension() {
58 DCHECK_EQ(Thread::Current(), this);
59 if (UNLIKELY(TestAllFlags())) {
60 CheckSuspend();
61 }
62}
63
64inline void Thread::CheckSuspend() {
65 DCHECK_EQ(Thread::Current(), this);
66 for (;;) {
67 if (ReadFlag(kCheckpointRequest)) {
68 RunCheckpointFunction();
69 } else if (ReadFlag(kSuspendRequest)) {
70 FullSuspendCheck();
71 } else {
72 break;
73 }
74 }
75}
76
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080077inline ThreadState Thread::SetState(ThreadState new_state) {
Yu Lieac44242015-06-29 10:50:03 +080078 // Should only be used to change between suspended states.
79 // Cannot use this code to change into or from Runnable as changing to Runnable should
80 // fail if old_state_and_flags.suspend_request is true and changing from Runnable might
81 // miss passing an active suspend barrier.
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080082 DCHECK_NE(new_state, kRunnable);
Andreas Gampeef048f62014-11-25 22:12:27 -080083 if (kIsDebugBuild && this != Thread::Current()) {
84 std::string name;
85 GetThreadName(name);
86 LOG(FATAL) << "Thread \"" << name << "\"(" << this << " != Thread::Current()="
87 << Thread::Current() << ") changing state to " << new_state;
88 }
Chris Dearman59cde532013-12-04 18:53:49 -080089 union StateAndFlags old_state_and_flags;
Ian Rogersdd7624d2014-03-14 17:43:00 -070090 old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
Yu Lieac44242015-06-29 10:50:03 +080091 CHECK_NE(old_state_and_flags.as_struct.state, kRunnable);
Ian Rogersdd7624d2014-03-14 17:43:00 -070092 tls32_.state_and_flags.as_struct.state = new_state;
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080093 return static_cast<ThreadState>(old_state_and_flags.as_struct.state);
94}
95
Ian Rogers693ff612013-02-01 10:56:12 -080096inline void Thread::AssertThreadSuspensionIsAllowable(bool check_locks) const {
Ian Rogersf3d874c2014-07-17 18:52:42 -070097 if (kIsDebugBuild) {
Nicolas Geoffraydb978712014-12-09 13:33:38 +000098 if (gAborting == 0) {
99 CHECK_EQ(0u, tls32_.no_thread_suspension) << tlsPtr_.last_no_thread_suspension_cause;
100 }
Ian Rogersf3d874c2014-07-17 18:52:42 -0700101 if (check_locks) {
102 bool bad_mutexes_held = false;
103 for (int i = kLockLevelCount - 1; i >= 0; --i) {
104 // We expect no locks except the mutator_lock_ or thread list suspend thread lock.
Ian Rogers4ad5cd32014-11-11 23:08:07 -0800105 if (i != kMutatorLock) {
Ian Rogersf3d874c2014-07-17 18:52:42 -0700106 BaseMutex* held_mutex = GetHeldMutex(static_cast<LockLevel>(i));
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700107 if (held_mutex != nullptr) {
Ian Rogersf3d874c2014-07-17 18:52:42 -0700108 LOG(ERROR) << "holding \"" << held_mutex->GetName()
109 << "\" at point where thread suspension is expected";
110 bad_mutexes_held = true;
111 }
Ian Rogers693ff612013-02-01 10:56:12 -0800112 }
113 }
Nicolas Geoffraydb978712014-12-09 13:33:38 +0000114 if (gAborting == 0) {
115 CHECK(!bad_mutexes_held);
116 }
Ian Rogers693ff612013-02-01 10:56:12 -0800117 }
Ian Rogers693ff612013-02-01 10:56:12 -0800118 }
Ian Rogers693ff612013-02-01 10:56:12 -0800119}
120
121inline void Thread::TransitionFromRunnableToSuspended(ThreadState new_state) {
122 AssertThreadSuspensionIsAllowable();
123 DCHECK_NE(new_state, kRunnable);
124 DCHECK_EQ(this, Thread::Current());
125 // Change to non-runnable state, thereby appearing suspended to the system.
126 DCHECK_EQ(GetState(), kRunnable);
127 union StateAndFlags old_state_and_flags;
128 union StateAndFlags new_state_and_flags;
Dave Allison0f5f6bb2013-11-22 17:39:19 -0800129 while (true) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700130 old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700131 if (UNLIKELY((old_state_and_flags.as_struct.flags & kCheckpointRequest) != 0)) {
132 RunCheckpointFunction();
133 continue;
134 }
Dave Allison0f5f6bb2013-11-22 17:39:19 -0800135 // Change the state but keep the current flags (kCheckpointRequest is clear).
136 DCHECK_EQ((old_state_and_flags.as_struct.flags & kCheckpointRequest), 0);
137 new_state_and_flags.as_struct.flags = old_state_and_flags.as_struct.flags;
Ian Rogers693ff612013-02-01 10:56:12 -0800138 new_state_and_flags.as_struct.state = new_state;
Ian Rogersb8e087e2014-07-09 21:12:06 -0700139
Yu Lieac44242015-06-29 10:50:03 +0800140 // CAS the value with a memory ordering.
Ian Rogersb8e087e2014-07-09 21:12:06 -0700141 bool done =
Yu Lieac44242015-06-29 10:50:03 +0800142 tls32_.state_and_flags.as_atomic_int.CompareExchangeWeakRelease(old_state_and_flags.as_int,
Ian Rogersb8e087e2014-07-09 21:12:06 -0700143 new_state_and_flags.as_int);
144 if (LIKELY(done)) {
Dave Allison0f5f6bb2013-11-22 17:39:19 -0800145 break;
146 }
147 }
Yu Lieac44242015-06-29 10:50:03 +0800148
149 // Change to non-runnable state, thereby appearing suspended to the system.
150 // Mark the release of the share of the mutator_lock_.
151 Locks::mutator_lock_->TransitionFromRunnableToSuspended(this);
152
153 // Once suspended - check the active suspend barrier flag
154 while (true) {
155 uint16_t current_flags = tls32_.state_and_flags.as_struct.flags;
156 if (LIKELY((current_flags & (kCheckpointRequest | kActiveSuspendBarrier)) == 0)) {
157 break;
158 } else if ((current_flags & kActiveSuspendBarrier) != 0) {
159 PassActiveSuspendBarriers(this);
160 } else {
161 // Impossible
162 LOG(FATAL) << "Fatal, thread transited into suspended without running the checkpoint";
163 }
164 }
Ian Rogers693ff612013-02-01 10:56:12 -0800165}
166
167inline ThreadState Thread::TransitionFromSuspendedToRunnable() {
Chris Dearman59cde532013-12-04 18:53:49 -0800168 union StateAndFlags old_state_and_flags;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700169 old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
Ian Rogers693ff612013-02-01 10:56:12 -0800170 int16_t old_state = old_state_and_flags.as_struct.state;
171 DCHECK_NE(static_cast<ThreadState>(old_state), kRunnable);
172 do {
173 Locks::mutator_lock_->AssertNotHeld(this); // Otherwise we starve GC..
Ian Rogersdd7624d2014-03-14 17:43:00 -0700174 old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
Ian Rogers693ff612013-02-01 10:56:12 -0800175 DCHECK_EQ(old_state_and_flags.as_struct.state, old_state);
Yu Lieac44242015-06-29 10:50:03 +0800176 if (LIKELY(old_state_and_flags.as_struct.flags == 0)) {
177 // Optimize for the return from native code case - this is the fast path.
178 // Atomically change from suspended to runnable if no suspend request pending.
179 union StateAndFlags new_state_and_flags;
180 new_state_and_flags.as_int = old_state_and_flags.as_int;
181 new_state_and_flags.as_struct.state = kRunnable;
182 // CAS the value with a memory barrier.
183 if (LIKELY(tls32_.state_and_flags.as_atomic_int.CompareExchangeWeakAcquire(
184 old_state_and_flags.as_int,
185 new_state_and_flags.as_int))) {
186 // Mark the acquisition of a share of the mutator_lock_.
187 Locks::mutator_lock_->TransitionFromSuspendedToRunnable(this);
188 break;
189 }
190 } else if ((old_state_and_flags.as_struct.flags & kActiveSuspendBarrier) != 0) {
191 PassActiveSuspendBarriers(this);
192 } else if ((old_state_and_flags.as_struct.flags & kCheckpointRequest) != 0) {
193 // Impossible
Mathieu Chartierdabdccc2015-10-01 14:46:29 -0700194 LOG(FATAL) << "Transitioning to runnable with checkpoint flag, "
195 << " flags=" << old_state_and_flags.as_struct.flags
196 << " state=" << old_state_and_flags.as_struct.state;
Yu Lieac44242015-06-29 10:50:03 +0800197 } else if ((old_state_and_flags.as_struct.flags & kSuspendRequest) != 0) {
Ian Rogers693ff612013-02-01 10:56:12 -0800198 // Wait while our suspend count is non-zero.
199 MutexLock mu(this, *Locks::thread_suspend_count_lock_);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700200 old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
Ian Rogers693ff612013-02-01 10:56:12 -0800201 DCHECK_EQ(old_state_and_flags.as_struct.state, old_state);
202 while ((old_state_and_flags.as_struct.flags & kSuspendRequest) != 0) {
203 // Re-check when Thread::resume_cond_ is notified.
204 Thread::resume_cond_->Wait(this);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700205 old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
Ian Rogers693ff612013-02-01 10:56:12 -0800206 DCHECK_EQ(old_state_and_flags.as_struct.state, old_state);
207 }
208 DCHECK_EQ(GetSuspendCount(), 0);
209 }
Ian Rogers719d1a32014-03-06 12:13:39 -0800210 } while (true);
Yu Lieac44242015-06-29 10:50:03 +0800211 // Run the flip function, if set.
212 Closure* flip_func = GetFlipFunction();
213 if (flip_func != nullptr) {
214 flip_func->Run(this);
215 }
216 return static_cast<ThreadState>(old_state);
Ian Rogers693ff612013-02-01 10:56:12 -0800217}
218
Ian Rogers04d7aa92013-03-16 14:29:17 -0700219inline void Thread::VerifyStack() {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800220 if (kVerifyStack) {
221 if (Runtime::Current()->GetHeap()->IsObjectValidationEnabled()) {
222 VerifyStackImpl();
223 }
Ian Rogers04d7aa92013-03-16 14:29:17 -0700224 }
225}
226
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800227inline size_t Thread::TlabSize() const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700228 return tlsPtr_.thread_local_end - tlsPtr_.thread_local_pos;
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800229}
230
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800231inline mirror::Object* Thread::AllocTlab(size_t bytes) {
232 DCHECK_GE(TlabSize(), bytes);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700233 ++tlsPtr_.thread_local_objects;
234 mirror::Object* ret = reinterpret_cast<mirror::Object*>(tlsPtr_.thread_local_pos);
235 tlsPtr_.thread_local_pos += bytes;
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800236 return ret;
237}
238
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800239inline bool Thread::PushOnThreadLocalAllocationStack(mirror::Object* obj) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700240 DCHECK_LE(tlsPtr_.thread_local_alloc_stack_top, tlsPtr_.thread_local_alloc_stack_end);
241 if (tlsPtr_.thread_local_alloc_stack_top < tlsPtr_.thread_local_alloc_stack_end) {
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800242 // There's room.
Ian Rogers13735952014-10-08 12:43:28 -0700243 DCHECK_LE(reinterpret_cast<uint8_t*>(tlsPtr_.thread_local_alloc_stack_top) +
Mathieu Chartiercb535da2015-01-23 13:50:03 -0800244 sizeof(StackReference<mirror::Object>),
Ian Rogers13735952014-10-08 12:43:28 -0700245 reinterpret_cast<uint8_t*>(tlsPtr_.thread_local_alloc_stack_end));
Mathieu Chartiercb535da2015-01-23 13:50:03 -0800246 DCHECK(tlsPtr_.thread_local_alloc_stack_top->AsMirrorPtr() == nullptr);
247 tlsPtr_.thread_local_alloc_stack_top->Assign(obj);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700248 ++tlsPtr_.thread_local_alloc_stack_top;
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800249 return true;
250 }
251 return false;
252}
253
Mathieu Chartiercb535da2015-01-23 13:50:03 -0800254inline void Thread::SetThreadLocalAllocationStack(StackReference<mirror::Object>* start,
255 StackReference<mirror::Object>* end) {
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800256 DCHECK(Thread::Current() == this) << "Should be called by self";
257 DCHECK(start != nullptr);
258 DCHECK(end != nullptr);
Mathieu Chartiercb535da2015-01-23 13:50:03 -0800259 DCHECK_ALIGNED(start, sizeof(StackReference<mirror::Object>));
260 DCHECK_ALIGNED(end, sizeof(StackReference<mirror::Object>));
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800261 DCHECK_LT(start, end);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700262 tlsPtr_.thread_local_alloc_stack_end = end;
263 tlsPtr_.thread_local_alloc_stack_top = start;
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800264}
265
266inline void Thread::RevokeThreadLocalAllocationStack() {
267 if (kIsDebugBuild) {
268 // Note: self is not necessarily equal to this thread since thread may be suspended.
269 Thread* self = Thread::Current();
270 DCHECK(this == self || IsSuspended() || GetState() == kWaitingPerformingGc)
271 << GetState() << " thread " << this << " self " << self;
272 }
Ian Rogersdd7624d2014-03-14 17:43:00 -0700273 tlsPtr_.thread_local_alloc_stack_end = nullptr;
274 tlsPtr_.thread_local_alloc_stack_top = nullptr;
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800275}
276
Ian Rogers693ff612013-02-01 10:56:12 -0800277} // namespace art
278
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700279#endif // ART_RUNTIME_THREAD_INL_H_