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" |
| 26 | #include "cutils/atomic-inline.h" |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 27 | #include "jni_internal.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 | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 48 | inline 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 Dearman | 59cde53 | 2013-12-04 18:53:49 -0800 | [diff] [blame] | 53 | union StateAndFlags old_state_and_flags; |
| 54 | old_state_and_flags.as_int = state_and_flags_.as_int; |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 55 | state_and_flags_.as_struct.state = new_state; |
| 56 | return static_cast<ThreadState>(old_state_and_flags.as_struct.state); |
| 57 | } |
| 58 | |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 59 | inline 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 Hughes | 0f82716 | 2013-02-26 12:12:58 -0800 | [diff] [blame] | 66 | for (int i = kLockLevelCount - 1; i >= 0; --i) { |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 67 | // 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 | |
| 82 | inline 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; |
| 90 | do { |
Chris Dearman | 59cde53 | 2013-12-04 18:53:49 -0800 | [diff] [blame] | 91 | old_state_and_flags.as_int = state_and_flags_.as_int; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 92 | if (UNLIKELY((old_state_and_flags.as_struct.flags & kCheckpointRequest) != 0)) { |
| 93 | RunCheckpointFunction(); |
| 94 | continue; |
| 95 | } |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 96 | // Copy over flags and try to clear the checkpoint bit if it is set. |
| 97 | new_state_and_flags.as_struct.flags = old_state_and_flags.as_struct.flags & ~kCheckpointRequest; |
| 98 | new_state_and_flags.as_struct.state = new_state; |
| 99 | // CAS the value without a memory barrier, that will occur in the unlock below. |
| 100 | } while (UNLIKELY(android_atomic_cas(old_state_and_flags.as_int, new_state_and_flags.as_int, |
| 101 | &state_and_flags_.as_int) != 0)); |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 102 | // Release share on mutator_lock_. |
| 103 | Locks::mutator_lock_->SharedUnlock(this); |
| 104 | } |
| 105 | |
| 106 | inline ThreadState Thread::TransitionFromSuspendedToRunnable() { |
| 107 | bool done = false; |
Chris Dearman | 59cde53 | 2013-12-04 18:53:49 -0800 | [diff] [blame] | 108 | union StateAndFlags old_state_and_flags; |
| 109 | old_state_and_flags.as_int = state_and_flags_.as_int; |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 110 | int16_t old_state = old_state_and_flags.as_struct.state; |
| 111 | DCHECK_NE(static_cast<ThreadState>(old_state), kRunnable); |
| 112 | do { |
| 113 | Locks::mutator_lock_->AssertNotHeld(this); // Otherwise we starve GC.. |
Chris Dearman | 59cde53 | 2013-12-04 18:53:49 -0800 | [diff] [blame] | 114 | old_state_and_flags.as_int = state_and_flags_.as_int; |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 115 | DCHECK_EQ(old_state_and_flags.as_struct.state, old_state); |
| 116 | if (UNLIKELY((old_state_and_flags.as_struct.flags & kSuspendRequest) != 0)) { |
| 117 | // Wait while our suspend count is non-zero. |
| 118 | MutexLock mu(this, *Locks::thread_suspend_count_lock_); |
Chris Dearman | 59cde53 | 2013-12-04 18:53:49 -0800 | [diff] [blame] | 119 | old_state_and_flags.as_int = state_and_flags_.as_int; |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 120 | DCHECK_EQ(old_state_and_flags.as_struct.state, old_state); |
| 121 | while ((old_state_and_flags.as_struct.flags & kSuspendRequest) != 0) { |
| 122 | // Re-check when Thread::resume_cond_ is notified. |
| 123 | Thread::resume_cond_->Wait(this); |
Chris Dearman | 59cde53 | 2013-12-04 18:53:49 -0800 | [diff] [blame] | 124 | old_state_and_flags.as_int = state_and_flags_.as_int; |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 125 | DCHECK_EQ(old_state_and_flags.as_struct.state, old_state); |
| 126 | } |
| 127 | DCHECK_EQ(GetSuspendCount(), 0); |
| 128 | } |
| 129 | // Re-acquire shared mutator_lock_ access. |
| 130 | Locks::mutator_lock_->SharedLock(this); |
| 131 | // Atomically change from suspended to runnable if no suspend request pending. |
Chris Dearman | 59cde53 | 2013-12-04 18:53:49 -0800 | [diff] [blame] | 132 | old_state_and_flags.as_int = state_and_flags_.as_int; |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 133 | DCHECK_EQ(old_state_and_flags.as_struct.state, old_state); |
| 134 | if (LIKELY((old_state_and_flags.as_struct.flags & kSuspendRequest) == 0)) { |
Chris Dearman | 59cde53 | 2013-12-04 18:53:49 -0800 | [diff] [blame] | 135 | union StateAndFlags new_state_and_flags; |
| 136 | new_state_and_flags.as_int = old_state_and_flags.as_int; |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 137 | new_state_and_flags.as_struct.state = kRunnable; |
| 138 | // CAS the value without a memory barrier, that occurred in the lock above. |
| 139 | done = android_atomic_cas(old_state_and_flags.as_int, new_state_and_flags.as_int, |
| 140 | &state_and_flags_.as_int) == 0; |
| 141 | } |
| 142 | if (UNLIKELY(!done)) { |
| 143 | // Failed to transition to Runnable. Release shared mutator_lock_ access and try again. |
| 144 | Locks::mutator_lock_->SharedUnlock(this); |
| 145 | } |
| 146 | } while (UNLIKELY(!done)); |
| 147 | return static_cast<ThreadState>(old_state); |
| 148 | } |
| 149 | |
Ian Rogers | 04d7aa9 | 2013-03-16 14:29:17 -0700 | [diff] [blame] | 150 | inline void Thread::VerifyStack() { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 151 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Ian Rogers | 04d7aa9 | 2013-03-16 14:29:17 -0700 | [diff] [blame] | 152 | if (heap->IsObjectValidationEnabled()) { |
| 153 | VerifyStackImpl(); |
| 154 | } |
| 155 | } |
| 156 | |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 157 | } // namespace art |
| 158 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 159 | #endif // ART_RUNTIME_THREAD_INL_H_ |