blob: 6698634dcf00fb5785d90143f3b5ad858001fc20 [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
Ian Rogers02ed4c02013-09-06 13:10:04 -070022#include <pthread.h>
23
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"
Ian Rogers7b078e82014-09-10 14:44:24 -070026#include "entrypoints/entrypoint_utils-inl.h"
Ian Rogers576ca0c2014-06-06 15:58:22 -070027#include "gc/heap.h"
Ian Rogers68d8b422014-07-17 11:09:10 -070028#include "jni_env_ext.h"
Ian Rogers693ff612013-02-01 10:56:12 -080029
30namespace art {
31
Ian Rogers1eb512d2013-10-18 15:42:20 -070032// Quickly access the current thread from a JNIEnv.
33static inline Thread* ThreadForEnv(JNIEnv* env) {
34 JNIEnvExt* full_env(down_cast<JNIEnvExt*>(env));
35 return full_env->self;
36}
37
Ian Rogers02ed4c02013-09-06 13:10:04 -070038inline Thread* Thread::Current() {
39 // We rely on Thread::Current returning NULL for a detached thread, so it's not obvious
40 // that we can replace this with a direct %fs access on x86.
41 if (!is_started_) {
42 return NULL;
43 } else {
44 void* thread = pthread_getspecific(Thread::pthread_key_self_);
45 return reinterpret_cast<Thread*>(thread);
46 }
47}
48
Ian Rogers7b078e82014-09-10 14:44:24 -070049inline void Thread::AllowThreadSuspension() {
50 DCHECK_EQ(Thread::Current(), this);
51 if (UNLIKELY(TestAllFlags())) {
52 CheckSuspend();
53 }
54}
55
56inline void Thread::CheckSuspend() {
57 DCHECK_EQ(Thread::Current(), this);
58 for (;;) {
59 if (ReadFlag(kCheckpointRequest)) {
60 RunCheckpointFunction();
61 } else if (ReadFlag(kSuspendRequest)) {
62 FullSuspendCheck();
63 } else {
64 break;
65 }
66 }
67}
68
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080069inline ThreadState Thread::SetState(ThreadState new_state) {
70 // Cannot use this code to change into Runnable as changing to Runnable should fail if
71 // old_state_and_flags.suspend_request is true.
72 DCHECK_NE(new_state, kRunnable);
73 DCHECK_EQ(this, Thread::Current());
Chris Dearman59cde532013-12-04 18:53:49 -080074 union StateAndFlags old_state_and_flags;
Ian Rogersdd7624d2014-03-14 17:43:00 -070075 old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
76 tls32_.state_and_flags.as_struct.state = new_state;
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080077 return static_cast<ThreadState>(old_state_and_flags.as_struct.state);
78}
79
Ian Rogers693ff612013-02-01 10:56:12 -080080inline void Thread::AssertThreadSuspensionIsAllowable(bool check_locks) const {
Ian Rogersf3d874c2014-07-17 18:52:42 -070081 if (kIsDebugBuild) {
82 CHECK_EQ(0u, tls32_.no_thread_suspension) << tlsPtr_.last_no_thread_suspension_cause;
83 if (check_locks) {
84 bool bad_mutexes_held = false;
85 for (int i = kLockLevelCount - 1; i >= 0; --i) {
86 // We expect no locks except the mutator_lock_ or thread list suspend thread lock.
87 if (i != kMutatorLock && i != kThreadListSuspendThreadLock) {
88 BaseMutex* held_mutex = GetHeldMutex(static_cast<LockLevel>(i));
89 if (held_mutex != NULL) {
90 LOG(ERROR) << "holding \"" << held_mutex->GetName()
91 << "\" at point where thread suspension is expected";
92 bad_mutexes_held = true;
93 }
Ian Rogers693ff612013-02-01 10:56:12 -080094 }
95 }
Ian Rogersf3d874c2014-07-17 18:52:42 -070096 CHECK(!bad_mutexes_held);
Ian Rogers693ff612013-02-01 10:56:12 -080097 }
Ian Rogers693ff612013-02-01 10:56:12 -080098 }
Ian Rogers693ff612013-02-01 10:56:12 -080099}
100
101inline void Thread::TransitionFromRunnableToSuspended(ThreadState new_state) {
102 AssertThreadSuspensionIsAllowable();
103 DCHECK_NE(new_state, kRunnable);
104 DCHECK_EQ(this, Thread::Current());
105 // Change to non-runnable state, thereby appearing suspended to the system.
106 DCHECK_EQ(GetState(), kRunnable);
107 union StateAndFlags old_state_and_flags;
108 union StateAndFlags new_state_and_flags;
Dave Allison0f5f6bb2013-11-22 17:39:19 -0800109 while (true) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700110 old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700111 if (UNLIKELY((old_state_and_flags.as_struct.flags & kCheckpointRequest) != 0)) {
112 RunCheckpointFunction();
113 continue;
114 }
Dave Allison0f5f6bb2013-11-22 17:39:19 -0800115 // Change the state but keep the current flags (kCheckpointRequest is clear).
116 DCHECK_EQ((old_state_and_flags.as_struct.flags & kCheckpointRequest), 0);
117 new_state_and_flags.as_struct.flags = old_state_and_flags.as_struct.flags;
Ian Rogers693ff612013-02-01 10:56:12 -0800118 new_state_and_flags.as_struct.state = new_state;
Ian Rogersb8e087e2014-07-09 21:12:06 -0700119
120 // CAS the value without a memory ordering as that is given by the lock release below.
121 bool done =
122 tls32_.state_and_flags.as_atomic_int.CompareExchangeWeakRelaxed(old_state_and_flags.as_int,
123 new_state_and_flags.as_int);
124 if (LIKELY(done)) {
Dave Allison0f5f6bb2013-11-22 17:39:19 -0800125 break;
126 }
127 }
Ian Rogers693ff612013-02-01 10:56:12 -0800128 // Release share on mutator_lock_.
129 Locks::mutator_lock_->SharedUnlock(this);
130}
131
132inline ThreadState Thread::TransitionFromSuspendedToRunnable() {
133 bool done = false;
Chris Dearman59cde532013-12-04 18:53:49 -0800134 union StateAndFlags old_state_and_flags;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700135 old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
Ian Rogers693ff612013-02-01 10:56:12 -0800136 int16_t old_state = old_state_and_flags.as_struct.state;
137 DCHECK_NE(static_cast<ThreadState>(old_state), kRunnable);
138 do {
139 Locks::mutator_lock_->AssertNotHeld(this); // Otherwise we starve GC..
Ian Rogersdd7624d2014-03-14 17:43:00 -0700140 old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
Ian Rogers693ff612013-02-01 10:56:12 -0800141 DCHECK_EQ(old_state_and_flags.as_struct.state, old_state);
142 if (UNLIKELY((old_state_and_flags.as_struct.flags & kSuspendRequest) != 0)) {
143 // Wait while our suspend count is non-zero.
144 MutexLock mu(this, *Locks::thread_suspend_count_lock_);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700145 old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
Ian Rogers693ff612013-02-01 10:56:12 -0800146 DCHECK_EQ(old_state_and_flags.as_struct.state, old_state);
147 while ((old_state_and_flags.as_struct.flags & kSuspendRequest) != 0) {
148 // Re-check when Thread::resume_cond_ is notified.
149 Thread::resume_cond_->Wait(this);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700150 old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
Ian Rogers693ff612013-02-01 10:56:12 -0800151 DCHECK_EQ(old_state_and_flags.as_struct.state, old_state);
152 }
153 DCHECK_EQ(GetSuspendCount(), 0);
154 }
155 // Re-acquire shared mutator_lock_ access.
156 Locks::mutator_lock_->SharedLock(this);
157 // Atomically change from suspended to runnable if no suspend request pending.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700158 old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
Ian Rogers693ff612013-02-01 10:56:12 -0800159 DCHECK_EQ(old_state_and_flags.as_struct.state, old_state);
160 if (LIKELY((old_state_and_flags.as_struct.flags & kSuspendRequest) == 0)) {
Chris Dearman59cde532013-12-04 18:53:49 -0800161 union StateAndFlags new_state_and_flags;
162 new_state_and_flags.as_int = old_state_and_flags.as_int;
Ian Rogers693ff612013-02-01 10:56:12 -0800163 new_state_and_flags.as_struct.state = kRunnable;
Ian Rogersb8e087e2014-07-09 21:12:06 -0700164 // CAS the value without a memory ordering as that is given by the lock acquisition above.
165 done =
166 tls32_.state_and_flags.as_atomic_int.CompareExchangeWeakRelaxed(old_state_and_flags.as_int,
167 new_state_and_flags.as_int);
Ian Rogers693ff612013-02-01 10:56:12 -0800168 }
169 if (UNLIKELY(!done)) {
170 // Failed to transition to Runnable. Release shared mutator_lock_ access and try again.
171 Locks::mutator_lock_->SharedUnlock(this);
Ian Rogers719d1a32014-03-06 12:13:39 -0800172 } else {
173 return static_cast<ThreadState>(old_state);
Ian Rogers693ff612013-02-01 10:56:12 -0800174 }
Ian Rogers719d1a32014-03-06 12:13:39 -0800175 } while (true);
Ian Rogers693ff612013-02-01 10:56:12 -0800176}
177
Ian Rogers04d7aa92013-03-16 14:29:17 -0700178inline void Thread::VerifyStack() {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800179 if (kVerifyStack) {
180 if (Runtime::Current()->GetHeap()->IsObjectValidationEnabled()) {
181 VerifyStackImpl();
182 }
Ian Rogers04d7aa92013-03-16 14:29:17 -0700183 }
184}
185
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800186inline size_t Thread::TlabSize() const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700187 return tlsPtr_.thread_local_end - tlsPtr_.thread_local_pos;
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800188}
189
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800190inline mirror::Object* Thread::AllocTlab(size_t bytes) {
191 DCHECK_GE(TlabSize(), bytes);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700192 ++tlsPtr_.thread_local_objects;
193 mirror::Object* ret = reinterpret_cast<mirror::Object*>(tlsPtr_.thread_local_pos);
194 tlsPtr_.thread_local_pos += bytes;
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800195 return ret;
196}
197
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800198inline bool Thread::PushOnThreadLocalAllocationStack(mirror::Object* obj) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700199 DCHECK_LE(tlsPtr_.thread_local_alloc_stack_top, tlsPtr_.thread_local_alloc_stack_end);
200 if (tlsPtr_.thread_local_alloc_stack_top < tlsPtr_.thread_local_alloc_stack_end) {
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800201 // There's room.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700202 DCHECK_LE(reinterpret_cast<byte*>(tlsPtr_.thread_local_alloc_stack_top) +
203 sizeof(mirror::Object*),
204 reinterpret_cast<byte*>(tlsPtr_.thread_local_alloc_stack_end));
205 DCHECK(*tlsPtr_.thread_local_alloc_stack_top == nullptr);
206 *tlsPtr_.thread_local_alloc_stack_top = obj;
207 ++tlsPtr_.thread_local_alloc_stack_top;
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800208 return true;
209 }
210 return false;
211}
212
213inline void Thread::SetThreadLocalAllocationStack(mirror::Object** start, mirror::Object** end) {
214 DCHECK(Thread::Current() == this) << "Should be called by self";
215 DCHECK(start != nullptr);
216 DCHECK(end != nullptr);
217 DCHECK_ALIGNED(start, sizeof(mirror::Object*));
218 DCHECK_ALIGNED(end, sizeof(mirror::Object*));
219 DCHECK_LT(start, end);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700220 tlsPtr_.thread_local_alloc_stack_end = end;
221 tlsPtr_.thread_local_alloc_stack_top = start;
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800222}
223
224inline void Thread::RevokeThreadLocalAllocationStack() {
225 if (kIsDebugBuild) {
226 // Note: self is not necessarily equal to this thread since thread may be suspended.
227 Thread* self = Thread::Current();
228 DCHECK(this == self || IsSuspended() || GetState() == kWaitingPerformingGc)
229 << GetState() << " thread " << this << " self " << self;
230 }
Ian Rogersdd7624d2014-03-14 17:43:00 -0700231 tlsPtr_.thread_local_alloc_stack_end = nullptr;
232 tlsPtr_.thread_local_alloc_stack_top = nullptr;
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800233}
234
Ian Rogers693ff612013-02-01 10:56:12 -0800235} // namespace art
236
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700237#endif // ART_RUNTIME_THREAD_INL_H_