blob: 84496072b119eb5ba3c1dadfd862b396fed572d6 [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 Rogers1eb512d2013-10-18 15:42:20 -070024#include "base/casts.h"
Ian Rogers693ff612013-02-01 10:56:12 -080025#include "base/mutex-inl.h"
26#include "cutils/atomic-inline.h"
Ian Rogers1eb512d2013-10-18 15:42:20 -070027#include "jni_internal.h"
Ian Rogers693ff612013-02-01 10:56:12 -080028
29namespace art {
30
Ian Rogers1eb512d2013-10-18 15:42:20 -070031// Quickly access the current thread from a JNIEnv.
32static inline Thread* ThreadForEnv(JNIEnv* env) {
33 JNIEnvExt* full_env(down_cast<JNIEnvExt*>(env));
34 return full_env->self;
35}
36
Ian Rogers02ed4c02013-09-06 13:10:04 -070037inline 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 Rogersc0fa3ad2013-02-05 00:11:55 -080048inline 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());
53 union StateAndFlags old_state_and_flags = state_and_flags_;
54 state_and_flags_.as_struct.state = new_state;
55 return static_cast<ThreadState>(old_state_and_flags.as_struct.state);
56}
57
Ian Rogers693ff612013-02-01 10:56:12 -080058inline void Thread::AssertThreadSuspensionIsAllowable(bool check_locks) const {
59#ifdef NDEBUG
60 UNUSED(check_locks); // Keep GCC happy about unused parameters.
61#else
62 CHECK_EQ(0u, no_thread_suspension_) << last_no_thread_suspension_cause_;
63 if (check_locks) {
64 bool bad_mutexes_held = false;
Elliott Hughes0f827162013-02-26 12:12:58 -080065 for (int i = kLockLevelCount - 1; i >= 0; --i) {
Ian Rogers693ff612013-02-01 10:56:12 -080066 // We expect no locks except the mutator_lock_.
67 if (i != kMutatorLock) {
68 BaseMutex* held_mutex = GetHeldMutex(static_cast<LockLevel>(i));
69 if (held_mutex != NULL) {
70 LOG(ERROR) << "holding \"" << held_mutex->GetName()
71 << "\" at point where thread suspension is expected";
72 bad_mutexes_held = true;
73 }
74 }
75 }
76 CHECK(!bad_mutexes_held);
77 }
78#endif
79}
80
81inline void Thread::TransitionFromRunnableToSuspended(ThreadState new_state) {
82 AssertThreadSuspensionIsAllowable();
83 DCHECK_NE(new_state, kRunnable);
84 DCHECK_EQ(this, Thread::Current());
85 // Change to non-runnable state, thereby appearing suspended to the system.
86 DCHECK_EQ(GetState(), kRunnable);
87 union StateAndFlags old_state_and_flags;
88 union StateAndFlags new_state_and_flags;
89 do {
90 old_state_and_flags = state_and_flags_;
Ian Rogersd9c4fc92013-10-01 19:45:43 -070091 if (UNLIKELY((old_state_and_flags.as_struct.flags & kCheckpointRequest) != 0)) {
92 RunCheckpointFunction();
93 continue;
94 }
Ian Rogers693ff612013-02-01 10:56:12 -080095 // Copy over flags and try to clear the checkpoint bit if it is set.
96 new_state_and_flags.as_struct.flags = old_state_and_flags.as_struct.flags & ~kCheckpointRequest;
97 new_state_and_flags.as_struct.state = new_state;
98 // CAS the value without a memory barrier, that will occur in the unlock below.
99 } while (UNLIKELY(android_atomic_cas(old_state_and_flags.as_int, new_state_and_flags.as_int,
100 &state_and_flags_.as_int) != 0));
Ian Rogers693ff612013-02-01 10:56:12 -0800101 // Release share on mutator_lock_.
102 Locks::mutator_lock_->SharedUnlock(this);
103}
104
105inline ThreadState Thread::TransitionFromSuspendedToRunnable() {
106 bool done = false;
107 union StateAndFlags old_state_and_flags = state_and_flags_;
108 int16_t old_state = old_state_and_flags.as_struct.state;
109 DCHECK_NE(static_cast<ThreadState>(old_state), kRunnable);
110 do {
111 Locks::mutator_lock_->AssertNotHeld(this); // Otherwise we starve GC..
112 old_state_and_flags = state_and_flags_;
113 DCHECK_EQ(old_state_and_flags.as_struct.state, old_state);
114 if (UNLIKELY((old_state_and_flags.as_struct.flags & kSuspendRequest) != 0)) {
115 // Wait while our suspend count is non-zero.
116 MutexLock mu(this, *Locks::thread_suspend_count_lock_);
117 old_state_and_flags = state_and_flags_;
118 DCHECK_EQ(old_state_and_flags.as_struct.state, old_state);
119 while ((old_state_and_flags.as_struct.flags & kSuspendRequest) != 0) {
120 // Re-check when Thread::resume_cond_ is notified.
121 Thread::resume_cond_->Wait(this);
122 old_state_and_flags = state_and_flags_;
123 DCHECK_EQ(old_state_and_flags.as_struct.state, old_state);
124 }
125 DCHECK_EQ(GetSuspendCount(), 0);
126 }
127 // Re-acquire shared mutator_lock_ access.
128 Locks::mutator_lock_->SharedLock(this);
129 // Atomically change from suspended to runnable if no suspend request pending.
130 old_state_and_flags = state_and_flags_;
131 DCHECK_EQ(old_state_and_flags.as_struct.state, old_state);
132 if (LIKELY((old_state_and_flags.as_struct.flags & kSuspendRequest) == 0)) {
133 union StateAndFlags new_state_and_flags = old_state_and_flags;
134 new_state_and_flags.as_struct.state = kRunnable;
135 // CAS the value without a memory barrier, that occurred in the lock above.
136 done = android_atomic_cas(old_state_and_flags.as_int, new_state_and_flags.as_int,
137 &state_and_flags_.as_int) == 0;
138 }
139 if (UNLIKELY(!done)) {
140 // Failed to transition to Runnable. Release shared mutator_lock_ access and try again.
141 Locks::mutator_lock_->SharedUnlock(this);
142 }
143 } while (UNLIKELY(!done));
144 return static_cast<ThreadState>(old_state);
145}
146
Ian Rogers04d7aa92013-03-16 14:29:17 -0700147inline void Thread::VerifyStack() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700148 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers04d7aa92013-03-16 14:29:17 -0700149 if (heap->IsObjectValidationEnabled()) {
150 VerifyStackImpl();
151 }
152}
153
Ian Rogers693ff612013-02-01 10:56:12 -0800154} // namespace art
155
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700156#endif // ART_RUNTIME_THREAD_INL_H_