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" |
Mathieu Chartier | 3cf2253 | 2015-07-09 15:15:09 -0700 | [diff] [blame] | 28 | #include "thread_pool.h" |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 29 | |
| 30 | namespace art { |
| 31 | |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 32 | // Quickly access the current thread from a JNIEnv. |
| 33 | static inline Thread* ThreadForEnv(JNIEnv* env) { |
| 34 | JNIEnvExt* full_env(down_cast<JNIEnvExt*>(env)); |
| 35 | return full_env->self; |
| 36 | } |
| 37 | |
Ian Rogers | 02ed4c0 | 2013-09-06 13:10:04 -0700 | [diff] [blame] | 38 | inline Thread* Thread::Current() { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 39 | // We rely on Thread::Current returning null for a detached thread, so it's not obvious |
Ian Rogers | 02ed4c0 | 2013-09-06 13:10:04 -0700 | [diff] [blame] | 40 | // that we can replace this with a direct %fs access on x86. |
| 41 | if (!is_started_) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 42 | return nullptr; |
Ian Rogers | 02ed4c0 | 2013-09-06 13:10:04 -0700 | [diff] [blame] | 43 | } else { |
| 44 | void* thread = pthread_getspecific(Thread::pthread_key_self_); |
| 45 | return reinterpret_cast<Thread*>(thread); |
| 46 | } |
| 47 | } |
| 48 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 49 | inline void Thread::AllowThreadSuspension() { |
| 50 | DCHECK_EQ(Thread::Current(), this); |
| 51 | if (UNLIKELY(TestAllFlags())) { |
| 52 | CheckSuspend(); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | inline 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 Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 69 | inline ThreadState Thread::SetState(ThreadState new_state) { |
Yu Li | eac4424 | 2015-06-29 10:50:03 +0800 | [diff] [blame] | 70 | // Should only be used to change between suspended states. |
| 71 | // Cannot use this code to change into or from Runnable as changing to Runnable should |
| 72 | // fail if old_state_and_flags.suspend_request is true and changing from Runnable might |
| 73 | // miss passing an active suspend barrier. |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 74 | DCHECK_NE(new_state, kRunnable); |
Andreas Gampe | ef048f6 | 2014-11-25 22:12:27 -0800 | [diff] [blame] | 75 | if (kIsDebugBuild && this != Thread::Current()) { |
| 76 | std::string name; |
| 77 | GetThreadName(name); |
| 78 | LOG(FATAL) << "Thread \"" << name << "\"(" << this << " != Thread::Current()=" |
| 79 | << Thread::Current() << ") changing state to " << new_state; |
| 80 | } |
Chris Dearman | 59cde53 | 2013-12-04 18:53:49 -0800 | [diff] [blame] | 81 | union StateAndFlags old_state_and_flags; |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 82 | old_state_and_flags.as_int = tls32_.state_and_flags.as_int; |
Yu Li | eac4424 | 2015-06-29 10:50:03 +0800 | [diff] [blame] | 83 | CHECK_NE(old_state_and_flags.as_struct.state, kRunnable); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 84 | tls32_.state_and_flags.as_struct.state = new_state; |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 85 | return static_cast<ThreadState>(old_state_and_flags.as_struct.state); |
| 86 | } |
| 87 | |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 88 | inline void Thread::AssertThreadSuspensionIsAllowable(bool check_locks) const { |
Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 89 | if (kIsDebugBuild) { |
Nicolas Geoffray | db97871 | 2014-12-09 13:33:38 +0000 | [diff] [blame] | 90 | if (gAborting == 0) { |
| 91 | CHECK_EQ(0u, tls32_.no_thread_suspension) << tlsPtr_.last_no_thread_suspension_cause; |
| 92 | } |
Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 93 | if (check_locks) { |
| 94 | bool bad_mutexes_held = false; |
| 95 | for (int i = kLockLevelCount - 1; i >= 0; --i) { |
| 96 | // We expect no locks except the mutator_lock_ or thread list suspend thread lock. |
Ian Rogers | 4ad5cd3 | 2014-11-11 23:08:07 -0800 | [diff] [blame] | 97 | if (i != kMutatorLock) { |
Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 98 | BaseMutex* held_mutex = GetHeldMutex(static_cast<LockLevel>(i)); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 99 | if (held_mutex != nullptr) { |
Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 100 | LOG(ERROR) << "holding \"" << held_mutex->GetName() |
| 101 | << "\" at point where thread suspension is expected"; |
| 102 | bad_mutexes_held = true; |
| 103 | } |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 104 | } |
| 105 | } |
Nicolas Geoffray | db97871 | 2014-12-09 13:33:38 +0000 | [diff] [blame] | 106 | if (gAborting == 0) { |
| 107 | CHECK(!bad_mutexes_held); |
| 108 | } |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 109 | } |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 110 | } |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | inline void Thread::TransitionFromRunnableToSuspended(ThreadState new_state) { |
| 114 | AssertThreadSuspensionIsAllowable(); |
| 115 | DCHECK_NE(new_state, kRunnable); |
| 116 | DCHECK_EQ(this, Thread::Current()); |
| 117 | // Change to non-runnable state, thereby appearing suspended to the system. |
| 118 | DCHECK_EQ(GetState(), kRunnable); |
| 119 | union StateAndFlags old_state_and_flags; |
| 120 | union StateAndFlags new_state_and_flags; |
Dave Allison | 0f5f6bb | 2013-11-22 17:39:19 -0800 | [diff] [blame] | 121 | while (true) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 122 | old_state_and_flags.as_int = tls32_.state_and_flags.as_int; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 123 | if (UNLIKELY((old_state_and_flags.as_struct.flags & kCheckpointRequest) != 0)) { |
| 124 | RunCheckpointFunction(); |
| 125 | continue; |
| 126 | } |
Dave Allison | 0f5f6bb | 2013-11-22 17:39:19 -0800 | [diff] [blame] | 127 | // Change the state but keep the current flags (kCheckpointRequest is clear). |
| 128 | DCHECK_EQ((old_state_and_flags.as_struct.flags & kCheckpointRequest), 0); |
| 129 | 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] | 130 | new_state_and_flags.as_struct.state = new_state; |
Ian Rogers | b8e087e | 2014-07-09 21:12:06 -0700 | [diff] [blame] | 131 | |
Yu Li | eac4424 | 2015-06-29 10:50:03 +0800 | [diff] [blame] | 132 | // CAS the value with a memory ordering. |
Ian Rogers | b8e087e | 2014-07-09 21:12:06 -0700 | [diff] [blame] | 133 | bool done = |
Yu Li | eac4424 | 2015-06-29 10:50:03 +0800 | [diff] [blame] | 134 | tls32_.state_and_flags.as_atomic_int.CompareExchangeWeakRelease(old_state_and_flags.as_int, |
Ian Rogers | b8e087e | 2014-07-09 21:12:06 -0700 | [diff] [blame] | 135 | new_state_and_flags.as_int); |
| 136 | if (LIKELY(done)) { |
Dave Allison | 0f5f6bb | 2013-11-22 17:39:19 -0800 | [diff] [blame] | 137 | break; |
| 138 | } |
| 139 | } |
Yu Li | eac4424 | 2015-06-29 10:50:03 +0800 | [diff] [blame] | 140 | |
| 141 | // Change to non-runnable state, thereby appearing suspended to the system. |
| 142 | // Mark the release of the share of the mutator_lock_. |
| 143 | Locks::mutator_lock_->TransitionFromRunnableToSuspended(this); |
| 144 | |
| 145 | // Once suspended - check the active suspend barrier flag |
| 146 | while (true) { |
| 147 | uint16_t current_flags = tls32_.state_and_flags.as_struct.flags; |
| 148 | if (LIKELY((current_flags & (kCheckpointRequest | kActiveSuspendBarrier)) == 0)) { |
| 149 | break; |
| 150 | } else if ((current_flags & kActiveSuspendBarrier) != 0) { |
| 151 | PassActiveSuspendBarriers(this); |
| 152 | } else { |
| 153 | // Impossible |
| 154 | LOG(FATAL) << "Fatal, thread transited into suspended without running the checkpoint"; |
| 155 | } |
| 156 | } |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | inline ThreadState Thread::TransitionFromSuspendedToRunnable() { |
Chris Dearman | 59cde53 | 2013-12-04 18:53:49 -0800 | [diff] [blame] | 160 | union StateAndFlags old_state_and_flags; |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 161 | old_state_and_flags.as_int = tls32_.state_and_flags.as_int; |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 162 | int16_t old_state = old_state_and_flags.as_struct.state; |
| 163 | DCHECK_NE(static_cast<ThreadState>(old_state), kRunnable); |
| 164 | do { |
| 165 | Locks::mutator_lock_->AssertNotHeld(this); // Otherwise we starve GC.. |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 166 | old_state_and_flags.as_int = tls32_.state_and_flags.as_int; |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 167 | DCHECK_EQ(old_state_and_flags.as_struct.state, old_state); |
Yu Li | eac4424 | 2015-06-29 10:50:03 +0800 | [diff] [blame] | 168 | if (LIKELY(old_state_and_flags.as_struct.flags == 0)) { |
| 169 | // Optimize for the return from native code case - this is the fast path. |
| 170 | // Atomically change from suspended to runnable if no suspend request pending. |
| 171 | union StateAndFlags new_state_and_flags; |
| 172 | new_state_and_flags.as_int = old_state_and_flags.as_int; |
| 173 | new_state_and_flags.as_struct.state = kRunnable; |
| 174 | // CAS the value with a memory barrier. |
| 175 | if (LIKELY(tls32_.state_and_flags.as_atomic_int.CompareExchangeWeakAcquire( |
| 176 | old_state_and_flags.as_int, |
| 177 | new_state_and_flags.as_int))) { |
| 178 | // Mark the acquisition of a share of the mutator_lock_. |
| 179 | Locks::mutator_lock_->TransitionFromSuspendedToRunnable(this); |
| 180 | break; |
| 181 | } |
| 182 | } else if ((old_state_and_flags.as_struct.flags & kActiveSuspendBarrier) != 0) { |
| 183 | PassActiveSuspendBarriers(this); |
| 184 | } else if ((old_state_and_flags.as_struct.flags & kCheckpointRequest) != 0) { |
| 185 | // Impossible |
| 186 | LOG(FATAL) << "Fatal, wrong checkpoint flag"; |
| 187 | } else if ((old_state_and_flags.as_struct.flags & kSuspendRequest) != 0) { |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 188 | // Wait while our suspend count is non-zero. |
| 189 | MutexLock mu(this, *Locks::thread_suspend_count_lock_); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 190 | old_state_and_flags.as_int = tls32_.state_and_flags.as_int; |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 191 | DCHECK_EQ(old_state_and_flags.as_struct.state, old_state); |
| 192 | while ((old_state_and_flags.as_struct.flags & kSuspendRequest) != 0) { |
| 193 | // Re-check when Thread::resume_cond_ is notified. |
| 194 | Thread::resume_cond_->Wait(this); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 195 | old_state_and_flags.as_int = tls32_.state_and_flags.as_int; |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 196 | DCHECK_EQ(old_state_and_flags.as_struct.state, old_state); |
| 197 | } |
| 198 | DCHECK_EQ(GetSuspendCount(), 0); |
| 199 | } |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 200 | } while (true); |
Yu Li | eac4424 | 2015-06-29 10:50:03 +0800 | [diff] [blame] | 201 | // Run the flip function, if set. |
| 202 | Closure* flip_func = GetFlipFunction(); |
| 203 | if (flip_func != nullptr) { |
| 204 | flip_func->Run(this); |
| 205 | } |
| 206 | return static_cast<ThreadState>(old_state); |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 207 | } |
| 208 | |
Ian Rogers | 04d7aa9 | 2013-03-16 14:29:17 -0700 | [diff] [blame] | 209 | inline void Thread::VerifyStack() { |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 210 | if (kVerifyStack) { |
| 211 | if (Runtime::Current()->GetHeap()->IsObjectValidationEnabled()) { |
| 212 | VerifyStackImpl(); |
| 213 | } |
Ian Rogers | 04d7aa9 | 2013-03-16 14:29:17 -0700 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 217 | inline size_t Thread::TlabSize() const { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 218 | return tlsPtr_.thread_local_end - tlsPtr_.thread_local_pos; |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 219 | } |
| 220 | |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 221 | inline mirror::Object* Thread::AllocTlab(size_t bytes) { |
| 222 | DCHECK_GE(TlabSize(), bytes); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 223 | ++tlsPtr_.thread_local_objects; |
| 224 | mirror::Object* ret = reinterpret_cast<mirror::Object*>(tlsPtr_.thread_local_pos); |
| 225 | tlsPtr_.thread_local_pos += bytes; |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 226 | return ret; |
| 227 | } |
| 228 | |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 229 | inline bool Thread::PushOnThreadLocalAllocationStack(mirror::Object* obj) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 230 | DCHECK_LE(tlsPtr_.thread_local_alloc_stack_top, tlsPtr_.thread_local_alloc_stack_end); |
| 231 | 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] | 232 | // There's room. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 233 | DCHECK_LE(reinterpret_cast<uint8_t*>(tlsPtr_.thread_local_alloc_stack_top) + |
Mathieu Chartier | cb535da | 2015-01-23 13:50:03 -0800 | [diff] [blame] | 234 | sizeof(StackReference<mirror::Object>), |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 235 | reinterpret_cast<uint8_t*>(tlsPtr_.thread_local_alloc_stack_end)); |
Mathieu Chartier | cb535da | 2015-01-23 13:50:03 -0800 | [diff] [blame] | 236 | DCHECK(tlsPtr_.thread_local_alloc_stack_top->AsMirrorPtr() == nullptr); |
| 237 | tlsPtr_.thread_local_alloc_stack_top->Assign(obj); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 238 | ++tlsPtr_.thread_local_alloc_stack_top; |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 239 | return true; |
| 240 | } |
| 241 | return false; |
| 242 | } |
| 243 | |
Mathieu Chartier | cb535da | 2015-01-23 13:50:03 -0800 | [diff] [blame] | 244 | inline void Thread::SetThreadLocalAllocationStack(StackReference<mirror::Object>* start, |
| 245 | StackReference<mirror::Object>* end) { |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 246 | DCHECK(Thread::Current() == this) << "Should be called by self"; |
| 247 | DCHECK(start != nullptr); |
| 248 | DCHECK(end != nullptr); |
Mathieu Chartier | cb535da | 2015-01-23 13:50:03 -0800 | [diff] [blame] | 249 | DCHECK_ALIGNED(start, sizeof(StackReference<mirror::Object>)); |
| 250 | DCHECK_ALIGNED(end, sizeof(StackReference<mirror::Object>)); |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 251 | DCHECK_LT(start, end); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 252 | tlsPtr_.thread_local_alloc_stack_end = end; |
| 253 | tlsPtr_.thread_local_alloc_stack_top = start; |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | inline void Thread::RevokeThreadLocalAllocationStack() { |
| 257 | if (kIsDebugBuild) { |
| 258 | // Note: self is not necessarily equal to this thread since thread may be suspended. |
| 259 | Thread* self = Thread::Current(); |
| 260 | DCHECK(this == self || IsSuspended() || GetState() == kWaitingPerformingGc) |
| 261 | << GetState() << " thread " << this << " self " << self; |
| 262 | } |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 263 | tlsPtr_.thread_local_alloc_stack_end = nullptr; |
| 264 | tlsPtr_.thread_local_alloc_stack_top = nullptr; |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 265 | } |
| 266 | |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 267 | } // namespace art |
| 268 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 269 | #endif // ART_RUNTIME_THREAD_INL_H_ |