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 
17 #ifndef ART_RUNTIME_THREAD_INL_H_
18 #define ART_RUNTIME_THREAD_INL_H_
19 
20 #include "thread.h"
21 
22 #include <pthread.h>
23 
24 #include "base/casts.h"
25 #include "base/mutex-inl.h"
26 #include "gc/heap.h"
27 #include "jni_internal.h"
28 
29 namespace art {
30 
31 // Quickly access the current thread from a JNIEnv.
ThreadForEnv(JNIEnv * env)32 static inline Thread* ThreadForEnv(JNIEnv* env) {
33   JNIEnvExt* full_env(down_cast<JNIEnvExt*>(env));
34   return full_env->self;
35 }
36 
Current()37 inline 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 
SetState(ThreadState new_state)48 inline 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;
54   old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
55   tls32_.state_and_flags.as_struct.state = new_state;
56   return static_cast<ThreadState>(old_state_and_flags.as_struct.state);
57 }
58 
AssertThreadSuspensionIsAllowable(bool check_locks)59 inline void Thread::AssertThreadSuspensionIsAllowable(bool check_locks) const {
60   if (kIsDebugBuild) {
61     CHECK_EQ(0u, tls32_.no_thread_suspension) << tlsPtr_.last_no_thread_suspension_cause;
62     if (check_locks) {
63       bool bad_mutexes_held = false;
64       for (int i = kLockLevelCount - 1; i >= 0; --i) {
65         // We expect no locks except the mutator_lock_ or thread list suspend thread lock.
66         if (i != kMutatorLock && i != kThreadListSuspendThreadLock) {
67           BaseMutex* held_mutex = GetHeldMutex(static_cast<LockLevel>(i));
68           if (held_mutex != NULL) {
69             LOG(ERROR) << "holding \"" << held_mutex->GetName()
70                       << "\" at point where thread suspension is expected";
71             bad_mutexes_held = true;
72           }
73         }
74       }
75       CHECK(!bad_mutexes_held);
76     }
77   }
78 }
79 
TransitionFromRunnableToSuspended(ThreadState new_state)80 inline void Thread::TransitionFromRunnableToSuspended(ThreadState new_state) {
81   AssertThreadSuspensionIsAllowable();
82   DCHECK_NE(new_state, kRunnable);
83   DCHECK_EQ(this, Thread::Current());
84   // Change to non-runnable state, thereby appearing suspended to the system.
85   DCHECK_EQ(GetState(), kRunnable);
86   union StateAndFlags old_state_and_flags;
87   union StateAndFlags new_state_and_flags;
88   while (true) {
89     old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
90     if (UNLIKELY((old_state_and_flags.as_struct.flags & kCheckpointRequest) != 0)) {
91       RunCheckpointFunction();
92       continue;
93     }
94     // Change the state but keep the current flags (kCheckpointRequest is clear).
95     DCHECK_EQ((old_state_and_flags.as_struct.flags & kCheckpointRequest), 0);
96     new_state_and_flags.as_struct.flags = old_state_and_flags.as_struct.flags;
97     new_state_and_flags.as_struct.state = new_state;
98 
99     // CAS the value without a memory ordering as that is given by the lock release below.
100     bool done =
101         tls32_.state_and_flags.as_atomic_int.CompareExchangeWeakRelaxed(old_state_and_flags.as_int,
102                                                                         new_state_and_flags.as_int);
103     if (LIKELY(done)) {
104       break;
105     }
106   }
107   // Release share on mutator_lock_.
108   Locks::mutator_lock_->SharedUnlock(this);
109 }
110 
TransitionFromSuspendedToRunnable()111 inline ThreadState Thread::TransitionFromSuspendedToRunnable() {
112   bool done = false;
113   union StateAndFlags old_state_and_flags;
114   old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
115   int16_t old_state = old_state_and_flags.as_struct.state;
116   DCHECK_NE(static_cast<ThreadState>(old_state), kRunnable);
117   do {
118     Locks::mutator_lock_->AssertNotHeld(this);  // Otherwise we starve GC..
119     old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
120     DCHECK_EQ(old_state_and_flags.as_struct.state, old_state);
121     if (UNLIKELY((old_state_and_flags.as_struct.flags & kSuspendRequest) != 0)) {
122       // Wait while our suspend count is non-zero.
123       MutexLock mu(this, *Locks::thread_suspend_count_lock_);
124       old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
125       DCHECK_EQ(old_state_and_flags.as_struct.state, old_state);
126       while ((old_state_and_flags.as_struct.flags & kSuspendRequest) != 0) {
127         // Re-check when Thread::resume_cond_ is notified.
128         Thread::resume_cond_->Wait(this);
129         old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
130         DCHECK_EQ(old_state_and_flags.as_struct.state, old_state);
131       }
132       DCHECK_EQ(GetSuspendCount(), 0);
133     }
134     // Re-acquire shared mutator_lock_ access.
135     Locks::mutator_lock_->SharedLock(this);
136     // Atomically change from suspended to runnable if no suspend request pending.
137     old_state_and_flags.as_int = tls32_.state_and_flags.as_int;
138     DCHECK_EQ(old_state_and_flags.as_struct.state, old_state);
139     if (LIKELY((old_state_and_flags.as_struct.flags & kSuspendRequest) == 0)) {
140       union StateAndFlags new_state_and_flags;
141       new_state_and_flags.as_int = old_state_and_flags.as_int;
142       new_state_and_flags.as_struct.state = kRunnable;
143       // CAS the value without a memory ordering as that is given by the lock acquisition above.
144       done =
145           tls32_.state_and_flags.as_atomic_int.CompareExchangeWeakRelaxed(old_state_and_flags.as_int,
146                                                                           new_state_and_flags.as_int);
147     }
148     if (UNLIKELY(!done)) {
149       // Failed to transition to Runnable. Release shared mutator_lock_ access and try again.
150       Locks::mutator_lock_->SharedUnlock(this);
151     } else {
152       return static_cast<ThreadState>(old_state);
153     }
154   } while (true);
155 }
156 
VerifyStack()157 inline void Thread::VerifyStack() {
158   if (kVerifyStack) {
159     if (Runtime::Current()->GetHeap()->IsObjectValidationEnabled()) {
160       VerifyStackImpl();
161     }
162   }
163 }
164 
TlabSize()165 inline size_t Thread::TlabSize() const {
166   return tlsPtr_.thread_local_end - tlsPtr_.thread_local_pos;
167 }
168 
AllocTlab(size_t bytes)169 inline mirror::Object* Thread::AllocTlab(size_t bytes) {
170   DCHECK_GE(TlabSize(), bytes);
171   ++tlsPtr_.thread_local_objects;
172   mirror::Object* ret = reinterpret_cast<mirror::Object*>(tlsPtr_.thread_local_pos);
173   tlsPtr_.thread_local_pos += bytes;
174   return ret;
175 }
176 
PushOnThreadLocalAllocationStack(mirror::Object * obj)177 inline bool Thread::PushOnThreadLocalAllocationStack(mirror::Object* obj) {
178   DCHECK_LE(tlsPtr_.thread_local_alloc_stack_top, tlsPtr_.thread_local_alloc_stack_end);
179   if (tlsPtr_.thread_local_alloc_stack_top < tlsPtr_.thread_local_alloc_stack_end) {
180     // There's room.
181     DCHECK_LE(reinterpret_cast<byte*>(tlsPtr_.thread_local_alloc_stack_top) +
182                   sizeof(mirror::Object*),
183               reinterpret_cast<byte*>(tlsPtr_.thread_local_alloc_stack_end));
184     DCHECK(*tlsPtr_.thread_local_alloc_stack_top == nullptr);
185     *tlsPtr_.thread_local_alloc_stack_top = obj;
186     ++tlsPtr_.thread_local_alloc_stack_top;
187     return true;
188   }
189   return false;
190 }
191 
SetThreadLocalAllocationStack(mirror::Object ** start,mirror::Object ** end)192 inline void Thread::SetThreadLocalAllocationStack(mirror::Object** start, mirror::Object** end) {
193   DCHECK(Thread::Current() == this) << "Should be called by self";
194   DCHECK(start != nullptr);
195   DCHECK(end != nullptr);
196   DCHECK_ALIGNED(start, sizeof(mirror::Object*));
197   DCHECK_ALIGNED(end, sizeof(mirror::Object*));
198   DCHECK_LT(start, end);
199   tlsPtr_.thread_local_alloc_stack_end = end;
200   tlsPtr_.thread_local_alloc_stack_top = start;
201 }
202 
RevokeThreadLocalAllocationStack()203 inline void Thread::RevokeThreadLocalAllocationStack() {
204   if (kIsDebugBuild) {
205     // Note: self is not necessarily equal to this thread since thread may be suspended.
206     Thread* self = Thread::Current();
207     DCHECK(this == self || IsSuspended() || GetState() == kWaitingPerformingGc)
208         << GetState() << " thread " << this << " self " << self;
209   }
210   tlsPtr_.thread_local_alloc_stack_end = nullptr;
211   tlsPtr_.thread_local_alloc_stack_top = nullptr;
212 }
213 
214 }  // namespace art
215 
216 #endif  // ART_RUNTIME_THREAD_INL_H_
217