Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 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 | */ |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_THREAD_H_ |
| 18 | #define ART_RUNTIME_THREAD_H_ |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 19 | |
Elliott Hughes | 02b48d1 | 2011-09-07 17:15:51 -0700 | [diff] [blame] | 20 | #include <bitset> |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 21 | #include <deque> |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 22 | #include <iosfwd> |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 23 | #include <list> |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 24 | #include <string> |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 25 | |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 26 | #include "base/macros.h" |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 27 | #include "base/mutex.h" |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 28 | #include "entrypoints/interpreter/interpreter_entrypoints.h" |
| 29 | #include "entrypoints/jni/jni_entrypoints.h" |
Ian Rogers | 7655f29 | 2013-07-29 11:07:13 -0700 | [diff] [blame] | 30 | #include "entrypoints/portable/portable_entrypoints.h" |
| 31 | #include "entrypoints/quick/quick_entrypoints.h" |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 32 | #include "gc/allocator/rosalloc.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 33 | #include "globals.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 34 | #include "handle_scope.h" |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 35 | #include "jvalue.h" |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 36 | #include "object_callbacks.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 37 | #include "offsets.h" |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 38 | #include "runtime_stats.h" |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 39 | #include "stack.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 40 | #include "thread_state.h" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 41 | #include "throw_location.h" |
Ian Rogers | 507dfdd | 2014-05-15 16:42:40 -0700 | [diff] [blame] | 42 | #include "UniquePtrCompat.h" |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 43 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 44 | namespace art { |
| 45 | |
Mathieu Chartier | 15d3402 | 2014-02-26 17:16:38 -0800 | [diff] [blame] | 46 | namespace gc { |
| 47 | namespace collector { |
| 48 | class SemiSpace; |
| 49 | } // namespace collector |
| 50 | } // namespace gc |
| 51 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 52 | namespace mirror { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 53 | class ArtMethod; |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 54 | 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 Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 64 | } // namespace mirror |
| 65 | class BaseMutex; |
| 66 | class ClassLinker; |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 67 | class Closure; |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 68 | class Context; |
Ian Rogers | 1b09b09 | 2012-08-20 15:35:52 -0700 | [diff] [blame] | 69 | struct DebugInvokeReq; |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 70 | class DexFile; |
Ian Rogers | b48b9eb | 2014-02-28 16:20:21 -0800 | [diff] [blame] | 71 | class JavaVMExt; |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 72 | struct JNIEnvExt; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 73 | class Monitor; |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 74 | class Runtime; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 75 | class ScopedObjectAccess; |
| 76 | class ScopedObjectAccessUnchecked; |
Logan Chien | f7ad17e | 2012-03-15 03:10:03 +0800 | [diff] [blame] | 77 | class ShadowFrame; |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 78 | struct SingleStepControl; |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 79 | class Thread; |
| 80 | class ThreadList; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 81 | |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 82 | // Thread priorities. These must match the Thread.MIN_PRIORITY, |
| 83 | // Thread.NORM_PRIORITY, and Thread.MAX_PRIORITY constants. |
| 84 | enum ThreadPriority { |
| 85 | kMinThreadPriority = 1, |
| 86 | kNormThreadPriority = 5, |
| 87 | kMaxThreadPriority = 10, |
| 88 | }; |
| 89 | |
Ian Rogers | 474b6da | 2012-09-25 00:20:38 -0700 | [diff] [blame] | 90 | enum ThreadFlag { |
Ian Rogers | 50ffee2 | 2012-11-20 11:47:44 -0800 | [diff] [blame] | 91 | kSuspendRequest = 1, // If set implies that suspend_count_ > 0 and the Thread should enter the |
| 92 | // safepoint handler. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 93 | kCheckpointRequest = 2 // Request that the thread do some checkpoint work and then continue. |
Ian Rogers | 474b6da | 2012-09-25 00:20:38 -0700 | [diff] [blame] | 94 | }; |
| 95 | |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 96 | class Thread { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 97 | public: |
Ian Rogers | 932746a | 2011-09-22 18:57:50 -0700 | [diff] [blame] | 98 | // Space to throw a StackOverflowError in. |
Ian Rogers | b3fabf4 | 2014-03-12 23:35:11 -0700 | [diff] [blame] | 99 | // TODO: shrink reserved space, in particular for 64bit. |
Andreas Gampe | e62a07e | 2014-03-26 14:53:21 -0700 | [diff] [blame] | 100 | #if defined(__x86_64__) |
Serguei Katkov | d508006 | 2014-05-14 17:00:05 +0700 | [diff] [blame] | 101 | static constexpr size_t kStackOverflowReservedBytes = 32 * KB; |
Andreas Gampe | e62a07e | 2014-03-26 14:53:21 -0700 | [diff] [blame] | 102 | #elif defined(__aarch64__) |
| 103 | // Worst-case, we would need about 2.6x the amount of x86_64 for many more registers. |
| 104 | // But this one works rather well. |
| 105 | static constexpr size_t kStackOverflowReservedBytes = 32 * KB; |
Brian Carlstrom | 2aef07c | 2014-05-15 11:18:29 -0700 | [diff] [blame] | 106 | #elif defined(__i386__) |
| 107 | // TODO: Bumped to workaround regression (http://b/14982147) Specifically to fix: |
| 108 | // test-art-host-run-test-interpreter-018-stack-overflow |
| 109 | // test-art-host-run-test-interpreter-107-int-math2 |
| 110 | static constexpr size_t kStackOverflowReservedBytes = 24 * KB; |
Ian Rogers | 463cb4d | 2014-03-12 16:23:09 -0700 | [diff] [blame] | 111 | #else |
| 112 | static constexpr size_t kStackOverflowReservedBytes = 16 * KB; |
| 113 | #endif |
Mathieu Chartier | 0d507d1 | 2014-03-19 10:17:28 -0700 | [diff] [blame] | 114 | // How much of the reserved bytes is reserved for incoming signals. |
| 115 | static constexpr size_t kStackOverflowSignalReservedBytes = 2 * KB; |
| 116 | // How much of the reserved bytes we may temporarily use during stack overflow checks as an |
| 117 | // optimization. |
| 118 | static constexpr size_t kStackOverflowReservedUsableBytes = |
| 119 | kStackOverflowReservedBytes - kStackOverflowSignalReservedBytes; |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 120 | |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 121 | // For implicit overflow checks we reserve an extra piece of memory at the bottom |
| 122 | // of the stack (lowest memory). The higher portion of the memory |
| 123 | // is protected against reads and the lower is available for use while |
| 124 | // throwing the StackOverflow exception. |
| 125 | static constexpr size_t kStackOverflowProtectedSize = 32 * KB; |
| 126 | static constexpr size_t kStackOverflowImplicitCheckSize = kStackOverflowProtectedSize + |
| 127 | kStackOverflowReservedBytes; |
| 128 | |
Elliott Hughes | 462c944 | 2012-03-23 18:47:50 -0700 | [diff] [blame] | 129 | // Creates a new native thread corresponding to the given managed peer. |
| 130 | // Used to implement Thread.start. |
Ian Rogers | 52673ff | 2012-06-27 23:25:34 -0700 | [diff] [blame] | 131 | static void CreateNativeThread(JNIEnv* env, jobject peer, size_t stack_size, bool daemon); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 132 | |
Elliott Hughes | 462c944 | 2012-03-23 18:47:50 -0700 | [diff] [blame] | 133 | // Attaches the calling native thread to the runtime, returning the new native peer. |
| 134 | // Used to implement JNI AttachCurrentThread and AttachCurrentThreadAsDaemon calls. |
Mathieu Chartier | 664bebf | 2012-11-12 16:54:11 -0800 | [diff] [blame] | 135 | static Thread* Attach(const char* thread_name, bool as_daemon, jobject thread_group, |
| 136 | bool create_peer); |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 137 | |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 138 | // Reset internal state of child thread after fork. |
| 139 | void InitAfterFork(); |
| 140 | |
Ian Rogers | 02ed4c0 | 2013-09-06 13:10:04 -0700 | [diff] [blame] | 141 | static Thread* Current(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 142 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 143 | static Thread* FromManagedThread(const ScopedObjectAccessUnchecked& ts, |
| 144 | mirror::Object* thread_peer) |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 145 | EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 146 | LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_) |
| 147 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 148 | static Thread* FromManagedThread(const ScopedObjectAccessUnchecked& ts, jobject thread) |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 149 | EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 150 | LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_) |
| 151 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 152 | |
Elliott Hughes | 28fa76d | 2012-04-09 17:31:46 -0700 | [diff] [blame] | 153 | // Translates 172 to pAllocArrayFromCode and so on. |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 154 | template<size_t size_of_pointers> |
| 155 | static void DumpThreadOffset(std::ostream& os, uint32_t offset); |
Elliott Hughes | 28fa76d | 2012-04-09 17:31:46 -0700 | [diff] [blame] | 156 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 157 | // Dumps a one-line summary of thread state (used for operator<<). |
| 158 | void ShortDump(std::ostream& os) const; |
| 159 | |
| 160 | // Dumps the detailed thread state and the thread stack (used for SIGQUIT). |
| 161 | void Dump(std::ostream& os) const |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 162 | LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_) |
| 163 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 164 | |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 165 | void DumpJavaStack(std::ostream& os) const |
| 166 | LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_) |
| 167 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 168 | |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 169 | // Dumps the SIGQUIT per-thread header. 'thread' can be NULL for a non-attached thread, in which |
| 170 | // case we use 'tid' to identify the thread, and we'll include as much information as we can. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 171 | static void DumpState(std::ostream& os, const Thread* thread, pid_t tid) |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 172 | LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_) |
| 173 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 174 | |
Ian Rogers | 474b6da | 2012-09-25 00:20:38 -0700 | [diff] [blame] | 175 | ThreadState GetState() const { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 176 | DCHECK_GE(tls32_.state_and_flags.as_struct.state, kTerminated); |
| 177 | DCHECK_LE(tls32_.state_and_flags.as_struct.state, kSuspended); |
| 178 | return static_cast<ThreadState>(tls32_.state_and_flags.as_struct.state); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Ian Rogers | 474b6da | 2012-09-25 00:20:38 -0700 | [diff] [blame] | 181 | ThreadState SetState(ThreadState new_state); |
Ian Rogers | 52673ff | 2012-06-27 23:25:34 -0700 | [diff] [blame] | 182 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 183 | int GetSuspendCount() const EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_suspend_count_lock_) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 184 | return tls32_.suspend_count; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 185 | } |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 186 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 187 | int GetDebugSuspendCount() const EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_suspend_count_lock_) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 188 | return tls32_.debug_suspend_count; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 191 | bool IsSuspended() const { |
Chris Dearman | 59cde53 | 2013-12-04 18:53:49 -0800 | [diff] [blame] | 192 | union StateAndFlags state_and_flags; |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 193 | state_and_flags.as_int = tls32_.state_and_flags.as_int; |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 194 | return state_and_flags.as_struct.state != kRunnable && |
| 195 | (state_and_flags.as_struct.flags & kSuspendRequest) != 0; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 198 | void ModifySuspendCount(Thread* self, int delta, bool for_debugger) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 199 | EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 200 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 201 | bool RequestCheckpoint(Closure* function) |
| 202 | EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_suspend_count_lock_); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 203 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 204 | // Called when thread detected that the thread_suspend_count_ was non-zero. Gives up share of |
| 205 | // mutator_lock_ and waits until it is resumed and thread_suspend_count_ is zero. |
Ian Rogers | 9da7f59 | 2012-08-20 17:14:28 -0700 | [diff] [blame] | 206 | void FullSuspendCheck() |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 207 | LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_) |
| 208 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 209 | |
| 210 | // Transition from non-runnable to runnable state acquiring share on mutator_lock_. |
| 211 | ThreadState TransitionFromSuspendedToRunnable() |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 212 | LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_) |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 213 | SHARED_LOCK_FUNCTION(Locks::mutator_lock_) |
Ian Rogers | 1ffa32f | 2013-02-05 18:29:08 -0800 | [diff] [blame] | 214 | ALWAYS_INLINE; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 215 | |
| 216 | // Transition from runnable into a state where mutator privileges are denied. Releases share of |
| 217 | // mutator lock. |
| 218 | void TransitionFromRunnableToSuspended(ThreadState new_state) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 219 | LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_) |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 220 | UNLOCK_FUNCTION(Locks::mutator_lock_) |
Ian Rogers | 1ffa32f | 2013-02-05 18:29:08 -0800 | [diff] [blame] | 221 | ALWAYS_INLINE; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 222 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 223 | // Once called thread suspension will cause an assertion failure. |
Ian Rogers | 52673ff | 2012-06-27 23:25:34 -0700 | [diff] [blame] | 224 | const char* StartAssertNoThreadSuspension(const char* cause) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 225 | if (kIsDebugBuild) { |
| 226 | CHECK(cause != NULL); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 227 | const char* previous_cause = tlsPtr_.last_no_thread_suspension_cause; |
| 228 | tls32_.no_thread_suspension++; |
| 229 | tlsPtr_.last_no_thread_suspension_cause = cause; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 230 | return previous_cause; |
| 231 | } else { |
| 232 | return nullptr; |
| 233 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 234 | } |
Ian Rogers | 52673ff | 2012-06-27 23:25:34 -0700 | [diff] [blame] | 235 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 236 | // End region where no thread suspension is expected. |
Ian Rogers | 52673ff | 2012-06-27 23:25:34 -0700 | [diff] [blame] | 237 | void EndAssertNoThreadSuspension(const char* old_cause) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 238 | if (kIsDebugBuild) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 239 | CHECK(old_cause != nullptr || tls32_.no_thread_suspension == 1); |
| 240 | CHECK_GT(tls32_.no_thread_suspension, 0U); |
| 241 | tls32_.no_thread_suspension--; |
| 242 | tlsPtr_.last_no_thread_suspension_cause = old_cause; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 243 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 244 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 245 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 246 | void AssertThreadSuspensionIsAllowable(bool check_locks = true) const; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 247 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 248 | bool IsDaemon() const { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 249 | return tls32_.daemon; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 252 | bool HoldsLock(mirror::Object*) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 253 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 254 | /* |
| 255 | * Changes the priority of this thread to match that of the java.lang.Thread object. |
| 256 | * |
| 257 | * We map a priority value from 1-10 to Linux "nice" values, where lower |
| 258 | * numbers indicate higher priority. |
| 259 | */ |
| 260 | void SetNativePriority(int newPriority); |
| 261 | |
| 262 | /* |
| 263 | * Returns the thread priority for the current thread by querying the system. |
| 264 | * This is useful when attaching a thread through JNI. |
| 265 | * |
| 266 | * Returns a value from 1 to 10 (compatible with java.lang.Thread values). |
| 267 | */ |
| 268 | static int GetNativePriority(); |
| 269 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 270 | uint32_t GetThreadId() const { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 271 | return tls32_.thin_lock_thread_id; |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 274 | pid_t GetTid() const { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 275 | return tls32_.tid; |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 276 | } |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 277 | |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 278 | // Returns the java.lang.Thread's name, or NULL if this Thread* doesn't have a peer. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 279 | mirror::String* GetThreadName(const ScopedObjectAccessUnchecked& ts) const |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 280 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | 899e789 | 2012-01-24 14:57:32 -0800 | [diff] [blame] | 281 | |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 282 | // Sets 'name' to the java.lang.Thread's name. This requires no transition to managed code, |
| 283 | // allocation, or locking. |
| 284 | void GetThreadName(std::string& name) const; |
| 285 | |
Elliott Hughes | 899e789 | 2012-01-24 14:57:32 -0800 | [diff] [blame] | 286 | // Sets the thread's name. |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 287 | void SetThreadName(const char* name) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 288 | |
Jeff Hao | 57dac6e | 2013-08-15 16:36:24 -0700 | [diff] [blame] | 289 | // Returns the thread-specific CPU-time clock in microseconds or -1 if unavailable. |
| 290 | uint64_t GetCpuMicroTime() const; |
| 291 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 292 | mirror::Object* GetPeer() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 293 | CHECK(tlsPtr_.jpeer == nullptr); |
| 294 | return tlsPtr_.opeer; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 297 | bool HasPeer() const { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 298 | return tlsPtr_.jpeer != nullptr || tlsPtr_.opeer != nullptr; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 301 | RuntimeStats* GetStats() { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 302 | return &tls64_.stats; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Elliott Hughes | 7dc5166 | 2012-05-16 14:48:43 -0700 | [diff] [blame] | 305 | bool IsStillStarting() const; |
| 306 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 307 | bool IsExceptionPending() const { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 308 | return tlsPtr_.exception != nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 311 | mirror::Throwable* GetException(ThrowLocation* throw_location) const |
| 312 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 313 | if (throw_location != nullptr) { |
| 314 | *throw_location = tlsPtr_.throw_location; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 315 | } |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 316 | return tlsPtr_.exception; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 319 | void AssertNoPendingException() const; |
Mathieu Chartier | 8d7672e | 2014-02-25 10:57:16 -0800 | [diff] [blame] | 320 | void AssertNoPendingExceptionForNewException(const char* msg) const; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 321 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 322 | void SetException(const ThrowLocation& throw_location, mirror::Throwable* new_exception) |
| 323 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 324 | CHECK(new_exception != NULL); |
Ian Rogers | 474b6da | 2012-09-25 00:20:38 -0700 | [diff] [blame] | 325 | // TODO: DCHECK(!IsExceptionPending()); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 326 | tlsPtr_.exception = new_exception; |
| 327 | tlsPtr_.throw_location = throw_location; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | void ClearException() { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 331 | tlsPtr_.exception = nullptr; |
| 332 | tlsPtr_.throw_location.Clear(); |
jeffhao | 94d6df4 | 2012-11-26 16:02:12 -0800 | [diff] [blame] | 333 | } |
| 334 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 335 | // Find catch block and perform long jump to appropriate exception handle |
jeffhao | 94d6df4 | 2012-11-26 16:02:12 -0800 | [diff] [blame] | 336 | void QuickDeliverException() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 337 | |
| 338 | Context* GetLongJumpContext(); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 339 | void ReleaseLongJumpContext(Context* context) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 340 | DCHECK(tlsPtr_.long_jump_context == nullptr); |
| 341 | tlsPtr_.long_jump_context = context; |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 344 | mirror::ArtMethod* GetCurrentMethod(uint32_t* dex_pc) const |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 345 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 346 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 347 | ThrowLocation GetCurrentLocationForThrow() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 348 | |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 349 | void SetTopOfStack(mirror::ArtMethod** top_method, uintptr_t pc) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 350 | tlsPtr_.managed_stack.SetTopQuickFrame(top_method); |
| 351 | tlsPtr_.managed_stack.SetTopQuickFramePc(pc); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Jeff Hao | 11ffc2d | 2013-02-01 11:52:17 -0800 | [diff] [blame] | 354 | void SetTopOfShadowStack(ShadowFrame* top) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 355 | tlsPtr_.managed_stack.SetTopShadowFrame(top); |
Jeff Hao | 11ffc2d | 2013-02-01 11:52:17 -0800 | [diff] [blame] | 356 | } |
| 357 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 358 | bool HasManagedStack() const { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 359 | return (tlsPtr_.managed_stack.GetTopQuickFrame() != nullptr) || |
| 360 | (tlsPtr_.managed_stack.GetTopShadowFrame() != nullptr); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 363 | // If 'msg' is NULL, no detail message is set. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 364 | void ThrowNewException(const ThrowLocation& throw_location, |
| 365 | const char* exception_class_descriptor, const char* msg) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 366 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 367 | |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 368 | // If 'msg' is NULL, no detail message is set. An exception must be pending, and will be |
| 369 | // used as the new exception's cause. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 370 | void ThrowNewWrappedException(const ThrowLocation& throw_location, |
| 371 | const char* exception_class_descriptor, |
| 372 | const char* msg) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 373 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 374 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 375 | void ThrowNewExceptionF(const ThrowLocation& throw_location, |
| 376 | const char* exception_class_descriptor, const char* fmt, ...) |
| 377 | __attribute__((format(printf, 4, 5))) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 378 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 379 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 380 | void ThrowNewExceptionV(const ThrowLocation& throw_location, |
| 381 | const char* exception_class_descriptor, const char* fmt, va_list ap) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 382 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 383 | |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 384 | // OutOfMemoryError is special, because we need to pre-allocate an instance. |
Elliott Hughes | 8a8b9cb | 2012-04-13 18:29:22 -0700 | [diff] [blame] | 385 | // Only the GC should call this. |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 386 | void ThrowOutOfMemoryError(const char* msg) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 387 | |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 388 | static void Startup(); |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 389 | static void FinishStartup(); |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 390 | static void Shutdown(); |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 391 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 392 | // JNI methods |
Elliott Hughes | 69f5bc6 | 2011-08-24 09:26:14 -0700 | [diff] [blame] | 393 | JNIEnvExt* GetJniEnv() const { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 394 | return tlsPtr_.jni_env; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 397 | // Convert a jobject into a Object* |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 398 | mirror::Object* DecodeJObject(jobject obj) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 399 | |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 400 | mirror::Object* GetMonitorEnterObject() const { |
| 401 | return tlsPtr_.monitor_enter_object; |
| 402 | } |
| 403 | |
| 404 | void SetMonitorEnterObject(mirror::Object* obj) { |
| 405 | tlsPtr_.monitor_enter_object = obj; |
| 406 | } |
| 407 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 408 | // Implements java.lang.Thread.interrupted. |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 409 | bool Interrupted() LOCKS_EXCLUDED(wait_mutex_); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 410 | // Implements java.lang.Thread.isInterrupted. |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 411 | bool IsInterrupted() LOCKS_EXCLUDED(wait_mutex_); |
| 412 | bool IsInterruptedLocked() EXCLUSIVE_LOCKS_REQUIRED(wait_mutex_) { |
| 413 | return interrupted_; |
| 414 | } |
| 415 | void Interrupt(Thread* self) LOCKS_EXCLUDED(wait_mutex_); |
| 416 | void SetInterruptedLocked(bool i) EXCLUSIVE_LOCKS_REQUIRED(wait_mutex_) { |
| 417 | interrupted_ = i; |
| 418 | } |
| 419 | void Notify() LOCKS_EXCLUDED(wait_mutex_); |
| 420 | |
| 421 | private: |
| 422 | void NotifyLocked(Thread* self) EXCLUSIVE_LOCKS_REQUIRED(wait_mutex_); |
| 423 | |
| 424 | public: |
| 425 | Mutex* GetWaitMutex() const LOCK_RETURNED(wait_mutex_) { |
| 426 | return wait_mutex_; |
| 427 | } |
| 428 | |
| 429 | ConditionVariable* GetWaitConditionVariable() const EXCLUSIVE_LOCKS_REQUIRED(wait_mutex_) { |
| 430 | return wait_cond_; |
| 431 | } |
| 432 | |
| 433 | Monitor* GetWaitMonitor() const EXCLUSIVE_LOCKS_REQUIRED(wait_mutex_) { |
| 434 | return wait_monitor_; |
| 435 | } |
| 436 | |
| 437 | void SetWaitMonitor(Monitor* mon) EXCLUSIVE_LOCKS_REQUIRED(wait_mutex_) { |
| 438 | wait_monitor_ = mon; |
| 439 | } |
| 440 | |
| 441 | |
| 442 | // Waiter link-list support. |
| 443 | Thread* GetWaitNext() const { |
| 444 | return tlsPtr_.wait_next; |
| 445 | } |
| 446 | |
| 447 | void SetWaitNext(Thread* next) { |
| 448 | tlsPtr_.wait_next = next; |
| 449 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 450 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 451 | mirror::ClassLoader* GetClassLoaderOverride() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 452 | return tlsPtr_.class_loader_override; |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 455 | void SetClassLoaderOverride(mirror::ClassLoader* class_loader_override) |
| 456 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 457 | |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 458 | // Create the internal representation of a stack trace, that is more time |
Sebastien Hertz | ee1d79a | 2014-02-21 15:46:30 +0100 | [diff] [blame] | 459 | // and space efficient to compute than the StackTraceElement[]. |
| 460 | template<bool kTransactionActive> |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 461 | jobject CreateInternalStackTrace(const ScopedObjectAccessUnchecked& soa) const |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 462 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 463 | |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 464 | // Convert an internal stack trace representation (returned by CreateInternalStackTrace) to a |
| 465 | // StackTraceElement[]. If output_array is NULL, a new array is created, otherwise as many |
| 466 | // frames as will fit are written into the given array. If stack_depth is non-NULL, it's updated |
| 467 | // with the number of valid frames in the returned array. |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 468 | static jobjectArray InternalStackTraceToStackTraceElementArray(const ScopedObjectAccess& soa, |
| 469 | jobject internal, jobjectArray output_array = nullptr, int* stack_depth = nullptr) |
| 470 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 471 | |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 472 | void VisitRoots(RootCallback* visitor, void* arg) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 473 | |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 474 | ALWAYS_INLINE void VerifyStack() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
jeffhao | 2504552 | 2012-03-13 19:34:37 -0700 | [diff] [blame] | 475 | |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 476 | // |
| 477 | // Offsets of various members of native Thread class, used by compiled code. |
| 478 | // |
| 479 | |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 480 | 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 Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 485 | } |
| 486 | |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 487 | 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 Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 492 | } |
| 493 | |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 494 | 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 Rogers | 07ec8e1 | 2012-12-01 01:26:51 -0800 | [diff] [blame] | 512 | } |
| 513 | |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 514 | 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 Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 519 | } |
| 520 | |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 521 | 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 Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 525 | } |
| 526 | |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 527 | 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 Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 531 | } |
| 532 | |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 533 | 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 Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 564 | } |
| 565 | |
Ian Rogers | 932746a | 2011-09-22 18:57:50 -0700 | [diff] [blame] | 566 | // Size of stack less any space reserved for stack overflow |
jeffhao | d752132 | 2012-11-21 15:38:24 -0800 | [diff] [blame] | 567 | size_t GetStackSize() const { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 568 | return tlsPtr_.stack_size - (tlsPtr_.stack_end - tlsPtr_.stack_begin); |
Ian Rogers | 932746a | 2011-09-22 18:57:50 -0700 | [diff] [blame] | 569 | } |
| 570 | |
jeffhao | d752132 | 2012-11-21 15:38:24 -0800 | [diff] [blame] | 571 | byte* GetStackEnd() const { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 572 | return tlsPtr_.stack_end; |
jeffhao | d752132 | 2012-11-21 15:38:24 -0800 | [diff] [blame] | 573 | } |
| 574 | |
Ian Rogers | 932746a | 2011-09-22 18:57:50 -0700 | [diff] [blame] | 575 | // Set the stack end to that to be used during a stack overflow |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 576 | void SetStackEndForStackOverflow() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 932746a | 2011-09-22 18:57:50 -0700 | [diff] [blame] | 577 | |
| 578 | // Set the stack end to that to be used during regular execution |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 579 | void ResetDefaultStackEnd(bool implicit_overflow_check) { |
Ian Rogers | 932746a | 2011-09-22 18:57:50 -0700 | [diff] [blame] | 580 | // Our stacks grow down, so we want stack_end_ to be near there, but reserving enough room |
| 581 | // to throw a StackOverflowError. |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 582 | if (implicit_overflow_check) { |
| 583 | // For implicit checks we also need to add in the protected region above the |
| 584 | // overflow region. |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 585 | tlsPtr_.stack_end = tlsPtr_.stack_begin + kStackOverflowImplicitCheckSize; |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 586 | } else { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 587 | tlsPtr_.stack_end = tlsPtr_.stack_begin + kStackOverflowReservedBytes; |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 588 | } |
Ian Rogers | 932746a | 2011-09-22 18:57:50 -0700 | [diff] [blame] | 589 | } |
| 590 | |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 591 | // Install the protected region for implicit stack checks. |
| 592 | void InstallImplicitProtection(bool is_main_stack); |
| 593 | |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 594 | bool IsHandlingStackOverflow() const { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 595 | return tlsPtr_.stack_end == tlsPtr_.stack_begin; |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 596 | } |
| 597 | |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 598 | 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 Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 602 | } |
| 603 | |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 604 | 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 Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 608 | } |
| 609 | |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 610 | 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 Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 615 | } |
| 616 | |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 617 | 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 Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 622 | } |
| 623 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 624 | const ManagedStack* GetManagedStack() const { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 625 | return &tlsPtr_.managed_stack; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | // Linked list recording fragments of managed stack. |
| 629 | void PushManagedStackFragment(ManagedStack* fragment) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 630 | tlsPtr_.managed_stack.PushManagedStackFragment(fragment); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 631 | } |
| 632 | void PopManagedStackFragment(const ManagedStack& fragment) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 633 | tlsPtr_.managed_stack.PopManagedStackFragment(fragment); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | ShadowFrame* PushShadowFrame(ShadowFrame* new_top_frame) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 637 | return tlsPtr_.managed_stack.PushShadowFrame(new_top_frame); |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | ShadowFrame* PopShadowFrame() { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 641 | return tlsPtr_.managed_stack.PopShadowFrame(); |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 642 | } |
Logan Chien | f7ad17e | 2012-03-15 03:10:03 +0800 | [diff] [blame] | 643 | |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 644 | 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()); |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 649 | } |
| 650 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 651 | // Number of references allocated in JNI ShadowFrames on this thread. |
| 652 | size_t NumJniShadowFrameReferences() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 653 | return tlsPtr_.managed_stack.NumJniShadowFrameReferences(); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 654 | } |
| 655 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 656 | // Number of references in handle scope on this thread. |
| 657 | size_t NumHandleReferences(); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 658 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 659 | // Number of references allocated in handle scopes & JNI shadow frames on this thread. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 660 | size_t NumStackReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 661 | return NumHandleReferences() + NumJniShadowFrameReferences(); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 662 | }; |
| 663 | |
| 664 | // Is the given obj in this thread's stack indirect reference table? |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 665 | bool HandleScopeContains(jobject obj) const; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 666 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 667 | void HandleScopeVisitRoots(RootCallback* visitor, void* arg, uint32_t thread_id) |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 668 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 669 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 670 | HandleScope* GetTopHandleScope() { |
| 671 | return tlsPtr_.top_handle_scope; |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 672 | } |
| 673 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 674 | 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 Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 684 | } |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 685 | |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 686 | template<size_t pointer_size> |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 687 | static ThreadOffset<pointer_size> TopHandleScopeOffset() { |
| 688 | return ThreadOffsetFromTlsPtr<pointer_size>(OFFSETOF_MEMBER(tls_ptr_sized_values, |
| 689 | top_handle_scope)); |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 690 | } |
| 691 | |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 692 | DebugInvokeReq* GetInvokeReq() const { |
| 693 | return tlsPtr_.debug_invoke_req; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 694 | } |
| 695 | |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 696 | SingleStepControl* GetSingleStepControl() const { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 697 | return tlsPtr_.single_step_control; |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 698 | } |
| 699 | |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 700 | // Returns the fake exception used to activate deoptimization. |
| 701 | static mirror::Throwable* GetDeoptimizationException() { |
| 702 | return reinterpret_cast<mirror::Throwable*>(-1); |
| 703 | } |
| 704 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 705 | void SetDeoptimizationShadowFrame(ShadowFrame* sf); |
| 706 | void SetDeoptimizationReturnValue(const JValue& ret_val); |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 707 | |
| 708 | ShadowFrame* GetAndClearDeoptimizationShadowFrame(JValue* ret_val); |
| 709 | |
Sebastien Hertz | 714f175 | 2014-04-28 15:03:08 +0200 | [diff] [blame] | 710 | bool HasDeoptimizationShadowFrame() const { |
| 711 | return tlsPtr_.deoptimization_shadow_frame != nullptr; |
| 712 | } |
| 713 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 714 | std::deque<instrumentation::InstrumentationStackFrame>* GetInstrumentationStack() { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 715 | return tlsPtr_.instrumentation_stack; |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 716 | } |
| 717 | |
Jeff Hao | 5ce4b17 | 2013-08-16 16:27:18 -0700 | [diff] [blame] | 718 | std::vector<mirror::ArtMethod*>* GetStackTraceSample() const { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 719 | return tlsPtr_.stack_trace_sample; |
Jeff Hao | 5ce4b17 | 2013-08-16 16:27:18 -0700 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | void SetStackTraceSample(std::vector<mirror::ArtMethod*>* sample) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 723 | tlsPtr_.stack_trace_sample = sample; |
Jeff Hao | 5ce4b17 | 2013-08-16 16:27:18 -0700 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | uint64_t GetTraceClockBase() const { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 727 | return tls64_.trace_clock_base; |
Jeff Hao | 5ce4b17 | 2013-08-16 16:27:18 -0700 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | void SetTraceClockBase(uint64_t clock_base) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 731 | tls64_.trace_clock_base = clock_base; |
Jeff Hao | 5ce4b17 | 2013-08-16 16:27:18 -0700 | [diff] [blame] | 732 | } |
| 733 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 734 | BaseMutex* GetHeldMutex(LockLevel level) const { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 735 | return tlsPtr_.held_mutexes[level]; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 736 | } |
| 737 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 738 | void SetHeldMutex(LockLevel level, BaseMutex* mutex) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 739 | tlsPtr_.held_mutexes[level] = mutex; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 740 | } |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 741 | |
Mathieu Chartier | 752a0e6 | 2013-06-27 11:03:27 -0700 | [diff] [blame] | 742 | void RunCheckpointFunction(); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 743 | |
| 744 | bool ReadFlag(ThreadFlag flag) const { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 745 | return (tls32_.state_and_flags.as_struct.flags & flag) != 0; |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 746 | } |
| 747 | |
Jeff Hao | 9cec247 | 2013-05-14 18:17:06 -0700 | [diff] [blame] | 748 | bool TestAllFlags() const { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 749 | return (tls32_.state_and_flags.as_struct.flags != 0); |
Jeff Hao | 9cec247 | 2013-05-14 18:17:06 -0700 | [diff] [blame] | 750 | } |
| 751 | |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 752 | void AtomicSetFlag(ThreadFlag flag); |
| 753 | |
| 754 | void AtomicClearFlag(ThreadFlag flag); |
| 755 | |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 756 | void ResetQuickAllocEntryPointsForThread(); |
| 757 | |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 758 | // 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 Hughes | 5d96a71 | 2012-06-28 12:24:27 -0700 | [diff] [blame] | 764 | |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 765 | // 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 Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 797 | 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 Rogers | 52673ff | 2012-06-27 23:25:34 -0700 | [diff] [blame] | 806 | explicit Thread(bool daemon); |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 807 | ~Thread() LOCKS_EXCLUDED(Locks::mutator_lock_, |
| 808 | Locks::thread_suspend_count_lock_); |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 809 | void Destroy(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 810 | |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 811 | void CreatePeer(const char* name, bool as_daemon, jobject thread_group); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 812 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 813 | 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 Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 818 | // Avoid use, callers should use SetState. Used only by SignalCatcher::HandleSigQuit, ~Thread and |
| 819 | // Dbg::Disconnected. |
Ian Rogers | 474b6da | 2012-09-25 00:20:38 -0700 | [diff] [blame] | 820 | ThreadState SetStateUnsafe(ThreadState new_state) { |
| 821 | ThreadState old_state = GetState(); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 822 | tls32_.state_and_flags.as_struct.state = new_state; |
Ian Rogers | c747cff | 2012-08-31 18:20:08 -0700 | [diff] [blame] | 823 | return old_state; |
| 824 | } |
Ian Rogers | c747cff | 2012-08-31 18:20:08 -0700 | [diff] [blame] | 825 | |
Ian Rogers | 04d7aa9 | 2013-03-16 14:29:17 -0700 | [diff] [blame] | 826 | void VerifyStackImpl() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 827 | |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 828 | void DumpState(std::ostream& os) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 829 | void DumpStack(std::ostream& os) const |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 830 | LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_) |
| 831 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 832 | |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 833 | // Out-of-line conveniences for debugging in gdb. |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 834 | static Thread* CurrentFromGdb(); // Like Thread::Current. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 835 | // Like Thread::Dump(std::cerr). |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 836 | void DumpFromGdb() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 837 | |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 838 | static void* CreateCallback(void* arg); |
| 839 | |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 840 | void HandleUncaughtExceptions(ScopedObjectAccess& soa) |
| 841 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 842 | void RemoveFromThreadGroup(ScopedObjectAccess& soa) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 843 | |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 844 | void Init(ThreadList*, JavaVMExt*) EXCLUSIVE_LOCKS_REQUIRED(Locks::runtime_shutdown_lock_); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 845 | void InitCardTable(); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 846 | void InitCpu(); |
Alexei Zavjalov | 1efa0a9 | 2014-02-04 02:08:31 +0700 | [diff] [blame] | 847 | void CleanupCpu(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 848 | void InitTlsEntryPoints(); |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 849 | void InitTid(); |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 850 | void InitPthreadKeySelf(); |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 851 | void InitStackHwm(); |
| 852 | |
Elliott Hughes | d6a23bd | 2013-07-16 14:19:52 -0700 | [diff] [blame] | 853 | void SetUpAlternateSignalStack(); |
| 854 | void TearDownAlternateSignalStack(); |
| 855 | |
Ian Rogers | 474b6da | 2012-09-25 00:20:38 -0700 | [diff] [blame] | 856 | // 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 Dearman | 59cde53 | 2013-12-04 18:53:49 -0800 | [diff] [blame] | 858 | union PACKED(4) StateAndFlags { |
| 859 | StateAndFlags() {} |
Ian Rogers | df1ce91 | 2012-11-27 17:07:11 -0800 | [diff] [blame] | 860 | struct PACKED(4) { |
Ian Rogers | 30e173f | 2012-09-26 14:35:03 -0700 | [diff] [blame] | 861 | // 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 Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 868 | volatile uint16_t state; |
Ian Rogers | 30e173f | 2012-09-26 14:35:03 -0700 | [diff] [blame] | 869 | } as_struct; |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 870 | volatile int32_t as_int; |
Chris Dearman | 59cde53 | 2013-12-04 18:53:49 -0800 | [diff] [blame] | 871 | |
| 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 Rogers | 474b6da | 2012-09-25 00:20:38 -0700 | [diff] [blame] | 876 | }; |
Ian Rogers | 474b6da | 2012-09-25 00:20:38 -0700 | [diff] [blame] | 877 | |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 878 | static void ThreadExitCallback(void* arg); |
Elliott Hughes | 5d96a71 | 2012-06-28 12:24:27 -0700 | [diff] [blame] | 879 | |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 880 | // Maximum number of checkpoint functions. |
| 881 | static constexpr uint32_t kMaxCheckpoints = 3; |
| 882 | |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 883 | // Has Thread::Startup been called? |
| 884 | static bool is_started_; |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 885 | |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 886 | // TLS key used to retrieve the Thread*. |
| 887 | static pthread_key_t pthread_key_self_; |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 888 | |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 889 | // 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 Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 892 | |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 893 | /***********************************************************************************************/ |
| 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 Hughes | 6a607ad | 2012-07-13 20:40:00 -0700 | [diff] [blame] | 898 | |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 899 | 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 Rogers | 22f454c | 2012-09-08 11:06:29 -0700 | [diff] [blame] | 903 | |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 904 | 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 Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 909 | |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 910 | union StateAndFlags state_and_flags; |
| 911 | COMPILE_ASSERT(sizeof(union StateAndFlags) == sizeof(int32_t), |
| 912 | sizeof_state_and_flags_and_int32_are_different); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 913 | |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 914 | // 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 Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 917 | |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 918 | // 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 Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 921 | |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 922 | // 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 Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 928 | |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 929 | // System thread id. |
| 930 | uint32_t tid; |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 931 | |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 932 | // Is the thread a daemon? |
| 933 | const bool32_t daemon; |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 934 | |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 935 | // 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 Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 963 | top_handle_scope(nullptr), class_loader_override(nullptr), long_jump_context(nullptr), |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 964 | 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 Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1019 | // Top of linked list of handle scopes or nullptr for none. |
| 1020 | HandleScope* top_handle_scope; |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 1021 | |
| 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 | |
| 1048 | // Support for Mutex lock hierarchy bug detection. |
| 1049 | BaseMutex* held_mutexes[kLockLevelCount]; |
| 1050 | |
| 1051 | // If no_thread_suspension_ is > 0, what is causing that assertion. |
| 1052 | const char* last_no_thread_suspension_cause; |
| 1053 | |
| 1054 | // Pending checkpoint function or NULL if non-pending. Installation guarding by |
| 1055 | // Locks::thread_suspend_count_lock_. |
| 1056 | Closure* checkpoint_functions[kMaxCheckpoints]; |
| 1057 | |
| 1058 | // Entrypoint function pointers. |
| 1059 | // TODO: move this to more of a global offset table model to avoid per-thread duplication. |
| 1060 | InterpreterEntryPoints interpreter_entrypoints; |
| 1061 | JniEntryPoints jni_entrypoints; |
| 1062 | PortableEntryPoints portable_entrypoints; |
| 1063 | QuickEntryPoints quick_entrypoints; |
| 1064 | |
| 1065 | // Thread-local allocation pointer. |
| 1066 | byte* thread_local_start; |
| 1067 | byte* thread_local_pos; |
| 1068 | byte* thread_local_end; |
| 1069 | size_t thread_local_objects; |
| 1070 | |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 1071 | // There are RosAlloc::kNumThreadLocalSizeBrackets thread-local size brackets per thread. |
| 1072 | void* rosalloc_runs[gc::allocator::RosAlloc::kNumThreadLocalSizeBrackets]; |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 1073 | |
| 1074 | // Thread-local allocation stack data/routines. |
| 1075 | mirror::Object** thread_local_alloc_stack_top; |
| 1076 | mirror::Object** thread_local_alloc_stack_end; |
| 1077 | } 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 Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 1090 | friend class Dbg; // For SetStateUnsafe. |
Mathieu Chartier | 15d3402 | 2014-02-26 17:16:38 -0800 | [diff] [blame] | 1091 | friend class gc::collector::SemiSpace; // For getting stack traces. |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 1092 | friend class Runtime; // For CreatePeer. |
| 1093 | friend class ScopedThreadStateChange; |
| 1094 | friend class SignalCatcher; // For SetStateUnsafe. |
Mathieu Chartier | 119c6bd | 2014-05-09 14:11:47 -0700 | [diff] [blame] | 1095 | friend class StubTest; // For accessing entrypoints. |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 1096 | friend class ThreadList; // For ~Thread and Destroy. |
| 1097 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1098 | DISALLOW_COPY_AND_ASSIGN(Thread); |
| 1099 | }; |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1100 | |
Elliott Hughes | 330304d | 2011-08-12 14:28:05 -0700 | [diff] [blame] | 1101 | std::ostream& operator<<(std::ostream& os, const Thread& thread); |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 1102 | std::ostream& operator<<(std::ostream& os, const ThreadState& state); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1103 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1104 | } // namespace art |
| 1105 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 1106 | #endif // ART_RUNTIME_THREAD_H_ |