blob: 6569a962a0c38390c19787a189fef5917380475f [file] [log] [blame]
Elliott Hughes8d768a92011-09-14 16:35:25 -07001/*
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 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_THREAD_H_
18#define ART_RUNTIME_THREAD_H_
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070019
Elliott Hughes02b48d12011-09-07 17:15:51 -070020#include <bitset>
Ian Rogers306057f2012-11-26 12:45:53 -080021#include <deque>
Elliott Hughesa0957642011-09-02 14:27:33 -070022#include <iosfwd>
Ian Rogersb033c752011-07-20 12:22:35 -070023#include <list>
Ian Rogers700a4022014-05-19 16:49:03 -070024#include <memory>
Elliott Hughes8daa0922011-09-11 13:46:25 -070025#include <string>
Carl Shapirob5573532011-07-12 18:22:59 -070026
Elliott Hughes76160052012-12-12 16:31:20 -080027#include "base/macros.h"
Ian Rogers719d1a32014-03-06 12:13:39 -080028#include "base/mutex.h"
Ian Rogers848871b2013-08-05 10:56:33 -070029#include "entrypoints/interpreter/interpreter_entrypoints.h"
30#include "entrypoints/jni/jni_entrypoints.h"
Ian Rogers7655f292013-07-29 11:07:13 -070031#include "entrypoints/portable/portable_entrypoints.h"
32#include "entrypoints/quick/quick_entrypoints.h"
Mathieu Chartier0651d412014-04-29 14:37:57 -070033#include "gc/allocator/rosalloc.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070034#include "globals.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070035#include "handle_scope.h"
Ian Rogers306057f2012-11-26 12:45:53 -080036#include "jvalue.h"
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080037#include "object_callbacks.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070038#include "offsets.h"
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070039#include "runtime_stats.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070040#include "stack.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041#include "thread_state.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080042#include "throw_location.h"
Ian Rogersb033c752011-07-20 12:22:35 -070043
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070044namespace art {
45
Mathieu Chartier15d34022014-02-26 17:16:38 -080046namespace gc {
47namespace collector {
48class SemiSpace;
49} // namespace collector
50} // namespace gc
51
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080052namespace mirror {
Brian Carlstromea46f952013-07-30 01:26:50 -070053 class ArtMethod;
Ian Rogers848871b2013-08-05 10:56:33 -070054 class Array;
55 class Class;
56 class ClassLoader;
57 class Object;
58 template<class T> class ObjectArray;
59 template<class T> class PrimitiveArray;
60 typedef PrimitiveArray<int32_t> IntArray;
61 class StackTraceElement;
62 class StaticStorageBase;
63 class Throwable;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080064} // namespace mirror
65class BaseMutex;
66class ClassLinker;
Ian Rogers7a22fa62013-01-23 12:16:16 -080067class Closure;
Ian Rogersbdb03912011-09-14 00:55:44 -070068class Context;
Ian Rogers1b09b092012-08-20 15:35:52 -070069struct DebugInvokeReq;
Ian Rogers81d425b2012-09-27 16:03:43 -070070class DexFile;
Ian Rogersb48b9eb2014-02-28 16:20:21 -080071class JavaVMExt;
Ian Rogers81d425b2012-09-27 16:03:43 -070072struct JNIEnvExt;
Elliott Hughes8daa0922011-09-11 13:46:25 -070073class Monitor;
Carl Shapirob5573532011-07-12 18:22:59 -070074class Runtime;
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -070075class ScopedObjectAccessAlreadyRunnable;
Logan Chienf7ad17e2012-03-15 03:10:03 +080076class ShadowFrame;
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +010077struct SingleStepControl;
Brian Carlstrom40381fb2011-10-19 14:13:40 -070078class Thread;
79class ThreadList;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070080
Elliott Hughes34e06962012-04-09 13:55:55 -070081// Thread priorities. These must match the Thread.MIN_PRIORITY,
82// Thread.NORM_PRIORITY, and Thread.MAX_PRIORITY constants.
83enum ThreadPriority {
84 kMinThreadPriority = 1,
85 kNormThreadPriority = 5,
86 kMaxThreadPriority = 10,
87};
88
Ian Rogers474b6da2012-09-25 00:20:38 -070089enum ThreadFlag {
Ian Rogers50ffee22012-11-20 11:47:44 -080090 kSuspendRequest = 1, // If set implies that suspend_count_ > 0 and the Thread should enter the
91 // safepoint handler.
Ian Rogers62d6c772013-02-27 08:32:07 -080092 kCheckpointRequest = 2 // Request that the thread do some checkpoint work and then continue.
Ian Rogers474b6da2012-09-25 00:20:38 -070093};
94
Ian Rogersdd7624d2014-03-14 17:43:00 -070095class Thread {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070096 public:
Ian Rogers932746a2011-09-22 18:57:50 -070097 // Space to throw a StackOverflowError in.
Ian Rogersb3fabf42014-03-12 23:35:11 -070098 // TODO: shrink reserved space, in particular for 64bit.
Andreas Gampee62a07e2014-03-26 14:53:21 -070099#if defined(__x86_64__)
Serguei Katkovd5080062014-05-14 17:00:05 +0700100 static constexpr size_t kStackOverflowReservedBytes = 32 * KB;
Andreas Gampee62a07e2014-03-26 14:53:21 -0700101#elif defined(__aarch64__)
102 // Worst-case, we would need about 2.6x the amount of x86_64 for many more registers.
103 // But this one works rather well.
104 static constexpr size_t kStackOverflowReservedBytes = 32 * KB;
Brian Carlstrom2aef07c2014-05-15 11:18:29 -0700105#elif defined(__i386__)
106 // TODO: Bumped to workaround regression (http://b/14982147) Specifically to fix:
107 // test-art-host-run-test-interpreter-018-stack-overflow
108 // test-art-host-run-test-interpreter-107-int-math2
109 static constexpr size_t kStackOverflowReservedBytes = 24 * KB;
Ian Rogers463cb4d2014-03-12 16:23:09 -0700110#else
111 static constexpr size_t kStackOverflowReservedBytes = 16 * KB;
112#endif
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700113 // How much of the reserved bytes is reserved for incoming signals.
114 static constexpr size_t kStackOverflowSignalReservedBytes = 2 * KB;
115 // How much of the reserved bytes we may temporarily use during stack overflow checks as an
116 // optimization.
117 static constexpr size_t kStackOverflowReservedUsableBytes =
118 kStackOverflowReservedBytes - kStackOverflowSignalReservedBytes;
buzbeec143c552011-08-20 17:38:58 -0700119
Dave Allisonf9439142014-03-27 15:10:22 -0700120 // For implicit overflow checks we reserve an extra piece of memory at the bottom
121 // of the stack (lowest memory). The higher portion of the memory
122 // is protected against reads and the lower is available for use while
123 // throwing the StackOverflow exception.
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700124 static constexpr size_t kStackOverflowProtectedSize = 16 * KB;
Dave Allisonf9439142014-03-27 15:10:22 -0700125 static constexpr size_t kStackOverflowImplicitCheckSize = kStackOverflowProtectedSize +
126 kStackOverflowReservedBytes;
127
Elliott Hughes462c9442012-03-23 18:47:50 -0700128 // Creates a new native thread corresponding to the given managed peer.
129 // Used to implement Thread.start.
Ian Rogers52673ff2012-06-27 23:25:34 -0700130 static void CreateNativeThread(JNIEnv* env, jobject peer, size_t stack_size, bool daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700131
Elliott Hughes462c9442012-03-23 18:47:50 -0700132 // Attaches the calling native thread to the runtime, returning the new native peer.
133 // Used to implement JNI AttachCurrentThread and AttachCurrentThreadAsDaemon calls.
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800134 static Thread* Attach(const char* thread_name, bool as_daemon, jobject thread_group,
135 bool create_peer);
Carl Shapirob5573532011-07-12 18:22:59 -0700136
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700137 // Reset internal state of child thread after fork.
138 void InitAfterFork();
139
Ian Rogers02ed4c02013-09-06 13:10:04 -0700140 static Thread* Current();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700141
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700142 static Thread* FromManagedThread(const ScopedObjectAccessAlreadyRunnable& ts,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800143 mirror::Object* thread_peer)
jeffhaoa77f0f62012-12-05 17:19:31 -0800144 EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700145 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
146 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700147 static Thread* FromManagedThread(const ScopedObjectAccessAlreadyRunnable& ts, jobject thread)
jeffhaoa77f0f62012-12-05 17:19:31 -0800148 EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700149 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
150 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700151
Elliott Hughes28fa76d2012-04-09 17:31:46 -0700152 // Translates 172 to pAllocArrayFromCode and so on.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700153 template<size_t size_of_pointers>
154 static void DumpThreadOffset(std::ostream& os, uint32_t offset);
Elliott Hughes28fa76d2012-04-09 17:31:46 -0700155
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700156 // Dumps a one-line summary of thread state (used for operator<<).
157 void ShortDump(std::ostream& os) const;
158
159 // Dumps the detailed thread state and the thread stack (used for SIGQUIT).
160 void Dump(std::ostream& os) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700161 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
162 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughesa0957642011-09-02 14:27:33 -0700163
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700164 void DumpJavaStack(std::ostream& os) const
165 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
166 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
167
Elliott Hughesabbe07d2012-06-05 17:42:23 -0700168 // Dumps the SIGQUIT per-thread header. 'thread' can be NULL for a non-attached thread, in which
169 // case we use 'tid' to identify the thread, and we'll include as much information as we can.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700170 static void DumpState(std::ostream& os, const Thread* thread, pid_t tid)
Ian Rogerscfaa4552012-11-26 21:00:08 -0800171 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
172 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughesabbe07d2012-06-05 17:42:23 -0700173
Ian Rogers474b6da2012-09-25 00:20:38 -0700174 ThreadState GetState() const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700175 DCHECK_GE(tls32_.state_and_flags.as_struct.state, kTerminated);
176 DCHECK_LE(tls32_.state_and_flags.as_struct.state, kSuspended);
177 return static_cast<ThreadState>(tls32_.state_and_flags.as_struct.state);
Dave Allison0aded082013-11-07 13:15:11 -0800178 }
179
Ian Rogers474b6da2012-09-25 00:20:38 -0700180 ThreadState SetState(ThreadState new_state);
Ian Rogers52673ff2012-06-27 23:25:34 -0700181
Ian Rogers81d425b2012-09-27 16:03:43 -0700182 int GetSuspendCount() const EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_suspend_count_lock_) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700183 return tls32_.suspend_count;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700184 }
Elliott Hughes038a8062011-09-18 14:12:41 -0700185
Ian Rogers81d425b2012-09-27 16:03:43 -0700186 int GetDebugSuspendCount() const EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_suspend_count_lock_) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700187 return tls32_.debug_suspend_count;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700188 }
189
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700190 bool IsSuspended() const {
Chris Dearman59cde532013-12-04 18:53:49 -0800191 union StateAndFlags state_and_flags;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700192 state_and_flags.as_int = tls32_.state_and_flags.as_int;
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700193 return state_and_flags.as_struct.state != kRunnable &&
194 (state_and_flags.as_struct.flags & kSuspendRequest) != 0;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700195 }
196
Ian Rogers01ae5802012-09-28 16:14:01 -0700197 void ModifySuspendCount(Thread* self, int delta, bool for_debugger)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700198 EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700199
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700200 bool RequestCheckpoint(Closure* function)
201 EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_suspend_count_lock_);
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700202
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700203 // Called when thread detected that the thread_suspend_count_ was non-zero. Gives up share of
204 // mutator_lock_ and waits until it is resumed and thread_suspend_count_ is zero.
Ian Rogers9da7f592012-08-20 17:14:28 -0700205 void FullSuspendCheck()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700206 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
207 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700208
209 // Transition from non-runnable to runnable state acquiring share on mutator_lock_.
210 ThreadState TransitionFromSuspendedToRunnable()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700211 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
Ian Rogers693ff612013-02-01 10:56:12 -0800212 SHARED_LOCK_FUNCTION(Locks::mutator_lock_)
Ian Rogers1ffa32f2013-02-05 18:29:08 -0800213 ALWAYS_INLINE;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700214
215 // Transition from runnable into a state where mutator privileges are denied. Releases share of
216 // mutator lock.
217 void TransitionFromRunnableToSuspended(ThreadState new_state)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700218 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
Ian Rogers693ff612013-02-01 10:56:12 -0800219 UNLOCK_FUNCTION(Locks::mutator_lock_)
Ian Rogers1ffa32f2013-02-05 18:29:08 -0800220 ALWAYS_INLINE;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700221
Ian Rogers0399dde2012-06-06 17:09:28 -0700222 // Once called thread suspension will cause an assertion failure.
Ian Rogers52673ff2012-06-27 23:25:34 -0700223 const char* StartAssertNoThreadSuspension(const char* cause) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700224 if (kIsDebugBuild) {
225 CHECK(cause != NULL);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700226 const char* previous_cause = tlsPtr_.last_no_thread_suspension_cause;
227 tls32_.no_thread_suspension++;
228 tlsPtr_.last_no_thread_suspension_cause = cause;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700229 return previous_cause;
230 } else {
231 return nullptr;
232 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700233 }
Ian Rogers52673ff2012-06-27 23:25:34 -0700234
Ian Rogers0399dde2012-06-06 17:09:28 -0700235 // End region where no thread suspension is expected.
Ian Rogers52673ff2012-06-27 23:25:34 -0700236 void EndAssertNoThreadSuspension(const char* old_cause) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700237 if (kIsDebugBuild) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700238 CHECK(old_cause != nullptr || tls32_.no_thread_suspension == 1);
239 CHECK_GT(tls32_.no_thread_suspension, 0U);
240 tls32_.no_thread_suspension--;
241 tlsPtr_.last_no_thread_suspension_cause = old_cause;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700242 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700243 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700244
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700245 void AssertThreadSuspensionIsAllowable(bool check_locks = true) const;
Ian Rogers0399dde2012-06-06 17:09:28 -0700246
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700247 bool IsDaemon() const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700248 return tls32_.daemon;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700249 }
250
Ian Rogersdd7624d2014-03-14 17:43:00 -0700251 bool HoldsLock(mirror::Object*) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700252
Elliott Hughes8daa0922011-09-11 13:46:25 -0700253 /*
254 * Changes the priority of this thread to match that of the java.lang.Thread object.
255 *
256 * We map a priority value from 1-10 to Linux "nice" values, where lower
257 * numbers indicate higher priority.
258 */
259 void SetNativePriority(int newPriority);
260
261 /*
262 * Returns the thread priority for the current thread by querying the system.
263 * This is useful when attaching a thread through JNI.
264 *
265 * Returns a value from 1 to 10 (compatible with java.lang.Thread values).
266 */
267 static int GetNativePriority();
268
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700269 uint32_t GetThreadId() const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700270 return tls32_.thin_lock_thread_id;
Carl Shapirob5573532011-07-12 18:22:59 -0700271 }
272
Elliott Hughesd92bec42011-09-02 17:04:36 -0700273 pid_t GetTid() const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700274 return tls32_.tid;
Elliott Hughesd92bec42011-09-02 17:04:36 -0700275 }
Elliott Hughese27955c2011-08-26 15:21:24 -0700276
Elliott Hughesffb465f2012-03-01 18:46:05 -0800277 // Returns the java.lang.Thread's name, or NULL if this Thread* doesn't have a peer.
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700278 mirror::String* GetThreadName(const ScopedObjectAccessAlreadyRunnable& ts) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700279 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes899e7892012-01-24 14:57:32 -0800280
Elliott Hughesffb465f2012-03-01 18:46:05 -0800281 // Sets 'name' to the java.lang.Thread's name. This requires no transition to managed code,
282 // allocation, or locking.
283 void GetThreadName(std::string& name) const;
284
Elliott Hughes899e7892012-01-24 14:57:32 -0800285 // Sets the thread's name.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700286 void SetThreadName(const char* name) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughesfc861622011-10-17 17:57:47 -0700287
Jeff Hao57dac6e2013-08-15 16:36:24 -0700288 // Returns the thread-specific CPU-time clock in microseconds or -1 if unavailable.
289 uint64_t GetCpuMicroTime() const;
290
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800291 mirror::Object* GetPeer() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700292 CHECK(tlsPtr_.jpeer == nullptr);
293 return tlsPtr_.opeer;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700294 }
295
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700296 bool HasPeer() const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700297 return tlsPtr_.jpeer != nullptr || tlsPtr_.opeer != nullptr;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700298 }
299
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700300 RuntimeStats* GetStats() {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700301 return &tls64_.stats;
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700302 }
303
Elliott Hughes7dc51662012-05-16 14:48:43 -0700304 bool IsStillStarting() const;
305
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700306 bool IsExceptionPending() const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700307 return tlsPtr_.exception != nullptr;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700308 }
309
Ian Rogers62d6c772013-02-27 08:32:07 -0800310 mirror::Throwable* GetException(ThrowLocation* throw_location) const
311 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700312 if (throw_location != nullptr) {
313 *throw_location = tlsPtr_.throw_location;
Ian Rogers62d6c772013-02-27 08:32:07 -0800314 }
Ian Rogersdd7624d2014-03-14 17:43:00 -0700315 return tlsPtr_.exception;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700316 }
317
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700318 void AssertNoPendingException() const;
Mathieu Chartier8d7672e2014-02-25 10:57:16 -0800319 void AssertNoPendingExceptionForNewException(const char* msg) const;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700320
Ian Rogers62d6c772013-02-27 08:32:07 -0800321 void SetException(const ThrowLocation& throw_location, mirror::Throwable* new_exception)
322 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700323 CHECK(new_exception != NULL);
Ian Rogers474b6da2012-09-25 00:20:38 -0700324 // TODO: DCHECK(!IsExceptionPending());
Ian Rogersdd7624d2014-03-14 17:43:00 -0700325 tlsPtr_.exception = new_exception;
326 tlsPtr_.throw_location = throw_location;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700327 }
328
Serguei Katkova309d762014-05-26 11:23:39 +0700329 void ClearException() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700330 tlsPtr_.exception = nullptr;
331 tlsPtr_.throw_location.Clear();
jeffhao94d6df42012-11-26 16:02:12 -0800332 }
333
Ian Rogersbdb03912011-09-14 00:55:44 -0700334 // Find catch block and perform long jump to appropriate exception handle
jeffhao94d6df42012-11-26 16:02:12 -0800335 void QuickDeliverException() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersbdb03912011-09-14 00:55:44 -0700336
337 Context* GetLongJumpContext();
Ian Rogers0399dde2012-06-06 17:09:28 -0700338 void ReleaseLongJumpContext(Context* context) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700339 DCHECK(tlsPtr_.long_jump_context == nullptr);
340 tlsPtr_.long_jump_context = context;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700341 }
342
Brian Carlstromea46f952013-07-30 01:26:50 -0700343 mirror::ArtMethod* GetCurrentMethod(uint32_t* dex_pc) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700344 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700345
Ian Rogers62d6c772013-02-27 08:32:07 -0800346 ThrowLocation GetCurrentLocationForThrow() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
347
Andreas Gampecf4035a2014-05-28 22:43:01 -0700348 void SetTopOfStack(StackReference<mirror::ArtMethod>* top_method, uintptr_t pc) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700349 tlsPtr_.managed_stack.SetTopQuickFrame(top_method);
350 tlsPtr_.managed_stack.SetTopQuickFramePc(pc);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700351 }
352
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800353 void SetTopOfShadowStack(ShadowFrame* top) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700354 tlsPtr_.managed_stack.SetTopShadowFrame(top);
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800355 }
356
Ian Rogers0399dde2012-06-06 17:09:28 -0700357 bool HasManagedStack() const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700358 return (tlsPtr_.managed_stack.GetTopQuickFrame() != nullptr) ||
359 (tlsPtr_.managed_stack.GetTopShadowFrame() != nullptr);
Ian Rogersbdb03912011-09-14 00:55:44 -0700360 }
361
Elliott Hughesa4f94742012-05-29 16:28:38 -0700362 // If 'msg' is NULL, no detail message is set.
Ian Rogers62d6c772013-02-27 08:32:07 -0800363 void ThrowNewException(const ThrowLocation& throw_location,
364 const char* exception_class_descriptor, const char* msg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700365 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700366
Elliott Hughesa4f94742012-05-29 16:28:38 -0700367 // If 'msg' is NULL, no detail message is set. An exception must be pending, and will be
368 // used as the new exception's cause.
Ian Rogers62d6c772013-02-27 08:32:07 -0800369 void ThrowNewWrappedException(const ThrowLocation& throw_location,
370 const char* exception_class_descriptor,
371 const char* msg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700372 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughesa4f94742012-05-29 16:28:38 -0700373
Ian Rogers62d6c772013-02-27 08:32:07 -0800374 void ThrowNewExceptionF(const ThrowLocation& throw_location,
375 const char* exception_class_descriptor, const char* fmt, ...)
376 __attribute__((format(printf, 4, 5)))
Ian Rogersb726dcb2012-09-05 08:57:23 -0700377 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700378
Ian Rogers62d6c772013-02-27 08:32:07 -0800379 void ThrowNewExceptionV(const ThrowLocation& throw_location,
380 const char* exception_class_descriptor, const char* fmt, va_list ap)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700381 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes4a2b4172011-09-20 17:08:25 -0700382
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700383 // OutOfMemoryError is special, because we need to pre-allocate an instance.
Elliott Hughes8a8b9cb2012-04-13 18:29:22 -0700384 // Only the GC should call this.
Ian Rogers120f1c72012-09-28 17:17:10 -0700385 void ThrowOutOfMemoryError(const char* msg) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700386
Elliott Hughesbe759c62011-09-08 19:38:21 -0700387 static void Startup();
Elliott Hughes038a8062011-09-18 14:12:41 -0700388 static void FinishStartup();
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700389 static void Shutdown();
Carl Shapirob5573532011-07-12 18:22:59 -0700390
Ian Rogersb033c752011-07-20 12:22:35 -0700391 // JNI methods
Elliott Hughes69f5bc62011-08-24 09:26:14 -0700392 JNIEnvExt* GetJniEnv() const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700393 return tlsPtr_.jni_env;
Ian Rogersb033c752011-07-20 12:22:35 -0700394 }
395
Ian Rogers408f79a2011-08-23 18:22:33 -0700396 // Convert a jobject into a Object*
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800397 mirror::Object* DecodeJObject(jobject obj) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersb033c752011-07-20 12:22:35 -0700398
Mathieu Chartiera6e7f082014-05-22 14:43:37 -0700399 mirror::Object* GetMonitorEnterObject() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700400 return tlsPtr_.monitor_enter_object;
401 }
402
Mathieu Chartiera6e7f082014-05-22 14:43:37 -0700403 void SetMonitorEnterObject(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700404 tlsPtr_.monitor_enter_object = obj;
405 }
406
Elliott Hughes8daa0922011-09-11 13:46:25 -0700407 // Implements java.lang.Thread.interrupted.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700408 bool Interrupted() LOCKS_EXCLUDED(wait_mutex_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700409 // Implements java.lang.Thread.isInterrupted.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700410 bool IsInterrupted() LOCKS_EXCLUDED(wait_mutex_);
411 bool IsInterruptedLocked() EXCLUSIVE_LOCKS_REQUIRED(wait_mutex_) {
412 return interrupted_;
413 }
414 void Interrupt(Thread* self) LOCKS_EXCLUDED(wait_mutex_);
415 void SetInterruptedLocked(bool i) EXCLUSIVE_LOCKS_REQUIRED(wait_mutex_) {
416 interrupted_ = i;
417 }
418 void Notify() LOCKS_EXCLUDED(wait_mutex_);
419
420 private:
421 void NotifyLocked(Thread* self) EXCLUSIVE_LOCKS_REQUIRED(wait_mutex_);
422
423 public:
424 Mutex* GetWaitMutex() const LOCK_RETURNED(wait_mutex_) {
425 return wait_mutex_;
426 }
427
428 ConditionVariable* GetWaitConditionVariable() const EXCLUSIVE_LOCKS_REQUIRED(wait_mutex_) {
429 return wait_cond_;
430 }
431
432 Monitor* GetWaitMonitor() const EXCLUSIVE_LOCKS_REQUIRED(wait_mutex_) {
433 return wait_monitor_;
434 }
435
436 void SetWaitMonitor(Monitor* mon) EXCLUSIVE_LOCKS_REQUIRED(wait_mutex_) {
437 wait_monitor_ = mon;
438 }
439
440
441 // Waiter link-list support.
442 Thread* GetWaitNext() const {
443 return tlsPtr_.wait_next;
444 }
445
446 void SetWaitNext(Thread* next) {
447 tlsPtr_.wait_next = next;
448 }
Elliott Hughes5f791332011-09-15 17:45:30 -0700449
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800450 mirror::ClassLoader* GetClassLoaderOverride() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700451 return tlsPtr_.class_loader_override;
buzbeec143c552011-08-20 17:38:58 -0700452 }
453
Mathieu Chartier4e305412014-02-19 10:54:44 -0800454 void SetClassLoaderOverride(mirror::ClassLoader* class_loader_override)
455 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
buzbeec143c552011-08-20 17:38:58 -0700456
Ian Rogersaaa20802011-09-11 21:47:37 -0700457 // Create the internal representation of a stack trace, that is more time
Sebastien Hertzee1d79a2014-02-21 15:46:30 +0100458 // and space efficient to compute than the StackTraceElement[].
459 template<bool kTransactionActive>
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700460 jobject CreateInternalStackTrace(const ScopedObjectAccessAlreadyRunnable& soa) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700461 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersaaa20802011-09-11 21:47:37 -0700462
Elliott Hughes01158d72011-09-19 19:47:10 -0700463 // Convert an internal stack trace representation (returned by CreateInternalStackTrace) to a
464 // StackTraceElement[]. If output_array is NULL, a new array is created, otherwise as many
465 // frames as will fit are written into the given array. If stack_depth is non-NULL, it's updated
466 // with the number of valid frames in the returned array.
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700467 static jobjectArray InternalStackTraceToStackTraceElementArray(
468 const ScopedObjectAccessAlreadyRunnable& soa, jobject internal,
469 jobjectArray output_array = nullptr, int* stack_depth = nullptr)
Ian Rogers53b8b092014-03-13 23:45:53 -0700470 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700471
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800472 void VisitRoots(RootCallback* visitor, void* arg) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes410c0c82011-09-01 17:58:25 -0700473
Mathieu Chartier4e305412014-02-19 10:54:44 -0800474 ALWAYS_INLINE void VerifyStack() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
jeffhao25045522012-03-13 19:34:37 -0700475
Elliott Hughesbe759c62011-09-08 19:38:21 -0700476 //
477 // Offsets of various members of native Thread class, used by compiled code.
478 //
479
Ian Rogersdd7624d2014-03-14 17:43:00 -0700480 template<size_t pointer_size>
481 static ThreadOffset<pointer_size> ThinLockIdOffset() {
482 return ThreadOffset<pointer_size>(
483 OFFSETOF_MEMBER(Thread, tls32_) +
484 OFFSETOF_MEMBER(tls_32bit_sized_values, thin_lock_thread_id));
Elliott Hughesbe759c62011-09-08 19:38:21 -0700485 }
486
Ian Rogersdd7624d2014-03-14 17:43:00 -0700487 template<size_t pointer_size>
488 static ThreadOffset<pointer_size> ThreadFlagsOffset() {
489 return ThreadOffset<pointer_size>(
490 OFFSETOF_MEMBER(Thread, tls32_) +
491 OFFSETOF_MEMBER(tls_32bit_sized_values, state_and_flags));
Elliott Hughesbe759c62011-09-08 19:38:21 -0700492 }
493
Ian Rogersdd7624d2014-03-14 17:43:00 -0700494 private:
495 template<size_t pointer_size>
496 static ThreadOffset<pointer_size> ThreadOffsetFromTlsPtr(size_t tls_ptr_offset) {
497 size_t base = OFFSETOF_MEMBER(Thread, tlsPtr_);
498 size_t scale;
499 size_t shrink;
500 if (pointer_size == sizeof(void*)) {
501 scale = 1;
502 shrink = 1;
503 } else if (pointer_size > sizeof(void*)) {
504 scale = pointer_size / sizeof(void*);
505 shrink = 1;
506 } else {
507 DCHECK_GT(sizeof(void*), pointer_size);
508 scale = 1;
509 shrink = sizeof(void*) / pointer_size;
510 }
511 return ThreadOffset<pointer_size>(base + ((tls_ptr_offset * scale) / shrink));
Ian Rogers07ec8e12012-12-01 01:26:51 -0800512 }
513
Ian Rogersdd7624d2014-03-14 17:43:00 -0700514 public:
515 template<size_t pointer_size>
516 static ThreadOffset<pointer_size> QuickEntryPointOffset(size_t quick_entrypoint_offset) {
517 return ThreadOffsetFromTlsPtr<pointer_size>(
518 OFFSETOF_MEMBER(tls_ptr_sized_values, quick_entrypoints) + quick_entrypoint_offset);
Elliott Hughesbe759c62011-09-08 19:38:21 -0700519 }
520
Ian Rogersdd7624d2014-03-14 17:43:00 -0700521 template<size_t pointer_size>
522 static ThreadOffset<pointer_size> InterpreterEntryPointOffset(size_t interp_entrypoint_offset) {
523 return ThreadOffsetFromTlsPtr<pointer_size>(
524 OFFSETOF_MEMBER(tls_ptr_sized_values, interpreter_entrypoints) + interp_entrypoint_offset);
Elliott Hughesbe759c62011-09-08 19:38:21 -0700525 }
526
Ian Rogersdd7624d2014-03-14 17:43:00 -0700527 template<size_t pointer_size>
528 static ThreadOffset<pointer_size> JniEntryPointOffset(size_t jni_entrypoint_offset) {
529 return ThreadOffsetFromTlsPtr<pointer_size>(
530 OFFSETOF_MEMBER(tls_ptr_sized_values, jni_entrypoints) + jni_entrypoint_offset);
Elliott Hughesbe759c62011-09-08 19:38:21 -0700531 }
532
Ian Rogersdd7624d2014-03-14 17:43:00 -0700533 template<size_t pointer_size>
534 static ThreadOffset<pointer_size> PortableEntryPointOffset(size_t port_entrypoint_offset) {
535 return ThreadOffsetFromTlsPtr<pointer_size>(
536 OFFSETOF_MEMBER(tls_ptr_sized_values, portable_entrypoints) + port_entrypoint_offset);
537 }
538
539 template<size_t pointer_size>
540 static ThreadOffset<pointer_size> SelfOffset() {
541 return ThreadOffsetFromTlsPtr<pointer_size>(OFFSETOF_MEMBER(tls_ptr_sized_values, self));
542 }
543
544 template<size_t pointer_size>
545 static ThreadOffset<pointer_size> ExceptionOffset() {
546 return ThreadOffsetFromTlsPtr<pointer_size>(OFFSETOF_MEMBER(tls_ptr_sized_values, exception));
547 }
548
549 template<size_t pointer_size>
550 static ThreadOffset<pointer_size> PeerOffset() {
551 return ThreadOffsetFromTlsPtr<pointer_size>(OFFSETOF_MEMBER(tls_ptr_sized_values, opeer));
552 }
553
554
555 template<size_t pointer_size>
556 static ThreadOffset<pointer_size> CardTableOffset() {
557 return ThreadOffsetFromTlsPtr<pointer_size>(OFFSETOF_MEMBER(tls_ptr_sized_values, card_table));
558 }
559
560 template<size_t pointer_size>
561 static ThreadOffset<pointer_size> ThreadSuspendTriggerOffset() {
562 return ThreadOffsetFromTlsPtr<pointer_size>(
563 OFFSETOF_MEMBER(tls_ptr_sized_values, suspend_trigger));
Dave Allisonb373e092014-02-20 16:06:36 -0800564 }
565
Ian Rogers932746a2011-09-22 18:57:50 -0700566 // Size of stack less any space reserved for stack overflow
jeffhaod7521322012-11-21 15:38:24 -0800567 size_t GetStackSize() const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700568 return tlsPtr_.stack_size - (tlsPtr_.stack_end - tlsPtr_.stack_begin);
Ian Rogers932746a2011-09-22 18:57:50 -0700569 }
570
jeffhaod7521322012-11-21 15:38:24 -0800571 byte* GetStackEnd() const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700572 return tlsPtr_.stack_end;
jeffhaod7521322012-11-21 15:38:24 -0800573 }
574
Ian Rogers932746a2011-09-22 18:57:50 -0700575 // Set the stack end to that to be used during a stack overflow
Ian Rogersb726dcb2012-09-05 08:57:23 -0700576 void SetStackEndForStackOverflow() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers932746a2011-09-22 18:57:50 -0700577
578 // Set the stack end to that to be used during regular execution
Dave Allisonf9439142014-03-27 15:10:22 -0700579 void ResetDefaultStackEnd(bool implicit_overflow_check) {
Ian Rogers932746a2011-09-22 18:57:50 -0700580 // Our stacks grow down, so we want stack_end_ to be near there, but reserving enough room
581 // to throw a StackOverflowError.
Dave Allisonf9439142014-03-27 15:10:22 -0700582 if (implicit_overflow_check) {
583 // For implicit checks we also need to add in the protected region above the
584 // overflow region.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700585 tlsPtr_.stack_end = tlsPtr_.stack_begin + kStackOverflowImplicitCheckSize;
Dave Allisonf9439142014-03-27 15:10:22 -0700586 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700587 tlsPtr_.stack_end = tlsPtr_.stack_begin + kStackOverflowReservedBytes;
Dave Allisonf9439142014-03-27 15:10:22 -0700588 }
Ian Rogers932746a2011-09-22 18:57:50 -0700589 }
590
Dave Allisonf9439142014-03-27 15:10:22 -0700591 // Install the protected region for implicit stack checks.
592 void InstallImplicitProtection(bool is_main_stack);
593
Ian Rogers120f1c72012-09-28 17:17:10 -0700594 bool IsHandlingStackOverflow() const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700595 return tlsPtr_.stack_end == tlsPtr_.stack_begin;
Ian Rogers120f1c72012-09-28 17:17:10 -0700596 }
597
Ian Rogersdd7624d2014-03-14 17:43:00 -0700598 template<size_t pointer_size>
599 static ThreadOffset<pointer_size> StackEndOffset() {
600 return ThreadOffsetFromTlsPtr<pointer_size>(
601 OFFSETOF_MEMBER(tls_ptr_sized_values, stack_end));
Elliott Hughesbe759c62011-09-08 19:38:21 -0700602 }
603
Ian Rogersdd7624d2014-03-14 17:43:00 -0700604 template<size_t pointer_size>
605 static ThreadOffset<pointer_size> JniEnvOffset() {
606 return ThreadOffsetFromTlsPtr<pointer_size>(
607 OFFSETOF_MEMBER(tls_ptr_sized_values, jni_env));
Elliott Hughesbe759c62011-09-08 19:38:21 -0700608 }
609
Ian Rogersdd7624d2014-03-14 17:43:00 -0700610 template<size_t pointer_size>
611 static ThreadOffset<pointer_size> TopOfManagedStackOffset() {
612 return ThreadOffsetFromTlsPtr<pointer_size>(
613 OFFSETOF_MEMBER(tls_ptr_sized_values, managed_stack) +
614 ManagedStack::TopQuickFrameOffset());
Elliott Hughesbe759c62011-09-08 19:38:21 -0700615 }
616
Ian Rogersdd7624d2014-03-14 17:43:00 -0700617 template<size_t pointer_size>
618 static ThreadOffset<pointer_size> TopOfManagedStackPcOffset() {
619 return ThreadOffsetFromTlsPtr<pointer_size>(
620 OFFSETOF_MEMBER(tls_ptr_sized_values, managed_stack) +
621 ManagedStack::TopQuickFramePcOffset());
Ian Rogersbdb03912011-09-14 00:55:44 -0700622 }
623
Ian Rogers0399dde2012-06-06 17:09:28 -0700624 const ManagedStack* GetManagedStack() const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700625 return &tlsPtr_.managed_stack;
Ian Rogers0399dde2012-06-06 17:09:28 -0700626 }
627
628 // Linked list recording fragments of managed stack.
629 void PushManagedStackFragment(ManagedStack* fragment) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700630 tlsPtr_.managed_stack.PushManagedStackFragment(fragment);
Ian Rogers0399dde2012-06-06 17:09:28 -0700631 }
632 void PopManagedStackFragment(const ManagedStack& fragment) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700633 tlsPtr_.managed_stack.PopManagedStackFragment(fragment);
Ian Rogers0399dde2012-06-06 17:09:28 -0700634 }
635
636 ShadowFrame* PushShadowFrame(ShadowFrame* new_top_frame) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700637 return tlsPtr_.managed_stack.PushShadowFrame(new_top_frame);
TDYa127de479be2012-05-31 08:03:26 -0700638 }
639
640 ShadowFrame* PopShadowFrame() {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700641 return tlsPtr_.managed_stack.PopShadowFrame();
TDYa127de479be2012-05-31 08:03:26 -0700642 }
Logan Chienf7ad17e2012-03-15 03:10:03 +0800643
Ian Rogersdd7624d2014-03-14 17:43:00 -0700644 template<size_t pointer_size>
645 static ThreadOffset<pointer_size> TopShadowFrameOffset() {
646 return ThreadOffsetFromTlsPtr<pointer_size>(
647 OFFSETOF_MEMBER(tls_ptr_sized_values, managed_stack) +
648 ManagedStack::TopShadowFrameOffset());
TDYa127d668a062012-04-13 12:36:57 -0700649 }
650
Ian Rogersef7d42f2014-01-06 12:55:46 -0800651 // Number of references allocated in JNI ShadowFrames on this thread.
652 size_t NumJniShadowFrameReferences() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700653 return tlsPtr_.managed_stack.NumJniShadowFrameReferences();
Ian Rogers0399dde2012-06-06 17:09:28 -0700654 }
655
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700656 // Number of references in handle scope on this thread.
657 size_t NumHandleReferences();
Ian Rogers0399dde2012-06-06 17:09:28 -0700658
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700659 // Number of references allocated in handle scopes & JNI shadow frames on this thread.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800660 size_t NumStackReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700661 return NumHandleReferences() + NumJniShadowFrameReferences();
Ian Rogers0399dde2012-06-06 17:09:28 -0700662 };
663
664 // Is the given obj in this thread's stack indirect reference table?
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700665 bool HandleScopeContains(jobject obj) const;
Ian Rogers0399dde2012-06-06 17:09:28 -0700666
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700667 void HandleScopeVisitRoots(RootCallback* visitor, void* arg, uint32_t thread_id)
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800668 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700669
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700670 HandleScope* GetTopHandleScope() {
671 return tlsPtr_.top_handle_scope;
Ian Rogers1f539342012-10-03 21:09:42 -0700672 }
673
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700674 void PushHandleScope(HandleScope* handle_scope) {
675 handle_scope->SetLink(tlsPtr_.top_handle_scope);
676 tlsPtr_.top_handle_scope = handle_scope;
677 }
678
679 HandleScope* PopHandleScope() {
680 HandleScope* handle_scope = tlsPtr_.top_handle_scope;
681 DCHECK(handle_scope != nullptr);
682 tlsPtr_.top_handle_scope = tlsPtr_.top_handle_scope->GetLink();
683 return handle_scope;
Ian Rogers1f539342012-10-03 21:09:42 -0700684 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700685
Ian Rogersdd7624d2014-03-14 17:43:00 -0700686 template<size_t pointer_size>
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700687 static ThreadOffset<pointer_size> TopHandleScopeOffset() {
688 return ThreadOffsetFromTlsPtr<pointer_size>(OFFSETOF_MEMBER(tls_ptr_sized_values,
689 top_handle_scope));
Elliott Hughesbe759c62011-09-08 19:38:21 -0700690 }
691
Ian Rogersdd7624d2014-03-14 17:43:00 -0700692 DebugInvokeReq* GetInvokeReq() const {
693 return tlsPtr_.debug_invoke_req;
Elliott Hughes475fc232011-10-25 15:00:35 -0700694 }
695
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +0100696 SingleStepControl* GetSingleStepControl() const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700697 return tlsPtr_.single_step_control;
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +0100698 }
699
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200700 // Returns the fake exception used to activate deoptimization.
701 static mirror::Throwable* GetDeoptimizationException() {
702 return reinterpret_cast<mirror::Throwable*>(-1);
703 }
704
Ian Rogers62d6c772013-02-27 08:32:07 -0800705 void SetDeoptimizationShadowFrame(ShadowFrame* sf);
706 void SetDeoptimizationReturnValue(const JValue& ret_val);
Ian Rogers306057f2012-11-26 12:45:53 -0800707
708 ShadowFrame* GetAndClearDeoptimizationShadowFrame(JValue* ret_val);
709
Sebastien Hertz714f1752014-04-28 15:03:08 +0200710 bool HasDeoptimizationShadowFrame() const {
711 return tlsPtr_.deoptimization_shadow_frame != nullptr;
712 }
713
Ian Rogers62d6c772013-02-27 08:32:07 -0800714 std::deque<instrumentation::InstrumentationStackFrame>* GetInstrumentationStack() {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700715 return tlsPtr_.instrumentation_stack;
jeffhaoe343b762011-12-05 16:36:44 -0800716 }
717
Jeff Hao5ce4b172013-08-16 16:27:18 -0700718 std::vector<mirror::ArtMethod*>* GetStackTraceSample() const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700719 return tlsPtr_.stack_trace_sample;
Jeff Hao5ce4b172013-08-16 16:27:18 -0700720 }
721
722 void SetStackTraceSample(std::vector<mirror::ArtMethod*>* sample) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700723 tlsPtr_.stack_trace_sample = sample;
Jeff Hao5ce4b172013-08-16 16:27:18 -0700724 }
725
726 uint64_t GetTraceClockBase() const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700727 return tls64_.trace_clock_base;
Jeff Hao5ce4b172013-08-16 16:27:18 -0700728 }
729
730 void SetTraceClockBase(uint64_t clock_base) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700731 tls64_.trace_clock_base = clock_base;
Jeff Hao5ce4b172013-08-16 16:27:18 -0700732 }
733
Ian Rogers81d425b2012-09-27 16:03:43 -0700734 BaseMutex* GetHeldMutex(LockLevel level) const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700735 return tlsPtr_.held_mutexes[level];
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700736 }
737
Ian Rogers81d425b2012-09-27 16:03:43 -0700738 void SetHeldMutex(LockLevel level, BaseMutex* mutex) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700739 tlsPtr_.held_mutexes[level] = mutex;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700740 }
Elliott Hughesffb465f2012-03-01 18:46:05 -0800741
Mathieu Chartier752a0e62013-06-27 11:03:27 -0700742 void RunCheckpointFunction();
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700743
744 bool ReadFlag(ThreadFlag flag) const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700745 return (tls32_.state_and_flags.as_struct.flags & flag) != 0;
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700746 }
747
Jeff Hao9cec2472013-05-14 18:17:06 -0700748 bool TestAllFlags() const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700749 return (tls32_.state_and_flags.as_struct.flags != 0);
Jeff Hao9cec2472013-05-14 18:17:06 -0700750 }
751
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700752 void AtomicSetFlag(ThreadFlag flag);
753
754 void AtomicClearFlag(ThreadFlag flag);
755
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700756 void ResetQuickAllocEntryPointsForThread();
757
Ian Rogersdd7624d2014-03-14 17:43:00 -0700758 // Returns the remaining space in the TLAB.
759 size_t TlabSize() const;
760 // Doesn't check that there is room.
761 mirror::Object* AllocTlab(size_t bytes);
762 void SetTlab(byte* start, byte* end);
763 bool HasTlab() const;
Elliott Hughes5d96a712012-06-28 12:24:27 -0700764
Ian Rogersdd7624d2014-03-14 17:43:00 -0700765 // Remove the suspend trigger for this thread by making the suspend_trigger_ TLS value
766 // equal to a valid pointer.
767 // TODO: does this need to atomic? I don't think so.
768 void RemoveSuspendTrigger() {
769 tlsPtr_.suspend_trigger = reinterpret_cast<uintptr_t*>(&tlsPtr_.suspend_trigger);
770 }
771
772 // Trigger a suspend check by making the suspend_trigger_ TLS value an invalid pointer.
773 // The next time a suspend check is done, it will load from the value at this address
774 // and trigger a SIGSEGV.
775 void TriggerSuspend() {
776 tlsPtr_.suspend_trigger = nullptr;
777 }
778
779
780 // Push an object onto the allocation stack.
781 bool PushOnThreadLocalAllocationStack(mirror::Object* obj);
782
783 // Set the thread local allocation pointers to the given pointers.
784 void SetThreadLocalAllocationStack(mirror::Object** start, mirror::Object** end);
785
786 // Resets the thread local allocation pointers.
787 void RevokeThreadLocalAllocationStack();
788
789 size_t GetThreadLocalBytesAllocated() const {
790 return tlsPtr_.thread_local_pos - tlsPtr_.thread_local_start;
791 }
792
793 size_t GetThreadLocalObjectsAllocated() const {
794 return tlsPtr_.thread_local_objects;
795 }
796
Ian Rogersdd7624d2014-03-14 17:43:00 -0700797 void* GetRosAllocRun(size_t index) const {
798 return tlsPtr_.rosalloc_runs[index];
799 }
800
801 void SetRosAllocRun(size_t index, void* run) {
802 tlsPtr_.rosalloc_runs[index] = run;
803 }
804
805 private:
Ian Rogers52673ff2012-06-27 23:25:34 -0700806 explicit Thread(bool daemon);
Ian Rogersb726dcb2012-09-05 08:57:23 -0700807 ~Thread() LOCKS_EXCLUDED(Locks::mutator_lock_,
808 Locks::thread_suspend_count_lock_);
Elliott Hughesc0f09332012-03-26 13:27:06 -0700809 void Destroy();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700810
Ian Rogers365c1022012-06-22 15:05:28 -0700811 void CreatePeer(const char* name, bool as_daemon, jobject thread_group);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700812
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100813 template<bool kTransactionActive>
814 void InitPeer(ScopedObjectAccess& soa, jboolean thread_is_daemon, jobject thread_group,
815 jobject thread_name, jint thread_priority)
816 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
817
Ian Rogers62d6c772013-02-27 08:32:07 -0800818 // Avoid use, callers should use SetState. Used only by SignalCatcher::HandleSigQuit, ~Thread and
819 // Dbg::Disconnected.
Ian Rogers474b6da2012-09-25 00:20:38 -0700820 ThreadState SetStateUnsafe(ThreadState new_state) {
821 ThreadState old_state = GetState();
Ian Rogersdd7624d2014-03-14 17:43:00 -0700822 tls32_.state_and_flags.as_struct.state = new_state;
Ian Rogersc747cff2012-08-31 18:20:08 -0700823 return old_state;
824 }
Ian Rogersc747cff2012-08-31 18:20:08 -0700825
Ian Rogers04d7aa92013-03-16 14:29:17 -0700826 void VerifyStackImpl() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
827
Ian Rogerscfaa4552012-11-26 21:00:08 -0800828 void DumpState(std::ostream& os) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700829 void DumpStack(std::ostream& os) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700830 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
831 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughesd92bec42011-09-02 17:04:36 -0700832
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700833 // Out-of-line conveniences for debugging in gdb.
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700834 static Thread* CurrentFromGdb(); // Like Thread::Current.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700835 // Like Thread::Dump(std::cerr).
Ian Rogersb726dcb2012-09-05 08:57:23 -0700836 void DumpFromGdb() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700837
Elliott Hughes93e74e82011-09-13 11:07:03 -0700838 static void* CreateCallback(void* arg);
839
Ian Rogerscfaa4552012-11-26 21:00:08 -0800840 void HandleUncaughtExceptions(ScopedObjectAccess& soa)
841 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
842 void RemoveFromThreadGroup(ScopedObjectAccess& soa) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700843
Ian Rogers120f1c72012-09-28 17:17:10 -0700844 void Init(ThreadList*, JavaVMExt*) EXCLUSIVE_LOCKS_REQUIRED(Locks::runtime_shutdown_lock_);
Ian Rogers5d76c432011-10-31 21:42:49 -0700845 void InitCardTable();
Ian Rogersb033c752011-07-20 12:22:35 -0700846 void InitCpu();
Alexei Zavjalov1efa0a92014-02-04 02:08:31 +0700847 void CleanupCpu();
Ian Rogers848871b2013-08-05 10:56:33 -0700848 void InitTlsEntryPoints();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700849 void InitTid();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700850 void InitPthreadKeySelf();
Elliott Hughesbe759c62011-09-08 19:38:21 -0700851 void InitStackHwm();
852
Elliott Hughesd6a23bd2013-07-16 14:19:52 -0700853 void SetUpAlternateSignalStack();
854 void TearDownAlternateSignalStack();
855
Ian Rogers474b6da2012-09-25 00:20:38 -0700856 // 32 bits of atomically changed state and flags. Keeping as 32 bits allows and atomic CAS to
857 // change from being Suspended to Runnable without a suspend request occurring.
Chris Dearman59cde532013-12-04 18:53:49 -0800858 union PACKED(4) StateAndFlags {
859 StateAndFlags() {}
Ian Rogersdf1ce912012-11-27 17:07:11 -0800860 struct PACKED(4) {
Ian Rogers30e173f2012-09-26 14:35:03 -0700861 // Bitfield of flag values. Must be changed atomically so that flag values aren't lost. See
862 // ThreadFlags for bit field meanings.
863 volatile uint16_t flags;
864 // Holds the ThreadState. May be changed non-atomically between Suspended (ie not Runnable)
865 // transitions. Changing to Runnable requires that the suspend_request be part of the atomic
866 // operation. If a thread is suspended and a suspend_request is present, a thread may not
867 // change to Runnable as a GC or other operation is in progress.
Ian Rogers01ae5802012-09-28 16:14:01 -0700868 volatile uint16_t state;
Ian Rogers30e173f2012-09-26 14:35:03 -0700869 } as_struct;
Ian Rogers01ae5802012-09-28 16:14:01 -0700870 volatile int32_t as_int;
Chris Dearman59cde532013-12-04 18:53:49 -0800871
872 private:
873 // gcc does not handle struct with volatile member assignments correctly.
874 // See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47409
875 DISALLOW_COPY_AND_ASSIGN(StateAndFlags);
Ian Rogers474b6da2012-09-25 00:20:38 -0700876 };
Ian Rogers474b6da2012-09-25 00:20:38 -0700877
Ian Rogersdd7624d2014-03-14 17:43:00 -0700878 static void ThreadExitCallback(void* arg);
Elliott Hughes5d96a712012-06-28 12:24:27 -0700879
Dave Allison0aded082013-11-07 13:15:11 -0800880 // Maximum number of checkpoint functions.
881 static constexpr uint32_t kMaxCheckpoints = 3;
882
Ian Rogersdd7624d2014-03-14 17:43:00 -0700883 // Has Thread::Startup been called?
884 static bool is_started_;
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700885
Ian Rogersdd7624d2014-03-14 17:43:00 -0700886 // TLS key used to retrieve the Thread*.
887 static pthread_key_t pthread_key_self_;
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800888
Ian Rogersdd7624d2014-03-14 17:43:00 -0700889 // Used to notify threads that they should attempt to resume, they will suspend again if
890 // their suspend count is > 0.
891 static ConditionVariable* resume_cond_ GUARDED_BY(Locks::thread_suspend_count_lock_);
Dave Allisonb373e092014-02-20 16:06:36 -0800892
Ian Rogersdd7624d2014-03-14 17:43:00 -0700893 /***********************************************************************************************/
894 // Thread local storage. Fields are grouped by size to enable 32 <-> 64 searching to account for
895 // pointer size differences. To encourage shorter encoding, more frequently used values appear
896 // first if possible.
897 /***********************************************************************************************/
Elliott Hughes6a607ad2012-07-13 20:40:00 -0700898
Ian Rogersdd7624d2014-03-14 17:43:00 -0700899 struct PACKED(4) tls_32bit_sized_values {
900 // We have no control over the size of 'bool', but want our boolean fields
901 // to be 4-byte quantities.
902 typedef uint32_t bool32_t;
Ian Rogers22f454c2012-09-08 11:06:29 -0700903
Ian Rogersdd7624d2014-03-14 17:43:00 -0700904 explicit tls_32bit_sized_values(bool is_daemon) :
905 suspend_count(0), debug_suspend_count(0), thin_lock_thread_id(0), tid(0),
906 daemon(is_daemon), throwing_OutOfMemoryError(false), no_thread_suspension(0),
907 thread_exit_check_count(0) {
908 }
Dave Allisonb373e092014-02-20 16:06:36 -0800909
Ian Rogersdd7624d2014-03-14 17:43:00 -0700910 union StateAndFlags state_and_flags;
911 COMPILE_ASSERT(sizeof(union StateAndFlags) == sizeof(int32_t),
912 sizeof_state_and_flags_and_int32_are_different);
Dave Allisonb373e092014-02-20 16:06:36 -0800913
Ian Rogersdd7624d2014-03-14 17:43:00 -0700914 // A non-zero value is used to tell the current thread to enter a safe point
915 // at the next poll.
916 int suspend_count GUARDED_BY(Locks::thread_suspend_count_lock_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700917
Ian Rogersdd7624d2014-03-14 17:43:00 -0700918 // How much of 'suspend_count_' is by request of the debugger, used to set things right
919 // when the debugger detaches. Must be <= suspend_count_.
920 int debug_suspend_count GUARDED_BY(Locks::thread_suspend_count_lock_);
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800921
Ian Rogersdd7624d2014-03-14 17:43:00 -0700922 // Thin lock thread id. This is a small integer used by the thin lock implementation.
923 // This is not to be confused with the native thread's tid, nor is it the value returned
924 // by java.lang.Thread.getId --- this is a distinct value, used only for locking. One
925 // important difference between this id and the ids visible to managed code is that these
926 // ones get reused (to ensure that they fit in the number of bits available).
927 uint32_t thin_lock_thread_id;
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800928
Ian Rogersdd7624d2014-03-14 17:43:00 -0700929 // System thread id.
930 uint32_t tid;
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800931
Ian Rogersdd7624d2014-03-14 17:43:00 -0700932 // Is the thread a daemon?
933 const bool32_t daemon;
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800934
Ian Rogersdd7624d2014-03-14 17:43:00 -0700935 // A boolean telling us whether we're recursively throwing OOME.
936 bool32_t throwing_OutOfMemoryError;
937
938 // A positive value implies we're in a region where thread suspension isn't expected.
939 uint32_t no_thread_suspension;
940
941 // How many times has our pthread key's destructor been called?
942 uint32_t thread_exit_check_count;
943 } tls32_;
944
945 struct PACKED(8) tls_64bit_sized_values {
946 tls_64bit_sized_values() : trace_clock_base(0), deoptimization_return_value() {
947 }
948
949 // The clock base used for tracing.
950 uint64_t trace_clock_base;
951
952 // Return value used by deoptimization.
953 JValue deoptimization_return_value;
954
955 RuntimeStats stats;
956 } tls64_;
957
958 struct PACKED(4) tls_ptr_sized_values {
959 tls_ptr_sized_values() : card_table(nullptr), exception(nullptr), stack_end(nullptr),
960 managed_stack(), suspend_trigger(nullptr), jni_env(nullptr), self(nullptr), opeer(nullptr),
961 jpeer(nullptr), stack_begin(nullptr), stack_size(0), throw_location(),
962 stack_trace_sample(nullptr), wait_next(nullptr), monitor_enter_object(nullptr),
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700963 top_handle_scope(nullptr), class_loader_override(nullptr), long_jump_context(nullptr),
Ian Rogersdd7624d2014-03-14 17:43:00 -0700964 instrumentation_stack(nullptr), debug_invoke_req(nullptr), single_step_control(nullptr),
965 deoptimization_shadow_frame(nullptr), name(nullptr), pthread_self(0),
966 last_no_thread_suspension_cause(nullptr), thread_local_start(nullptr),
967 thread_local_pos(nullptr), thread_local_end(nullptr), thread_local_objects(0),
968 thread_local_alloc_stack_top(nullptr), thread_local_alloc_stack_end(nullptr) {
969 }
970
971 // The biased card table, see CardTable for details.
972 byte* card_table;
973
974 // The pending exception or NULL.
975 mirror::Throwable* exception;
976
977 // The end of this thread's stack. This is the lowest safely-addressable address on the stack.
978 // We leave extra space so there's room for the code that throws StackOverflowError.
979 byte* stack_end;
980
981 // The top of the managed stack often manipulated directly by compiler generated code.
982 ManagedStack managed_stack;
983
984 // In certain modes, setting this to 0 will trigger a SEGV and thus a suspend check. It is
985 // normally set to the address of itself.
986 uintptr_t* suspend_trigger;
987
988 // Every thread may have an associated JNI environment
989 JNIEnvExt* jni_env;
990
991 // Initialized to "this". On certain architectures (such as x86) reading off of Thread::Current
992 // is easy but getting the address of Thread::Current is hard. This field can be read off of
993 // Thread::Current to give the address.
994 Thread* self;
995
996 // Our managed peer (an instance of java.lang.Thread). The jobject version is used during thread
997 // start up, until the thread is registered and the local opeer_ is used.
998 mirror::Object* opeer;
999 jobject jpeer;
1000
1001 // The "lowest addressable byte" of the stack.
1002 byte* stack_begin;
1003
1004 // Size of the stack.
1005 size_t stack_size;
1006
1007 // The location the current exception was thrown from.
1008 ThrowLocation throw_location;
1009
1010 // Pointer to previous stack trace captured by sampling profiler.
1011 std::vector<mirror::ArtMethod*>* stack_trace_sample;
1012
1013 // The next thread in the wait set this thread is part of or NULL if not waiting.
1014 Thread* wait_next;
1015
1016 // If we're blocked in MonitorEnter, this is the object we're trying to lock.
1017 mirror::Object* monitor_enter_object;
1018
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001019 // Top of linked list of handle scopes or nullptr for none.
1020 HandleScope* top_handle_scope;
Ian Rogersdd7624d2014-03-14 17:43:00 -07001021
1022 // Needed to get the right ClassLoader in JNI_OnLoad, but also
1023 // useful for testing.
1024 mirror::ClassLoader* class_loader_override;
1025
1026 // Thread local, lazily allocated, long jump context. Used to deliver exceptions.
1027 Context* long_jump_context;
1028
1029 // Additional stack used by method instrumentation to store method and return pc values.
1030 // Stored as a pointer since std::deque is not PACKED.
1031 std::deque<instrumentation::InstrumentationStackFrame>* instrumentation_stack;
1032
1033 // JDWP invoke-during-breakpoint support.
1034 DebugInvokeReq* debug_invoke_req;
1035
1036 // JDWP single-stepping support.
1037 SingleStepControl* single_step_control;
1038
1039 // Shadow frame stack that is used temporarily during the deoptimization of a method.
1040 ShadowFrame* deoptimization_shadow_frame;
1041
1042 // A cached copy of the java.lang.Thread's name.
1043 std::string* name;
1044
1045 // A cached pthread_t for the pthread underlying this Thread*.
1046 pthread_t pthread_self;
1047
Ian Rogersdd7624d2014-03-14 17:43:00 -07001048 // If no_thread_suspension_ is > 0, what is causing that assertion.
1049 const char* last_no_thread_suspension_cause;
1050
1051 // Pending checkpoint function or NULL if non-pending. Installation guarding by
1052 // Locks::thread_suspend_count_lock_.
1053 Closure* checkpoint_functions[kMaxCheckpoints];
1054
1055 // Entrypoint function pointers.
1056 // TODO: move this to more of a global offset table model to avoid per-thread duplication.
1057 InterpreterEntryPoints interpreter_entrypoints;
1058 JniEntryPoints jni_entrypoints;
1059 PortableEntryPoints portable_entrypoints;
1060 QuickEntryPoints quick_entrypoints;
1061
1062 // Thread-local allocation pointer.
1063 byte* thread_local_start;
1064 byte* thread_local_pos;
1065 byte* thread_local_end;
1066 size_t thread_local_objects;
1067
Mathieu Chartier0651d412014-04-29 14:37:57 -07001068 // There are RosAlloc::kNumThreadLocalSizeBrackets thread-local size brackets per thread.
1069 void* rosalloc_runs[gc::allocator::RosAlloc::kNumThreadLocalSizeBrackets];
Ian Rogersdd7624d2014-03-14 17:43:00 -07001070
1071 // Thread-local allocation stack data/routines.
1072 mirror::Object** thread_local_alloc_stack_top;
1073 mirror::Object** thread_local_alloc_stack_end;
Chao-ying Fu9e369312014-05-21 11:20:52 -07001074
1075 // Support for Mutex lock hierarchy bug detection.
1076 BaseMutex* held_mutexes[kLockLevelCount];
Ian Rogersdd7624d2014-03-14 17:43:00 -07001077 } tlsPtr_;
1078
1079 // Guards the 'interrupted_' and 'wait_monitor_' members.
1080 Mutex* wait_mutex_ DEFAULT_MUTEX_ACQUIRED_AFTER;
1081
1082 // Condition variable waited upon during a wait.
1083 ConditionVariable* wait_cond_ GUARDED_BY(wait_mutex_);
1084 // Pointer to the monitor lock we're currently waiting on or NULL if not waiting.
1085 Monitor* wait_monitor_ GUARDED_BY(wait_mutex_);
1086
1087 // Thread "interrupted" status; stays raised until queried or thrown.
1088 bool interrupted_ GUARDED_BY(wait_mutex_);
1089
Mathieu Chartiere6da9af2013-12-16 11:54:42 -08001090 friend class Dbg; // For SetStateUnsafe.
Mathieu Chartier15d34022014-02-26 17:16:38 -08001091 friend class gc::collector::SemiSpace; // For getting stack traces.
Mathieu Chartier692fafd2013-11-29 17:24:40 -08001092 friend class Runtime; // For CreatePeer.
Ian Rogers5cf98192014-05-29 21:31:50 -07001093 friend class QuickExceptionHandler; // For dumping the stack.
Mathieu Chartier692fafd2013-11-29 17:24:40 -08001094 friend class ScopedThreadStateChange;
1095 friend class SignalCatcher; // For SetStateUnsafe.
Mathieu Chartier119c6bd2014-05-09 14:11:47 -07001096 friend class StubTest; // For accessing entrypoints.
Mathieu Chartier692fafd2013-11-29 17:24:40 -08001097 friend class ThreadList; // For ~Thread and Destroy.
1098
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001099 DISALLOW_COPY_AND_ASSIGN(Thread);
1100};
Ian Rogersbdb03912011-09-14 00:55:44 -07001101
Elliott Hughes330304d2011-08-12 14:28:05 -07001102std::ostream& operator<<(std::ostream& os, const Thread& thread);
Elliott Hughes34e06962012-04-09 13:55:55 -07001103std::ostream& operator<<(std::ostream& os, const ThreadState& state);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001104
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001105} // namespace art
1106
Brian Carlstromfc0e3212013-07-17 14:40:12 -07001107#endif // ART_RUNTIME_THREAD_H_