blob: e47fd372c7afc46a9c4d644cc4af05a3d55bf98c [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());
Chris Dearman59cde532013-12-04 18:53:49 -080053 union StateAndFlags old_state_and_flags;
54 old_state_and_flags.as_int = state_and_flags_.as_int;
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080055 state_and_flags_.as_struct.state = new_state;
56 return static_cast<ThreadState>(old_state_and_flags.as_struct.state);
57}
58
Ian Rogers693ff612013-02-01 10:56:12 -080059inline 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 Hughes0f827162013-02-26 12:12:58 -080066 for (int i = kLockLevelCount - 1; i >= 0; --i) {
Ian Rogers693ff612013-02-01 10:56:12 -080067 // 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
82inline 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 Dearman59cde532013-12-04 18:53:49 -080091 old_state_and_flags.as_int = state_and_flags_.as_int;
Ian Rogersd9c4fc92013-10-01 19:45:43 -070092 if (UNLIKELY((old_state_and_flags.as_struct.flags & kCheckpointRequest) != 0)) {
93 RunCheckpointFunction();
94 continue;
95 }
Ian Rogers693ff612013-02-01 10:56:12 -080096 // 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 Rogers693ff612013-02-01 10:56:12 -0800102 // Release share on mutator_lock_.
103 Locks::mutator_lock_->SharedUnlock(this);
104}
105
106inline ThreadState Thread::TransitionFromSuspendedToRunnable() {
107 bool done = false;
Chris Dearman59cde532013-12-04 18:53:49 -0800108 union StateAndFlags old_state_and_flags;
109 old_state_and_flags.as_int = state_and_flags_.as_int;
Ian Rogers693ff612013-02-01 10:56:12 -0800110 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 Dearman59cde532013-12-04 18:53:49 -0800114 old_state_and_flags.as_int = state_and_flags_.as_int;
Ian Rogers693ff612013-02-01 10:56:12 -0800115 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 Dearman59cde532013-12-04 18:53:49 -0800119 old_state_and_flags.as_int = state_and_flags_.as_int;
Ian Rogers693ff612013-02-01 10:56:12 -0800120 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 Dearman59cde532013-12-04 18:53:49 -0800124 old_state_and_flags.as_int = state_and_flags_.as_int;
Ian Rogers693ff612013-02-01 10:56:12 -0800125 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 Dearman59cde532013-12-04 18:53:49 -0800132 old_state_and_flags.as_int = state_and_flags_.as_int;
Ian Rogers693ff612013-02-01 10:56:12 -0800133 DCHECK_EQ(old_state_and_flags.as_struct.state, old_state);
134 if (LIKELY((old_state_and_flags.as_struct.flags & kSuspendRequest) == 0)) {
Chris Dearman59cde532013-12-04 18:53:49 -0800135 union StateAndFlags new_state_and_flags;
136 new_state_and_flags.as_int = old_state_and_flags.as_int;
Ian Rogers693ff612013-02-01 10:56:12 -0800137 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 Rogers04d7aa92013-03-16 14:29:17 -0700150inline void Thread::VerifyStack() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700151 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers04d7aa92013-03-16 14:29:17 -0700152 if (heap->IsObjectValidationEnabled()) {
153 VerifyStackImpl();
154 }
155}
156
Ian Rogers693ff612013-02-01 10:56:12 -0800157} // namespace art
158
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700159#endif // ART_RUNTIME_THREAD_INL_H_