blob: 94f7585bea1793d8e2b86d3bd7068671a4e918cb [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 Rogers576ca0c2014-06-06 15:58:22 -070026#include "gc/heap.h"
Ian Rogers68d8b422014-07-17 11:09:10 -070027#include "jni_env_ext.h"
Ian Rogers693ff612013-02-01 10:56:12 -080028
29namespace art {
30
Ian Rogers1eb512d2013-10-18 15:42:20 -070031// Quickly access the current thread from a JNIEnv.
32static inline Thread* ThreadForEnv(JNIEnv* env) {
33 JNIEnvExt* full_env(down_cast<JNIEnvExt*>(env));
34 return full_env->self;
35}
36
Ian Rogers02ed4c02013-09-06 13:10:04 -070037inline Thread* Thread::Current() {
38 // We rely on Thread::Current returning NULL for a detached thread, so it's not obvious
39 // that we can replace this with a direct %fs access on x86.
40 if (!is_started_) {
41 return NULL;
42 } else {
43 void* thread = pthread_getspecific(Thread::pthread_key_self_);
44 return reinterpret_cast<Thread*>(thread);
45 }
46}
47
Ian Rogers7b078e82014-09-10 14:44:24 -070048inline void Thread::AllowThreadSuspension() {
49 DCHECK_EQ(Thread::Current(), this);
50 if (UNLIKELY(TestAllFlags())) {
51 CheckSuspend();
52 }
53}
54
55inline void Thread::CheckSuspend() {
56 DCHECK_EQ(Thread::Current(), this);
57 for (;;) {
58 if (ReadFlag(kCheckpointRequest)) {
59 RunCheckpointFunction();
60 } else if (ReadFlag(kSuspendRequest)) {
61 FullSuspendCheck();
62 } else {
63 break;
64 }
65 }
66}
67
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080068inline ThreadState Thread::SetState(ThreadState new_state) {
69 // Cannot use this code to change into Runnable as changing to Runnable should fail if
70 // old_state_and_flags.suspend_request is true.
71 DCHECK_NE(new_state, kRunnable);
72 DCHECK_EQ(this, Thread::Current());
Chris Dearman59cde532013-12-04 18:53:49 -080073 union StateAndFlags old_state_and_flags;
Ian Rogersdd7624d2014-03-14 17:43:00 -070074 old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
75 tls32_.state_and_flags.as_struct.state = new_state;
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080076 return static_cast<ThreadState>(old_state_and_flags.as_struct.state);
77}
78
Ian Rogers693ff612013-02-01 10:56:12 -080079inline void Thread::AssertThreadSuspensionIsAllowable(bool check_locks) const {
Ian Rogersf3d874c2014-07-17 18:52:42 -070080 if (kIsDebugBuild) {
Ian Rogers8409ec42014-11-04 17:57:02 -080081 if (gAborting == 0) {
82 CHECK_EQ(0u, tls32_.no_thread_suspension) << tlsPtr_.last_no_thread_suspension_cause;
83 }
Ian Rogersf3d874c2014-07-17 18:52:42 -070084 if (check_locks) {
85 bool bad_mutexes_held = false;
86 for (int i = kLockLevelCount - 1; i >= 0; --i) {
87 // We expect no locks except the mutator_lock_ or thread list suspend thread lock.
88 if (i != kMutatorLock && i != kThreadListSuspendThreadLock) {
89 BaseMutex* held_mutex = GetHeldMutex(static_cast<LockLevel>(i));
90 if (held_mutex != NULL) {
91 LOG(ERROR) << "holding \"" << held_mutex->GetName()
92 << "\" at point where thread suspension is expected";
93 bad_mutexes_held = true;
94 }
Ian Rogers693ff612013-02-01 10:56:12 -080095 }
96 }
Ian Rogers8409ec42014-11-04 17:57:02 -080097 if (gAborting == 0) {
98 CHECK(!bad_mutexes_held);
99 }
Ian Rogers693ff612013-02-01 10:56:12 -0800100 }
Ian Rogers693ff612013-02-01 10:56:12 -0800101 }
Ian Rogers693ff612013-02-01 10:56:12 -0800102}
103
104inline void Thread::TransitionFromRunnableToSuspended(ThreadState new_state) {
105 AssertThreadSuspensionIsAllowable();
106 DCHECK_NE(new_state, kRunnable);
107 DCHECK_EQ(this, Thread::Current());
108 // Change to non-runnable state, thereby appearing suspended to the system.
109 DCHECK_EQ(GetState(), kRunnable);
110 union StateAndFlags old_state_and_flags;
111 union StateAndFlags new_state_and_flags;
Dave Allison0f5f6bb2013-11-22 17:39:19 -0800112 while (true) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700113 old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700114 if (UNLIKELY((old_state_and_flags.as_struct.flags & kCheckpointRequest) != 0)) {
115 RunCheckpointFunction();
116 continue;
117 }
Dave Allison0f5f6bb2013-11-22 17:39:19 -0800118 // Change the state but keep the current flags (kCheckpointRequest is clear).
119 DCHECK_EQ((old_state_and_flags.as_struct.flags & kCheckpointRequest), 0);
120 new_state_and_flags.as_struct.flags = old_state_and_flags.as_struct.flags;
Ian Rogers693ff612013-02-01 10:56:12 -0800121 new_state_and_flags.as_struct.state = new_state;
Ian Rogersb8e087e2014-07-09 21:12:06 -0700122
123 // CAS the value without a memory ordering as that is given by the lock release below.
124 bool done =
125 tls32_.state_and_flags.as_atomic_int.CompareExchangeWeakRelaxed(old_state_and_flags.as_int,
126 new_state_and_flags.as_int);
127 if (LIKELY(done)) {
Dave Allison0f5f6bb2013-11-22 17:39:19 -0800128 break;
129 }
130 }
Ian Rogers693ff612013-02-01 10:56:12 -0800131 // Release share on mutator_lock_.
132 Locks::mutator_lock_->SharedUnlock(this);
133}
134
135inline ThreadState Thread::TransitionFromSuspendedToRunnable() {
136 bool done = false;
Chris Dearman59cde532013-12-04 18:53:49 -0800137 union StateAndFlags old_state_and_flags;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700138 old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
Ian Rogers693ff612013-02-01 10:56:12 -0800139 int16_t old_state = old_state_and_flags.as_struct.state;
140 DCHECK_NE(static_cast<ThreadState>(old_state), kRunnable);
141 do {
142 Locks::mutator_lock_->AssertNotHeld(this); // Otherwise we starve GC..
Ian Rogersdd7624d2014-03-14 17:43:00 -0700143 old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
Ian Rogers693ff612013-02-01 10:56:12 -0800144 DCHECK_EQ(old_state_and_flags.as_struct.state, old_state);
145 if (UNLIKELY((old_state_and_flags.as_struct.flags & kSuspendRequest) != 0)) {
146 // Wait while our suspend count is non-zero.
147 MutexLock mu(this, *Locks::thread_suspend_count_lock_);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700148 old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
Ian Rogers693ff612013-02-01 10:56:12 -0800149 DCHECK_EQ(old_state_and_flags.as_struct.state, old_state);
150 while ((old_state_and_flags.as_struct.flags & kSuspendRequest) != 0) {
151 // Re-check when Thread::resume_cond_ is notified.
152 Thread::resume_cond_->Wait(this);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700153 old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
Ian Rogers693ff612013-02-01 10:56:12 -0800154 DCHECK_EQ(old_state_and_flags.as_struct.state, old_state);
155 }
156 DCHECK_EQ(GetSuspendCount(), 0);
157 }
158 // Re-acquire shared mutator_lock_ access.
159 Locks::mutator_lock_->SharedLock(this);
160 // Atomically change from suspended to runnable if no suspend request pending.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700161 old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
Ian Rogers693ff612013-02-01 10:56:12 -0800162 DCHECK_EQ(old_state_and_flags.as_struct.state, old_state);
163 if (LIKELY((old_state_and_flags.as_struct.flags & kSuspendRequest) == 0)) {
Chris Dearman59cde532013-12-04 18:53:49 -0800164 union StateAndFlags new_state_and_flags;
165 new_state_and_flags.as_int = old_state_and_flags.as_int;
Ian Rogers693ff612013-02-01 10:56:12 -0800166 new_state_and_flags.as_struct.state = kRunnable;
Ian Rogersb8e087e2014-07-09 21:12:06 -0700167 // CAS the value without a memory ordering as that is given by the lock acquisition above.
168 done =
169 tls32_.state_and_flags.as_atomic_int.CompareExchangeWeakRelaxed(old_state_and_flags.as_int,
170 new_state_and_flags.as_int);
Ian Rogers693ff612013-02-01 10:56:12 -0800171 }
172 if (UNLIKELY(!done)) {
173 // Failed to transition to Runnable. Release shared mutator_lock_ access and try again.
174 Locks::mutator_lock_->SharedUnlock(this);
Ian Rogers719d1a32014-03-06 12:13:39 -0800175 } else {
176 return static_cast<ThreadState>(old_state);
Ian Rogers693ff612013-02-01 10:56:12 -0800177 }
Ian Rogers719d1a32014-03-06 12:13:39 -0800178 } while (true);
Ian Rogers693ff612013-02-01 10:56:12 -0800179}
180
Ian Rogers04d7aa92013-03-16 14:29:17 -0700181inline void Thread::VerifyStack() {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800182 if (kVerifyStack) {
183 if (Runtime::Current()->GetHeap()->IsObjectValidationEnabled()) {
184 VerifyStackImpl();
185 }
Ian Rogers04d7aa92013-03-16 14:29:17 -0700186 }
187}
188
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800189inline size_t Thread::TlabSize() const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700190 return tlsPtr_.thread_local_end - tlsPtr_.thread_local_pos;
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800191}
192
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800193inline mirror::Object* Thread::AllocTlab(size_t bytes) {
194 DCHECK_GE(TlabSize(), bytes);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700195 ++tlsPtr_.thread_local_objects;
196 mirror::Object* ret = reinterpret_cast<mirror::Object*>(tlsPtr_.thread_local_pos);
197 tlsPtr_.thread_local_pos += bytes;
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800198 return ret;
199}
200
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800201inline bool Thread::PushOnThreadLocalAllocationStack(mirror::Object* obj) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700202 DCHECK_LE(tlsPtr_.thread_local_alloc_stack_top, tlsPtr_.thread_local_alloc_stack_end);
203 if (tlsPtr_.thread_local_alloc_stack_top < tlsPtr_.thread_local_alloc_stack_end) {
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800204 // There's room.
Ian Rogers13735952014-10-08 12:43:28 -0700205 DCHECK_LE(reinterpret_cast<uint8_t*>(tlsPtr_.thread_local_alloc_stack_top) +
Ian Rogersdd7624d2014-03-14 17:43:00 -0700206 sizeof(mirror::Object*),
Ian Rogers13735952014-10-08 12:43:28 -0700207 reinterpret_cast<uint8_t*>(tlsPtr_.thread_local_alloc_stack_end));
Ian Rogersdd7624d2014-03-14 17:43:00 -0700208 DCHECK(*tlsPtr_.thread_local_alloc_stack_top == nullptr);
209 *tlsPtr_.thread_local_alloc_stack_top = obj;
210 ++tlsPtr_.thread_local_alloc_stack_top;
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800211 return true;
212 }
213 return false;
214}
215
216inline void Thread::SetThreadLocalAllocationStack(mirror::Object** start, mirror::Object** end) {
217 DCHECK(Thread::Current() == this) << "Should be called by self";
218 DCHECK(start != nullptr);
219 DCHECK(end != nullptr);
220 DCHECK_ALIGNED(start, sizeof(mirror::Object*));
221 DCHECK_ALIGNED(end, sizeof(mirror::Object*));
222 DCHECK_LT(start, end);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700223 tlsPtr_.thread_local_alloc_stack_end = end;
224 tlsPtr_.thread_local_alloc_stack_top = start;
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800225}
226
227inline void Thread::RevokeThreadLocalAllocationStack() {
228 if (kIsDebugBuild) {
229 // Note: self is not necessarily equal to this thread since thread may be suspended.
230 Thread* self = Thread::Current();
231 DCHECK(this == self || IsSuspended() || GetState() == kWaitingPerformingGc)
232 << GetState() << " thread " << this << " self " << self;
233 }
Ian Rogersdd7624d2014-03-14 17:43:00 -0700234 tlsPtr_.thread_local_alloc_stack_end = nullptr;
235 tlsPtr_.thread_local_alloc_stack_top = nullptr;
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800236}
237
Ian Rogers693ff612013-02-01 10:56:12 -0800238} // namespace art
239
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700240#endif // ART_RUNTIME_THREAD_INL_H_