blob: 66077f904e998bde7dd3620b532a0e900c5553b6 [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"
26#include "cutils/atomic-inline.h"
Ian Rogers1eb512d2013-10-18 15:42:20 -070027#include "jni_internal.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 Rogersc0fa3ad2013-02-05 00:11:55 -080048inline ThreadState Thread::SetState(ThreadState new_state) {
49 // Cannot use this code to change into Runnable as changing to Runnable should fail if
50 // old_state_and_flags.suspend_request is true.
51 DCHECK_NE(new_state, kRunnable);
52 DCHECK_EQ(this, Thread::Current());
Chris Dearman59cde532013-12-04 18:53:49 -080053 union StateAndFlags old_state_and_flags;
54 old_state_and_flags.as_int = state_and_flags_.as_int;
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080055 state_and_flags_.as_struct.state = new_state;
56 return static_cast<ThreadState>(old_state_and_flags.as_struct.state);
57}
58
Ian Rogers693ff612013-02-01 10:56:12 -080059inline void Thread::AssertThreadSuspensionIsAllowable(bool check_locks) const {
60#ifdef NDEBUG
61 UNUSED(check_locks); // Keep GCC happy about unused parameters.
62#else
63 CHECK_EQ(0u, no_thread_suspension_) << last_no_thread_suspension_cause_;
64 if (check_locks) {
65 bool bad_mutexes_held = false;
Elliott Hughes0f827162013-02-26 12:12:58 -080066 for (int i = kLockLevelCount - 1; i >= 0; --i) {
Ian Rogers693ff612013-02-01 10:56:12 -080067 // We expect no locks except the mutator_lock_.
68 if (i != kMutatorLock) {
69 BaseMutex* held_mutex = GetHeldMutex(static_cast<LockLevel>(i));
70 if (held_mutex != NULL) {
71 LOG(ERROR) << "holding \"" << held_mutex->GetName()
72 << "\" at point where thread suspension is expected";
73 bad_mutexes_held = true;
74 }
75 }
76 }
77 CHECK(!bad_mutexes_held);
78 }
79#endif
80}
81
82inline void Thread::TransitionFromRunnableToSuspended(ThreadState new_state) {
83 AssertThreadSuspensionIsAllowable();
84 DCHECK_NE(new_state, kRunnable);
85 DCHECK_EQ(this, Thread::Current());
86 // Change to non-runnable state, thereby appearing suspended to the system.
87 DCHECK_EQ(GetState(), kRunnable);
88 union StateAndFlags old_state_and_flags;
89 union StateAndFlags new_state_and_flags;
Dave Allison0f5f6bb2013-11-22 17:39:19 -080090 while (true) {
Chris Dearman59cde532013-12-04 18:53:49 -080091 old_state_and_flags.as_int = state_and_flags_.as_int;
Ian Rogersd9c4fc92013-10-01 19:45:43 -070092 if (UNLIKELY((old_state_and_flags.as_struct.flags & kCheckpointRequest) != 0)) {
93 RunCheckpointFunction();
94 continue;
95 }
Dave Allison0f5f6bb2013-11-22 17:39:19 -080096 // Change the state but keep the current flags (kCheckpointRequest is clear).
97 DCHECK_EQ((old_state_and_flags.as_struct.flags & kCheckpointRequest), 0);
98 new_state_and_flags.as_struct.flags = old_state_and_flags.as_struct.flags;
Ian Rogers693ff612013-02-01 10:56:12 -080099 new_state_and_flags.as_struct.state = new_state;
Dave Allison0f5f6bb2013-11-22 17:39:19 -0800100 int status = android_atomic_cas(old_state_and_flags.as_int, new_state_and_flags.as_int,
101 &state_and_flags_.as_int);
102 if (LIKELY(status == 0)) {
103 break;
104 }
105 }
Ian Rogers693ff612013-02-01 10:56:12 -0800106 // Release share on mutator_lock_.
107 Locks::mutator_lock_->SharedUnlock(this);
108}
109
110inline ThreadState Thread::TransitionFromSuspendedToRunnable() {
111 bool done = false;
Chris Dearman59cde532013-12-04 18:53:49 -0800112 union StateAndFlags old_state_and_flags;
113 old_state_and_flags.as_int = state_and_flags_.as_int;
Ian Rogers693ff612013-02-01 10:56:12 -0800114 int16_t old_state = old_state_and_flags.as_struct.state;
115 DCHECK_NE(static_cast<ThreadState>(old_state), kRunnable);
116 do {
117 Locks::mutator_lock_->AssertNotHeld(this); // Otherwise we starve GC..
Chris Dearman59cde532013-12-04 18:53:49 -0800118 old_state_and_flags.as_int = state_and_flags_.as_int;
Ian Rogers693ff612013-02-01 10:56:12 -0800119 DCHECK_EQ(old_state_and_flags.as_struct.state, old_state);
120 if (UNLIKELY((old_state_and_flags.as_struct.flags & kSuspendRequest) != 0)) {
121 // Wait while our suspend count is non-zero.
122 MutexLock mu(this, *Locks::thread_suspend_count_lock_);
Chris Dearman59cde532013-12-04 18:53:49 -0800123 old_state_and_flags.as_int = state_and_flags_.as_int;
Ian Rogers693ff612013-02-01 10:56:12 -0800124 DCHECK_EQ(old_state_and_flags.as_struct.state, old_state);
125 while ((old_state_and_flags.as_struct.flags & kSuspendRequest) != 0) {
126 // Re-check when Thread::resume_cond_ is notified.
127 Thread::resume_cond_->Wait(this);
Chris Dearman59cde532013-12-04 18:53:49 -0800128 old_state_and_flags.as_int = state_and_flags_.as_int;
Ian Rogers693ff612013-02-01 10:56:12 -0800129 DCHECK_EQ(old_state_and_flags.as_struct.state, old_state);
130 }
131 DCHECK_EQ(GetSuspendCount(), 0);
132 }
133 // Re-acquire shared mutator_lock_ access.
134 Locks::mutator_lock_->SharedLock(this);
135 // Atomically change from suspended to runnable if no suspend request pending.
Chris Dearman59cde532013-12-04 18:53:49 -0800136 old_state_and_flags.as_int = state_and_flags_.as_int;
Ian Rogers693ff612013-02-01 10:56:12 -0800137 DCHECK_EQ(old_state_and_flags.as_struct.state, old_state);
138 if (LIKELY((old_state_and_flags.as_struct.flags & kSuspendRequest) == 0)) {
Chris Dearman59cde532013-12-04 18:53:49 -0800139 union StateAndFlags new_state_and_flags;
140 new_state_and_flags.as_int = old_state_and_flags.as_int;
Ian Rogers693ff612013-02-01 10:56:12 -0800141 new_state_and_flags.as_struct.state = kRunnable;
142 // CAS the value without a memory barrier, that occurred in the lock above.
143 done = android_atomic_cas(old_state_and_flags.as_int, new_state_and_flags.as_int,
144 &state_and_flags_.as_int) == 0;
145 }
146 if (UNLIKELY(!done)) {
147 // Failed to transition to Runnable. Release shared mutator_lock_ access and try again.
148 Locks::mutator_lock_->SharedUnlock(this);
Ian Rogers719d1a32014-03-06 12:13:39 -0800149 } else {
150 return static_cast<ThreadState>(old_state);
Ian Rogers693ff612013-02-01 10:56:12 -0800151 }
Ian Rogers719d1a32014-03-06 12:13:39 -0800152 } while (true);
Ian Rogers693ff612013-02-01 10:56:12 -0800153}
154
Ian Rogers04d7aa92013-03-16 14:29:17 -0700155inline void Thread::VerifyStack() {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800156 if (kVerifyStack) {
157 if (Runtime::Current()->GetHeap()->IsObjectValidationEnabled()) {
158 VerifyStackImpl();
159 }
Ian Rogers04d7aa92013-03-16 14:29:17 -0700160 }
161}
162
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800163inline size_t Thread::TlabSize() const {
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800164 return thread_local_end_ - thread_local_pos_;
165}
166
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800167inline mirror::Object* Thread::AllocTlab(size_t bytes) {
168 DCHECK_GE(TlabSize(), bytes);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800169 ++thread_local_objects_;
170 mirror::Object* ret = reinterpret_cast<mirror::Object*>(thread_local_pos_);
171 thread_local_pos_ += bytes;
172 return ret;
173}
174
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800175inline bool Thread::PushOnThreadLocalAllocationStack(mirror::Object* obj) {
176 DCHECK_LE(thread_local_alloc_stack_top_, thread_local_alloc_stack_end_);
177 if (thread_local_alloc_stack_top_ < thread_local_alloc_stack_end_) {
178 // There's room.
179 DCHECK_LE(reinterpret_cast<byte*>(thread_local_alloc_stack_top_) + sizeof(mirror::Object*),
180 reinterpret_cast<byte*>(thread_local_alloc_stack_end_));
181 DCHECK(*thread_local_alloc_stack_top_ == nullptr);
182 *thread_local_alloc_stack_top_ = obj;
183 ++thread_local_alloc_stack_top_;
184 return true;
185 }
186 return false;
187}
188
189inline void Thread::SetThreadLocalAllocationStack(mirror::Object** start, mirror::Object** end) {
190 DCHECK(Thread::Current() == this) << "Should be called by self";
191 DCHECK(start != nullptr);
192 DCHECK(end != nullptr);
193 DCHECK_ALIGNED(start, sizeof(mirror::Object*));
194 DCHECK_ALIGNED(end, sizeof(mirror::Object*));
195 DCHECK_LT(start, end);
196 thread_local_alloc_stack_end_ = end;
197 thread_local_alloc_stack_top_ = start;
198}
199
200inline void Thread::RevokeThreadLocalAllocationStack() {
201 if (kIsDebugBuild) {
202 // Note: self is not necessarily equal to this thread since thread may be suspended.
203 Thread* self = Thread::Current();
204 DCHECK(this == self || IsSuspended() || GetState() == kWaitingPerformingGc)
205 << GetState() << " thread " << this << " self " << self;
206 }
207 thread_local_alloc_stack_end_ = nullptr;
208 thread_local_alloc_stack_top_ = nullptr;
209}
210
Ian Rogers693ff612013-02-01 10:56:12 -0800211} // namespace art
212
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700213#endif // ART_RUNTIME_THREAD_INL_H_