blob: 7d28785f58421fab443b466ab77e569086a19508 [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_;
Ian Rogersd9c4fc92013-10-01 19:45:43 -070083 if (UNLIKELY((old_state_and_flags.as_struct.flags & kCheckpointRequest) != 0)) {
84 RunCheckpointFunction();
85 continue;
86 }
Ian Rogers693ff612013-02-01 10:56:12 -080087 // 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 Rogers693ff612013-02-01 10:56:12 -080093 // Release share on mutator_lock_.
94 Locks::mutator_lock_->SharedUnlock(this);
95}
96
97inline 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 Rogers04d7aa92013-03-16 14:29:17 -0700139inline void Thread::VerifyStack() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700140 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers04d7aa92013-03-16 14:29:17 -0700141 if (heap->IsObjectValidationEnabled()) {
142 VerifyStackImpl();
143 }
144}
145
Ian Rogers693ff612013-02-01 10:56:12 -0800146} // namespace art
147
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700148#endif // ART_RUNTIME_THREAD_INL_H_