blob: b1180bd676b90a88f2af4f6ffffa8bf62c321c03 [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 Rogers576ca0c2014-06-06 15:58:22 -070024#include "cutils/atomic-inline.h"
25
Ian Rogers1eb512d2013-10-18 15:42:20 -070026#include "base/casts.h"
Ian Rogers693ff612013-02-01 10:56:12 -080027#include "base/mutex-inl.h"
Ian Rogers576ca0c2014-06-06 15:58:22 -070028#include "gc/heap.h"
Ian Rogers1eb512d2013-10-18 15:42:20 -070029#include "jni_internal.h"
Ian Rogers693ff612013-02-01 10:56:12 -080030
31namespace art {
32
Ian Rogers1eb512d2013-10-18 15:42:20 -070033// Quickly access the current thread from a JNIEnv.
34static inline Thread* ThreadForEnv(JNIEnv* env) {
35 JNIEnvExt* full_env(down_cast<JNIEnvExt*>(env));
36 return full_env->self;
37}
38
Ian Rogers02ed4c02013-09-06 13:10:04 -070039inline Thread* Thread::Current() {
40 // We rely on Thread::Current returning NULL for a detached thread, so it's not obvious
41 // that we can replace this with a direct %fs access on x86.
42 if (!is_started_) {
43 return NULL;
44 } else {
45 void* thread = pthread_getspecific(Thread::pthread_key_self_);
46 return reinterpret_cast<Thread*>(thread);
47 }
48}
49
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080050inline ThreadState Thread::SetState(ThreadState new_state) {
51 // Cannot use this code to change into Runnable as changing to Runnable should fail if
52 // old_state_and_flags.suspend_request is true.
53 DCHECK_NE(new_state, kRunnable);
54 DCHECK_EQ(this, Thread::Current());
Chris Dearman59cde532013-12-04 18:53:49 -080055 union StateAndFlags old_state_and_flags;
Ian Rogersdd7624d2014-03-14 17:43:00 -070056 old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
57 tls32_.state_and_flags.as_struct.state = new_state;
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080058 return static_cast<ThreadState>(old_state_and_flags.as_struct.state);
59}
60
Ian Rogers693ff612013-02-01 10:56:12 -080061inline void Thread::AssertThreadSuspensionIsAllowable(bool check_locks) const {
62#ifdef NDEBUG
63 UNUSED(check_locks); // Keep GCC happy about unused parameters.
64#else
Ian Rogersdd7624d2014-03-14 17:43:00 -070065 CHECK_EQ(0u, tls32_.no_thread_suspension) << tlsPtr_.last_no_thread_suspension_cause;
Ian Rogers693ff612013-02-01 10:56:12 -080066 if (check_locks) {
67 bool bad_mutexes_held = false;
Elliott Hughes0f827162013-02-26 12:12:58 -080068 for (int i = kLockLevelCount - 1; i >= 0; --i) {
Ian Rogers693ff612013-02-01 10:56:12 -080069 // We expect no locks except the mutator_lock_.
70 if (i != kMutatorLock) {
71 BaseMutex* held_mutex = GetHeldMutex(static_cast<LockLevel>(i));
72 if (held_mutex != NULL) {
73 LOG(ERROR) << "holding \"" << held_mutex->GetName()
74 << "\" at point where thread suspension is expected";
75 bad_mutexes_held = true;
76 }
77 }
78 }
79 CHECK(!bad_mutexes_held);
80 }
81#endif
82}
83
84inline void Thread::TransitionFromRunnableToSuspended(ThreadState new_state) {
85 AssertThreadSuspensionIsAllowable();
86 DCHECK_NE(new_state, kRunnable);
87 DCHECK_EQ(this, Thread::Current());
88 // Change to non-runnable state, thereby appearing suspended to the system.
89 DCHECK_EQ(GetState(), kRunnable);
90 union StateAndFlags old_state_and_flags;
91 union StateAndFlags new_state_and_flags;
Dave Allison0f5f6bb2013-11-22 17:39:19 -080092 while (true) {
Ian Rogersdd7624d2014-03-14 17:43:00 -070093 old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
Ian Rogersd9c4fc92013-10-01 19:45:43 -070094 if (UNLIKELY((old_state_and_flags.as_struct.flags & kCheckpointRequest) != 0)) {
95 RunCheckpointFunction();
96 continue;
97 }
Dave Allison0f5f6bb2013-11-22 17:39:19 -080098 // Change the state but keep the current flags (kCheckpointRequest is clear).
99 DCHECK_EQ((old_state_and_flags.as_struct.flags & kCheckpointRequest), 0);
100 new_state_and_flags.as_struct.flags = old_state_and_flags.as_struct.flags;
Ian Rogers693ff612013-02-01 10:56:12 -0800101 new_state_and_flags.as_struct.state = new_state;
Dave Allison0f5f6bb2013-11-22 17:39:19 -0800102 int status = android_atomic_cas(old_state_and_flags.as_int, new_state_and_flags.as_int,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700103 &tls32_.state_and_flags.as_int);
Dave Allison0f5f6bb2013-11-22 17:39:19 -0800104 if (LIKELY(status == 0)) {
105 break;
106 }
107 }
Ian Rogers693ff612013-02-01 10:56:12 -0800108 // Release share on mutator_lock_.
109 Locks::mutator_lock_->SharedUnlock(this);
110}
111
112inline ThreadState Thread::TransitionFromSuspendedToRunnable() {
113 bool done = false;
Chris Dearman59cde532013-12-04 18:53:49 -0800114 union StateAndFlags old_state_and_flags;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700115 old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
Ian Rogers693ff612013-02-01 10:56:12 -0800116 int16_t old_state = old_state_and_flags.as_struct.state;
117 DCHECK_NE(static_cast<ThreadState>(old_state), kRunnable);
118 do {
119 Locks::mutator_lock_->AssertNotHeld(this); // Otherwise we starve GC..
Ian Rogersdd7624d2014-03-14 17:43:00 -0700120 old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
Ian Rogers693ff612013-02-01 10:56:12 -0800121 DCHECK_EQ(old_state_and_flags.as_struct.state, old_state);
122 if (UNLIKELY((old_state_and_flags.as_struct.flags & kSuspendRequest) != 0)) {
123 // Wait while our suspend count is non-zero.
124 MutexLock mu(this, *Locks::thread_suspend_count_lock_);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700125 old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
Ian Rogers693ff612013-02-01 10:56:12 -0800126 DCHECK_EQ(old_state_and_flags.as_struct.state, old_state);
127 while ((old_state_and_flags.as_struct.flags & kSuspendRequest) != 0) {
128 // Re-check when Thread::resume_cond_ is notified.
129 Thread::resume_cond_->Wait(this);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700130 old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
Ian Rogers693ff612013-02-01 10:56:12 -0800131 DCHECK_EQ(old_state_and_flags.as_struct.state, old_state);
132 }
133 DCHECK_EQ(GetSuspendCount(), 0);
134 }
135 // Re-acquire shared mutator_lock_ access.
136 Locks::mutator_lock_->SharedLock(this);
137 // Atomically change from suspended to runnable if no suspend request pending.
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 DCHECK_EQ(old_state_and_flags.as_struct.state, old_state);
140 if (LIKELY((old_state_and_flags.as_struct.flags & kSuspendRequest) == 0)) {
Chris Dearman59cde532013-12-04 18:53:49 -0800141 union StateAndFlags new_state_and_flags;
142 new_state_and_flags.as_int = old_state_and_flags.as_int;
Ian Rogers693ff612013-02-01 10:56:12 -0800143 new_state_and_flags.as_struct.state = kRunnable;
144 // CAS the value without a memory barrier, that occurred in the lock above.
145 done = android_atomic_cas(old_state_and_flags.as_int, new_state_and_flags.as_int,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700146 &tls32_.state_and_flags.as_int) == 0;
Ian Rogers693ff612013-02-01 10:56:12 -0800147 }
148 if (UNLIKELY(!done)) {
149 // Failed to transition to Runnable. Release shared mutator_lock_ access and try again.
150 Locks::mutator_lock_->SharedUnlock(this);
Ian Rogers719d1a32014-03-06 12:13:39 -0800151 } else {
152 return static_cast<ThreadState>(old_state);
Ian Rogers693ff612013-02-01 10:56:12 -0800153 }
Ian Rogers719d1a32014-03-06 12:13:39 -0800154 } while (true);
Ian Rogers693ff612013-02-01 10:56:12 -0800155}
156
Ian Rogers04d7aa92013-03-16 14:29:17 -0700157inline void Thread::VerifyStack() {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800158 if (kVerifyStack) {
159 if (Runtime::Current()->GetHeap()->IsObjectValidationEnabled()) {
160 VerifyStackImpl();
161 }
Ian Rogers04d7aa92013-03-16 14:29:17 -0700162 }
163}
164
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800165inline size_t Thread::TlabSize() const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700166 return tlsPtr_.thread_local_end - tlsPtr_.thread_local_pos;
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800167}
168
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800169inline mirror::Object* Thread::AllocTlab(size_t bytes) {
170 DCHECK_GE(TlabSize(), bytes);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700171 ++tlsPtr_.thread_local_objects;
172 mirror::Object* ret = reinterpret_cast<mirror::Object*>(tlsPtr_.thread_local_pos);
173 tlsPtr_.thread_local_pos += bytes;
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800174 return ret;
175}
176
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800177inline bool Thread::PushOnThreadLocalAllocationStack(mirror::Object* obj) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700178 DCHECK_LE(tlsPtr_.thread_local_alloc_stack_top, tlsPtr_.thread_local_alloc_stack_end);
179 if (tlsPtr_.thread_local_alloc_stack_top < tlsPtr_.thread_local_alloc_stack_end) {
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800180 // There's room.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700181 DCHECK_LE(reinterpret_cast<byte*>(tlsPtr_.thread_local_alloc_stack_top) +
182 sizeof(mirror::Object*),
183 reinterpret_cast<byte*>(tlsPtr_.thread_local_alloc_stack_end));
184 DCHECK(*tlsPtr_.thread_local_alloc_stack_top == nullptr);
185 *tlsPtr_.thread_local_alloc_stack_top = obj;
186 ++tlsPtr_.thread_local_alloc_stack_top;
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800187 return true;
188 }
189 return false;
190}
191
192inline void Thread::SetThreadLocalAllocationStack(mirror::Object** start, mirror::Object** end) {
193 DCHECK(Thread::Current() == this) << "Should be called by self";
194 DCHECK(start != nullptr);
195 DCHECK(end != nullptr);
196 DCHECK_ALIGNED(start, sizeof(mirror::Object*));
197 DCHECK_ALIGNED(end, sizeof(mirror::Object*));
198 DCHECK_LT(start, end);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700199 tlsPtr_.thread_local_alloc_stack_end = end;
200 tlsPtr_.thread_local_alloc_stack_top = start;
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800201}
202
203inline void Thread::RevokeThreadLocalAllocationStack() {
204 if (kIsDebugBuild) {
205 // Note: self is not necessarily equal to this thread since thread may be suspended.
206 Thread* self = Thread::Current();
207 DCHECK(this == self || IsSuspended() || GetState() == kWaitingPerformingGc)
208 << GetState() << " thread " << this << " self " << self;
209 }
Ian Rogersdd7624d2014-03-14 17:43:00 -0700210 tlsPtr_.thread_local_alloc_stack_end = nullptr;
211 tlsPtr_.thread_local_alloc_stack_top = nullptr;
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800212}
213
Ian Rogers693ff612013-02-01 10:56:12 -0800214} // namespace art
215
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700216#endif // ART_RUNTIME_THREAD_INL_H_