blob: 4552062319b1025ee797054d1f951e9706fc1101 [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 Rogers693ff612013-02-01 10:56:12 -080024#include "base/mutex-inl.h"
25#include "cutils/atomic-inline.h"
26
27namespace art {
28
Ian Rogers02ed4c02013-09-06 13:10:04 -070029inline 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 Rogersc0fa3ad2013-02-05 00:11:55 -080040inline 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 Rogers693ff612013-02-01 10:56:12 -080050inline 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 Hughes0f827162013-02-26 12:12:58 -080057 for (int i = kLockLevelCount - 1; i >= 0; --i) {
Ian Rogers693ff612013-02-01 10:56:12 -080058 // 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
73inline 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_;
83 // Copy over flags and try to clear the checkpoint bit if it is set.
84 new_state_and_flags.as_struct.flags = old_state_and_flags.as_struct.flags & ~kCheckpointRequest;
85 new_state_and_flags.as_struct.state = new_state;
86 // CAS the value without a memory barrier, that will occur in the unlock below.
87 } while (UNLIKELY(android_atomic_cas(old_state_and_flags.as_int, new_state_and_flags.as_int,
88 &state_and_flags_.as_int) != 0));
89 // If we toggled the checkpoint flag we must have cleared it.
90 uint16_t flag_change = new_state_and_flags.as_struct.flags ^ old_state_and_flags.as_struct.flags;
91 if (UNLIKELY((flag_change & kCheckpointRequest) != 0)) {
92 RunCheckpointFunction();
93 }
94 // Release share on mutator_lock_.
95 Locks::mutator_lock_->SharedUnlock(this);
96}
97
98inline ThreadState Thread::TransitionFromSuspendedToRunnable() {
99 bool done = false;
100 union StateAndFlags old_state_and_flags = state_and_flags_;
101 int16_t old_state = old_state_and_flags.as_struct.state;
102 DCHECK_NE(static_cast<ThreadState>(old_state), kRunnable);
103 do {
104 Locks::mutator_lock_->AssertNotHeld(this); // Otherwise we starve GC..
105 old_state_and_flags = state_and_flags_;
106 DCHECK_EQ(old_state_and_flags.as_struct.state, old_state);
107 if (UNLIKELY((old_state_and_flags.as_struct.flags & kSuspendRequest) != 0)) {
108 // Wait while our suspend count is non-zero.
109 MutexLock mu(this, *Locks::thread_suspend_count_lock_);
110 old_state_and_flags = state_and_flags_;
111 DCHECK_EQ(old_state_and_flags.as_struct.state, old_state);
112 while ((old_state_and_flags.as_struct.flags & kSuspendRequest) != 0) {
113 // Re-check when Thread::resume_cond_ is notified.
114 Thread::resume_cond_->Wait(this);
115 old_state_and_flags = state_and_flags_;
116 DCHECK_EQ(old_state_and_flags.as_struct.state, old_state);
117 }
118 DCHECK_EQ(GetSuspendCount(), 0);
119 }
120 // Re-acquire shared mutator_lock_ access.
121 Locks::mutator_lock_->SharedLock(this);
122 // Atomically change from suspended to runnable if no suspend request pending.
123 old_state_and_flags = state_and_flags_;
124 DCHECK_EQ(old_state_and_flags.as_struct.state, old_state);
125 if (LIKELY((old_state_and_flags.as_struct.flags & kSuspendRequest) == 0)) {
126 union StateAndFlags new_state_and_flags = old_state_and_flags;
127 new_state_and_flags.as_struct.state = kRunnable;
128 // CAS the value without a memory barrier, that occurred in the lock above.
129 done = android_atomic_cas(old_state_and_flags.as_int, new_state_and_flags.as_int,
130 &state_and_flags_.as_int) == 0;
131 }
132 if (UNLIKELY(!done)) {
133 // Failed to transition to Runnable. Release shared mutator_lock_ access and try again.
134 Locks::mutator_lock_->SharedUnlock(this);
135 }
136 } while (UNLIKELY(!done));
137 return static_cast<ThreadState>(old_state);
138}
139
Ian Rogers04d7aa92013-03-16 14:29:17 -0700140inline void Thread::VerifyStack() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700141 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers04d7aa92013-03-16 14:29:17 -0700142 if (heap->IsObjectValidationEnabled()) {
143 VerifyStackImpl();
144 }
145}
146
Ian Rogers693ff612013-02-01 10:56:12 -0800147} // namespace art
148
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700149#endif // ART_RUNTIME_THREAD_INL_H_