blob: d1829a3b28cfd22128f6cff522f032ba9530e95c [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
David Srbecky28f6cff2018-10-16 15:07:28 +010020#include <atomic>
Elliott Hughes02b48d12011-09-07 17:15:51 -070021#include <bitset>
Ian Rogers306057f2012-11-26 12:45:53 -080022#include <deque>
Elliott Hughesa0957642011-09-02 14:27:33 -070023#include <iosfwd>
Ian Rogersb033c752011-07-20 12:22:35 -070024#include <list>
Ian Rogers700a4022014-05-19 16:49:03 -070025#include <memory>
Elliott Hughes8daa0922011-09-11 13:46:25 -070026#include <string>
Carl Shapirob5573532011-07-12 18:22:59 -070027
David Sehrc431b9d2018-03-02 12:01:51 -080028#include "base/atomic.h"
Vladimir Markoddf4fd32021-11-22 16:31:57 +000029#include "base/bit_field.h"
30#include "base/bit_utils.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070031#include "base/enums.h"
Andreas Gampe7fbc4a52018-11-28 08:26:47 -080032#include "base/locks.h"
Elliott Hughes76160052012-12-12 16:31:20 -080033#include "base/macros.h"
Alex Light184f0752018-07-13 11:18:22 -070034#include "base/safe_map.h"
Andreas Gampea1ffdba2019-01-04 16:08:51 -080035#include "base/value_object.h"
Ian Rogers848871b2013-08-05 10:56:33 -070036#include "entrypoints/jni/jni_entrypoints.h"
Ian Rogers7655f292013-07-29 11:07:13 -070037#include "entrypoints/quick/quick_entrypoints.h"
Alex Lightb7c640d2019-03-20 15:52:13 -070038#include "handle.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070039#include "handle_scope.h"
David Srbecky912f36c2018-09-08 12:22:58 +010040#include "interpreter/interpreter_cache.h"
Wessam Hassaneinb5a10be2020-11-11 16:42:52 -080041#include "javaheapprof/javaheapsampler.h"
Ian Rogers306057f2012-11-26 12:45:53 -080042#include "jvalue.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070043#include "managed_stack.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070044#include "offsets.h"
Andreas Gampe217488a2017-09-18 08:34:42 -070045#include "read_barrier_config.h"
Alex Light55eccdf2019-10-07 13:51:13 +000046#include "reflective_handle_scope.h"
Andreas Gampe5a0430d2019-01-04 14:33:57 -080047#include "runtime_globals.h"
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070048#include "runtime_stats.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049#include "thread_state.h"
Ian Rogersb033c752011-07-20 12:22:35 -070050
Christopher Ferris6cff48f2014-01-26 21:36:13 -080051class BacktraceMap;
52
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070053namespace art {
54
Mathieu Chartier15d34022014-02-26 17:16:38 -080055namespace gc {
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -070056namespace accounting {
Igor Murashkin2ffb7032017-11-08 13:35:21 -080057template<class T> class AtomicStack;
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -070058} // namespace accounting
Mathieu Chartier15d34022014-02-26 17:16:38 -080059namespace collector {
Igor Murashkin2ffb7032017-11-08 13:35:21 -080060class SemiSpace;
Mathieu Chartier15d34022014-02-26 17:16:38 -080061} // namespace collector
62} // namespace gc
63
Andreas Gamped77abd92019-01-02 16:10:20 -080064namespace instrumentation {
65struct InstrumentationStackFrame;
66} // namespace instrumentation
67
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080068namespace mirror {
Igor Murashkin2ffb7032017-11-08 13:35:21 -080069class Array;
70class Class;
71class ClassLoader;
72class Object;
73template<class T> class ObjectArray;
74template<class T> class PrimitiveArray;
Vladimir Marko4f990712021-07-14 12:45:13 +010075using IntArray = PrimitiveArray<int32_t>;
Igor Murashkin2ffb7032017-11-08 13:35:21 -080076class StackTraceElement;
77class String;
78class Throwable;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080079} // namespace mirror
Mathieu Chartier12d625f2015-03-13 11:33:37 -070080
81namespace verifier {
Igor Murashkin2ffb7032017-11-08 13:35:21 -080082class MethodVerifier;
83class VerifierDeps;
Mathieu Chartier12d625f2015-03-13 11:33:37 -070084} // namespace verifier
85
Mathieu Chartiere401d142015-04-22 13:56:20 -070086class ArtMethod;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080087class BaseMutex;
88class ClassLinker;
Ian Rogers7a22fa62013-01-23 12:16:16 -080089class Closure;
Ian Rogersbdb03912011-09-14 00:55:44 -070090class Context;
Sebastien Hertz07474662015-08-25 15:12:33 +000091class DeoptimizationContextRecord;
Ian Rogers81d425b2012-09-27 16:03:43 -070092class DexFile;
Mingyao Yang99170c62015-07-06 11:10:37 -070093class FrameIdToShadowFrame;
Nicolas Geoffraye3f775b2019-12-04 14:41:52 +000094class IsMarkedVisitor;
Ian Rogersb48b9eb2014-02-28 16:20:21 -080095class JavaVMExt;
Ian Rogers55256cb2017-12-21 17:07:11 -080096class JNIEnvExt;
Elliott Hughes8daa0922011-09-11 13:46:25 -070097class Monitor;
Andreas Gampe513061a2017-06-01 09:17:34 -070098class RootVisitor;
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -070099class ScopedObjectAccessAlreadyRunnable;
Logan Chienf7ad17e2012-03-15 03:10:03 +0800100class ShadowFrame;
Sebastien Hertzf7958692015-06-09 14:09:14 +0200101class StackedShadowFrameRecord;
Andreas Gampe0c2313c2019-05-14 09:47:00 -0700102enum class SuspendReason : char;
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700103class Thread;
104class ThreadList;
Andreas Gampe513061a2017-06-01 09:17:34 -0700105enum VisitRootFlags : uint8_t;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700106
Alex Light184f0752018-07-13 11:18:22 -0700107// A piece of data that can be held in the CustomTls. The destructor will be called during thread
108// shutdown. The thread the destructor is called on is not necessarily the same thread it was stored
109// on.
110class TLSData {
111 public:
112 virtual ~TLSData() {}
113};
114
Elliott Hughes34e06962012-04-09 13:55:55 -0700115// Thread priorities. These must match the Thread.MIN_PRIORITY,
116// Thread.NORM_PRIORITY, and Thread.MAX_PRIORITY constants.
117enum ThreadPriority {
118 kMinThreadPriority = 1,
119 kNormThreadPriority = 5,
120 kMaxThreadPriority = 10,
121};
122
Vladimir Markoddf4fd32021-11-22 16:31:57 +0000123enum class ThreadFlag : uint32_t {
124 // If set, implies that suspend_count_ > 0 and the Thread should enter the safepoint handler.
125 kSuspendRequest = 1u << 0,
126
127 // Request that the thread do some checkpoint work and then continue.
128 kCheckpointRequest = 1u << 1,
129
130 // Request that the thread do empty checkpoint and then continue.
131 kEmptyCheckpointRequest = 1u << 2,
132
133 // Register that at least 1 suspend barrier needs to be passed.
134 kActiveSuspendBarrier = 1u << 3,
135
Vladimir Marko9c0f7642021-12-06 16:17:52 +0000136 // Marks that a "flip function" needs to be executed on this thread.
137 kPendingFlipFunction = 1u << 4,
138
139 // Marks that the "flip function" is being executed by another thread.
140 //
141 // This is used to guards against multiple threads trying to run the
142 // "flip function" for the same thread while the thread is suspended.
143 //
144 // This is not needed when the thread is running the flip function
145 // on its own after transitioning to Runnable.
146 kRunningFlipFunction = 1u << 5,
147
148 // Marks that a thread is wating for "flip function" to complete.
149 //
150 // This is used to check if we need to broadcast the completion of the
151 // "flip function" to other threads. See also `kRunningFlipFunction`.
152 kWaitingForFlipFunction = 1u << 6,
153
Vladimir Markoce2a3442021-11-24 15:10:26 +0000154 // Request that compiled JNI stubs do not transition to Native or Runnable with
155 // inlined code, but take a slow path for monitoring method entry and exit events.
Vladimir Marko9c0f7642021-12-06 16:17:52 +0000156 kMonitorJniEntryExit = 1u << 7,
Vladimir Markoce2a3442021-11-24 15:10:26 +0000157
Vladimir Markoddf4fd32021-11-22 16:31:57 +0000158 // Indicates the last flag. Used for checking that the flags do not overlap thread state.
Vladimir Markoce2a3442021-11-24 15:10:26 +0000159 kLastFlag = kMonitorJniEntryExit
Ian Rogers474b6da2012-09-25 00:20:38 -0700160};
161
Sebastien Hertzf7958692015-06-09 14:09:14 +0200162enum class StackedShadowFrameType {
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700163 kShadowFrameUnderConstruction,
Andreas Gampe639bdd12015-06-03 11:22:45 -0700164 kDeoptimizationShadowFrame,
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700165};
166
Mingyao Yang2ee17902017-08-30 11:37:08 -0700167// The type of method that triggers deoptimization. It contains info on whether
168// the deoptimized method should advance dex_pc.
169enum class DeoptimizationMethodType {
170 kKeepDexPc, // dex pc is required to be kept upon deoptimization.
171 kDefault // dex pc may or may not advance depending on other conditions.
172};
173
Hans Boehm1b3ec0f2022-01-26 16:53:07 +0000174// For the CC colector, normal weak reference access can be disabled on a per-thread basis, while
175// processing references. After finishing, the reference processor asynchronously sets the
176// per-thread flags back to kEnabled with release memory ordering semantics. Each mutator thread
177// should check its flag with acquire semantics before assuming that it is enabled. However,
178// that is often too expensive, so the reading thread sets it to kVisiblyEnabled after seeing it
179// kEnabled. The Reference.get() intrinsic can thus read it in relaxed mode, and reread (by
180// resorting to the slow path) with acquire semantics if it sees a value of kEnabled rather than
181// kVisiblyEnabled.
182enum class WeakRefAccessState : int32_t {
183 kVisiblyEnabled = 0, // Enabled, and previously read with acquire load by this thread.
184 kEnabled,
185 kDisabled
186};
187
Hiroshi Yamauchi7ed9c562016-02-02 15:22:09 -0800188// This should match RosAlloc::kNumThreadLocalSizeBrackets.
189static constexpr size_t kNumRosAllocThreadLocalSizeBracketsInThread = 16;
Ian Rogerse63db272014-07-15 15:36:11 -0700190
Dave Allison648d7112014-07-25 16:15:27 -0700191// Thread's stack layout for implicit stack overflow checks:
192//
193// +---------------------+ <- highest address of stack memory
194// | |
195// . . <- SP
196// | |
197// | |
198// +---------------------+ <- stack_end
199// | |
200// | Gap |
201// | |
202// +---------------------+ <- stack_begin
203// | |
204// | Protected region |
205// | |
206// +---------------------+ <- lowest address of stack memory
207//
208// The stack always grows down in memory. At the lowest address is a region of memory
209// that is set mprotect(PROT_NONE). Any attempt to read/write to this region will
210// result in a segmentation fault signal. At any point, the thread's SP will be somewhere
211// between the stack_end and the highest address in stack memory. An implicit stack
212// overflow check is a read of memory at a certain offset below the current SP (4K typically).
213// If the thread's SP is below the stack_end address this will be a read into the protected
214// region. If the SP is above the stack_end address, the thread is guaranteed to have
215// at least 4K of space. Because stack overflow checks are only performed in generated code,
216// if the thread makes a call out to a native function (through JNI), that native function
217// might only have 4K of memory (if the SP is adjacent to stack_end).
218
Ian Rogersdd7624d2014-03-14 17:43:00 -0700219class Thread {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700220 public:
Andreas Gampe7ea6f792014-07-14 16:21:44 -0700221 static const size_t kStackOverflowImplicitCheckSize;
Andreas Gampeb486a982017-06-01 13:45:54 -0700222 static constexpr bool kVerifyStack = kIsDebugBuild;
Dave Allisonf9439142014-03-27 15:10:22 -0700223
Elliott Hughes462c9442012-03-23 18:47:50 -0700224 // Creates a new native thread corresponding to the given managed peer.
225 // Used to implement Thread.start.
Ian Rogers52673ff2012-06-27 23:25:34 -0700226 static void CreateNativeThread(JNIEnv* env, jobject peer, size_t stack_size, bool daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700227
Elliott Hughes462c9442012-03-23 18:47:50 -0700228 // Attaches the calling native thread to the runtime, returning the new native peer.
229 // Used to implement JNI AttachCurrentThread and AttachCurrentThreadAsDaemon calls.
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800230 static Thread* Attach(const char* thread_name, bool as_daemon, jobject thread_group,
231 bool create_peer);
Andreas Gampe732b0ac2017-01-18 15:23:39 -0800232 // Attaches the calling native thread to the runtime, returning the new native peer.
233 static Thread* Attach(const char* thread_name, bool as_daemon, jobject thread_peer);
Carl Shapirob5573532011-07-12 18:22:59 -0700234
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700235 // Reset internal state of child thread after fork.
236 void InitAfterFork();
237
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700238 // Get the currently executing thread, frequently referred to as 'self'. This call has reasonably
239 // high cost and so we favor passing self around when possible.
240 // TODO: mark as PURE so the compiler may coalesce and remove?
Ian Rogers02ed4c02013-09-06 13:10:04 -0700241 static Thread* Current();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700242
Ian Rogers7b078e82014-09-10 14:44:24 -0700243 // On a runnable thread, check for pending thread suspension request and handle if pending.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700244 void AllowThreadSuspension() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700245
246 // Process pending thread suspension request and handle if pending.
Vladimir Markoe45883e2022-01-11 12:38:35 +0000247 void CheckSuspend(bool implicit = false) REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700248
Hiroshi Yamauchi30493242016-11-03 13:06:52 -0700249 // Process a pending empty checkpoint if pending.
Hiroshi Yamauchia2224042017-02-08 16:35:45 -0800250 void CheckEmptyCheckpointFromWeakRefAccess(BaseMutex* cond_var_mutex);
251 void CheckEmptyCheckpointFromMutex();
Hiroshi Yamauchi30493242016-11-03 13:06:52 -0700252
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700253 static Thread* FromManagedThread(const ScopedObjectAccessAlreadyRunnable& ts,
Mathieu Chartierf5769e12017-01-10 15:54:41 -0800254 ObjPtr<mirror::Object> thread_peer)
Mathieu Chartier90443472015-07-16 20:32:27 -0700255 REQUIRES(Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700256 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700257 static Thread* FromManagedThread(const ScopedObjectAccessAlreadyRunnable& ts, jobject thread)
Mathieu Chartier90443472015-07-16 20:32:27 -0700258 REQUIRES(Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700259 REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700260
Elliott Hughes28fa76d2012-04-09 17:31:46 -0700261 // Translates 172 to pAllocArrayFromCode and so on.
Andreas Gampe542451c2016-07-26 09:02:02 -0700262 template<PointerSize size_of_pointers>
Ian Rogersdd7624d2014-03-14 17:43:00 -0700263 static void DumpThreadOffset(std::ostream& os, uint32_t offset);
Elliott Hughes28fa76d2012-04-09 17:31:46 -0700264
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700265 // Dumps a one-line summary of thread state (used for operator<<).
266 void ShortDump(std::ostream& os) const;
267
268 // Dumps the detailed thread state and the thread stack (used for SIGQUIT).
Nicolas Geoffraya73280d2016-02-15 13:05:16 +0000269 void Dump(std::ostream& os,
Nicolas Geoffray6ee49712018-03-30 14:39:05 +0000270 bool dump_native_stack = true,
Hiroshi Yamauchi13c16352017-01-31 10:15:08 -0800271 BacktraceMap* backtrace_map = nullptr,
272 bool force_dump_stack = false) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700273 REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughesa0957642011-09-02 14:27:33 -0700274
Hiroshi Yamauchi02f365f2017-02-03 15:06:00 -0800275 void DumpJavaStack(std::ostream& os,
276 bool check_suspended = true,
277 bool dump_locks = true) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700278 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700279
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700280 // Dumps the SIGQUIT per-thread header. 'thread' can be null for a non-attached thread, in which
Elliott Hughesabbe07d2012-06-05 17:42:23 -0700281 // 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 -0700282 static void DumpState(std::ostream& os, const Thread* thread, pid_t tid)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700283 REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughesabbe07d2012-06-05 17:42:23 -0700284
Ian Rogers474b6da2012-09-25 00:20:38 -0700285 ThreadState GetState() const {
Vladimir Marko9c0f7642021-12-06 16:17:52 +0000286 return GetStateAndFlags(std::memory_order_relaxed).GetState();
Dave Allison0aded082013-11-07 13:15:11 -0800287 }
288
Ian Rogers474b6da2012-09-25 00:20:38 -0700289 ThreadState SetState(ThreadState new_state);
Ian Rogers52673ff2012-06-27 23:25:34 -0700290
Mathieu Chartier90443472015-07-16 20:32:27 -0700291 int GetSuspendCount() const REQUIRES(Locks::thread_suspend_count_lock_) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700292 return tls32_.suspend_count;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700293 }
Elliott Hughes038a8062011-09-18 14:12:41 -0700294
Alex Light88fd7202017-06-30 08:31:59 -0700295 int GetUserCodeSuspendCount() const REQUIRES(Locks::thread_suspend_count_lock_,
296 Locks::user_code_suspension_lock_) {
297 return tls32_.user_code_suspend_count;
298 }
299
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700300 bool IsSuspended() const {
Vladimir Marko9c0f7642021-12-06 16:17:52 +0000301 StateAndFlags state_and_flags = GetStateAndFlags(std::memory_order_relaxed);
Vladimir Markoddf4fd32021-11-22 16:31:57 +0000302 return state_and_flags.GetState() != ThreadState::kRunnable &&
303 state_and_flags.IsFlagSet(ThreadFlag::kSuspendRequest);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700304 }
305
Alex Light270db1c2019-12-03 12:20:01 +0000306 void DecrDefineClassCount() {
307 tls32_.define_class_counter--;
308 }
309
310 void IncrDefineClassCount() {
311 tls32_.define_class_counter++;
312 }
313 uint32_t GetDefineClassCount() const {
314 return tls32_.define_class_counter;
315 }
316
Hiroshi Yamauchi02e7f1a2016-10-03 15:32:01 -0700317 // If delta > 0 and (this != self or suspend_barrier is not null), this function may temporarily
318 // release thread_suspend_count_lock_ internally.
319 ALWAYS_INLINE
320 bool ModifySuspendCount(Thread* self,
321 int delta,
322 AtomicInteger* suspend_barrier,
Alex Light46f93402017-06-29 11:59:50 -0700323 SuspendReason reason)
Sebastien Hertz1c8f4ff2017-04-14 15:05:12 +0200324 WARN_UNUSED
Mathieu Chartier90443472015-07-16 20:32:27 -0700325 REQUIRES(Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700326
Hans Boehm891cb882020-07-31 12:06:58 -0700327 // Requests a checkpoint closure to run on another thread. The closure will be run when the
328 // thread notices the request, either in an explicit runtime CheckSuspend() call, or in a call
329 // originating from a compiler generated suspend point check. This returns true if the closure
330 // was added and will (eventually) be executed. It returns false otherwise.
Alex Light318afe62018-03-22 16:50:10 -0700331 //
Hans Boehm891cb882020-07-31 12:06:58 -0700332 // Since multiple closures can be queued and some closures can delay other threads from running,
333 // no closure should attempt to suspend another thread while running.
Alex Light318afe62018-03-22 16:50:10 -0700334 // TODO We should add some debug option that verifies this.
Hans Boehm891cb882020-07-31 12:06:58 -0700335 //
336 // This guarantees that the RequestCheckpoint invocation happens-before the function invocation:
337 // RequestCheckpointFunction holds thread_suspend_count_lock_, and RunCheckpointFunction
338 // acquires it.
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700339 bool RequestCheckpoint(Closure* function)
Mathieu Chartier90443472015-07-16 20:32:27 -0700340 REQUIRES(Locks::thread_suspend_count_lock_);
Alex Lightb1e31a82017-10-04 16:57:36 -0700341
342 // RequestSynchronousCheckpoint releases the thread_list_lock_ as a part of its execution. This is
343 // due to the fact that Thread::Current() needs to go to sleep to allow the targeted thread to
Alex Light318afe62018-03-22 16:50:10 -0700344 // execute the checkpoint for us if it is Runnable. The suspend_state is the state that the thread
345 // will go into while it is awaiting the checkpoint to be run.
346 // NB Passing ThreadState::kRunnable may cause the current thread to wait in a condition variable
347 // while holding the mutator_lock_. Callers should ensure that this will not cause any problems
348 // for the closure or the rest of the system.
349 // NB Since multiple closures can be queued and some closures can delay other threads from running
350 // no closure should attempt to suspend another thread while running.
351 bool RequestSynchronousCheckpoint(Closure* function,
352 ThreadState suspend_state = ThreadState::kWaiting)
Andreas Gampe28c4a232017-06-21 21:21:31 -0700353 REQUIRES_SHARED(Locks::mutator_lock_)
Alex Lightb1e31a82017-10-04 16:57:36 -0700354 RELEASE(Locks::thread_list_lock_)
Andreas Gampe28c4a232017-06-21 21:21:31 -0700355 REQUIRES(!Locks::thread_suspend_count_lock_);
Alex Light318afe62018-03-22 16:50:10 -0700356
Hiroshi Yamauchi30493242016-11-03 13:06:52 -0700357 bool RequestEmptyCheckpoint()
358 REQUIRES(Locks::thread_suspend_count_lock_);
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700359
Vladimir Marko9c0f7642021-12-06 16:17:52 +0000360 // Set the flip function. This is done with all threads suspended, except for the calling thread.
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800361 void SetFlipFunction(Closure* function);
Vladimir Marko9c0f7642021-12-06 16:17:52 +0000362
363 // Ensure that thread flip function started running. If no other thread is executing
364 // it, the calling thread shall run the flip function and then notify other threads
365 // that have tried to do that concurrently. After this function returns, the
366 // `ThreadFlag::kPendingFlipFunction` is cleared but another thread may still
367 // run the flip function as indicated by the `ThreadFlag::kRunningFlipFunction`.
368 void EnsureFlipFunctionStarted(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
369
370 // Wait for the flip function to complete if still running on another thread.
371 void WaitForFlipFunction(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800372
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700373 gc::accounting::AtomicStack<mirror::Object>* GetThreadLocalMarkStack() {
374 CHECK(kUseReadBarrier);
375 return tlsPtr_.thread_local_mark_stack;
376 }
377 void SetThreadLocalMarkStack(gc::accounting::AtomicStack<mirror::Object>* stack) {
378 CHECK(kUseReadBarrier);
379 tlsPtr_.thread_local_mark_stack = stack;
380 }
381
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700382 // Called when thread detected that the thread_suspend_count_ was non-zero. Gives up share of
383 // mutator_lock_ and waits until it is resumed and thread_suspend_count_ is zero.
Vladimir Markoe45883e2022-01-11 12:38:35 +0000384 void FullSuspendCheck(bool implicit = false)
Mathieu Chartier90443472015-07-16 20:32:27 -0700385 REQUIRES(!Locks::thread_suspend_count_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700386 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700387
388 // Transition from non-runnable to runnable state acquiring share on mutator_lock_.
Mathieu Chartier8ac9c912015-10-01 15:58:41 -0700389 ALWAYS_INLINE ThreadState TransitionFromSuspendedToRunnable()
Mathieu Chartier90443472015-07-16 20:32:27 -0700390 REQUIRES(!Locks::thread_suspend_count_lock_)
Mathieu Chartier8ac9c912015-10-01 15:58:41 -0700391 SHARED_LOCK_FUNCTION(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700392
393 // Transition from runnable into a state where mutator privileges are denied. Releases share of
394 // mutator lock.
Mathieu Chartier8ac9c912015-10-01 15:58:41 -0700395 ALWAYS_INLINE void TransitionFromRunnableToSuspended(ThreadState new_state)
Mathieu Chartier4e2cb092015-07-22 16:17:51 -0700396 REQUIRES(!Locks::thread_suspend_count_lock_, !Roles::uninterruptible_)
Mathieu Chartier8ac9c912015-10-01 15:58:41 -0700397 UNLOCK_FUNCTION(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700398
Ian Rogers0399dde2012-06-06 17:09:28 -0700399 // Once called thread suspension will cause an assertion failure.
Mathieu Chartier4e2cb092015-07-22 16:17:51 -0700400 const char* StartAssertNoThreadSuspension(const char* cause) ACQUIRE(Roles::uninterruptible_) {
401 Roles::uninterruptible_.Acquire(); // No-op.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700402 if (kIsDebugBuild) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700403 CHECK(cause != nullptr);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700404 const char* previous_cause = tlsPtr_.last_no_thread_suspension_cause;
405 tls32_.no_thread_suspension++;
406 tlsPtr_.last_no_thread_suspension_cause = cause;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700407 return previous_cause;
408 } else {
409 return nullptr;
410 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700411 }
Ian Rogers52673ff2012-06-27 23:25:34 -0700412
Ian Rogers0399dde2012-06-06 17:09:28 -0700413 // End region where no thread suspension is expected.
Mathieu Chartier4e2cb092015-07-22 16:17:51 -0700414 void EndAssertNoThreadSuspension(const char* old_cause) RELEASE(Roles::uninterruptible_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700415 if (kIsDebugBuild) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700416 CHECK(old_cause != nullptr || tls32_.no_thread_suspension == 1);
417 CHECK_GT(tls32_.no_thread_suspension, 0U);
418 tls32_.no_thread_suspension--;
419 tlsPtr_.last_no_thread_suspension_cause = old_cause;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700420 }
Mathieu Chartier4e2cb092015-07-22 16:17:51 -0700421 Roles::uninterruptible_.Release(); // No-op.
Ian Rogers0399dde2012-06-06 17:09:28 -0700422 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700423
Mathieu Chartierdc540df2019-11-15 17:11:44 -0800424 // End region where no thread suspension is expected. Returns the current open region in case we
425 // want to reopen it. Used for ScopedAllowThreadSuspension. Not supported if no_thread_suspension
426 // is larger than one.
427 const char* EndAssertNoThreadSuspension() RELEASE(Roles::uninterruptible_) WARN_UNUSED {
428 const char* ret = nullptr;
429 if (kIsDebugBuild) {
430 CHECK_EQ(tls32_.no_thread_suspension, 1u);
431 tls32_.no_thread_suspension--;
432 ret = tlsPtr_.last_no_thread_suspension_cause;
433 tlsPtr_.last_no_thread_suspension_cause = nullptr;
434 }
435 Roles::uninterruptible_.Release(); // No-op.
436 return ret;
437 }
438
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700439 void AssertThreadSuspensionIsAllowable(bool check_locks = true) const;
Ian Rogers0399dde2012-06-06 17:09:28 -0700440
Mathieu Chartier10b218d2016-07-25 17:48:52 -0700441 // Return true if thread suspension is allowable.
442 bool IsThreadSuspensionAllowable() const;
443
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700444 bool IsDaemon() const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700445 return tls32_.daemon;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700446 }
447
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700448 size_t NumberOfHeldMutexes() const;
449
Mathieu Chartierf5769e12017-01-10 15:54:41 -0800450 bool HoldsLock(ObjPtr<mirror::Object> object) const REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700451
Elliott Hughes8daa0922011-09-11 13:46:25 -0700452 /*
453 * Changes the priority of this thread to match that of the java.lang.Thread object.
454 *
455 * We map a priority value from 1-10 to Linux "nice" values, where lower
456 * numbers indicate higher priority.
457 */
458 void SetNativePriority(int newPriority);
459
460 /*
Nicolas Geoffrayfa595882019-08-06 17:40:09 +0100461 * Returns the priority of this thread by querying the system.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700462 * This is useful when attaching a thread through JNI.
463 *
464 * Returns a value from 1 to 10 (compatible with java.lang.Thread values).
465 */
Nicolas Geoffrayfa595882019-08-06 17:40:09 +0100466 int GetNativePriority() const;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700467
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700468 // Guaranteed to be non-zero.
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700469 uint32_t GetThreadId() const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700470 return tls32_.thin_lock_thread_id;
Carl Shapirob5573532011-07-12 18:22:59 -0700471 }
472
Elliott Hughesd92bec42011-09-02 17:04:36 -0700473 pid_t GetTid() const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700474 return tls32_.tid;
Elliott Hughesd92bec42011-09-02 17:04:36 -0700475 }
Elliott Hughese27955c2011-08-26 15:21:24 -0700476
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700477 // Returns the java.lang.Thread's name, or null if this Thread* doesn't have a peer.
Vladimir Marko4617d582019-03-28 13:48:31 +0000478 ObjPtr<mirror::String> GetThreadName() const REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughes899e7892012-01-24 14:57:32 -0800479
Elliott Hughesffb465f2012-03-01 18:46:05 -0800480 // Sets 'name' to the java.lang.Thread's name. This requires no transition to managed code,
481 // allocation, or locking.
482 void GetThreadName(std::string& name) const;
483
Elliott Hughes899e7892012-01-24 14:57:32 -0800484 // Sets the thread's name.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700485 void SetThreadName(const char* name) REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughesfc861622011-10-17 17:57:47 -0700486
Jeff Hao57dac6e2013-08-15 16:36:24 -0700487 // Returns the thread-specific CPU-time clock in microseconds or -1 if unavailable.
488 uint64_t GetCpuMicroTime() const;
489
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700490 mirror::Object* GetPeer() const REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffrayffc8cad2017-02-10 10:59:22 +0000491 DCHECK(Thread::Current() == this) << "Use GetPeerFromOtherThread instead";
Ian Rogersdd7624d2014-03-14 17:43:00 -0700492 CHECK(tlsPtr_.jpeer == nullptr);
493 return tlsPtr_.opeer;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700494 }
Andreas Gampe202f85a2017-02-06 10:23:26 -0800495 // GetPeer is not safe if called on another thread in the middle of the CC thread flip and
496 // the thread's stack may have not been flipped yet and peer may be a from-space (stale) ref.
497 // This function will explicitly mark/forward it.
498 mirror::Object* GetPeerFromOtherThread() const REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700499
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700500 bool HasPeer() const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700501 return tlsPtr_.jpeer != nullptr || tlsPtr_.opeer != nullptr;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700502 }
503
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700504 RuntimeStats* GetStats() {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700505 return &tls64_.stats;
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700506 }
507
Elliott Hughes7dc51662012-05-16 14:48:43 -0700508 bool IsStillStarting() const;
509
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700510 bool IsExceptionPending() const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700511 return tlsPtr_.exception != nullptr;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700512 }
513
Alex Light848574c2017-09-25 16:59:39 -0700514 bool IsAsyncExceptionPending() const {
515 return tlsPtr_.async_exception != nullptr;
516 }
517
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700518 mirror::Throwable* GetException() const REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700519 return tlsPtr_.exception;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700520 }
521
Andreas Gamped9efea62014-07-21 22:56:08 -0700522 void AssertPendingException() const;
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700523 void AssertPendingOOMException() const REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700524 void AssertNoPendingException() const;
Mathieu Chartier8d7672e2014-02-25 10:57:16 -0800525 void AssertNoPendingExceptionForNewException(const char* msg) const;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700526
Mathieu Chartier0795f232016-09-27 18:43:30 -0700527 void SetException(ObjPtr<mirror::Throwable> new_exception) REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700528
Alex Light848574c2017-09-25 16:59:39 -0700529 // Set an exception that is asynchronously thrown from a different thread. This will be checked
530 // periodically and might overwrite the current 'Exception'. This can only be called from a
531 // checkpoint.
532 //
533 // The caller should also make sure that the thread has been deoptimized so that the exception
534 // could be detected on back-edges.
535 void SetAsyncException(ObjPtr<mirror::Throwable> new_exception)
536 REQUIRES_SHARED(Locks::mutator_lock_);
537
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700538 void ClearException() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700539 tlsPtr_.exception = nullptr;
jeffhao94d6df42012-11-26 16:02:12 -0800540 }
541
Alex Light848574c2017-09-25 16:59:39 -0700542 // Move the current async-exception to the main exception. This should be called when the current
543 // thread is ready to deal with any async exceptions. Returns true if there is an async exception
544 // that needs to be dealt with, false otherwise.
545 bool ObserveAsyncException() REQUIRES_SHARED(Locks::mutator_lock_);
546
Ian Rogersbdb03912011-09-14 00:55:44 -0700547 // Find catch block and perform long jump to appropriate exception handle
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700548 NO_RETURN void QuickDeliverException() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogersbdb03912011-09-14 00:55:44 -0700549
550 Context* GetLongJumpContext();
Ian Rogers0399dde2012-06-06 17:09:28 -0700551 void ReleaseLongJumpContext(Context* context) {
Mingyao Yang4dcfc432015-04-21 16:55:22 -0700552 if (tlsPtr_.long_jump_context != nullptr) {
Andreas Gampee5d23982019-01-08 10:34:26 -0800553 ReleaseLongJumpContextInternal();
Mingyao Yang4dcfc432015-04-21 16:55:22 -0700554 }
Ian Rogersdd7624d2014-03-14 17:43:00 -0700555 tlsPtr_.long_jump_context = context;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700556 }
557
Andreas Gampe6ec8ebd2014-07-25 13:36:56 -0700558 // Get the current method and dex pc. If there are errors in retrieving the dex pc, this will
559 // abort the runtime iff abort_on_error is true.
Hiroshi Yamauchi02f365f2017-02-03 15:06:00 -0800560 ArtMethod* GetCurrentMethod(uint32_t* dex_pc,
561 bool check_suspended = true,
562 bool abort_on_error = true) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700563 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700564
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000565 // Returns whether the given exception was thrown by the current Java method being executed
566 // (Note that this includes native Java methods).
Mathieu Chartierf5769e12017-01-10 15:54:41 -0800567 bool IsExceptionThrownByCurrentMethod(ObjPtr<mirror::Throwable> exception) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700568 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000569
Mathieu Chartiere401d142015-04-22 13:56:20 -0700570 void SetTopOfStack(ArtMethod** top_method) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700571 tlsPtr_.managed_stack.SetTopQuickFrame(top_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700572 }
573
Vladimir Marko2196c652017-11-30 16:16:07 +0000574 void SetTopOfStackTagged(ArtMethod** top_method) {
575 tlsPtr_.managed_stack.SetTopQuickFrameTagged(top_method);
576 }
577
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800578 void SetTopOfShadowStack(ShadowFrame* top) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700579 tlsPtr_.managed_stack.SetTopShadowFrame(top);
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800580 }
581
Ian Rogers0399dde2012-06-06 17:09:28 -0700582 bool HasManagedStack() const {
Vladimir Marko2196c652017-11-30 16:16:07 +0000583 return tlsPtr_.managed_stack.HasTopQuickFrame() || tlsPtr_.managed_stack.HasTopShadowFrame();
Ian Rogersbdb03912011-09-14 00:55:44 -0700584 }
585
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700586 // If 'msg' is null, no detail message is set.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000587 void ThrowNewException(const char* exception_class_descriptor, const char* msg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700588 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700589
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700590 // If 'msg' is null, no detail message is set. An exception must be pending, and will be
Elliott Hughesa4f94742012-05-29 16:28:38 -0700591 // used as the new exception's cause.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000592 void ThrowNewWrappedException(const char* exception_class_descriptor, const char* msg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700593 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Elliott Hughesa4f94742012-05-29 16:28:38 -0700594
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000595 void ThrowNewExceptionF(const char* exception_class_descriptor, const char* fmt, ...)
596 __attribute__((format(printf, 3, 4)))
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700597 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700598
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000599 void ThrowNewExceptionV(const char* exception_class_descriptor, const char* fmt, va_list ap)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700600 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Elliott Hughes4a2b4172011-09-20 17:08:25 -0700601
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700602 // OutOfMemoryError is special, because we need to pre-allocate an instance.
Elliott Hughes8a8b9cb2012-04-13 18:29:22 -0700603 // Only the GC should call this.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700604 void ThrowOutOfMemoryError(const char* msg) REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartiered8990a2015-07-23 14:11:16 -0700605 REQUIRES(!Roles::uninterruptible_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700606
Elliott Hughesbe759c62011-09-08 19:38:21 -0700607 static void Startup();
Elliott Hughes038a8062011-09-18 14:12:41 -0700608 static void FinishStartup();
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700609 static void Shutdown();
Carl Shapirob5573532011-07-12 18:22:59 -0700610
Andreas Gampe56776012018-01-26 17:40:55 -0800611 // Notify this thread's thread-group that this thread has started.
612 // Note: the given thread-group is used as a fast path and verified in debug build. If the value
613 // is null, the thread's thread-group is loaded from the peer.
614 void NotifyThreadGroup(ScopedObjectAccessAlreadyRunnable& soa, jobject thread_group = nullptr)
615 REQUIRES_SHARED(Locks::mutator_lock_);
616
Ian Rogersb033c752011-07-20 12:22:35 -0700617 // JNI methods
Elliott Hughes69f5bc62011-08-24 09:26:14 -0700618 JNIEnvExt* GetJniEnv() const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700619 return tlsPtr_.jni_env;
Ian Rogersb033c752011-07-20 12:22:35 -0700620 }
621
Ian Rogers408f79a2011-08-23 18:22:33 -0700622 // Convert a jobject into a Object*
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700623 ObjPtr<mirror::Object> DecodeJObject(jobject obj) const REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -0800624 // Checks if the weak global ref has been cleared by the GC without decoding it.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700625 bool IsJWeakCleared(jweak obj) const REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogersb033c752011-07-20 12:22:35 -0700626
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700627 mirror::Object* GetMonitorEnterObject() const REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700628 return tlsPtr_.monitor_enter_object;
629 }
630
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700631 void SetMonitorEnterObject(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700632 tlsPtr_.monitor_enter_object = obj;
633 }
634
Elliott Hughes8daa0922011-09-11 13:46:25 -0700635 // Implements java.lang.Thread.interrupted.
Nicolas Geoffray365719c2017-03-08 13:11:50 +0000636 bool Interrupted();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700637 // Implements java.lang.Thread.isInterrupted.
Nicolas Geoffray365719c2017-03-08 13:11:50 +0000638 bool IsInterrupted();
Andreas Gampe7fbc4a52018-11-28 08:26:47 -0800639 void Interrupt(Thread* self) REQUIRES(!wait_mutex_);
Nicolas Geoffray365719c2017-03-08 13:11:50 +0000640 void SetInterrupted(bool i) {
Orion Hodson88591fe2018-03-06 13:35:43 +0000641 tls32_.interrupted.store(i, std::memory_order_seq_cst);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700642 }
Andreas Gampe7fbc4a52018-11-28 08:26:47 -0800643 void Notify() REQUIRES(!wait_mutex_);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700644
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700645 ALWAYS_INLINE void PoisonObjectPointers() {
646 ++poison_object_cookie_;
647 }
648
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700649 ALWAYS_INLINE static void PoisonObjectPointersIfDebug();
650
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700651 ALWAYS_INLINE uintptr_t GetPoisonObjectCookie() const {
652 return poison_object_cookie_;
653 }
654
Charles Mungeraa31f492018-11-01 18:57:38 +0000655 // Parking for 0ns of relative time means an untimed park, negative (though
656 // should be handled in java code) returns immediately
657 void Park(bool is_absolute, int64_t time) REQUIRES_SHARED(Locks::mutator_lock_);
658 void Unpark();
659
Ian Rogersdd7624d2014-03-14 17:43:00 -0700660 private:
Mathieu Chartier90443472015-07-16 20:32:27 -0700661 void NotifyLocked(Thread* self) REQUIRES(wait_mutex_);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700662
663 public:
664 Mutex* GetWaitMutex() const LOCK_RETURNED(wait_mutex_) {
665 return wait_mutex_;
666 }
667
Mathieu Chartier90443472015-07-16 20:32:27 -0700668 ConditionVariable* GetWaitConditionVariable() const REQUIRES(wait_mutex_) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700669 return wait_cond_;
670 }
671
Mathieu Chartier90443472015-07-16 20:32:27 -0700672 Monitor* GetWaitMonitor() const REQUIRES(wait_mutex_) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700673 return wait_monitor_;
674 }
675
Mathieu Chartier90443472015-07-16 20:32:27 -0700676 void SetWaitMonitor(Monitor* mon) REQUIRES(wait_mutex_) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700677 wait_monitor_ = mon;
678 }
679
Ian Rogersdd7624d2014-03-14 17:43:00 -0700680 // Waiter link-list support.
681 Thread* GetWaitNext() const {
682 return tlsPtr_.wait_next;
683 }
684
685 void SetWaitNext(Thread* next) {
686 tlsPtr_.wait_next = next;
687 }
Elliott Hughes5f791332011-09-15 17:45:30 -0700688
Ian Rogers68d8b422014-07-17 11:09:10 -0700689 jobject GetClassLoaderOverride() {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700690 return tlsPtr_.class_loader_override;
buzbeec143c552011-08-20 17:38:58 -0700691 }
692
Ian Rogers68d8b422014-07-17 11:09:10 -0700693 void SetClassLoaderOverride(jobject class_loader_override);
buzbeec143c552011-08-20 17:38:58 -0700694
Ian Rogersaaa20802011-09-11 21:47:37 -0700695 // Create the internal representation of a stack trace, that is more time
Sebastien Hertzee1d79a2014-02-21 15:46:30 +0100696 // and space efficient to compute than the StackTraceElement[].
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700697 jobject CreateInternalStackTrace(const ScopedObjectAccessAlreadyRunnable& soa) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700698 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogersaaa20802011-09-11 21:47:37 -0700699
Elliott Hughes01158d72011-09-19 19:47:10 -0700700 // Convert an internal stack trace representation (returned by CreateInternalStackTrace) to a
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700701 // StackTraceElement[]. If output_array is null, a new array is created, otherwise as many
702 // frames as will fit are written into the given array. If stack_depth is non-null, it's updated
Elliott Hughes01158d72011-09-19 19:47:10 -0700703 // with the number of valid frames in the returned array.
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700704 static jobjectArray InternalStackTraceToStackTraceElementArray(
705 const ScopedObjectAccessAlreadyRunnable& soa, jobject internal,
706 jobjectArray output_array = nullptr, int* stack_depth = nullptr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700707 REQUIRES_SHARED(Locks::mutator_lock_);
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700708
Andreas Gampefb6b0b12017-12-11 20:47:56 -0800709 jobjectArray CreateAnnotatedStackTrace(const ScopedObjectAccessAlreadyRunnable& soa) const
710 REQUIRES_SHARED(Locks::mutator_lock_);
711
Mingyao Yang99170c62015-07-06 11:10:37 -0700712 bool HasDebuggerShadowFrames() const {
713 return tlsPtr_.frame_id_to_shadow_frame != nullptr;
714 }
715
Andreas Gampe513061a2017-06-01 09:17:34 -0700716 void VisitRoots(RootVisitor* visitor, VisitRootFlags flags)
Andreas Gampe585da952016-12-02 14:52:29 -0800717 REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughes410c0c82011-09-01 17:58:25 -0700718
Alex Light55eccdf2019-10-07 13:51:13 +0000719 void VisitReflectiveTargets(ReflectiveValueVisitor* visitor)
720 REQUIRES(Locks::mutator_lock_);
721
Andreas Gampeb486a982017-06-01 13:45:54 -0700722 void VerifyStack() REQUIRES_SHARED(Locks::mutator_lock_) {
723 if (kVerifyStack) {
724 VerifyStackImpl();
725 }
726 }
jeffhao25045522012-03-13 19:34:37 -0700727
Elliott Hughesbe759c62011-09-08 19:38:21 -0700728 //
729 // Offsets of various members of native Thread class, used by compiled code.
730 //
731
Andreas Gampe542451c2016-07-26 09:02:02 -0700732 template<PointerSize pointer_size>
David Srbecky56de89a2018-10-01 15:32:20 +0100733 static constexpr ThreadOffset<pointer_size> ThinLockIdOffset() {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700734 return ThreadOffset<pointer_size>(
735 OFFSETOF_MEMBER(Thread, tls32_) +
736 OFFSETOF_MEMBER(tls_32bit_sized_values, thin_lock_thread_id));
Elliott Hughesbe759c62011-09-08 19:38:21 -0700737 }
738
Andreas Gampe542451c2016-07-26 09:02:02 -0700739 template<PointerSize pointer_size>
David Srbecky56de89a2018-10-01 15:32:20 +0100740 static constexpr ThreadOffset<pointer_size> InterruptedOffset() {
Nicolas Geoffray365719c2017-03-08 13:11:50 +0000741 return ThreadOffset<pointer_size>(
742 OFFSETOF_MEMBER(Thread, tls32_) +
743 OFFSETOF_MEMBER(tls_32bit_sized_values, interrupted));
744 }
745
746 template<PointerSize pointer_size>
Vladimir Marko01b65522020-10-28 15:43:54 +0000747 static constexpr ThreadOffset<pointer_size> WeakRefAccessEnabledOffset() {
748 return ThreadOffset<pointer_size>(
749 OFFSETOF_MEMBER(Thread, tls32_) +
750 OFFSETOF_MEMBER(tls_32bit_sized_values, weak_ref_access_enabled));
751 }
752
753 template<PointerSize pointer_size>
David Srbecky56de89a2018-10-01 15:32:20 +0100754 static constexpr ThreadOffset<pointer_size> ThreadFlagsOffset() {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700755 return ThreadOffset<pointer_size>(
756 OFFSETOF_MEMBER(Thread, tls32_) +
757 OFFSETOF_MEMBER(tls_32bit_sized_values, state_and_flags));
Elliott Hughesbe759c62011-09-08 19:38:21 -0700758 }
759
Andreas Gampe542451c2016-07-26 09:02:02 -0700760 template<PointerSize pointer_size>
David Srbecky56de89a2018-10-01 15:32:20 +0100761 static constexpr ThreadOffset<pointer_size> IsGcMarkingOffset() {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000762 return ThreadOffset<pointer_size>(
763 OFFSETOF_MEMBER(Thread, tls32_) +
764 OFFSETOF_MEMBER(tls_32bit_sized_values, is_gc_marking));
765 }
766
Igor Murashkinae7ff922016-10-06 14:59:19 -0700767 static constexpr size_t IsGcMarkingSize() {
768 return sizeof(tls32_.is_gc_marking);
769 }
770
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000771 // Deoptimize the Java stack.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700772 void DeoptimizeWithDeoptimizationException(JValue* result) REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000773
Ian Rogersdd7624d2014-03-14 17:43:00 -0700774 private:
Andreas Gampe542451c2016-07-26 09:02:02 -0700775 template<PointerSize pointer_size>
David Srbecky56de89a2018-10-01 15:32:20 +0100776 static constexpr ThreadOffset<pointer_size> ThreadOffsetFromTlsPtr(size_t tls_ptr_offset) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700777 size_t base = OFFSETOF_MEMBER(Thread, tlsPtr_);
David Srbecky56de89a2018-10-01 15:32:20 +0100778 size_t scale = (pointer_size > kRuntimePointerSize) ?
779 static_cast<size_t>(pointer_size) / static_cast<size_t>(kRuntimePointerSize) : 1;
780 size_t shrink = (kRuntimePointerSize > pointer_size) ?
781 static_cast<size_t>(kRuntimePointerSize) / static_cast<size_t>(pointer_size) : 1;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700782 return ThreadOffset<pointer_size>(base + ((tls_ptr_offset * scale) / shrink));
Ian Rogers07ec8e12012-12-01 01:26:51 -0800783 }
784
Ian Rogersdd7624d2014-03-14 17:43:00 -0700785 public:
Nicolas Geoffraya00b54b2019-12-03 14:36:42 +0000786 template<PointerSize pointer_size>
787 static constexpr ThreadOffset<pointer_size> QuickEntryPointOffset(
788 size_t quick_entrypoint_offset) {
789 return ThreadOffsetFromTlsPtr<pointer_size>(
790 OFFSETOF_MEMBER(tls_ptr_sized_values, quick_entrypoints) + quick_entrypoint_offset);
791 }
792
793 static constexpr uint32_t QuickEntryPointOffsetWithSize(size_t quick_entrypoint_offset,
Nicolas Geoffraye3f775b2019-12-04 14:41:52 +0000794 PointerSize pointer_size) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700795 if (pointer_size == PointerSize::k32) {
796 return QuickEntryPointOffset<PointerSize::k32>(quick_entrypoint_offset).
797 Uint32Value();
Jeff Hao848f70a2014-01-15 13:49:50 -0800798 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700799 return QuickEntryPointOffset<PointerSize::k64>(quick_entrypoint_offset).
800 Uint32Value();
Jeff Hao848f70a2014-01-15 13:49:50 -0800801 }
802 }
803
Andreas Gampe542451c2016-07-26 09:02:02 -0700804 template<PointerSize pointer_size>
Ian Rogersdd7624d2014-03-14 17:43:00 -0700805 static ThreadOffset<pointer_size> JniEntryPointOffset(size_t jni_entrypoint_offset) {
806 return ThreadOffsetFromTlsPtr<pointer_size>(
807 OFFSETOF_MEMBER(tls_ptr_sized_values, jni_entrypoints) + jni_entrypoint_offset);
Elliott Hughesbe759c62011-09-08 19:38:21 -0700808 }
809
Roland Levillain97c46462017-05-11 14:04:03 +0100810 // Return the entry point offset integer value for ReadBarrierMarkRegX, where X is `reg`.
811 template <PointerSize pointer_size>
Nicolas Geoffraya00b54b2019-12-03 14:36:42 +0000812 static constexpr int32_t ReadBarrierMarkEntryPointsOffset(size_t reg) {
Roland Levillain97c46462017-05-11 14:04:03 +0100813 // The entry point list defines 30 ReadBarrierMarkRegX entry points.
814 DCHECK_LT(reg, 30u);
815 // The ReadBarrierMarkRegX entry points are ordered by increasing
816 // register number in Thread::tls_Ptr_.quick_entrypoints.
817 return QUICK_ENTRYPOINT_OFFSET(pointer_size, pReadBarrierMarkReg00).Int32Value()
818 + static_cast<size_t>(pointer_size) * reg;
819 }
820
Andreas Gampe542451c2016-07-26 09:02:02 -0700821 template<PointerSize pointer_size>
David Srbecky56de89a2018-10-01 15:32:20 +0100822 static constexpr ThreadOffset<pointer_size> SelfOffset() {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700823 return ThreadOffsetFromTlsPtr<pointer_size>(OFFSETOF_MEMBER(tls_ptr_sized_values, self));
824 }
825
Andreas Gampe542451c2016-07-26 09:02:02 -0700826 template<PointerSize pointer_size>
David Srbecky56de89a2018-10-01 15:32:20 +0100827 static constexpr ThreadOffset<pointer_size> ExceptionOffset() {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700828 return ThreadOffsetFromTlsPtr<pointer_size>(OFFSETOF_MEMBER(tls_ptr_sized_values, exception));
829 }
830
Andreas Gampe542451c2016-07-26 09:02:02 -0700831 template<PointerSize pointer_size>
David Srbecky56de89a2018-10-01 15:32:20 +0100832 static constexpr ThreadOffset<pointer_size> PeerOffset() {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700833 return ThreadOffsetFromTlsPtr<pointer_size>(OFFSETOF_MEMBER(tls_ptr_sized_values, opeer));
834 }
835
836
Andreas Gampe542451c2016-07-26 09:02:02 -0700837 template<PointerSize pointer_size>
David Srbecky56de89a2018-10-01 15:32:20 +0100838 static constexpr ThreadOffset<pointer_size> CardTableOffset() {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700839 return ThreadOffsetFromTlsPtr<pointer_size>(OFFSETOF_MEMBER(tls_ptr_sized_values, card_table));
840 }
841
Andreas Gampe542451c2016-07-26 09:02:02 -0700842 template<PointerSize pointer_size>
David Srbecky56de89a2018-10-01 15:32:20 +0100843 static constexpr ThreadOffset<pointer_size> ThreadSuspendTriggerOffset() {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700844 return ThreadOffsetFromTlsPtr<pointer_size>(
845 OFFSETOF_MEMBER(tls_ptr_sized_values, suspend_trigger));
Dave Allisonb373e092014-02-20 16:06:36 -0800846 }
847
Andreas Gampe542451c2016-07-26 09:02:02 -0700848 template<PointerSize pointer_size>
David Srbecky56de89a2018-10-01 15:32:20 +0100849 static constexpr ThreadOffset<pointer_size> ThreadLocalPosOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -0700850 return ThreadOffsetFromTlsPtr<pointer_size>(OFFSETOF_MEMBER(tls_ptr_sized_values,
851 thread_local_pos));
Hiroshi Yamauchie01a5202015-03-19 12:35:04 -0700852 }
853
Andreas Gampe542451c2016-07-26 09:02:02 -0700854 template<PointerSize pointer_size>
David Srbecky56de89a2018-10-01 15:32:20 +0100855 static constexpr ThreadOffset<pointer_size> ThreadLocalEndOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -0700856 return ThreadOffsetFromTlsPtr<pointer_size>(OFFSETOF_MEMBER(tls_ptr_sized_values,
857 thread_local_end));
Hiroshi Yamauchie01a5202015-03-19 12:35:04 -0700858 }
859
Andreas Gampe542451c2016-07-26 09:02:02 -0700860 template<PointerSize pointer_size>
David Srbecky56de89a2018-10-01 15:32:20 +0100861 static constexpr ThreadOffset<pointer_size> ThreadLocalObjectsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -0700862 return ThreadOffsetFromTlsPtr<pointer_size>(OFFSETOF_MEMBER(tls_ptr_sized_values,
863 thread_local_objects));
Hiroshi Yamauchie01a5202015-03-19 12:35:04 -0700864 }
865
Andreas Gampe542451c2016-07-26 09:02:02 -0700866 template<PointerSize pointer_size>
David Srbecky56de89a2018-10-01 15:32:20 +0100867 static constexpr ThreadOffset<pointer_size> RosAllocRunsOffset() {
Hiroshi Yamauchidc412b62015-10-15 12:26:57 -0700868 return ThreadOffsetFromTlsPtr<pointer_size>(OFFSETOF_MEMBER(tls_ptr_sized_values,
869 rosalloc_runs));
870 }
871
Andreas Gampe542451c2016-07-26 09:02:02 -0700872 template<PointerSize pointer_size>
David Srbecky56de89a2018-10-01 15:32:20 +0100873 static constexpr ThreadOffset<pointer_size> ThreadLocalAllocStackTopOffset() {
Hiroshi Yamauchidc412b62015-10-15 12:26:57 -0700874 return ThreadOffsetFromTlsPtr<pointer_size>(OFFSETOF_MEMBER(tls_ptr_sized_values,
875 thread_local_alloc_stack_top));
876 }
877
Andreas Gampe542451c2016-07-26 09:02:02 -0700878 template<PointerSize pointer_size>
David Srbecky56de89a2018-10-01 15:32:20 +0100879 static constexpr ThreadOffset<pointer_size> ThreadLocalAllocStackEndOffset() {
Hiroshi Yamauchidc412b62015-10-15 12:26:57 -0700880 return ThreadOffsetFromTlsPtr<pointer_size>(OFFSETOF_MEMBER(tls_ptr_sized_values,
881 thread_local_alloc_stack_end));
882 }
883
Ian Rogers932746a2011-09-22 18:57:50 -0700884 // Size of stack less any space reserved for stack overflow
jeffhaod7521322012-11-21 15:38:24 -0800885 size_t GetStackSize() const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700886 return tlsPtr_.stack_size - (tlsPtr_.stack_end - tlsPtr_.stack_begin);
Ian Rogers932746a2011-09-22 18:57:50 -0700887 }
888
Andreas Gampe639b2b12019-01-08 10:32:50 -0800889 ALWAYS_INLINE uint8_t* GetStackEndForInterpreter(bool implicit_overflow_check) const;
Nicolas Geoffray535a3fb2014-07-22 15:17:38 +0100890
Ian Rogers13735952014-10-08 12:43:28 -0700891 uint8_t* GetStackEnd() const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700892 return tlsPtr_.stack_end;
jeffhaod7521322012-11-21 15:38:24 -0800893 }
894
Ian Rogers932746a2011-09-22 18:57:50 -0700895 // Set the stack end to that to be used during a stack overflow
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700896 void SetStackEndForStackOverflow() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers932746a2011-09-22 18:57:50 -0700897
898 // Set the stack end to that to be used during regular execution
Andreas Gampe639b2b12019-01-08 10:32:50 -0800899 ALWAYS_INLINE void ResetDefaultStackEnd();
Ian Rogers932746a2011-09-22 18:57:50 -0700900
Ian Rogers120f1c72012-09-28 17:17:10 -0700901 bool IsHandlingStackOverflow() const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700902 return tlsPtr_.stack_end == tlsPtr_.stack_begin;
Ian Rogers120f1c72012-09-28 17:17:10 -0700903 }
904
Andreas Gampe542451c2016-07-26 09:02:02 -0700905 template<PointerSize pointer_size>
David Srbecky56de89a2018-10-01 15:32:20 +0100906 static constexpr ThreadOffset<pointer_size> StackEndOffset() {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700907 return ThreadOffsetFromTlsPtr<pointer_size>(
908 OFFSETOF_MEMBER(tls_ptr_sized_values, stack_end));
Elliott Hughesbe759c62011-09-08 19:38:21 -0700909 }
910
Andreas Gampe542451c2016-07-26 09:02:02 -0700911 template<PointerSize pointer_size>
David Srbecky56de89a2018-10-01 15:32:20 +0100912 static constexpr ThreadOffset<pointer_size> JniEnvOffset() {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700913 return ThreadOffsetFromTlsPtr<pointer_size>(
914 OFFSETOF_MEMBER(tls_ptr_sized_values, jni_env));
Elliott Hughesbe759c62011-09-08 19:38:21 -0700915 }
916
Andreas Gampe542451c2016-07-26 09:02:02 -0700917 template<PointerSize pointer_size>
David Srbecky56de89a2018-10-01 15:32:20 +0100918 static constexpr ThreadOffset<pointer_size> TopOfManagedStackOffset() {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700919 return ThreadOffsetFromTlsPtr<pointer_size>(
920 OFFSETOF_MEMBER(tls_ptr_sized_values, managed_stack) +
Vladimir Marko2196c652017-11-30 16:16:07 +0000921 ManagedStack::TaggedTopQuickFrameOffset());
Elliott Hughesbe759c62011-09-08 19:38:21 -0700922 }
923
Ian Rogers0399dde2012-06-06 17:09:28 -0700924 const ManagedStack* GetManagedStack() const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700925 return &tlsPtr_.managed_stack;
Ian Rogers0399dde2012-06-06 17:09:28 -0700926 }
927
928 // Linked list recording fragments of managed stack.
929 void PushManagedStackFragment(ManagedStack* fragment) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700930 tlsPtr_.managed_stack.PushManagedStackFragment(fragment);
Ian Rogers0399dde2012-06-06 17:09:28 -0700931 }
932 void PopManagedStackFragment(const ManagedStack& fragment) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700933 tlsPtr_.managed_stack.PopManagedStackFragment(fragment);
Ian Rogers0399dde2012-06-06 17:09:28 -0700934 }
935
Andreas Gampe513061a2017-06-01 09:17:34 -0700936 ALWAYS_INLINE ShadowFrame* PushShadowFrame(ShadowFrame* new_top_frame);
937 ALWAYS_INLINE ShadowFrame* PopShadowFrame();
Logan Chienf7ad17e2012-03-15 03:10:03 +0800938
Andreas Gampe542451c2016-07-26 09:02:02 -0700939 template<PointerSize pointer_size>
David Srbecky56de89a2018-10-01 15:32:20 +0100940 static constexpr ThreadOffset<pointer_size> TopShadowFrameOffset() {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700941 return ThreadOffsetFromTlsPtr<pointer_size>(
942 OFFSETOF_MEMBER(tls_ptr_sized_values, managed_stack) +
943 ManagedStack::TopShadowFrameOffset());
TDYa127d668a062012-04-13 12:36:57 -0700944 }
945
Vladimir Markocedec9d2021-02-08 16:16:13 +0000946 // Is the given obj in one of this thread's JNI transition frames?
947 bool IsJniTransitionReference(jobject obj) const REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700948
Eric Holkf1e1dd12020-08-21 15:38:12 -0700949 void HandleScopeVisitRoots(RootVisitor* visitor, uint32_t thread_id)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700950 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700951
Vladimir Marko1d326f92021-06-01 09:26:55 +0100952 BaseHandleScope* GetTopHandleScope() REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700953 return tlsPtr_.top_handle_scope;
Ian Rogers1f539342012-10-03 21:09:42 -0700954 }
955
Vladimir Marko1d326f92021-06-01 09:26:55 +0100956 void PushHandleScope(BaseHandleScope* handle_scope) REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers59c07062014-10-10 13:03:39 -0700957 DCHECK_EQ(handle_scope->GetLink(), tlsPtr_.top_handle_scope);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700958 tlsPtr_.top_handle_scope = handle_scope;
959 }
960
Vladimir Marko1d326f92021-06-01 09:26:55 +0100961 BaseHandleScope* PopHandleScope() REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700962 BaseHandleScope* handle_scope = tlsPtr_.top_handle_scope;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700963 DCHECK(handle_scope != nullptr);
964 tlsPtr_.top_handle_scope = tlsPtr_.top_handle_scope->GetLink();
965 return handle_scope;
Ian Rogers1f539342012-10-03 21:09:42 -0700966 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700967
Andreas Gampe542451c2016-07-26 09:02:02 -0700968 template<PointerSize pointer_size>
David Srbecky56de89a2018-10-01 15:32:20 +0100969 static constexpr ThreadOffset<pointer_size> TopHandleScopeOffset() {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700970 return ThreadOffsetFromTlsPtr<pointer_size>(OFFSETOF_MEMBER(tls_ptr_sized_values,
971 top_handle_scope));
Elliott Hughesbe759c62011-09-08 19:38:21 -0700972 }
973
Vladimir Markoce2a3442021-11-24 15:10:26 +0000974 template<PointerSize pointer_size>
Vladimir Markoe74e0ce2021-12-08 14:16:21 +0000975 static constexpr ThreadOffset<pointer_size> MutatorLockOffset() {
976 return ThreadOffsetFromTlsPtr<pointer_size>(OFFSETOF_MEMBER(tls_ptr_sized_values,
977 mutator_lock));
978 }
979
980 template<PointerSize pointer_size>
Vladimir Markoce2a3442021-11-24 15:10:26 +0000981 static constexpr ThreadOffset<pointer_size> HeldMutexOffset(LockLevel level) {
982 DCHECK_LT(enum_cast<size_t>(level), arraysize(tlsPtr_.held_mutexes));
983 return ThreadOffsetFromTlsPtr<pointer_size>(OFFSETOF_MEMBER(tls_ptr_sized_values,
984 held_mutexes[level]));
985 }
986
Alex Light55eccdf2019-10-07 13:51:13 +0000987 BaseReflectiveHandleScope* GetTopReflectiveHandleScope() {
988 return tlsPtr_.top_reflective_handle_scope;
989 }
990
991 void PushReflectiveHandleScope(BaseReflectiveHandleScope* scope) {
992 DCHECK_EQ(scope->GetLink(), tlsPtr_.top_reflective_handle_scope);
993 DCHECK_EQ(scope->GetThread(), this);
994 tlsPtr_.top_reflective_handle_scope = scope;
995 }
996
997 BaseReflectiveHandleScope* PopReflectiveHandleScope() {
998 BaseReflectiveHandleScope* handle_scope = tlsPtr_.top_reflective_handle_scope;
999 DCHECK(handle_scope != nullptr);
1000 tlsPtr_.top_reflective_handle_scope = tlsPtr_.top_reflective_handle_scope->GetLink();
1001 return handle_scope;
1002 }
1003
Hiroshi Yamauchi00370822015-08-18 14:47:25 -07001004 bool GetIsGcMarking() const {
1005 CHECK(kUseReadBarrier);
1006 return tls32_.is_gc_marking;
1007 }
1008
Mathieu Chartierfe814e82016-11-09 14:32:49 -08001009 void SetIsGcMarkingAndUpdateEntrypoints(bool is_marking);
Hiroshi Yamauchi00370822015-08-18 14:47:25 -07001010
Hans Boehm1b3ec0f2022-01-26 16:53:07 +00001011 bool GetWeakRefAccessEnabled() const; // Only safe for current thread.
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -07001012
1013 void SetWeakRefAccessEnabled(bool enabled) {
1014 CHECK(kUseReadBarrier);
Hans Boehm1b3ec0f2022-01-26 16:53:07 +00001015 WeakRefAccessState new_state = enabled ?
1016 WeakRefAccessState::kEnabled : WeakRefAccessState::kDisabled;
1017 tls32_.weak_ref_access_enabled.store(new_state, std::memory_order_release);
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -07001018 }
1019
Hiroshi Yamauchi20a0be02016-02-19 15:44:06 -08001020 uint32_t GetDisableThreadFlipCount() const {
1021 CHECK(kUseReadBarrier);
1022 return tls32_.disable_thread_flip_count;
1023 }
1024
1025 void IncrementDisableThreadFlipCount() {
1026 CHECK(kUseReadBarrier);
1027 ++tls32_.disable_thread_flip_count;
1028 }
1029
1030 void DecrementDisableThreadFlipCount() {
1031 CHECK(kUseReadBarrier);
1032 DCHECK_GT(tls32_.disable_thread_flip_count, 0U);
1033 --tls32_.disable_thread_flip_count;
1034 }
1035
Alex Light185a4612018-10-04 15:54:25 -07001036 // Returns true if the thread is a runtime thread (eg from a ThreadPool).
Alex Lighte9f61032018-09-24 16:04:51 -07001037 bool IsRuntimeThread() const {
1038 return is_runtime_thread_;
Calin Juravleccd56952016-12-15 17:57:38 +00001039 }
1040
Alex Lighte9f61032018-09-24 16:04:51 -07001041 void SetIsRuntimeThread(bool is_runtime_thread) {
1042 is_runtime_thread_ = is_runtime_thread;
Calin Juravleccd56952016-12-15 17:57:38 +00001043 }
1044
Orion Hodson01ecfa12019-07-18 12:57:47 +01001045 uint32_t CorePlatformApiCookie() {
1046 return core_platform_api_cookie_;
1047 }
1048
1049 void SetCorePlatformApiCookie(uint32_t cookie) {
1050 core_platform_api_cookie_ = cookie;
1051 }
1052
Alex Lighte9f61032018-09-24 16:04:51 -07001053 // Returns true if the thread is allowed to load java classes.
1054 bool CanLoadClasses() const;
1055
Sebastien Hertzfd3077e2014-04-23 10:32:43 +02001056 // Returns the fake exception used to activate deoptimization.
1057 static mirror::Throwable* GetDeoptimizationException() {
Mathieu Chartier0795f232016-09-27 18:43:30 -07001058 // Note that the mirror::Throwable must be aligned to kObjectAlignment or else it cannot be
1059 // represented by ObjPtr.
1060 return reinterpret_cast<mirror::Throwable*>(0x100);
Sebastien Hertzfd3077e2014-04-23 10:32:43 +02001061 }
1062
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -07001063 // Currently deoptimization invokes verifier which can trigger class loading
1064 // and execute Java code, so there might be nested deoptimizations happening.
1065 // We need to save the ongoing deoptimization shadow frames and return
1066 // values on stacks.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001067 // 'from_code' denotes whether the deoptimization was explicitly made from
1068 // compiled code.
Mingyao Yang2ee17902017-08-30 11:37:08 -07001069 // 'method_type' contains info on whether deoptimization should advance
1070 // dex_pc.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001071 void PushDeoptimizationContext(const JValue& return_value,
1072 bool is_reference,
Mingyao Yang2ee17902017-08-30 11:37:08 -07001073 ObjPtr<mirror::Throwable> exception,
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001074 bool from_code,
Mingyao Yang2ee17902017-08-30 11:37:08 -07001075 DeoptimizationMethodType method_type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001076 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierf5769e12017-01-10 15:54:41 -08001077 void PopDeoptimizationContext(JValue* result,
1078 ObjPtr<mirror::Throwable>* exception,
Mingyao Yang2ee17902017-08-30 11:37:08 -07001079 bool* from_code,
1080 DeoptimizationMethodType* method_type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001081 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz07474662015-08-25 15:12:33 +00001082 void AssertHasDeoptimizationContext()
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001083 REQUIRES_SHARED(Locks::mutator_lock_);
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -07001084 void PushStackedShadowFrame(ShadowFrame* sf, StackedShadowFrameType type);
Andreas Gampe639bdd12015-06-03 11:22:45 -07001085 ShadowFrame* PopStackedShadowFrame(StackedShadowFrameType type, bool must_be_present = true);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001086
Mingyao Yang99170c62015-07-06 11:10:37 -07001087 // For debugger, find the shadow frame that corresponds to a frame id.
1088 // Or return null if there is none.
1089 ShadowFrame* FindDebuggerShadowFrame(size_t frame_id)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001090 REQUIRES_SHARED(Locks::mutator_lock_);
Mingyao Yang99170c62015-07-06 11:10:37 -07001091 // For debugger, find the bool array that keeps track of the updated vreg set
1092 // for a frame id.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001093 bool* GetUpdatedVRegFlags(size_t frame_id) REQUIRES_SHARED(Locks::mutator_lock_);
Mingyao Yang99170c62015-07-06 11:10:37 -07001094 // For debugger, find the shadow frame that corresponds to a frame id. If
1095 // one doesn't exist yet, create one and track it in frame_id_to_shadow_frame.
1096 ShadowFrame* FindOrCreateDebuggerShadowFrame(size_t frame_id,
1097 uint32_t num_vregs,
1098 ArtMethod* method,
1099 uint32_t dex_pc)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001100 REQUIRES_SHARED(Locks::mutator_lock_);
Mingyao Yang99170c62015-07-06 11:10:37 -07001101
1102 // Delete the entry that maps from frame_id to shadow_frame.
1103 void RemoveDebuggerShadowFrameMapping(size_t frame_id)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001104 REQUIRES_SHARED(Locks::mutator_lock_);
Mingyao Yang99170c62015-07-06 11:10:37 -07001105
Nicolas Geoffraye91e7952020-01-23 10:15:56 +00001106 // While getting this map requires shared the mutator lock, manipulating it
1107 // should actually follow these rules:
1108 // (1) The owner of this map (the thread) can change it with its mutator lock.
1109 // (2) Other threads can read this map when the owner is suspended and they
1110 // hold the mutator lock.
1111 // (3) Other threads can change this map when owning the mutator lock exclusively.
1112 //
1113 // The reason why (3) needs the mutator lock exclusively (and not just having
1114 // the owner suspended) is that we don't want other threads to concurrently read the map.
1115 //
1116 // TODO: Add a class abstraction to express these rules.
1117 std::map<uintptr_t, instrumentation::InstrumentationStackFrame>* GetInstrumentationStack()
1118 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001119 return tlsPtr_.instrumentation_stack;
jeffhaoe343b762011-12-05 16:36:44 -08001120 }
1121
Mathieu Chartiere401d142015-04-22 13:56:20 -07001122 std::vector<ArtMethod*>* GetStackTraceSample() const {
Andreas Gampe2c19f5b2016-11-28 08:10:18 -08001123 DCHECK(!IsAotCompiler());
Nicolas Geoffray340dafa2016-11-18 16:03:10 +00001124 return tlsPtr_.deps_or_stack_trace_sample.stack_trace_sample;
Jeff Hao5ce4b172013-08-16 16:27:18 -07001125 }
1126
Mathieu Chartiere401d142015-04-22 13:56:20 -07001127 void SetStackTraceSample(std::vector<ArtMethod*>* sample) {
Andreas Gampe2c19f5b2016-11-28 08:10:18 -08001128 DCHECK(!IsAotCompiler());
Nicolas Geoffray340dafa2016-11-18 16:03:10 +00001129 tlsPtr_.deps_or_stack_trace_sample.stack_trace_sample = sample;
1130 }
1131
1132 verifier::VerifierDeps* GetVerifierDeps() const {
Andreas Gampe2c19f5b2016-11-28 08:10:18 -08001133 DCHECK(IsAotCompiler());
Nicolas Geoffray340dafa2016-11-18 16:03:10 +00001134 return tlsPtr_.deps_or_stack_trace_sample.verifier_deps;
1135 }
1136
1137 // It is the responsability of the caller to make sure the verifier_deps
1138 // entry in the thread is cleared before destruction of the actual VerifierDeps
1139 // object, or the thread.
1140 void SetVerifierDeps(verifier::VerifierDeps* verifier_deps) {
Andreas Gampe2c19f5b2016-11-28 08:10:18 -08001141 DCHECK(IsAotCompiler());
Nicolas Geoffraye424c932016-11-23 12:52:01 +00001142 DCHECK(verifier_deps == nullptr || tlsPtr_.deps_or_stack_trace_sample.verifier_deps == nullptr);
Nicolas Geoffray340dafa2016-11-18 16:03:10 +00001143 tlsPtr_.deps_or_stack_trace_sample.verifier_deps = verifier_deps;
Jeff Hao5ce4b172013-08-16 16:27:18 -07001144 }
1145
1146 uint64_t GetTraceClockBase() const {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001147 return tls64_.trace_clock_base;
Jeff Hao5ce4b172013-08-16 16:27:18 -07001148 }
1149
1150 void SetTraceClockBase(uint64_t clock_base) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001151 tls64_.trace_clock_base = clock_base;
Jeff Hao5ce4b172013-08-16 16:27:18 -07001152 }
1153
Ian Rogers81d425b2012-09-27 16:03:43 -07001154 BaseMutex* GetHeldMutex(LockLevel level) const {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001155 return tlsPtr_.held_mutexes[level];
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001156 }
1157
Ian Rogers81d425b2012-09-27 16:03:43 -07001158 void SetHeldMutex(LockLevel level, BaseMutex* mutex) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001159 tlsPtr_.held_mutexes[level] = mutex;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001160 }
Elliott Hughesffb465f2012-03-01 18:46:05 -08001161
Yu Lieac44242015-06-29 10:50:03 +08001162 void ClearSuspendBarrier(AtomicInteger* target)
Mathieu Chartier90443472015-07-16 20:32:27 -07001163 REQUIRES(Locks::thread_suspend_count_lock_);
Yu Lieac44242015-06-29 10:50:03 +08001164
Mathieu Chartier858f1c52012-10-17 17:45:55 -07001165 bool ReadFlag(ThreadFlag flag) const {
Vladimir Marko9c0f7642021-12-06 16:17:52 +00001166 return GetStateAndFlags(std::memory_order_relaxed).IsFlagSet(flag);
Mathieu Chartier858f1c52012-10-17 17:45:55 -07001167 }
1168
Vladimir Marko9c0f7642021-12-06 16:17:52 +00001169 void AtomicSetFlag(ThreadFlag flag, std::memory_order order = std::memory_order_seq_cst) {
1170 tls32_.state_and_flags.fetch_or(enum_cast<uint32_t>(flag), order);
Ian Rogers8c1b5f72014-07-09 22:02:36 -07001171 }
Mathieu Chartier858f1c52012-10-17 17:45:55 -07001172
Vladimir Marko9c0f7642021-12-06 16:17:52 +00001173 void AtomicClearFlag(ThreadFlag flag, std::memory_order order = std::memory_order_seq_cst) {
1174 tls32_.state_and_flags.fetch_and(~enum_cast<uint32_t>(flag), order);
Ian Rogers8c1b5f72014-07-09 22:02:36 -07001175 }
Mathieu Chartier858f1c52012-10-17 17:45:55 -07001176
Lokesh Gidra7e678d32020-04-28 16:17:49 -07001177 void ResetQuickAllocEntryPointsForThread();
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -07001178
Ian Rogersdd7624d2014-03-14 17:43:00 -07001179 // Returns the remaining space in the TLAB.
Mathieu Chartier6bc77742017-04-18 17:46:23 -07001180 size_t TlabSize() const {
1181 return tlsPtr_.thread_local_end - tlsPtr_.thread_local_pos;
1182 }
1183
Wessam Hassaneinb5a10be2020-11-11 16:42:52 -08001184 // Returns pos offset from start.
1185 size_t GetTlabPosOffset() const {
1186 return tlsPtr_.thread_local_pos - tlsPtr_.thread_local_start;
1187 }
1188
Mathieu Chartier6bc77742017-04-18 17:46:23 -07001189 // Returns the remaining space in the TLAB if we were to expand it to maximum capacity.
1190 size_t TlabRemainingCapacity() const {
1191 return tlsPtr_.thread_local_limit - tlsPtr_.thread_local_pos;
1192 }
1193
1194 // Expand the TLAB by a fixed number of bytes. There must be enough capacity to do so.
1195 void ExpandTlab(size_t bytes) {
1196 tlsPtr_.thread_local_end += bytes;
1197 DCHECK_LE(tlsPtr_.thread_local_end, tlsPtr_.thread_local_limit);
1198 }
1199
Ian Rogersdd7624d2014-03-14 17:43:00 -07001200 // Doesn't check that there is room.
1201 mirror::Object* AllocTlab(size_t bytes);
Mathieu Chartier6bc77742017-04-18 17:46:23 -07001202 void SetTlab(uint8_t* start, uint8_t* end, uint8_t* limit);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001203 bool HasTlab() const;
Mathieu Chartierc4bf6672020-01-13 13:07:16 -08001204 void ResetTlab();
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -08001205 uint8_t* GetTlabStart() {
1206 return tlsPtr_.thread_local_start;
1207 }
1208 uint8_t* GetTlabPos() {
1209 return tlsPtr_.thread_local_pos;
1210 }
Lokesh Gidra4f9d62b2020-01-06 15:06:04 -08001211 uint8_t* GetTlabEnd() {
1212 return tlsPtr_.thread_local_end;
1213 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001214 // Remove the suspend trigger for this thread by making the suspend_trigger_ TLS value
1215 // equal to a valid pointer.
1216 // TODO: does this need to atomic? I don't think so.
1217 void RemoveSuspendTrigger() {
1218 tlsPtr_.suspend_trigger = reinterpret_cast<uintptr_t*>(&tlsPtr_.suspend_trigger);
1219 }
1220
1221 // Trigger a suspend check by making the suspend_trigger_ TLS value an invalid pointer.
1222 // The next time a suspend check is done, it will load from the value at this address
1223 // and trigger a SIGSEGV.
Hans Boehm891cb882020-07-31 12:06:58 -07001224 // Only needed if Runtime::implicit_suspend_checks_ is true and fully implemented. It currently
1225 // is always false. Client code currently just looks at the thread flags directly to determine
1226 // whether we should suspend, so this call is currently unnecessary.
Ian Rogersdd7624d2014-03-14 17:43:00 -07001227 void TriggerSuspend() {
1228 tlsPtr_.suspend_trigger = nullptr;
1229 }
1230
1231
1232 // Push an object onto the allocation stack.
Mathieu Chartiercb535da2015-01-23 13:50:03 -08001233 bool PushOnThreadLocalAllocationStack(mirror::Object* obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001234 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001235
1236 // Set the thread local allocation pointers to the given pointers.
Mathieu Chartiercb535da2015-01-23 13:50:03 -08001237 void SetThreadLocalAllocationStack(StackReference<mirror::Object>* start,
1238 StackReference<mirror::Object>* end);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001239
1240 // Resets the thread local allocation pointers.
1241 void RevokeThreadLocalAllocationStack();
1242
1243 size_t GetThreadLocalBytesAllocated() const {
Mathieu Chartier14cc9be2014-07-11 10:26:37 -07001244 return tlsPtr_.thread_local_end - tlsPtr_.thread_local_start;
Ian Rogersdd7624d2014-03-14 17:43:00 -07001245 }
1246
1247 size_t GetThreadLocalObjectsAllocated() const {
1248 return tlsPtr_.thread_local_objects;
1249 }
1250
Ian Rogersdd7624d2014-03-14 17:43:00 -07001251 void* GetRosAllocRun(size_t index) const {
1252 return tlsPtr_.rosalloc_runs[index];
1253 }
1254
1255 void SetRosAllocRun(size_t index, void* run) {
1256 tlsPtr_.rosalloc_runs[index] = run;
1257 }
1258
Andreas Gampe2c2d2a02016-03-17 21:27:19 -07001259 bool ProtectStack(bool fatal_on_error = true);
Dave Allison648d7112014-07-25 16:15:27 -07001260 bool UnprotectStack();
1261
Hiroshi Yamauchiee235822016-08-19 17:03:27 -07001262 bool IsTransitioningToRunnable() const {
1263 return tls32_.is_transitioning_to_runnable;
1264 }
1265
1266 void SetIsTransitioningToRunnable(bool value) {
1267 tls32_.is_transitioning_to_runnable = value;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -08001268 }
1269
Alex Light3dacdd62019-03-12 15:45:47 +00001270 uint32_t DecrementForceInterpreterCount() REQUIRES(Locks::thread_list_lock_) {
1271 return --tls32_.force_interpreter_count;
1272 }
1273
1274 uint32_t IncrementForceInterpreterCount() REQUIRES(Locks::thread_list_lock_) {
1275 return ++tls32_.force_interpreter_count;
1276 }
1277
1278 void SetForceInterpreterCount(uint32_t value) REQUIRES(Locks::thread_list_lock_) {
1279 tls32_.force_interpreter_count = value;
1280 }
1281
1282 uint32_t ForceInterpreterCount() const {
1283 return tls32_.force_interpreter_count;
1284 }
1285
1286 bool IsForceInterpreter() const {
1287 return tls32_.force_interpreter_count != 0;
1288 }
1289
Vladimir Markobf121912019-06-04 13:49:05 +01001290 bool IncrementMakeVisiblyInitializedCounter() {
1291 tls32_.make_visibly_initialized_counter += 1u;
1292 return tls32_.make_visibly_initialized_counter == kMakeVisiblyInitializedCounterTriggerCount;
1293 }
1294
1295 void ClearMakeVisiblyInitializedCounter() {
1296 tls32_.make_visibly_initialized_counter = 0u;
1297 }
1298
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -07001299 void PushVerifier(verifier::MethodVerifier* verifier);
1300 void PopVerifier(verifier::MethodVerifier* verifier);
Mathieu Chartier12d625f2015-03-13 11:33:37 -07001301
Jeff Hao848f70a2014-01-15 13:49:50 -08001302 void InitStringEntryPoints();
1303
Mathieu Chartierdfe02f62016-02-01 20:15:11 -08001304 void ModifyDebugDisallowReadBarrier(int8_t delta) {
Vladimir Marko468de592022-02-04 16:47:46 +00001305 if (kCheckDebugDisallowReadBarrierCount) {
1306 debug_disallow_read_barrier_ += delta;
1307 }
Mathieu Chartierdfe02f62016-02-01 20:15:11 -08001308 }
1309
1310 uint8_t GetDebugDisallowReadBarrierCount() const {
Vladimir Marko468de592022-02-04 16:47:46 +00001311 return kCheckDebugDisallowReadBarrierCount ? debug_disallow_read_barrier_ : 0u;
Mathieu Chartierdfe02f62016-02-01 20:15:11 -08001312 }
1313
Alex Light184f0752018-07-13 11:18:22 -07001314 // Gets the current TLSData associated with the key or nullptr if there isn't any. Note that users
1315 // do not gain ownership of TLSData and must synchronize with SetCustomTls themselves to prevent
1316 // it from being deleted.
1317 TLSData* GetCustomTLS(const char* key) REQUIRES(!Locks::custom_tls_lock_);
Andreas Gampef26bf2d2017-01-13 16:47:14 -08001318
Alex Light184f0752018-07-13 11:18:22 -07001319 // Sets the tls entry at 'key' to data. The thread takes ownership of the TLSData. The destructor
1320 // will be run when the thread exits or when SetCustomTLS is called again with the same key.
1321 void SetCustomTLS(const char* key, TLSData* data) REQUIRES(!Locks::custom_tls_lock_);
Andreas Gampef26bf2d2017-01-13 16:47:14 -08001322
Calin Juravle97cbc922016-04-15 16:16:35 +01001323 // Returns true if the current thread is the jit sensitive thread.
1324 bool IsJitSensitiveThread() const {
1325 return this == jit_sensitive_thread_;
1326 }
1327
Alex Lighte0b2ce42019-02-21 19:23:42 +00001328 bool IsSystemDaemon() const REQUIRES_SHARED(Locks::mutator_lock_);
1329
Calin Juravle97cbc922016-04-15 16:16:35 +01001330 // Returns true if StrictMode events are traced for the current thread.
Calin Juravleb2771b42016-04-07 17:09:25 +01001331 static bool IsSensitiveThread() {
1332 if (is_sensitive_thread_hook_ != nullptr) {
1333 return (*is_sensitive_thread_hook_)();
1334 }
1335 return false;
1336 }
1337
Mathieu Chartier3768ade2017-05-02 14:04:39 -07001338 // Set to the read barrier marking entrypoints to be non-null.
1339 void SetReadBarrierEntrypoints();
1340
Andreas Gampebad529d2017-02-13 18:52:10 -08001341 static jobject CreateCompileTimePeer(JNIEnv* env,
1342 const char* name,
1343 bool as_daemon,
1344 jobject thread_group)
1345 REQUIRES_SHARED(Locks::mutator_lock_);
1346
David Srbecky912f36c2018-09-08 12:22:58 +01001347 ALWAYS_INLINE InterpreterCache* GetInterpreterCache() {
1348 return &interpreter_cache_;
1349 }
1350
1351 // Clear all thread-local interpreter caches.
1352 //
1353 // Since the caches are keyed by memory pointer to dex instructions, this must be
1354 // called when any dex code is unloaded (before different code gets loaded at the
1355 // same memory location).
1356 //
1357 // If presence of cache entry implies some pre-conditions, this must also be
1358 // called if the pre-conditions might no longer hold true.
1359 static void ClearAllInterpreterCaches();
1360
1361 template<PointerSize pointer_size>
David Srbecky56de89a2018-10-01 15:32:20 +01001362 static constexpr ThreadOffset<pointer_size> InterpreterCacheOffset() {
David Srbecky912f36c2018-09-08 12:22:58 +01001363 return ThreadOffset<pointer_size>(OFFSETOF_MEMBER(Thread, interpreter_cache_));
1364 }
1365
Hans Boehm3d2f1482022-01-17 01:32:55 +00001366 static constexpr int InterpreterCacheSizeLog2() {
1367 return WhichPowerOf2(InterpreterCache::kSize);
1368 }
1369
Vladimir Marko254a8582021-11-29 14:08:37 +00001370 static constexpr uint32_t AllThreadFlags() {
1371 return enum_cast<uint32_t>(ThreadFlag::kLastFlag) |
1372 (enum_cast<uint32_t>(ThreadFlag::kLastFlag) - 1u);
1373 }
1374
1375 static constexpr uint32_t SuspendOrCheckpointRequestFlags() {
1376 return enum_cast<uint32_t>(ThreadFlag::kSuspendRequest) |
1377 enum_cast<uint32_t>(ThreadFlag::kCheckpointRequest) |
1378 enum_cast<uint32_t>(ThreadFlag::kEmptyCheckpointRequest);
1379 }
1380
Vladimir Marko9c0f7642021-12-06 16:17:52 +00001381 static constexpr uint32_t FlipFunctionFlags() {
1382 return enum_cast<uint32_t>(ThreadFlag::kPendingFlipFunction) |
1383 enum_cast<uint32_t>(ThreadFlag::kRunningFlipFunction) |
1384 enum_cast<uint32_t>(ThreadFlag::kWaitingForFlipFunction);
1385 }
1386
Vladimir Markoce2a3442021-11-24 15:10:26 +00001387 static constexpr uint32_t StoredThreadStateValue(ThreadState state) {
1388 return StateAndFlags::EncodeState(state);
1389 }
1390
Ian Rogersdd7624d2014-03-14 17:43:00 -07001391 private:
Ian Rogers52673ff2012-06-27 23:25:34 -07001392 explicit Thread(bool daemon);
Mathieu Chartier90443472015-07-16 20:32:27 -07001393 ~Thread() REQUIRES(!Locks::mutator_lock_, !Locks::thread_suspend_count_lock_);
Elliott Hughesc0f09332012-03-26 13:27:06 -07001394 void Destroy();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001395
Alex Light4847a072019-12-12 16:13:47 -08001396 // Deletes and clears the tlsPtr_.jpeer field. Done in a way so that both it and opeer cannot be
1397 // observed to be set at the same time by instrumentation.
1398 void DeleteJPeer(JNIEnv* env);
1399
Andreas Gampe732b0ac2017-01-18 15:23:39 -08001400 // Attaches the calling native thread to the runtime, returning the new native peer.
1401 // Used to implement JNI AttachCurrentThread and AttachCurrentThreadAsDaemon calls.
1402 template <typename PeerAction>
1403 static Thread* Attach(const char* thread_name,
1404 bool as_daemon,
1405 PeerAction p);
1406
Ian Rogers365c1022012-06-22 15:05:28 -07001407 void CreatePeer(const char* name, bool as_daemon, jobject thread_group);
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001408
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001409 template<bool kTransactionActive>
Andreas Gampebad529d2017-02-13 18:52:10 -08001410 static void InitPeer(ScopedObjectAccessAlreadyRunnable& soa,
1411 ObjPtr<mirror::Object> peer,
1412 jboolean thread_is_daemon,
1413 jobject thread_group,
1414 jobject thread_name,
1415 jint thread_priority)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001416 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001417
Vladimir Markoddf4fd32021-11-22 16:31:57 +00001418 // Avoid use, callers should use SetState.
1419 // Used only by `Thread` destructor and stack trace collection in semi-space GC (currently
1420 // disabled by `kStoreStackTraces = false`).
Vladimir Marko9c0f7642021-12-06 16:17:52 +00001421 // NO_THREAD_SAFETY_ANALYSIS: This function is "Unsafe" and can be called in
1422 // different states, so clang cannot perform the thread safety analysis.
1423 ThreadState SetStateUnsafe(ThreadState new_state) NO_THREAD_SAFETY_ANALYSIS {
1424 StateAndFlags old_state_and_flags = GetStateAndFlags(std::memory_order_relaxed);
Vladimir Markoddf4fd32021-11-22 16:31:57 +00001425 ThreadState old_state = old_state_and_flags.GetState();
1426 if (old_state == new_state) {
1427 // Nothing to do.
1428 } else if (old_state == ThreadState::kRunnable) {
Mathieu Chartier8ac9c912015-10-01 15:58:41 -07001429 // Need to run pending checkpoint and suspend barriers. Run checkpoints in runnable state in
1430 // case they need to use a ScopedObjectAccess. If we are holding the mutator lock and a SOA
1431 // attempts to TransitionFromSuspendedToRunnable, it results in a deadlock.
1432 TransitionToSuspendedAndRunCheckpoints(new_state);
1433 // Since we transitioned to a suspended state, check the pass barrier requests.
1434 PassActiveSuspendBarriers();
1435 } else {
Vladimir Markoddf4fd32021-11-22 16:31:57 +00001436 while (true) {
1437 StateAndFlags new_state_and_flags = old_state_and_flags;
1438 new_state_and_flags.SetState(new_state);
1439 if (LIKELY(tls32_.state_and_flags.CompareAndSetWeakAcquire(
1440 old_state_and_flags.GetValue(),
1441 new_state_and_flags.GetValue()))) {
1442 break;
1443 }
1444 // Reload state and flags.
Vladimir Marko9c0f7642021-12-06 16:17:52 +00001445 old_state_and_flags = GetStateAndFlags(std::memory_order_relaxed);
Vladimir Markoddf4fd32021-11-22 16:31:57 +00001446 DCHECK_EQ(old_state, old_state_and_flags.GetState());
1447 }
Yu Lieac44242015-06-29 10:50:03 +08001448 }
Ian Rogersc747cff2012-08-31 18:20:08 -07001449 return old_state;
1450 }
Ian Rogersc747cff2012-08-31 18:20:08 -07001451
Vladimir Marko23cf32f2021-11-15 13:38:02 +00001452 MutatorMutex* GetMutatorLock() RETURN_CAPABILITY(Locks::mutator_lock_) {
1453 DCHECK_EQ(tlsPtr_.mutator_lock, Locks::mutator_lock_);
1454 return tlsPtr_.mutator_lock;
1455 }
1456
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001457 void VerifyStackImpl() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers04d7aa92013-03-16 14:29:17 -07001458
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001459 void DumpState(std::ostream& os) const REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffraya73280d2016-02-15 13:05:16 +00001460 void DumpStack(std::ostream& os,
Nicolas Geoffray6ee49712018-03-30 14:39:05 +00001461 bool dump_native_stack = true,
Hiroshi Yamauchi13c16352017-01-31 10:15:08 -08001462 BacktraceMap* backtrace_map = nullptr,
1463 bool force_dump_stack = false) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001464 REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughesd92bec42011-09-02 17:04:36 -07001465
Elliott Hughesaccd83d2011-10-17 14:25:58 -07001466 // Out-of-line conveniences for debugging in gdb.
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001467 static Thread* CurrentFromGdb(); // Like Thread::Current.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001468 // Like Thread::Dump(std::cerr).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001469 void DumpFromGdb() const REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughesaccd83d2011-10-17 14:25:58 -07001470
Elliott Hughes93e74e82011-09-13 11:07:03 -07001471 static void* CreateCallback(void* arg);
1472
Andreas Gampe513061a2017-06-01 09:17:34 -07001473 void HandleUncaughtExceptions(ScopedObjectAccessAlreadyRunnable& soa)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001474 REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampe513061a2017-06-01 09:17:34 -07001475 void RemoveFromThreadGroup(ScopedObjectAccessAlreadyRunnable& soa)
1476 REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughesaccd83d2011-10-17 14:25:58 -07001477
Andreas Gampe449357d2015-06-01 22:29:51 -07001478 // Initialize a thread.
1479 //
1480 // The third parameter is not mandatory. If given, the thread will use this JNIEnvExt. In case
1481 // Init succeeds, this means the thread takes ownership of it. If Init fails, it is the caller's
1482 // responsibility to destroy the given JNIEnvExt. If the parameter is null, Init will try to
1483 // create a JNIEnvExt on its own (and potentially fail at that stage, indicated by a return value
1484 // of false).
1485 bool Init(ThreadList*, JavaVMExt*, JNIEnvExt* jni_env_ext = nullptr)
Mathieu Chartier90443472015-07-16 20:32:27 -07001486 REQUIRES(Locks::runtime_shutdown_lock_);
Ian Rogers5d76c432011-10-31 21:42:49 -07001487 void InitCardTable();
Ian Rogersb033c752011-07-20 12:22:35 -07001488 void InitCpu();
Alexei Zavjalov1efa0a92014-02-04 02:08:31 +07001489 void CleanupCpu();
Ian Rogers848871b2013-08-05 10:56:33 -07001490 void InitTlsEntryPoints();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -07001491 void InitTid();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -07001492 void InitPthreadKeySelf();
Ian Rogersf4d4da12014-11-11 16:10:33 -08001493 bool InitStackHwm();
Elliott Hughesbe759c62011-09-08 19:38:21 -07001494
Elliott Hughesd6a23bd2013-07-16 14:19:52 -07001495 void SetUpAlternateSignalStack();
1496 void TearDownAlternateSignalStack();
Vladimir Markoe45883e2022-01-11 12:38:35 +00001497 void MadviseAwayAlternateSignalStack();
Elliott Hughesd6a23bd2013-07-16 14:19:52 -07001498
Mathieu Chartier8ac9c912015-10-01 15:58:41 -07001499 ALWAYS_INLINE void TransitionToSuspendedAndRunCheckpoints(ThreadState new_state)
Vladimir Marko9c0f7642021-12-06 16:17:52 +00001500 REQUIRES(!Locks::thread_suspend_count_lock_, !Roles::uninterruptible_)
1501 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier8ac9c912015-10-01 15:58:41 -07001502
1503 ALWAYS_INLINE void PassActiveSuspendBarriers()
1504 REQUIRES(!Locks::thread_suspend_count_lock_, !Roles::uninterruptible_);
1505
Calin Juravle97cbc922016-04-15 16:16:35 +01001506 // Registers the current thread as the jit sensitive thread. Should be called just once.
1507 static void SetJitSensitiveThread() {
1508 if (jit_sensitive_thread_ == nullptr) {
1509 jit_sensitive_thread_ = Thread::Current();
1510 } else {
1511 LOG(WARNING) << "Attempt to set the sensitive thread twice. Tid:"
1512 << Thread::Current()->GetTid();
1513 }
1514 }
1515
Calin Juravleb2771b42016-04-07 17:09:25 +01001516 static void SetSensitiveThreadHook(bool (*is_sensitive_thread_hook)()) {
1517 is_sensitive_thread_hook_ = is_sensitive_thread_hook;
1518 }
1519
Hiroshi Yamauchi02e7f1a2016-10-03 15:32:01 -07001520 bool ModifySuspendCountInternal(Thread* self,
1521 int delta,
1522 AtomicInteger* suspend_barrier,
Alex Light46f93402017-06-29 11:59:50 -07001523 SuspendReason reason)
Sebastien Hertz1c8f4ff2017-04-14 15:05:12 +02001524 WARN_UNUSED
Hiroshi Yamauchi02e7f1a2016-10-03 15:32:01 -07001525 REQUIRES(Locks::thread_suspend_count_lock_);
1526
Alex Lightdf00a1e2017-11-01 09:29:53 -07001527 // Runs a single checkpoint function. If there are no more pending checkpoint functions it will
1528 // clear the kCheckpointRequest flag. The caller is responsible for calling this in a loop until
1529 // the kCheckpointRequest flag is cleared.
Vladimir Marko9c0f7642021-12-06 16:17:52 +00001530 void RunCheckpointFunction()
1531 REQUIRES(!Locks::thread_suspend_count_lock_)
1532 REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi30493242016-11-03 13:06:52 -07001533 void RunEmptyCheckpoint();
Andreas Gampe0a855762016-10-26 13:43:14 -07001534
1535 bool PassActiveSuspendBarriers(Thread* self)
1536 REQUIRES(!Locks::thread_suspend_count_lock_);
1537
1538 // Install the protected region for implicit stack checks.
1539 void InstallImplicitProtection();
1540
Andreas Gampe585da952016-12-02 14:52:29 -08001541 template <bool kPrecise>
1542 void VisitRoots(RootVisitor* visitor) REQUIRES_SHARED(Locks::mutator_lock_);
1543
David Srbeckybe2b1092022-02-19 23:13:27 +00001544 static void SweepInterpreterCaches(IsMarkedVisitor* visitor)
1545 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffraye3f775b2019-12-04 14:41:52 +00001546
Andreas Gampe2c19f5b2016-11-28 08:10:18 -08001547 static bool IsAotCompiler();
1548
Andreas Gampee5d23982019-01-08 10:34:26 -08001549 void ReleaseLongJumpContextInternal();
1550
Hans Boehm30bc7772022-01-28 15:07:02 -08001551 void SetCachedThreadName(const char* name);
1552
Vladimir Markoddf4fd32021-11-22 16:31:57 +00001553 // Helper class for manipulating the 32 bits of atomically changed state and flags.
1554 class StateAndFlags {
1555 public:
1556 explicit StateAndFlags(uint32_t value) :value_(value) {}
1557
1558 uint32_t GetValue() const {
1559 return value_;
1560 }
1561
1562 void SetValue(uint32_t value) {
1563 value_ = value;
1564 }
1565
Vladimir Marko254a8582021-11-29 14:08:37 +00001566 bool IsAnyOfFlagsSet(uint32_t flags) const {
1567 DCHECK_EQ(flags & ~AllThreadFlags(), 0u);
1568 return (value_ & flags) != 0u;
1569 }
1570
Vladimir Markoddf4fd32021-11-22 16:31:57 +00001571 bool IsFlagSet(ThreadFlag flag) const {
1572 return (value_ & enum_cast<uint32_t>(flag)) != 0u;
1573 }
1574
1575 void SetFlag(ThreadFlag flag) {
1576 value_ |= enum_cast<uint32_t>(flag);
1577 }
1578
Vladimir Marko9c0f7642021-12-06 16:17:52 +00001579 StateAndFlags WithFlag(ThreadFlag flag) const {
1580 StateAndFlags result = *this;
1581 result.SetFlag(flag);
1582 return result;
1583 }
1584
1585 StateAndFlags WithoutFlag(ThreadFlag flag) const {
1586 StateAndFlags result = *this;
1587 result.ClearFlag(flag);
1588 return result;
1589 }
1590
Vladimir Markoddf4fd32021-11-22 16:31:57 +00001591 void ClearFlag(ThreadFlag flag) {
1592 value_ &= ~enum_cast<uint32_t>(flag);
1593 }
1594
1595 ThreadState GetState() const {
1596 ThreadState state = ThreadStateField::Decode(value_);
1597 ValidateThreadState(state);
1598 return state;
1599 }
1600
1601 void SetState(ThreadState state) {
1602 ValidateThreadState(state);
1603 value_ = ThreadStateField::Update(state, value_);
1604 }
Chris Dearman59cde532013-12-04 18:53:49 -08001605
Vladimir Marko9c0f7642021-12-06 16:17:52 +00001606 StateAndFlags WithState(ThreadState state) const {
1607 StateAndFlags result = *this;
1608 result.SetState(state);
1609 return result;
1610 }
1611
Vladimir Markoce2a3442021-11-24 15:10:26 +00001612 static constexpr uint32_t EncodeState(ThreadState state) {
1613 ValidateThreadState(state);
1614 return ThreadStateField::Encode(state);
1615 }
1616
Chris Dearman59cde532013-12-04 18:53:49 -08001617 private:
Vladimir Markoce2a3442021-11-24 15:10:26 +00001618 static constexpr void ValidateThreadState(ThreadState state) {
Vladimir Markoddf4fd32021-11-22 16:31:57 +00001619 if (kIsDebugBuild && state != ThreadState::kRunnable) {
1620 CHECK_GE(state, ThreadState::kTerminated);
1621 CHECK_LE(state, ThreadState::kSuspended);
1622 CHECK_NE(state, ThreadState::kObsoleteRunnable);
1623 }
1624 }
1625
1626 // The value holds thread flags and thread state.
1627 uint32_t value_;
1628
1629 static constexpr size_t kThreadStateBitSize = BitSizeOf<std::underlying_type_t<ThreadState>>();
1630 static constexpr size_t kThreadStatePosition = BitSizeOf<uint32_t>() - kThreadStateBitSize;
1631 using ThreadStateField = BitField<ThreadState, kThreadStatePosition, kThreadStateBitSize>;
1632 static_assert(
1633 WhichPowerOf2(enum_cast<uint32_t>(ThreadFlag::kLastFlag)) < kThreadStatePosition);
Ian Rogers474b6da2012-09-25 00:20:38 -07001634 };
Vladimir Markoddf4fd32021-11-22 16:31:57 +00001635 static_assert(sizeof(StateAndFlags) == sizeof(uint32_t), "Unexpected StateAndFlags size");
Ian Rogers474b6da2012-09-25 00:20:38 -07001636
Vladimir Marko9c0f7642021-12-06 16:17:52 +00001637 StateAndFlags GetStateAndFlags(std::memory_order order) const {
1638 return StateAndFlags(tls32_.state_and_flags.load(order));
1639 }
1640
Hans Boehm9d27fbc2021-05-21 09:23:38 -07001641 // Format state and flags as a hex string. For diagnostic output.
1642 std::string StateAndFlagsAsHexString() const;
1643
Vladimir Marko9c0f7642021-12-06 16:17:52 +00001644 // Run the flip function and, if requested, notify other threads that may have tried
1645 // to do that concurrently.
1646 void RunFlipFunction(Thread* self, bool notify) REQUIRES_SHARED(Locks::mutator_lock_);
1647
Ian Rogersdd7624d2014-03-14 17:43:00 -07001648 static void ThreadExitCallback(void* arg);
Elliott Hughes5d96a712012-06-28 12:24:27 -07001649
Yu Lieac44242015-06-29 10:50:03 +08001650 // Maximum number of suspend barriers.
1651 static constexpr uint32_t kMaxSuspendBarriers = 3;
1652
Ian Rogersdd7624d2014-03-14 17:43:00 -07001653 // Has Thread::Startup been called?
1654 static bool is_started_;
Mathieu Chartier858f1c52012-10-17 17:45:55 -07001655
Ian Rogersdd7624d2014-03-14 17:43:00 -07001656 // TLS key used to retrieve the Thread*.
1657 static pthread_key_t pthread_key_self_;
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001658
Ian Rogersdd7624d2014-03-14 17:43:00 -07001659 // Used to notify threads that they should attempt to resume, they will suspend again if
1660 // their suspend count is > 0.
1661 static ConditionVariable* resume_cond_ GUARDED_BY(Locks::thread_suspend_count_lock_);
Dave Allisonb373e092014-02-20 16:06:36 -08001662
Calin Juravleb2771b42016-04-07 17:09:25 +01001663 // Hook passed by framework which returns true
1664 // when StrictMode events are traced for the current thread.
1665 static bool (*is_sensitive_thread_hook_)();
Calin Juravle97cbc922016-04-15 16:16:35 +01001666 // Stores the jit sensitive thread (which for now is the UI thread).
1667 static Thread* jit_sensitive_thread_;
Calin Juravleb2771b42016-04-07 17:09:25 +01001668
Vladimir Marko9f18fbc2019-07-31 15:06:12 +01001669 static constexpr uint32_t kMakeVisiblyInitializedCounterTriggerCount = 128;
Vladimir Markobf121912019-06-04 13:49:05 +01001670
Ian Rogersdd7624d2014-03-14 17:43:00 -07001671 /***********************************************************************************************/
1672 // Thread local storage. Fields are grouped by size to enable 32 <-> 64 searching to account for
1673 // pointer size differences. To encourage shorter encoding, more frequently used values appear
1674 // first if possible.
1675 /***********************************************************************************************/
Elliott Hughes6a607ad2012-07-13 20:40:00 -07001676
Zuo Wangf37a88b2014-07-10 04:26:41 -07001677 struct PACKED(4) tls_32bit_sized_values {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001678 // We have no control over the size of 'bool', but want our boolean fields
1679 // to be 4-byte quantities.
Vladimir Marko4f990712021-07-14 12:45:13 +01001680 using bool32_t = uint32_t;
Ian Rogers22f454c2012-09-08 11:06:29 -07001681
Vladimir Markobf121912019-06-04 13:49:05 +01001682 explicit tls_32bit_sized_values(bool is_daemon)
Vladimir Markoddf4fd32021-11-22 16:31:57 +00001683 : state_and_flags(0u),
1684 suspend_count(0),
Vladimir Markobf121912019-06-04 13:49:05 +01001685 thin_lock_thread_id(0),
1686 tid(0),
1687 daemon(is_daemon),
1688 throwing_OutOfMemoryError(false),
1689 no_thread_suspension(0),
1690 thread_exit_check_count(0),
Vladimir Markobf121912019-06-04 13:49:05 +01001691 is_transitioning_to_runnable(false),
Vladimir Markobf121912019-06-04 13:49:05 +01001692 is_gc_marking(false),
Hans Boehm1b3ec0f2022-01-26 16:53:07 +00001693 weak_ref_access_enabled(WeakRefAccessState::kVisiblyEnabled),
Vladimir Markobf121912019-06-04 13:49:05 +01001694 disable_thread_flip_count(0),
1695 user_code_suspend_count(0),
1696 force_interpreter_count(0),
Alex Light270db1c2019-12-03 12:20:01 +00001697 make_visibly_initialized_counter(0),
Hans Boehm30bc7772022-01-28 15:07:02 -08001698 define_class_counter(0),
1699 num_name_readers(0) {}
Dave Allisonb373e092014-02-20 16:06:36 -08001700
Vladimir Markoddf4fd32021-11-22 16:31:57 +00001701 // The state and flags field must be changed atomically so that flag values aren't lost.
1702 // See `StateAndFlags` for bit assignments of `ThreadFlag` and `ThreadState` values.
1703 // Keeping the state and flags together allows an atomic CAS to change from being
1704 // Suspended to Runnable without a suspend request occurring.
1705 Atomic<uint32_t> state_and_flags;
1706 static_assert(sizeof(state_and_flags) == sizeof(uint32_t),
1707 "Size of state_and_flags and uint32 are different");
Dave Allisonb373e092014-02-20 16:06:36 -08001708
Ian Rogersdd7624d2014-03-14 17:43:00 -07001709 // A non-zero value is used to tell the current thread to enter a safe point
1710 // at the next poll.
1711 int suspend_count GUARDED_BY(Locks::thread_suspend_count_lock_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001712
Ian Rogersdd7624d2014-03-14 17:43:00 -07001713 // Thin lock thread id. This is a small integer used by the thin lock implementation.
1714 // This is not to be confused with the native thread's tid, nor is it the value returned
1715 // by java.lang.Thread.getId --- this is a distinct value, used only for locking. One
1716 // important difference between this id and the ids visible to managed code is that these
1717 // ones get reused (to ensure that they fit in the number of bits available).
1718 uint32_t thin_lock_thread_id;
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -08001719
Ian Rogersdd7624d2014-03-14 17:43:00 -07001720 // System thread id.
1721 uint32_t tid;
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -08001722
Ian Rogersdd7624d2014-03-14 17:43:00 -07001723 // Is the thread a daemon?
1724 const bool32_t daemon;
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -08001725
Ian Rogersdd7624d2014-03-14 17:43:00 -07001726 // A boolean telling us whether we're recursively throwing OOME.
1727 bool32_t throwing_OutOfMemoryError;
1728
1729 // A positive value implies we're in a region where thread suspension isn't expected.
1730 uint32_t no_thread_suspension;
1731
1732 // How many times has our pthread key's destructor been called?
1733 uint32_t thread_exit_check_count;
Sebastien Hertz9f102032014-05-23 08:59:42 +02001734
Hiroshi Yamauchiee235822016-08-19 17:03:27 -07001735 // True if the thread is in TransitionFromSuspendedToRunnable(). This is used to distinguish the
1736 // non-runnable threads (eg. kNative, kWaiting) that are about to transition to runnable from
1737 // the rest of them.
1738 bool32_t is_transitioning_to_runnable;
Sebastien Hertz1558b572015-02-25 15:05:59 +01001739
Hiroshi Yamauchi00370822015-08-18 14:47:25 -07001740 // True if the GC is in the marking phase. This is used for the CC collector only. This is
1741 // thread local so that we can simplify the logic to check for the fast path of read barriers of
1742 // GC roots.
1743 bool32_t is_gc_marking;
1744
Nicolas Geoffray365719c2017-03-08 13:11:50 +00001745 // Thread "interrupted" status; stays raised until queried or thrown.
1746 Atomic<bool32_t> interrupted;
1747
Charles Mungeraa31f492018-11-01 18:57:38 +00001748 AtomicInteger park_state_;
1749
Hans Boehm1b3ec0f2022-01-26 16:53:07 +00001750 // Determines whether the thread is allowed to directly access a weak ref
1751 // (Reference::GetReferent() and system weaks) and to potentially mark an object alive/gray.
1752 // This is used for concurrent reference processing of the CC collector only. This is thread
1753 // local so that we can enable/disable weak ref access by using a checkpoint and avoid a race
1754 // around the time weak ref access gets disabled and concurrent reference processing begins
1755 // (if weak ref access is disabled during a pause, this is not an issue.) Other collectors use
1756 // Runtime::DisallowNewSystemWeaks() and ReferenceProcessor::EnableSlowPath(). Can be
1757 // concurrently accessed by GetReferent() and set (by iterating over threads).
1758 // Can be changed from kEnabled to kVisiblyEnabled by readers. No other concurrent access is
1759 // possible when that happens.
1760 mutable std::atomic<WeakRefAccessState> weak_ref_access_enabled;
Hiroshi Yamauchi20a0be02016-02-19 15:44:06 -08001761
1762 // A thread local version of Heap::disable_thread_flip_count_. This keeps track of how many
1763 // levels of (nested) JNI critical sections the thread is in and is used to detect a nested JNI
1764 // critical section enter.
1765 uint32_t disable_thread_flip_count;
Alex Light88fd7202017-06-30 08:31:59 -07001766
Alex Lightcea42152018-09-18 22:51:55 +00001767 // How much of 'suspend_count_' is by request of user code, used to distinguish threads
1768 // suspended by the runtime from those suspended by user code.
Alex Light88fd7202017-06-30 08:31:59 -07001769 // This should have GUARDED_BY(Locks::user_code_suspension_lock_) but auto analysis cannot be
1770 // told that AssertHeld should be good enough.
1771 int user_code_suspend_count GUARDED_BY(Locks::thread_suspend_count_lock_);
David Srbecky28f6cff2018-10-16 15:07:28 +01001772
Alex Light3dacdd62019-03-12 15:45:47 +00001773 // Count of how many times this thread has been forced to interpreter. If this is not 0 the
1774 // thread must remain in interpreted code as much as possible.
1775 uint32_t force_interpreter_count;
1776
Vladimir Markobf121912019-06-04 13:49:05 +01001777 // Counter for calls to initialize a class that's initialized but not visibly initialized.
1778 // When this reaches kMakeVisiblyInitializedCounterTriggerCount, we call the runtime to
1779 // make initialized classes visibly initialized. This is needed because we usually make
1780 // classes visibly initialized in batches but we do not want to be stuck with a class
1781 // initialized but not visibly initialized for a long time even if no more classes are
1782 // being initialized anymore.
1783 uint32_t make_visibly_initialized_counter;
Alex Light270db1c2019-12-03 12:20:01 +00001784
1785 // Counter for how many nested define-classes are ongoing in this thread. Used to allow waiting
1786 // for threads to be done with class-definition work.
1787 uint32_t define_class_counter;
Hans Boehm30bc7772022-01-28 15:07:02 -08001788
1789 // A count of the number of readers of tlsPtr_.name that may still be looking at a string they
1790 // retrieved.
1791 mutable std::atomic<uint32_t> num_name_readers;
1792 static_assert(std::atomic<uint32_t>::is_always_lock_free);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001793 } tls32_;
1794
1795 struct PACKED(8) tls_64bit_sized_values {
Sebastien Hertz07474662015-08-25 15:12:33 +00001796 tls_64bit_sized_values() : trace_clock_base(0) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001797 }
1798
1799 // The clock base used for tracing.
1800 uint64_t trace_clock_base;
1801
Ian Rogersdd7624d2014-03-14 17:43:00 -07001802 RuntimeStats stats;
1803 } tls64_;
1804
Andreas Gampe6aa13702015-10-28 10:57:25 -07001805 struct PACKED(sizeof(void*)) tls_ptr_sized_values {
Nicolas Geoffrayf9795d12021-10-04 16:28:54 +01001806 tls_ptr_sized_values() : card_table(nullptr),
1807 exception(nullptr),
1808 stack_end(nullptr),
1809 managed_stack(),
1810 suspend_trigger(nullptr),
1811 jni_env(nullptr),
1812 tmp_jni_env(nullptr),
1813 self(nullptr),
1814 opeer(nullptr),
1815 jpeer(nullptr),
1816 stack_begin(nullptr),
1817 stack_size(0),
1818 deps_or_stack_trace_sample(),
1819 wait_next(nullptr),
1820 monitor_enter_object(nullptr),
1821 top_handle_scope(nullptr),
1822 class_loader_override(nullptr),
1823 long_jump_context(nullptr),
1824 instrumentation_stack(nullptr),
1825 stacked_shadow_frame_record(nullptr),
1826 deoptimization_context_stack(nullptr),
1827 frame_id_to_shadow_frame(nullptr),
1828 name(nullptr),
1829 pthread_self(0),
1830 last_no_thread_suspension_cause(nullptr),
1831 checkpoint_function(nullptr),
1832 thread_local_start(nullptr),
1833 thread_local_pos(nullptr),
1834 thread_local_end(nullptr),
1835 thread_local_limit(nullptr),
1836 thread_local_objects(0),
1837 thread_local_alloc_stack_top(nullptr),
1838 thread_local_alloc_stack_end(nullptr),
Vladimir Marko23cf32f2021-11-15 13:38:02 +00001839 mutator_lock(nullptr),
Nicolas Geoffrayf9795d12021-10-04 16:28:54 +01001840 flip_function(nullptr),
1841 method_verifier(nullptr),
1842 thread_local_mark_stack(nullptr),
1843 async_exception(nullptr),
1844 top_reflective_handle_scope(nullptr) {
Mathieu Chartier12d625f2015-03-13 11:33:37 -07001845 std::fill(held_mutexes, held_mutexes + kLockLevelCount, nullptr);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001846 }
1847
1848 // The biased card table, see CardTable for details.
Ian Rogers13735952014-10-08 12:43:28 -07001849 uint8_t* card_table;
Ian Rogersdd7624d2014-03-14 17:43:00 -07001850
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001851 // The pending exception or null.
Ian Rogersdd7624d2014-03-14 17:43:00 -07001852 mirror::Throwable* exception;
1853
1854 // The end of this thread's stack. This is the lowest safely-addressable address on the stack.
1855 // We leave extra space so there's room for the code that throws StackOverflowError.
Ian Rogers13735952014-10-08 12:43:28 -07001856 uint8_t* stack_end;
Ian Rogersdd7624d2014-03-14 17:43:00 -07001857
1858 // The top of the managed stack often manipulated directly by compiler generated code.
1859 ManagedStack managed_stack;
1860
1861 // In certain modes, setting this to 0 will trigger a SEGV and thus a suspend check. It is
1862 // normally set to the address of itself.
1863 uintptr_t* suspend_trigger;
1864
1865 // Every thread may have an associated JNI environment
1866 JNIEnvExt* jni_env;
1867
Andreas Gampe449357d2015-06-01 22:29:51 -07001868 // Temporary storage to transfer a pre-allocated JNIEnvExt from the creating thread to the
1869 // created thread.
1870 JNIEnvExt* tmp_jni_env;
1871
Ian Rogersdd7624d2014-03-14 17:43:00 -07001872 // Initialized to "this". On certain architectures (such as x86) reading off of Thread::Current
1873 // is easy but getting the address of Thread::Current is hard. This field can be read off of
1874 // Thread::Current to give the address.
1875 Thread* self;
1876
1877 // Our managed peer (an instance of java.lang.Thread). The jobject version is used during thread
1878 // start up, until the thread is registered and the local opeer_ is used.
1879 mirror::Object* opeer;
1880 jobject jpeer;
1881
1882 // The "lowest addressable byte" of the stack.
Ian Rogers13735952014-10-08 12:43:28 -07001883 uint8_t* stack_begin;
Ian Rogersdd7624d2014-03-14 17:43:00 -07001884
1885 // Size of the stack.
1886 size_t stack_size;
1887
Nicolas Geoffray340dafa2016-11-18 16:03:10 +00001888 // Sampling profiler and AOT verification cannot happen on the same run, so we share
1889 // the same entry for the stack trace and the verifier deps.
1890 union DepsOrStackTraceSample {
1891 DepsOrStackTraceSample() {
1892 verifier_deps = nullptr;
1893 stack_trace_sample = nullptr;
1894 }
1895 // Pointer to previous stack trace captured by sampling profiler.
1896 std::vector<ArtMethod*>* stack_trace_sample;
1897 // When doing AOT verification, per-thread VerifierDeps.
1898 verifier::VerifierDeps* verifier_deps;
1899 } deps_or_stack_trace_sample;
Ian Rogersdd7624d2014-03-14 17:43:00 -07001900
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001901 // The next thread in the wait set this thread is part of or null if not waiting.
Ian Rogersdd7624d2014-03-14 17:43:00 -07001902 Thread* wait_next;
1903
1904 // If we're blocked in MonitorEnter, this is the object we're trying to lock.
1905 mirror::Object* monitor_enter_object;
1906
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001907 // Top of linked list of handle scopes or null for none.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -07001908 BaseHandleScope* top_handle_scope;
Ian Rogersdd7624d2014-03-14 17:43:00 -07001909
1910 // Needed to get the right ClassLoader in JNI_OnLoad, but also
1911 // useful for testing.
Ian Rogers68d8b422014-07-17 11:09:10 -07001912 jobject class_loader_override;
Ian Rogersdd7624d2014-03-14 17:43:00 -07001913
1914 // Thread local, lazily allocated, long jump context. Used to deliver exceptions.
1915 Context* long_jump_context;
1916
1917 // Additional stack used by method instrumentation to store method and return pc values.
Nicolas Geoffraye91e7952020-01-23 10:15:56 +00001918 // Stored as a pointer since std::map is not PACKED.
1919 // !DO NOT CHANGE! to std::unordered_map: the users of this map require an
1920 // ordered iteration on the keys (which are stack addresses).
1921 // Also see Thread::GetInstrumentationStack for the requirements on
1922 // manipulating and reading this map.
1923 std::map<uintptr_t, instrumentation::InstrumentationStackFrame>* instrumentation_stack;
Ian Rogersdd7624d2014-03-14 17:43:00 -07001924
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -07001925 // For gc purpose, a shadow frame record stack that keeps track of:
1926 // 1) shadow frames under construction.
1927 // 2) deoptimization shadow frames.
1928 StackedShadowFrameRecord* stacked_shadow_frame_record;
Ian Rogersdd7624d2014-03-14 17:43:00 -07001929
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -07001930 // Deoptimization return value record stack.
Sebastien Hertz07474662015-08-25 15:12:33 +00001931 DeoptimizationContextRecord* deoptimization_context_stack;
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001932
Mingyao Yang99170c62015-07-06 11:10:37 -07001933 // For debugger, a linked list that keeps the mapping from frame_id to shadow frame.
1934 // Shadow frames may be created before deoptimization happens so that the debugger can
1935 // set local values there first.
1936 FrameIdToShadowFrame* frame_id_to_shadow_frame;
1937
Hans Boehm30bc7772022-01-28 15:07:02 -08001938 // A cached copy of the java.lang.Thread's (modified UTF-8) name.
1939 // If this is not null or kThreadNameDuringStartup, then it owns the malloc memory holding
1940 // the string. Updated in an RCU-like manner.
1941 std::atomic<const char*> name;
1942 static_assert(std::atomic<const char*>::is_always_lock_free);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001943
1944 // A cached pthread_t for the pthread underlying this Thread*.
1945 pthread_t pthread_self;
1946
Ian Rogersdd7624d2014-03-14 17:43:00 -07001947 // If no_thread_suspension_ is > 0, what is causing that assertion.
1948 const char* last_no_thread_suspension_cause;
1949
Mathieu Chartier952e1e32016-06-13 14:04:02 -07001950 // Pending checkpoint function or null if non-pending. If this checkpoint is set and someone\
1951 // requests another checkpoint, it goes to the checkpoint overflow list.
1952 Closure* checkpoint_function GUARDED_BY(Locks::thread_suspend_count_lock_);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001953
Yu Lieac44242015-06-29 10:50:03 +08001954 // Pending barriers that require passing or NULL if non-pending. Installation guarding by
1955 // Locks::thread_suspend_count_lock_.
1956 // They work effectively as art::Barrier, but implemented directly using AtomicInteger and futex
1957 // to avoid additional cost of a mutex and a condition variable, as used in art::Barrier.
1958 AtomicInteger* active_suspend_barriers[kMaxSuspendBarriers];
1959
Roland Levillaine71b3542017-01-16 14:58:23 +00001960 // Thread-local allocation pointer. Moved here to force alignment for thread_local_pos on ARM.
1961 uint8_t* thread_local_start;
Ian Rogersdd7624d2014-03-14 17:43:00 -07001962
Hiroshi Yamauchi7e1ce282015-12-11 15:46:19 -08001963 // thread_local_pos and thread_local_end must be consecutive for ldrd and are 8 byte aligned for
1964 // potentially better performance.
Ian Rogers13735952014-10-08 12:43:28 -07001965 uint8_t* thread_local_pos;
1966 uint8_t* thread_local_end;
Igor Murashkinaf1e2992016-10-12 17:44:50 -07001967
Mathieu Chartier6bc77742017-04-18 17:46:23 -07001968 // Thread local limit is how much we can expand the thread local buffer to, it is greater or
1969 // equal to thread_local_end.
1970 uint8_t* thread_local_limit;
1971
Vladimir Marko05846472016-09-14 12:49:57 +01001972 size_t thread_local_objects;
Ian Rogersdd7624d2014-03-14 17:43:00 -07001973
Roland Levillaine71b3542017-01-16 14:58:23 +00001974 // Entrypoint function pointers.
1975 // TODO: move this to more of a global offset table model to avoid per-thread duplication.
1976 JniEntryPoints jni_entrypoints;
1977 QuickEntryPoints quick_entrypoints;
1978
Mathieu Chartier0651d412014-04-29 14:37:57 -07001979 // There are RosAlloc::kNumThreadLocalSizeBrackets thread-local size brackets per thread.
Hiroshi Yamauchi7ed9c562016-02-02 15:22:09 -08001980 void* rosalloc_runs[kNumRosAllocThreadLocalSizeBracketsInThread];
Ian Rogersdd7624d2014-03-14 17:43:00 -07001981
1982 // Thread-local allocation stack data/routines.
Mathieu Chartiercb535da2015-01-23 13:50:03 -08001983 StackReference<mirror::Object>* thread_local_alloc_stack_top;
1984 StackReference<mirror::Object>* thread_local_alloc_stack_end;
Chao-ying Fu9e369312014-05-21 11:20:52 -07001985
Vladimir Marko23cf32f2021-11-15 13:38:02 +00001986 // Pointer to the mutator lock.
1987 // This is the same as `Locks::mutator_lock_` but cached for faster state transitions.
1988 MutatorMutex* mutator_lock;
1989
Chao-ying Fu9e369312014-05-21 11:20:52 -07001990 // Support for Mutex lock hierarchy bug detection.
1991 BaseMutex* held_mutexes[kLockLevelCount];
Dave Allison8ce6b902014-08-26 11:07:58 -07001992
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -08001993 // The function used for thread flip.
1994 Closure* flip_function;
Mathieu Chartier12d625f2015-03-13 11:33:37 -07001995
1996 // Current method verifier, used for root marking.
1997 verifier::MethodVerifier* method_verifier;
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -07001998
1999 // Thread-local mark stack for the concurrent copying collector.
2000 gc::accounting::AtomicStack<mirror::Object>* thread_local_mark_stack;
Alex Light848574c2017-09-25 16:59:39 -07002001
2002 // The pending async-exception or null.
2003 mirror::Throwable* async_exception;
Alex Light55eccdf2019-10-07 13:51:13 +00002004
2005 // Top of the linked-list for reflective-handle scopes or null if none.
2006 BaseReflectiveHandleScope* top_reflective_handle_scope;
Ian Rogersdd7624d2014-03-14 17:43:00 -07002007 } tlsPtr_;
2008
Alex Lightbb68fda2018-10-01 13:21:47 -07002009 // Small thread-local cache to be used from the interpreter.
2010 // It is keyed by dex instruction pointer.
2011 // The value is opcode-depended (e.g. field offset).
2012 InterpreterCache interpreter_cache_;
2013
2014 // All fields below this line should not be accessed by native code. This means these fields can
2015 // be modified, rearranged, added or removed without having to modify asm_support.h
2016
Nicolas Geoffray365719c2017-03-08 13:11:50 +00002017 // Guards the 'wait_monitor_' members.
Ian Rogersdd7624d2014-03-14 17:43:00 -07002018 Mutex* wait_mutex_ DEFAULT_MUTEX_ACQUIRED_AFTER;
2019
2020 // Condition variable waited upon during a wait.
2021 ConditionVariable* wait_cond_ GUARDED_BY(wait_mutex_);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002022 // Pointer to the monitor lock we're currently waiting on or null if not waiting.
Ian Rogersdd7624d2014-03-14 17:43:00 -07002023 Monitor* wait_monitor_ GUARDED_BY(wait_mutex_);
2024
Mathieu Chartierdfe02f62016-02-01 20:15:11 -08002025 // Debug disable read barrier count, only is checked for debug builds and only in the runtime.
2026 uint8_t debug_disallow_read_barrier_ = 0;
2027
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -07002028 // Note that it is not in the packed struct, may not be accessed for cross compilation.
2029 uintptr_t poison_object_cookie_ = 0;
2030
Mathieu Chartier952e1e32016-06-13 14:04:02 -07002031 // Pending extra checkpoints if checkpoint_function_ is already used.
2032 std::list<Closure*> checkpoint_overflow_ GUARDED_BY(Locks::thread_suspend_count_lock_);
2033
Alex Light184f0752018-07-13 11:18:22 -07002034 // Custom TLS field that can be used by plugins or the runtime. Should not be accessed directly by
2035 // compiled code or entrypoints.
Alex Light6e1b7d82021-05-06 09:58:46 -07002036 SafeMap<std::string, std::unique_ptr<TLSData>, std::less<>> custom_tls_
2037 GUARDED_BY(Locks::custom_tls_lock_);
Andreas Gampef26bf2d2017-01-13 16:47:14 -08002038
Andreas Gampe82372002019-07-24 15:42:09 -07002039#ifndef __BIONIC__
Andreas Gampe2be43062019-07-24 16:52:33 -07002040 __attribute__((tls_model("initial-exec")))
Andreas Gampe82372002019-07-24 15:42:09 -07002041 static thread_local Thread* self_tls_;
2042#endif
2043
Alex Lighte9f61032018-09-24 16:04:51 -07002044 // True if the thread is some form of runtime thread (ex, GC or JIT).
2045 bool is_runtime_thread_;
Calin Juravleccd56952016-12-15 17:57:38 +00002046
Orion Hodson01ecfa12019-07-18 12:57:47 +01002047 // Set during execution of JNI methods that get field and method id's as part of determining if
2048 // the caller is allowed to access all fields and methods in the Core Platform API.
2049 uint32_t core_platform_api_cookie_ = 0;
2050
Mathieu Chartier15d34022014-02-26 17:16:38 -08002051 friend class gc::collector::SemiSpace; // For getting stack traces.
Mathieu Chartier692fafd2013-11-29 17:24:40 -08002052 friend class Runtime; // For CreatePeer.
Ian Rogers5cf98192014-05-29 21:31:50 -07002053 friend class QuickExceptionHandler; // For dumping the stack.
Mathieu Chartier692fafd2013-11-29 17:24:40 -08002054 friend class ScopedThreadStateChange;
Mathieu Chartier119c6bd2014-05-09 14:11:47 -07002055 friend class StubTest; // For accessing entrypoints.
Mathieu Chartier692fafd2013-11-29 17:24:40 -08002056 friend class ThreadList; // For ~Thread and Destroy.
2057
Andreas Gampe4352b452014-06-04 18:59:01 -07002058 friend class EntrypointsOrderTest; // To test the order of tls entries.
Vladimir Markocedec9d2021-02-08 16:16:13 +00002059 friend class JniCompilerTest; // For intercepting JNI entrypoint calls.
Andreas Gampe4352b452014-06-04 18:59:01 -07002060
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002061 DISALLOW_COPY_AND_ASSIGN(Thread);
2062};
Ian Rogersbdb03912011-09-14 00:55:44 -07002063
Mathieu Chartier4e2cb092015-07-22 16:17:51 -07002064class SCOPED_CAPABILITY ScopedAssertNoThreadSuspension {
Mathieu Chartier2d5f39e2014-09-19 17:52:37 -07002065 public:
Mingyao Yangf26828b2017-07-27 12:49:01 -07002066 ALWAYS_INLINE ScopedAssertNoThreadSuspension(const char* cause,
2067 bool enabled = true)
2068 ACQUIRE(Roles::uninterruptible_)
2069 : enabled_(enabled) {
2070 if (!enabled_) {
2071 return;
2072 }
Mathieu Chartier268764d2016-09-13 12:09:38 -07002073 if (kIsDebugBuild) {
2074 self_ = Thread::Current();
2075 old_cause_ = self_->StartAssertNoThreadSuspension(cause);
2076 } else {
2077 Roles::uninterruptible_.Acquire(); // No-op.
2078 }
Mathieu Chartier2d5f39e2014-09-19 17:52:37 -07002079 }
Mathieu Chartier268764d2016-09-13 12:09:38 -07002080 ALWAYS_INLINE ~ScopedAssertNoThreadSuspension() RELEASE(Roles::uninterruptible_) {
Mingyao Yangf26828b2017-07-27 12:49:01 -07002081 if (!enabled_) {
2082 return;
2083 }
Mathieu Chartier268764d2016-09-13 12:09:38 -07002084 if (kIsDebugBuild) {
2085 self_->EndAssertNoThreadSuspension(old_cause_);
2086 } else {
2087 Roles::uninterruptible_.Release(); // No-op.
2088 }
Mathieu Chartier2d5f39e2014-09-19 17:52:37 -07002089 }
2090
2091 private:
Mathieu Chartier268764d2016-09-13 12:09:38 -07002092 Thread* self_;
Mingyao Yangf26828b2017-07-27 12:49:01 -07002093 const bool enabled_;
Mathieu Chartier268764d2016-09-13 12:09:38 -07002094 const char* old_cause_;
Mathieu Chartier2d5f39e2014-09-19 17:52:37 -07002095};
2096
Mathieu Chartierdc540df2019-11-15 17:11:44 -08002097class ScopedAllowThreadSuspension {
2098 public:
2099 ALWAYS_INLINE ScopedAllowThreadSuspension() RELEASE(Roles::uninterruptible_) {
2100 if (kIsDebugBuild) {
2101 self_ = Thread::Current();
2102 old_cause_ = self_->EndAssertNoThreadSuspension();
2103 } else {
2104 Roles::uninterruptible_.Release(); // No-op.
2105 }
2106 }
2107 ALWAYS_INLINE ~ScopedAllowThreadSuspension() ACQUIRE(Roles::uninterruptible_) {
2108 if (kIsDebugBuild) {
2109 CHECK(self_->StartAssertNoThreadSuspension(old_cause_) == nullptr);
2110 } else {
2111 Roles::uninterruptible_.Acquire(); // No-op.
2112 }
2113 }
2114
2115 private:
2116 Thread* self_;
2117 const char* old_cause_;
2118};
2119
2120
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -07002121class ScopedStackedShadowFramePusher {
2122 public:
2123 ScopedStackedShadowFramePusher(Thread* self, ShadowFrame* sf, StackedShadowFrameType type)
2124 : self_(self), type_(type) {
2125 self_->PushStackedShadowFrame(sf, type);
2126 }
2127 ~ScopedStackedShadowFramePusher() {
2128 self_->PopStackedShadowFrame(type_);
2129 }
2130
2131 private:
2132 Thread* const self_;
2133 const StackedShadowFrameType type_;
2134
2135 DISALLOW_COPY_AND_ASSIGN(ScopedStackedShadowFramePusher);
2136};
2137
Mathieu Chartierdfe02f62016-02-01 20:15:11 -08002138// Only works for debug builds.
2139class ScopedDebugDisallowReadBarriers {
2140 public:
2141 explicit ScopedDebugDisallowReadBarriers(Thread* self) : self_(self) {
2142 self_->ModifyDebugDisallowReadBarrier(1);
2143 }
2144 ~ScopedDebugDisallowReadBarriers() {
2145 self_->ModifyDebugDisallowReadBarrier(-1);
2146 }
2147
2148 private:
2149 Thread* const self_;
2150};
2151
Hiroshi Yamauchiee235822016-08-19 17:03:27 -07002152class ScopedTransitioningToRunnable : public ValueObject {
2153 public:
2154 explicit ScopedTransitioningToRunnable(Thread* self)
2155 : self_(self) {
2156 DCHECK_EQ(self, Thread::Current());
2157 if (kUseReadBarrier) {
2158 self_->SetIsTransitioningToRunnable(true);
2159 }
2160 }
2161
2162 ~ScopedTransitioningToRunnable() {
2163 if (kUseReadBarrier) {
2164 self_->SetIsTransitioningToRunnable(false);
2165 }
2166 }
2167
2168 private:
2169 Thread* const self_;
2170};
2171
Andreas Gampe04bbb5b2017-01-19 17:49:03 +00002172class ThreadLifecycleCallback {
2173 public:
2174 virtual ~ThreadLifecycleCallback() {}
2175
2176 virtual void ThreadStart(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_) = 0;
2177 virtual void ThreadDeath(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_) = 0;
2178};
2179
Alex Lightb7c640d2019-03-20 15:52:13 -07002180// Store an exception from the thread and suppress it for the duration of this object.
2181class ScopedExceptionStorage {
2182 public:
2183 explicit ScopedExceptionStorage(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
Alex Light79d6c802019-06-27 15:50:11 +00002184 void SuppressOldException(const char* message = "") REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lightb7c640d2019-03-20 15:52:13 -07002185 ~ScopedExceptionStorage() REQUIRES_SHARED(Locks::mutator_lock_);
2186
2187 private:
2188 Thread* self_;
2189 StackHandleScope<1> hs_;
Alex Light79d6c802019-06-27 15:50:11 +00002190 MutableHandle<mirror::Throwable> excp_;
Alex Lightb7c640d2019-03-20 15:52:13 -07002191};
2192
Elliott Hughes330304d2011-08-12 14:28:05 -07002193std::ostream& operator<<(std::ostream& os, const Thread& thread);
Vladimir Marko9974e3c2020-06-10 16:27:06 +01002194std::ostream& operator<<(std::ostream& os, StackedShadowFrameType thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002195
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002196} // namespace art
2197
Brian Carlstromfc0e3212013-07-17 14:40:12 -07002198#endif // ART_RUNTIME_THREAD_H_