Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 1 | /* |
| 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 Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_THREAD_INL_H_ |
| 18 | #define ART_RUNTIME_THREAD_INL_H_ |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 19 | |
| 20 | #include "thread.h" |
| 21 | |
Ian Rogers | 02ed4c0 | 2013-09-06 13:10:04 -0700 | [diff] [blame] | 22 | #include <pthread.h> |
| 23 | |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 24 | #include "base/casts.h" |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 25 | #include "base/mutex-inl.h" |
Ian Rogers | 576ca0c | 2014-06-06 15:58:22 -0700 | [diff] [blame] | 26 | #include "gc/heap.h" |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 27 | #include "jni_env_ext.h" |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 28 | |
| 29 | namespace art { |
| 30 | |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 31 | // Quickly access the current thread from a JNIEnv. |
| 32 | static inline Thread* ThreadForEnv(JNIEnv* env) { |
| 33 | JNIEnvExt* full_env(down_cast<JNIEnvExt*>(env)); |
| 34 | return full_env->self; |
| 35 | } |
| 36 | |
Ian Rogers | 02ed4c0 | 2013-09-06 13:10:04 -0700 | [diff] [blame] | 37 | inline 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 Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 48 | inline void Thread::AllowThreadSuspension() { |
| 49 | DCHECK_EQ(Thread::Current(), this); |
| 50 | if (UNLIKELY(TestAllFlags())) { |
| 51 | CheckSuspend(); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | inline 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 Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 68 | inline 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 Dearman | 59cde53 | 2013-12-04 18:53:49 -0800 | [diff] [blame] | 73 | union StateAndFlags old_state_and_flags; |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 74 | old_state_and_flags.as_int = tls32_.state_and_flags.as_int; |
| 75 | tls32_.state_and_flags.as_struct.state = new_state; |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 76 | return static_cast<ThreadState>(old_state_and_flags.as_struct.state); |
| 77 | } |
| 78 | |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 79 | inline void Thread::AssertThreadSuspensionIsAllowable(bool check_locks) const { |
Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 80 | if (kIsDebugBuild) { |
| 81 | CHECK_EQ(0u, tls32_.no_thread_suspension) << tlsPtr_.last_no_thread_suspension_cause; |
| 82 | if (check_locks) { |
| 83 | bool bad_mutexes_held = false; |
| 84 | for (int i = kLockLevelCount - 1; i >= 0; --i) { |
| 85 | // We expect no locks except the mutator_lock_ or thread list suspend thread lock. |
| 86 | if (i != kMutatorLock && i != kThreadListSuspendThreadLock) { |
| 87 | BaseMutex* held_mutex = GetHeldMutex(static_cast<LockLevel>(i)); |
| 88 | if (held_mutex != NULL) { |
| 89 | LOG(ERROR) << "holding \"" << held_mutex->GetName() |
| 90 | << "\" at point where thread suspension is expected"; |
| 91 | bad_mutexes_held = true; |
| 92 | } |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 93 | } |
| 94 | } |
Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 95 | CHECK(!bad_mutexes_held); |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 96 | } |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 97 | } |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | inline void Thread::TransitionFromRunnableToSuspended(ThreadState new_state) { |
| 101 | AssertThreadSuspensionIsAllowable(); |
| 102 | DCHECK_NE(new_state, kRunnable); |
| 103 | DCHECK_EQ(this, Thread::Current()); |
| 104 | // Change to non-runnable state, thereby appearing suspended to the system. |
| 105 | DCHECK_EQ(GetState(), kRunnable); |
| 106 | union StateAndFlags old_state_and_flags; |
| 107 | union StateAndFlags new_state_and_flags; |
Dave Allison | 0f5f6bb | 2013-11-22 17:39:19 -0800 | [diff] [blame] | 108 | while (true) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 109 | old_state_and_flags.as_int = tls32_.state_and_flags.as_int; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 110 | if (UNLIKELY((old_state_and_flags.as_struct.flags & kCheckpointRequest) != 0)) { |
| 111 | RunCheckpointFunction(); |
| 112 | continue; |
| 113 | } |
Dave Allison | 0f5f6bb | 2013-11-22 17:39:19 -0800 | [diff] [blame] | 114 | // Change the state but keep the current flags (kCheckpointRequest is clear). |
| 115 | DCHECK_EQ((old_state_and_flags.as_struct.flags & kCheckpointRequest), 0); |
| 116 | new_state_and_flags.as_struct.flags = old_state_and_flags.as_struct.flags; |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 117 | new_state_and_flags.as_struct.state = new_state; |
Ian Rogers | b8e087e | 2014-07-09 21:12:06 -0700 | [diff] [blame] | 118 | |
| 119 | // CAS the value without a memory ordering as that is given by the lock release below. |
| 120 | bool done = |
| 121 | tls32_.state_and_flags.as_atomic_int.CompareExchangeWeakRelaxed(old_state_and_flags.as_int, |
| 122 | new_state_and_flags.as_int); |
| 123 | if (LIKELY(done)) { |
Dave Allison | 0f5f6bb | 2013-11-22 17:39:19 -0800 | [diff] [blame] | 124 | break; |
| 125 | } |
| 126 | } |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 127 | // Release share on mutator_lock_. |
| 128 | Locks::mutator_lock_->SharedUnlock(this); |
| 129 | } |
| 130 | |
| 131 | inline ThreadState Thread::TransitionFromSuspendedToRunnable() { |
| 132 | bool done = false; |
Chris Dearman | 59cde53 | 2013-12-04 18:53:49 -0800 | [diff] [blame] | 133 | union StateAndFlags old_state_and_flags; |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 134 | old_state_and_flags.as_int = tls32_.state_and_flags.as_int; |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 135 | int16_t old_state = old_state_and_flags.as_struct.state; |
| 136 | DCHECK_NE(static_cast<ThreadState>(old_state), kRunnable); |
| 137 | do { |
| 138 | Locks::mutator_lock_->AssertNotHeld(this); // Otherwise we starve GC.. |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 139 | old_state_and_flags.as_int = tls32_.state_and_flags.as_int; |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 140 | DCHECK_EQ(old_state_and_flags.as_struct.state, old_state); |
| 141 | if (UNLIKELY((old_state_and_flags.as_struct.flags & kSuspendRequest) != 0)) { |
| 142 | // Wait while our suspend count is non-zero. |
| 143 | MutexLock mu(this, *Locks::thread_suspend_count_lock_); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 144 | old_state_and_flags.as_int = tls32_.state_and_flags.as_int; |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 145 | DCHECK_EQ(old_state_and_flags.as_struct.state, old_state); |
| 146 | while ((old_state_and_flags.as_struct.flags & kSuspendRequest) != 0) { |
| 147 | // Re-check when Thread::resume_cond_ is notified. |
| 148 | Thread::resume_cond_->Wait(this); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 149 | old_state_and_flags.as_int = tls32_.state_and_flags.as_int; |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 150 | DCHECK_EQ(old_state_and_flags.as_struct.state, old_state); |
| 151 | } |
| 152 | DCHECK_EQ(GetSuspendCount(), 0); |
| 153 | } |
| 154 | // Re-acquire shared mutator_lock_ access. |
| 155 | Locks::mutator_lock_->SharedLock(this); |
| 156 | // Atomically change from suspended to runnable if no suspend request pending. |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 157 | old_state_and_flags.as_int = tls32_.state_and_flags.as_int; |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 158 | DCHECK_EQ(old_state_and_flags.as_struct.state, old_state); |
| 159 | if (LIKELY((old_state_and_flags.as_struct.flags & kSuspendRequest) == 0)) { |
Chris Dearman | 59cde53 | 2013-12-04 18:53:49 -0800 | [diff] [blame] | 160 | union StateAndFlags new_state_and_flags; |
| 161 | new_state_and_flags.as_int = old_state_and_flags.as_int; |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 162 | new_state_and_flags.as_struct.state = kRunnable; |
Ian Rogers | b8e087e | 2014-07-09 21:12:06 -0700 | [diff] [blame] | 163 | // CAS the value without a memory ordering as that is given by the lock acquisition above. |
| 164 | done = |
| 165 | tls32_.state_and_flags.as_atomic_int.CompareExchangeWeakRelaxed(old_state_and_flags.as_int, |
| 166 | new_state_and_flags.as_int); |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 167 | } |
| 168 | if (UNLIKELY(!done)) { |
| 169 | // Failed to transition to Runnable. Release shared mutator_lock_ access and try again. |
| 170 | Locks::mutator_lock_->SharedUnlock(this); |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 171 | } else { |
| 172 | return static_cast<ThreadState>(old_state); |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 173 | } |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 174 | } while (true); |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Ian Rogers | 04d7aa9 | 2013-03-16 14:29:17 -0700 | [diff] [blame] | 177 | inline void Thread::VerifyStack() { |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 178 | if (kVerifyStack) { |
| 179 | if (Runtime::Current()->GetHeap()->IsObjectValidationEnabled()) { |
| 180 | VerifyStackImpl(); |
| 181 | } |
Ian Rogers | 04d7aa9 | 2013-03-16 14:29:17 -0700 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 185 | inline size_t Thread::TlabSize() const { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 186 | return tlsPtr_.thread_local_end - tlsPtr_.thread_local_pos; |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 187 | } |
| 188 | |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 189 | inline mirror::Object* Thread::AllocTlab(size_t bytes) { |
| 190 | DCHECK_GE(TlabSize(), bytes); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 191 | ++tlsPtr_.thread_local_objects; |
| 192 | mirror::Object* ret = reinterpret_cast<mirror::Object*>(tlsPtr_.thread_local_pos); |
| 193 | tlsPtr_.thread_local_pos += bytes; |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 194 | return ret; |
| 195 | } |
| 196 | |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 197 | inline bool Thread::PushOnThreadLocalAllocationStack(mirror::Object* obj) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 198 | DCHECK_LE(tlsPtr_.thread_local_alloc_stack_top, tlsPtr_.thread_local_alloc_stack_end); |
| 199 | if (tlsPtr_.thread_local_alloc_stack_top < tlsPtr_.thread_local_alloc_stack_end) { |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 200 | // There's room. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame^] | 201 | DCHECK_LE(reinterpret_cast<uint8_t*>(tlsPtr_.thread_local_alloc_stack_top) + |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 202 | sizeof(mirror::Object*), |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame^] | 203 | reinterpret_cast<uint8_t*>(tlsPtr_.thread_local_alloc_stack_end)); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 204 | DCHECK(*tlsPtr_.thread_local_alloc_stack_top == nullptr); |
| 205 | *tlsPtr_.thread_local_alloc_stack_top = obj; |
| 206 | ++tlsPtr_.thread_local_alloc_stack_top; |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 207 | return true; |
| 208 | } |
| 209 | return false; |
| 210 | } |
| 211 | |
| 212 | inline void Thread::SetThreadLocalAllocationStack(mirror::Object** start, mirror::Object** end) { |
| 213 | DCHECK(Thread::Current() == this) << "Should be called by self"; |
| 214 | DCHECK(start != nullptr); |
| 215 | DCHECK(end != nullptr); |
| 216 | DCHECK_ALIGNED(start, sizeof(mirror::Object*)); |
| 217 | DCHECK_ALIGNED(end, sizeof(mirror::Object*)); |
| 218 | DCHECK_LT(start, end); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 219 | tlsPtr_.thread_local_alloc_stack_end = end; |
| 220 | tlsPtr_.thread_local_alloc_stack_top = start; |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | inline void Thread::RevokeThreadLocalAllocationStack() { |
| 224 | if (kIsDebugBuild) { |
| 225 | // Note: self is not necessarily equal to this thread since thread may be suspended. |
| 226 | Thread* self = Thread::Current(); |
| 227 | DCHECK(this == self || IsSuspended() || GetState() == kWaitingPerformingGc) |
| 228 | << GetState() << " thread " << this << " self " << self; |
| 229 | } |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 230 | tlsPtr_.thread_local_alloc_stack_end = nullptr; |
| 231 | tlsPtr_.thread_local_alloc_stack_top = nullptr; |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 232 | } |
| 233 | |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 234 | } // namespace art |
| 235 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 236 | #endif // ART_RUNTIME_THREAD_INL_H_ |