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 | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 24 | #include "base/mutex-inl.h" |
| 25 | #include "cutils/atomic-inline.h" |
| 26 | |
| 27 | namespace art { |
| 28 | |
Ian Rogers | 02ed4c0 | 2013-09-06 13:10:04 -0700 | [diff] [blame] | 29 | inline Thread* Thread::Current() { |
| 30 | // We rely on Thread::Current returning NULL for a detached thread, so it's not obvious |
| 31 | // that we can replace this with a direct %fs access on x86. |
| 32 | if (!is_started_) { |
| 33 | return NULL; |
| 34 | } else { |
| 35 | void* thread = pthread_getspecific(Thread::pthread_key_self_); |
| 36 | return reinterpret_cast<Thread*>(thread); |
| 37 | } |
| 38 | } |
| 39 | |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 40 | inline ThreadState Thread::SetState(ThreadState new_state) { |
| 41 | // Cannot use this code to change into Runnable as changing to Runnable should fail if |
| 42 | // old_state_and_flags.suspend_request is true. |
| 43 | DCHECK_NE(new_state, kRunnable); |
| 44 | DCHECK_EQ(this, Thread::Current()); |
| 45 | union StateAndFlags old_state_and_flags = state_and_flags_; |
| 46 | state_and_flags_.as_struct.state = new_state; |
| 47 | return static_cast<ThreadState>(old_state_and_flags.as_struct.state); |
| 48 | } |
| 49 | |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 50 | inline void Thread::AssertThreadSuspensionIsAllowable(bool check_locks) const { |
| 51 | #ifdef NDEBUG |
| 52 | UNUSED(check_locks); // Keep GCC happy about unused parameters. |
| 53 | #else |
| 54 | CHECK_EQ(0u, no_thread_suspension_) << last_no_thread_suspension_cause_; |
| 55 | if (check_locks) { |
| 56 | bool bad_mutexes_held = false; |
Elliott Hughes | 0f82716 | 2013-02-26 12:12:58 -0800 | [diff] [blame] | 57 | for (int i = kLockLevelCount - 1; i >= 0; --i) { |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 58 | // We expect no locks except the mutator_lock_. |
| 59 | if (i != kMutatorLock) { |
| 60 | BaseMutex* held_mutex = GetHeldMutex(static_cast<LockLevel>(i)); |
| 61 | if (held_mutex != NULL) { |
| 62 | LOG(ERROR) << "holding \"" << held_mutex->GetName() |
| 63 | << "\" at point where thread suspension is expected"; |
| 64 | bad_mutexes_held = true; |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | CHECK(!bad_mutexes_held); |
| 69 | } |
| 70 | #endif |
| 71 | } |
| 72 | |
| 73 | inline void Thread::TransitionFromRunnableToSuspended(ThreadState new_state) { |
| 74 | AssertThreadSuspensionIsAllowable(); |
| 75 | DCHECK_NE(new_state, kRunnable); |
| 76 | DCHECK_EQ(this, Thread::Current()); |
| 77 | // Change to non-runnable state, thereby appearing suspended to the system. |
| 78 | DCHECK_EQ(GetState(), kRunnable); |
| 79 | union StateAndFlags old_state_and_flags; |
| 80 | union StateAndFlags new_state_and_flags; |
| 81 | do { |
| 82 | old_state_and_flags = state_and_flags_; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame^] | 83 | if (UNLIKELY((old_state_and_flags.as_struct.flags & kCheckpointRequest) != 0)) { |
| 84 | RunCheckpointFunction(); |
| 85 | continue; |
| 86 | } |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 87 | // Copy over flags and try to clear the checkpoint bit if it is set. |
| 88 | new_state_and_flags.as_struct.flags = old_state_and_flags.as_struct.flags & ~kCheckpointRequest; |
| 89 | new_state_and_flags.as_struct.state = new_state; |
| 90 | // CAS the value without a memory barrier, that will occur in the unlock below. |
| 91 | } while (UNLIKELY(android_atomic_cas(old_state_and_flags.as_int, new_state_and_flags.as_int, |
| 92 | &state_and_flags_.as_int) != 0)); |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 93 | // Release share on mutator_lock_. |
| 94 | Locks::mutator_lock_->SharedUnlock(this); |
| 95 | } |
| 96 | |
| 97 | inline ThreadState Thread::TransitionFromSuspendedToRunnable() { |
| 98 | bool done = false; |
| 99 | union StateAndFlags old_state_and_flags = state_and_flags_; |
| 100 | int16_t old_state = old_state_and_flags.as_struct.state; |
| 101 | DCHECK_NE(static_cast<ThreadState>(old_state), kRunnable); |
| 102 | do { |
| 103 | Locks::mutator_lock_->AssertNotHeld(this); // Otherwise we starve GC.. |
| 104 | old_state_and_flags = state_and_flags_; |
| 105 | DCHECK_EQ(old_state_and_flags.as_struct.state, old_state); |
| 106 | if (UNLIKELY((old_state_and_flags.as_struct.flags & kSuspendRequest) != 0)) { |
| 107 | // Wait while our suspend count is non-zero. |
| 108 | MutexLock mu(this, *Locks::thread_suspend_count_lock_); |
| 109 | old_state_and_flags = state_and_flags_; |
| 110 | DCHECK_EQ(old_state_and_flags.as_struct.state, old_state); |
| 111 | while ((old_state_and_flags.as_struct.flags & kSuspendRequest) != 0) { |
| 112 | // Re-check when Thread::resume_cond_ is notified. |
| 113 | Thread::resume_cond_->Wait(this); |
| 114 | old_state_and_flags = state_and_flags_; |
| 115 | DCHECK_EQ(old_state_and_flags.as_struct.state, old_state); |
| 116 | } |
| 117 | DCHECK_EQ(GetSuspendCount(), 0); |
| 118 | } |
| 119 | // Re-acquire shared mutator_lock_ access. |
| 120 | Locks::mutator_lock_->SharedLock(this); |
| 121 | // Atomically change from suspended to runnable if no suspend request pending. |
| 122 | old_state_and_flags = state_and_flags_; |
| 123 | DCHECK_EQ(old_state_and_flags.as_struct.state, old_state); |
| 124 | if (LIKELY((old_state_and_flags.as_struct.flags & kSuspendRequest) == 0)) { |
| 125 | union StateAndFlags new_state_and_flags = old_state_and_flags; |
| 126 | new_state_and_flags.as_struct.state = kRunnable; |
| 127 | // CAS the value without a memory barrier, that occurred in the lock above. |
| 128 | done = android_atomic_cas(old_state_and_flags.as_int, new_state_and_flags.as_int, |
| 129 | &state_and_flags_.as_int) == 0; |
| 130 | } |
| 131 | if (UNLIKELY(!done)) { |
| 132 | // Failed to transition to Runnable. Release shared mutator_lock_ access and try again. |
| 133 | Locks::mutator_lock_->SharedUnlock(this); |
| 134 | } |
| 135 | } while (UNLIKELY(!done)); |
| 136 | return static_cast<ThreadState>(old_state); |
| 137 | } |
| 138 | |
Ian Rogers | 04d7aa9 | 2013-03-16 14:29:17 -0700 | [diff] [blame] | 139 | inline void Thread::VerifyStack() { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 140 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Ian Rogers | 04d7aa9 | 2013-03-16 14:29:17 -0700 | [diff] [blame] | 141 | if (heap->IsObjectValidationEnabled()) { |
| 142 | VerifyStackImpl(); |
| 143 | } |
| 144 | } |
| 145 | |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 146 | } // namespace art |
| 147 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 148 | #endif // ART_RUNTIME_THREAD_INL_H_ |