thread.h revision ea46f950e7a51585db293cd7f047de190a482414
18d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes/*
28d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * Copyright (C) 2011 The Android Open Source Project
38d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes *
48d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
58d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * you may not use this file except in compliance with the License.
68d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * You may obtain a copy of the License at
78d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes *
88d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
98d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes *
108d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * Unless required by applicable law or agreed to in writing, software
118d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
128d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * See the License for the specific language governing permissions and
148d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * limitations under the License.
158d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes */
160e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
17fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#ifndef ART_RUNTIME_THREAD_H_
18fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#define ART_RUNTIME_THREAD_H_
190e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
20b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro#include <pthread.h>
21a09576416788b916095739e43a16917e7948f3a4Elliott Hughes
2202b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes#include <bitset>
23306057fd278d75bf3794bd5243a3b6652c487d18Ian Rogers#include <deque>
24a09576416788b916095739e43a16917e7948f3a4Elliott Hughes#include <iosfwd>
25b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers#include <list>
268daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes#include <string>
27b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro
28761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes#include "base/macros.h"
29848871b4d8481229c32e0d048a9856e5a9a17ef9Ian Rogers#include "entrypoints/interpreter/interpreter_entrypoints.h"
30848871b4d8481229c32e0d048a9856e5a9a17ef9Ian Rogers#include "entrypoints/jni/jni_entrypoints.h"
317655f29fabc0a12765de828914a18314382e5a35Ian Rogers#include "entrypoints/portable/portable_entrypoints.h"
327655f29fabc0a12765de828914a18314382e5a35Ian Rogers#include "entrypoints/quick/quick_entrypoints.h"
33578bbdc684db8ed68e9fedbc678669d27fa68b6eBrian Carlstrom#include "globals.h"
34306057fd278d75bf3794bd5243a3b6652c487d18Ian Rogers#include "jvalue.h"
3581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers#include "locks.h"
36578bbdc684db8ed68e9fedbc678669d27fa68b6eBrian Carlstrom#include "offsets.h"
372dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "root_visitor.h"
389d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes#include "runtime_stats.h"
3968e76526e98432625464022cb26f66b9ef6f5af4Elliott Hughes#include "stack.h"
401f5393447b9f45be7918042d9ee7b521376de866Ian Rogers#include "stack_indirect_reference_table.h"
412dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "thread_state.h"
4262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers#include "throw_location.h"
43bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers#include "UniquePtr.h"
44b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers
450e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapironamespace art {
460e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
472dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersnamespace mirror {
48ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  class ArtMethod;
49848871b4d8481229c32e0d048a9856e5a9a17ef9Ian Rogers  class Array;
50848871b4d8481229c32e0d048a9856e5a9a17ef9Ian Rogers  class Class;
51848871b4d8481229c32e0d048a9856e5a9a17ef9Ian Rogers  class ClassLoader;
52848871b4d8481229c32e0d048a9856e5a9a17ef9Ian Rogers  class Object;
53848871b4d8481229c32e0d048a9856e5a9a17ef9Ian Rogers  template<class T> class ObjectArray;
54848871b4d8481229c32e0d048a9856e5a9a17ef9Ian Rogers  template<class T> class PrimitiveArray;
55848871b4d8481229c32e0d048a9856e5a9a17ef9Ian Rogers  typedef PrimitiveArray<int32_t> IntArray;
56848871b4d8481229c32e0d048a9856e5a9a17ef9Ian Rogers  class StackTraceElement;
57848871b4d8481229c32e0d048a9856e5a9a17ef9Ian Rogers  class StaticStorageBase;
58848871b4d8481229c32e0d048a9856e5a9a17ef9Ian Rogers  class Throwable;
592dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers}  // namespace mirror
602dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersclass BaseMutex;
612dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersclass ClassLinker;
627a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogersclass Closure;
63bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogersclass Context;
641b09b094a85e03f6ef5f687f58bb91c433273ba1Ian Rogersstruct DebugInvokeReq;
6581d425b0b232962441616f8b14f73620bffef5e5Ian Rogersclass DexFile;
66120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogersstruct JavaVMExt;
6781d425b0b232962441616f8b14f73620bffef5e5Ian Rogersstruct JNIEnvExt;
688daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughesclass Monitor;
69b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiroclass Runtime;
7000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersclass ScopedObjectAccess;
7100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersclass ScopedObjectAccessUnchecked;
72f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chienclass ShadowFrame;
7340381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstromclass Thread;
7440381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstromclass ThreadList;
750e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
7634e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes// Thread priorities. These must match the Thread.MIN_PRIORITY,
7734e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes// Thread.NORM_PRIORITY, and Thread.MAX_PRIORITY constants.
7834e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughesenum ThreadPriority {
7934e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes  kMinThreadPriority = 1,
8034e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes  kNormThreadPriority = 5,
8134e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes  kMaxThreadPriority = 10,
8234e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes};
83fc1d9f581592d54cc14240b9909824af38656931Elliott Hughes
84474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogersenum ThreadFlag {
8550ffee20ced9c0c7ac68889c86be8844cf120cf2Ian Rogers  kSuspendRequest   = 1,  // If set implies that suspend_count_ > 0 and the Thread should enter the
8650ffee20ced9c0c7ac68889c86be8844cf120cf2Ian Rogers                          // safepoint handler.
8762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  kCheckpointRequest = 2  // Request that the thread do some checkpoint work and then continue.
88474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers};
89474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers
90df1ce91ba97bc79a0637e5504b39318fb1c9f577Ian Rogersclass PACKED(4) Thread {
9134e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes public:
92932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers  // Space to throw a StackOverflowError in.
9362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  static const size_t kStackOverflowReservedBytes = 16 * KB;
94c143c55718342519db5398e41dda31422cf16c79buzbee
95462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes  // Creates a new native thread corresponding to the given managed peer.
96462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes  // Used to implement Thread.start.
9752673ffae0025d86f4023735581f19ebcc477487Ian Rogers  static void CreateNativeThread(JNIEnv* env, jobject peer, size_t stack_size, bool daemon);
9861e019d291583029c01b61b93bea750f2b663c37Carl Shapiro
99462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes  // Attaches the calling native thread to the runtime, returning the new native peer.
100462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes  // Used to implement JNI AttachCurrentThread and AttachCurrentThreadAsDaemon calls.
101664bebf92eb2151b9b570ccd42ac4b6056c3ea9cMathieu Chartier  static Thread* Attach(const char* thread_name, bool as_daemon, jobject thread_group,
102664bebf92eb2151b9b570ccd42ac4b6056c3ea9cMathieu Chartier                        bool create_peer);
103b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro
104caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom  // Reset internal state of child thread after fork.
105caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom  void InitAfterFork();
106caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom
1070878d654e7be8c9666579e22522704d8887415ccIan Rogers  static Thread* Current() {
108e0447357fa8dece635c213d360cd03160cc2d8a0Elliott Hughes    // We rely on Thread::Current returning NULL for a detached thread, so it's not obvious
109e0447357fa8dece635c213d360cd03160cc2d8a0Elliott Hughes    // that we can replace this with a direct %fs access on x86.
110df62950e7a32031b82360c407d46a37b94188fbbBrian Carlstrom    if (!is_started_) {
1110878d654e7be8c9666579e22522704d8887415ccIan Rogers      return NULL;
1120878d654e7be8c9666579e22522704d8887415ccIan Rogers    } else {
1130878d654e7be8c9666579e22522704d8887415ccIan Rogers      void* thread = pthread_getspecific(Thread::pthread_key_self_);
1140878d654e7be8c9666579e22522704d8887415ccIan Rogers      return reinterpret_cast<Thread*>(thread);
1150878d654e7be8c9666579e22522704d8887415ccIan Rogers    }
1160e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro  }
1170e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
1182dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  static Thread* FromManagedThread(const ScopedObjectAccessUnchecked& ts,
1192dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers                                   mirror::Object* thread_peer)
120a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao      EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_)
121b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
122b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
12300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  static Thread* FromManagedThread(const ScopedObjectAccessUnchecked& ts, jobject thread)
124a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao      EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_)
125b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
126b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1278daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
12828fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes  // Translates 172 to pAllocArrayFromCode and so on.
12928fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes  static void DumpThreadOffset(std::ostream& os, uint32_t offset, size_t size_of_pointers);
13028fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes
13100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Dumps a one-line summary of thread state (used for operator<<).
13200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  void ShortDump(std::ostream& os) const;
13300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
13400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Dumps the detailed thread state and the thread stack (used for SIGQUIT).
13500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  void Dump(std::ostream& os) const
136b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
137b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
138a09576416788b916095739e43a16917e7948f3a4Elliott Hughes
139abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes  // Dumps the SIGQUIT per-thread header. 'thread' can be NULL for a non-attached thread, in which
140abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes  // case we use 'tid' to identify the thread, and we'll include as much information as we can.
14100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  static void DumpState(std::ostream& os, const Thread* thread, pid_t tid)
142cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers      LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
143cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
144abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes
145474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers  ThreadState GetState() const {
14630e173f6da202e4caa37ceb477e2fbbe09cec990Ian Rogers    return static_cast<ThreadState>(state_and_flags_.as_struct.state);
1470cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers  }
1480cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers
149474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers  ThreadState SetState(ThreadState new_state);
1508d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes
15181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  int GetSuspendCount() const EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_suspend_count_lock_) {
15200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return suspend_count_;
15352673ffae0025d86f4023735581f19ebcc477487Ian Rogers  }
15452673ffae0025d86f4023735581f19ebcc477487Ian Rogers
15581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  int GetDebugSuspendCount() const EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_suspend_count_lock_) {
15600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return debug_suspend_count_;
15700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
15800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
159858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier  bool IsSuspended() const {
160858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier    union StateAndFlags state_and_flags = state_and_flags_;
161858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier    return state_and_flags.as_struct.state != kRunnable &&
162858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier        (state_and_flags.as_struct.flags & kSuspendRequest) != 0;
16300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
16400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
16501ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers  void ModifySuspendCount(Thread* self, int delta, bool for_debugger)
166b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_suspend_count_lock_);
16700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
1680e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier  bool RequestCheckpoint(Closure* function);
169858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier
17000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Called when thread detected that the thread_suspend_count_ was non-zero. Gives up share of
17100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // mutator_lock_ and waits until it is resumed and thread_suspend_count_ is zero.
1729da7f59c9059397182b9a97e898a42ec06d4d646Ian Rogers  void FullSuspendCheck()
173b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
174b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
17500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
17600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Transition from non-runnable to runnable state acquiring share on mutator_lock_.
17700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ThreadState TransitionFromSuspendedToRunnable()
178b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
179693ff61274cd2c9b8eb7e68c370f84a911b8ca52Ian Rogers      SHARED_LOCK_FUNCTION(Locks::mutator_lock_)
1801ffa32f0be7becec4907b26ead353e4b17e1219cIan Rogers      ALWAYS_INLINE;
18100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
18200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Transition from runnable into a state where mutator privileges are denied. Releases share of
18300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // mutator lock.
18400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  void TransitionFromRunnableToSuspended(ThreadState new_state)
185b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
186693ff61274cd2c9b8eb7e68c370f84a911b8ca52Ian Rogers      UNLOCK_FUNCTION(Locks::mutator_lock_)
1871ffa32f0be7becec4907b26ead353e4b17e1219cIan Rogers      ALWAYS_INLINE;
188038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes
18900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Wait for a debugger suspension on the thread associated with the given peer. Returns the
19000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // thread on success, else NULL. If the thread should be suspended then request_suspension should
19100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // be true on entry. If the suspension times out then *timeout is set to true.
192f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  static Thread* SuspendForDebugger(jobject peer,  bool request_suspension, bool* timed_out)
193b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      LOCKS_EXCLUDED(Locks::mutator_lock_,
194b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers                     Locks::thread_list_lock_,
195b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers                     Locks::thread_suspend_count_lock_);
1960cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers
1970399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // Once called thread suspension will cause an assertion failure.
1980399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers#ifndef NDEBUG
19952673ffae0025d86f4023735581f19ebcc477487Ian Rogers  const char* StartAssertNoThreadSuspension(const char* cause) {
20052673ffae0025d86f4023735581f19ebcc477487Ian Rogers    CHECK(cause != NULL);
20152673ffae0025d86f4023735581f19ebcc477487Ian Rogers    const char* previous_cause = last_no_thread_suspension_cause_;
2020399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    no_thread_suspension_++;
20352673ffae0025d86f4023735581f19ebcc477487Ian Rogers    last_no_thread_suspension_cause_ = cause;
20452673ffae0025d86f4023735581f19ebcc477487Ian Rogers    return previous_cause;
2050399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  }
20652673ffae0025d86f4023735581f19ebcc477487Ian Rogers#else
20752673ffae0025d86f4023735581f19ebcc477487Ian Rogers  const char* StartAssertNoThreadSuspension(const char* cause) {
20852673ffae0025d86f4023735581f19ebcc477487Ian Rogers    CHECK(cause != NULL);
20952673ffae0025d86f4023735581f19ebcc477487Ian Rogers    return NULL;
21052673ffae0025d86f4023735581f19ebcc477487Ian Rogers  }
21152673ffae0025d86f4023735581f19ebcc477487Ian Rogers#endif
21252673ffae0025d86f4023735581f19ebcc477487Ian Rogers
2130399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // End region where no thread suspension is expected.
2140399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers#ifndef NDEBUG
21552673ffae0025d86f4023735581f19ebcc477487Ian Rogers  void EndAssertNoThreadSuspension(const char* old_cause) {
21652673ffae0025d86f4023735581f19ebcc477487Ian Rogers    CHECK(old_cause != NULL || no_thread_suspension_ == 1);
21752673ffae0025d86f4023735581f19ebcc477487Ian Rogers    CHECK_GT(no_thread_suspension_, 0U);
2180399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    no_thread_suspension_--;
21952673ffae0025d86f4023735581f19ebcc477487Ian Rogers    last_no_thread_suspension_cause_ = old_cause;
22052673ffae0025d86f4023735581f19ebcc477487Ian Rogers  }
22152673ffae0025d86f4023735581f19ebcc477487Ian Rogers#else
22252673ffae0025d86f4023735581f19ebcc477487Ian Rogers  void EndAssertNoThreadSuspension(const char*) {
2230399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  }
22452673ffae0025d86f4023735581f19ebcc477487Ian Rogers#endif
2250399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
22600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
22700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  void AssertThreadSuspensionIsAllowable(bool check_locks = true) const;
2280399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
22900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  bool IsDaemon() const {
23000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return daemon_;
23100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
23200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
2332dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  bool HoldsLock(mirror::Object*);
2345f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
2358daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  /*
2368daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes   * Changes the priority of this thread to match that of the java.lang.Thread object.
2378daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes   *
2388daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes   * We map a priority value from 1-10 to Linux "nice" values, where lower
2398daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes   * numbers indicate higher priority.
2408daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes   */
2418daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  void SetNativePriority(int newPriority);
2428daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
2438daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  /*
2448daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes   * Returns the thread priority for the current thread by querying the system.
2458daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes   * This is useful when attaching a thread through JNI.
2468daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes   *
2478daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes   * Returns a value from 1 to 10 (compatible with java.lang.Thread values).
2488daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes   */
2498daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  static int GetNativePriority();
2508daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
251dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes  uint32_t GetThinLockId() const {
252dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes    return thin_lock_id_;
253b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro  }
254b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro
255d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes  pid_t GetTid() const {
256d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes    return tid_;
257d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes  }
258e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes
259ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes  // Returns the java.lang.Thread's name, or NULL if this Thread* doesn't have a peer.
2602dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::String* GetThreadName(const ScopedObjectAccessUnchecked& ts) const
261b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
262899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes
263ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes  // Sets 'name' to the java.lang.Thread's name. This requires no transition to managed code,
264ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes  // allocation, or locking.
265ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes  void GetThreadName(std::string& name) const;
266ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes
267899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes  // Sets the thread's name.
268b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  void SetThreadName(const char* name) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
269fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes
2702dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* GetPeer() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
271cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers    CHECK(jpeer_ == NULL);
272cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers    return opeer_;
2738daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  }
2748daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
27500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  bool HasPeer() const {
27680537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes    return jpeer_ != NULL || opeer_ != NULL;
27700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
27800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
2799d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  RuntimeStats* GetStats() {
2809d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    return &stats_;
2819d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  }
2829d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
2837dc5166ea740359d381097a7ab382c1dd404055fElliott Hughes  bool IsStillStarting() const;
2847dc5166ea740359d381097a7ab382c1dd404055fElliott Hughes
2850e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro  bool IsExceptionPending() const {
2861eab958cde39a7e2f0e5ce01730f4e2e75c72519jeffhao    return exception_ != NULL;
2870e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro  }
2880e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
28962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  mirror::Throwable* GetException(ThrowLocation* throw_location) const
29062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
29162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    if (throw_location != NULL) {
29262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      *throw_location = throw_location_;
29362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    }
2940e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro    return exception_;
2950e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro  }
2960e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
29700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  void AssertNoPendingException() const;
29800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
29962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  void SetException(const ThrowLocation& throw_location, mirror::Throwable* new_exception)
30062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3010cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers    CHECK(new_exception != NULL);
302474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers    // TODO: DCHECK(!IsExceptionPending());
303474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers    exception_ = new_exception;
30462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    throw_location_ = throw_location;
3050cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers  }
3060cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers
3070cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers  void ClearException() {
3080cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers    exception_ = NULL;
30962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    throw_location_.Clear();
31094d6df471a406a03bb1afba8ca3ae9c0fbf366b5jeffhao  }
31194d6df471a406a03bb1afba8ca3ae9c0fbf366b5jeffhao
312bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers  // Find catch block and perform long jump to appropriate exception handle
31394d6df471a406a03bb1afba8ca3ae9c0fbf366b5jeffhao  void QuickDeliverException() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
314bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers
315bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers  Context* GetLongJumpContext();
3160399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  void ReleaseLongJumpContext(Context* context) {
3170399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    DCHECK(long_jump_context_ == NULL);
3180399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    long_jump_context_ = context;
3191a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao  }
3201a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao
321ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* GetCurrentMethod(uint32_t* dex_pc) const
322b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
3230399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
32462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  ThrowLocation GetCurrentLocationForThrow() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
32562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
326bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers  void SetTopOfStack(void* stack, uintptr_t pc) {
327ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    mirror::ArtMethod** top_method = reinterpret_cast<mirror::ArtMethod**>(stack);
3280399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    managed_stack_.SetTopQuickFrame(top_method);
3290399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    managed_stack_.SetTopQuickFramePc(pc);
3300e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro  }
3310e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
33211ffc2d02b7d979f26a8a04013fce3eb4f9a2816Jeff Hao  void SetTopOfShadowStack(ShadowFrame* top) {
33311ffc2d02b7d979f26a8a04013fce3eb4f9a2816Jeff Hao    managed_stack_.SetTopShadowFrame(top);
33411ffc2d02b7d979f26a8a04013fce3eb4f9a2816Jeff Hao  }
33511ffc2d02b7d979f26a8a04013fce3eb4f9a2816Jeff Hao
3360399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  bool HasManagedStack() const {
3370399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    return managed_stack_.GetTopQuickFrame() != NULL || managed_stack_.GetTopShadowFrame() != NULL;
338bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers  }
339bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers
340a4f94740f9d66b21f4bcd3a225c434aa29cfa323Elliott Hughes  // If 'msg' is NULL, no detail message is set.
34162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  void ThrowNewException(const ThrowLocation& throw_location,
34262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers                         const char* exception_class_descriptor, const char* msg)
343b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
3445cb5ad27944efb08d4556b3c0d362302e37e832bElliott Hughes
345a4f94740f9d66b21f4bcd3a225c434aa29cfa323Elliott Hughes  // If 'msg' is NULL, no detail message is set. An exception must be pending, and will be
346a4f94740f9d66b21f4bcd3a225c434aa29cfa323Elliott Hughes  // used as the new exception's cause.
34762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  void ThrowNewWrappedException(const ThrowLocation& throw_location,
34862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers                                const char* exception_class_descriptor,
34962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers                                const char* msg)
350b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
351a4f94740f9d66b21f4bcd3a225c434aa29cfa323Elliott Hughes
35262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  void ThrowNewExceptionF(const ThrowLocation& throw_location,
35362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers                          const char* exception_class_descriptor, const char* fmt, ...)
35462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      __attribute__((format(printf, 4, 5)))
355b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
356a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes
35762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  void ThrowNewExceptionV(const ThrowLocation& throw_location,
35862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers                          const char* exception_class_descriptor, const char* fmt, va_list ap)
359b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
3604a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes
3612ced6a534157d5d963693346904389c19775d2daElliott Hughes  // OutOfMemoryError is special, because we need to pre-allocate an instance.
3628a8b9cbe158ee13477f2381f164c519762a06fefElliott Hughes  // Only the GC should call this.
363120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers  void ThrowOutOfMemoryError(const char* msg) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
36479082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughes
365be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  static void Startup();
366038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes  static void FinishStartup();
367c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes  static void Shutdown();
368b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro
369b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers  // JNI methods
37069f5bc6759f256a146eefd8a7141d39fcc3b0421Elliott Hughes  JNIEnvExt* GetJniEnv() const {
371b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers    return jni_env_;
372b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers  }
373b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers
374408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers  // Convert a jobject into a Object*
3752dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* DecodeJObject(jobject obj) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
376b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers
3778daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  // Implements java.lang.Thread.interrupted.
37881d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  bool Interrupted();
3798daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  // Implements java.lang.Thread.isInterrupted.
38081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  bool IsInterrupted();
38181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void Interrupt();
38281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void Notify();
3835f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
3842dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::ClassLoader* GetClassLoaderOverride() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
385c143c55718342519db5398e41dda31422cf16c79buzbee    return class_loader_override_;
386c143c55718342519db5398e41dda31422cf16c79buzbee  }
387c143c55718342519db5398e41dda31422cf16c79buzbee
3882dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  void SetClassLoaderOverride(mirror::ClassLoader* class_loader_override) {
389c143c55718342519db5398e41dda31422cf16c79buzbee    class_loader_override_ = class_loader_override;
390c143c55718342519db5398e41dda31422cf16c79buzbee  }
391c143c55718342519db5398e41dda31422cf16c79buzbee
392aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers  // Create the internal representation of a stack trace, that is more time
393aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers  // and space efficient to compute than the StackTraceElement[]
39464b6d145fa53b8dfb07a8fc2426af13f155d5a4dIan Rogers  jobject CreateInternalStackTrace(const ScopedObjectAccessUnchecked& soa) const
395b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
39601158d7a57c8321370667a6045220237d16e0da8Elliott Hughes
39701158d7a57c8321370667a6045220237d16e0da8Elliott Hughes  // Convert an internal stack trace representation (returned by CreateInternalStackTrace) to a
39801158d7a57c8321370667a6045220237d16e0da8Elliott Hughes  // StackTraceElement[]. If output_array is NULL, a new array is created, otherwise as many
39901158d7a57c8321370667a6045220237d16e0da8Elliott Hughes  // frames as will fit are written into the given array. If stack_depth is non-NULL, it's updated
40001158d7a57c8321370667a6045220237d16e0da8Elliott Hughes  // with the number of valid frames in the returned array.
40101158d7a57c8321370667a6045220237d16e0da8Elliott Hughes  static jobjectArray InternalStackTraceToStackTraceElementArray(JNIEnv* env, jobject internal,
40201158d7a57c8321370667a6045220237d16e0da8Elliott Hughes      jobjectArray output_array = NULL, int* stack_depth = NULL);
40355df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao
40462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  void VisitRoots(RootVisitor* visitor, void* arg) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
4056f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier
4062dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  void VerifyRoots(VerifyRootVisitor* visitor, void* arg)
4076f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
408410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes
409b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  void VerifyStack() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
410250455229aa0cc07bbd18174efe510bd52631a99jeffhao
411be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  //
412be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  // Offsets of various members of native Thread class, used by compiled code.
413be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  //
414be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes
415be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  static ThreadOffset SelfOffset() {
416be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes    return ThreadOffset(OFFSETOF_MEMBER(Thread, self_));
417be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  }
418be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes
419be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  static ThreadOffset ExceptionOffset() {
420be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes    return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_));
421be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  }
422be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes
42307ec8e17c978e3bda0202693e6432b56349a5867Ian Rogers  static ThreadOffset PeerOffset() {
42407ec8e17c978e3bda0202693e6432b56349a5867Ian Rogers    return ThreadOffset(OFFSETOF_MEMBER(Thread, opeer_));
42507ec8e17c978e3bda0202693e6432b56349a5867Ian Rogers  }
42607ec8e17c978e3bda0202693e6432b56349a5867Ian Rogers
42754e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes  static ThreadOffset ThinLockIdOffset() {
428be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes    return ThreadOffset(OFFSETOF_MEMBER(Thread, thin_lock_id_));
429be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  }
430be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes
431be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  static ThreadOffset CardTableOffset() {
432be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes    return ThreadOffset(OFFSETOF_MEMBER(Thread, card_table_));
433be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  }
434be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes
435474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers  static ThreadOffset ThreadFlagsOffset() {
436474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers    return ThreadOffset(OFFSETOF_MEMBER(Thread, state_and_flags_));
437be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  }
438be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes
439932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers  // Size of stack less any space reserved for stack overflow
440d752132c73072084a3def9257cca4fcee76047b6jeffhao  size_t GetStackSize() const {
44130fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    return stack_size_ - (stack_end_ - stack_begin_);
442932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers  }
443932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers
444d752132c73072084a3def9257cca4fcee76047b6jeffhao  byte* GetStackEnd() const {
445d752132c73072084a3def9257cca4fcee76047b6jeffhao    return stack_end_;
446d752132c73072084a3def9257cca4fcee76047b6jeffhao  }
447d752132c73072084a3def9257cca4fcee76047b6jeffhao
448932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers  // Set the stack end to that to be used during a stack overflow
449b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  void SetStackEndForStackOverflow() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
450932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers
451932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers  // Set the stack end to that to be used during regular execution
452932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers  void ResetDefaultStackEnd() {
453932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers    // Our stacks grow down, so we want stack_end_ to be near there, but reserving enough room
454932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers    // to throw a StackOverflowError.
45530fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    stack_end_ = stack_begin_ + kStackOverflowReservedBytes;
456932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers  }
457932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers
458120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers  bool IsHandlingStackOverflow() const {
459120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers    return stack_end_ == stack_begin_;
460120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers  }
461120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers
462449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes  static ThreadOffset StackEndOffset() {
463449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes    return ThreadOffset(OFFSETOF_MEMBER(Thread, stack_end_));
464be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  }
465be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes
466be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  static ThreadOffset JniEnvOffset() {
467be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes    return ThreadOffset(OFFSETOF_MEMBER(Thread, jni_env_));
468be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  }
469be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes
470be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  static ThreadOffset TopOfManagedStackOffset() {
4710399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    return ThreadOffset(OFFSETOF_MEMBER(Thread, managed_stack_) +
4720399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers                        ManagedStack::TopQuickFrameOffset());
473be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  }
474be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes
475bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers  static ThreadOffset TopOfManagedStackPcOffset() {
4760399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    return ThreadOffset(OFFSETOF_MEMBER(Thread, managed_stack_) +
4770399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers                        ManagedStack::TopQuickFramePcOffset());
4780399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  }
4790399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
4800399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  const ManagedStack* GetManagedStack() const {
4810399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    return &managed_stack_;
4820399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  }
4830399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
4840399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // Linked list recording fragments of managed stack.
4850399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  void PushManagedStackFragment(ManagedStack* fragment) {
4860399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    managed_stack_.PushManagedStackFragment(fragment);
4870399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  }
4880399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  void PopManagedStackFragment(const ManagedStack& fragment) {
4890399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    managed_stack_.PopManagedStackFragment(fragment);
490bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers  }
491bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers
4920399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  ShadowFrame* PushShadowFrame(ShadowFrame* new_top_frame) {
4930399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    return managed_stack_.PushShadowFrame(new_top_frame);
494de479be99328d2113bf483e082c9ecf235a34d69TDYa  }
495de479be99328d2113bf483e082c9ecf235a34d69TDYa
496de479be99328d2113bf483e082c9ecf235a34d69TDYa  ShadowFrame* PopShadowFrame() {
4970399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    return managed_stack_.PopShadowFrame();
498de479be99328d2113bf483e082c9ecf235a34d69TDYa  }
499f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien
500d668a06b5dcc3b0f7f788da4d756cd4ee6f1d0faTDYa  static ThreadOffset TopShadowFrameOffset() {
5010399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    return ThreadOffset(OFFSETOF_MEMBER(Thread, managed_stack_) +
5020399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers                        ManagedStack::TopShadowFrameOffset());
503d668a06b5dcc3b0f7f788da4d756cd4ee6f1d0faTDYa  }
504d668a06b5dcc3b0f7f788da4d756cd4ee6f1d0faTDYa
505ce4cc0d1818e872c1c7f3c3519a82259afd5c288TDYa  // Number of references allocated in JNI ShadowFrames on this thread
506ce4cc0d1818e872c1c7f3c3519a82259afd5c288TDYa  size_t NumJniShadowFrameReferences() const {
507ce4cc0d1818e872c1c7f3c3519a82259afd5c288TDYa    return managed_stack_.NumJniShadowFrameReferences();
5080399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  }
5090399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
5100399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // Number of references in SIRTs on this thread
5110399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  size_t NumSirtReferences();
5120399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
513ce4cc0d1818e872c1c7f3c3519a82259afd5c288TDYa  // Number of references allocated in SIRTs & JNI shadow frames on this thread
5140399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  size_t NumStackReferences() {
515ce4cc0d1818e872c1c7f3c3519a82259afd5c288TDYa    return NumSirtReferences() + NumJniShadowFrameReferences();
5160399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  };
5170399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
5180399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // Is the given obj in this thread's stack indirect reference table?
519cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers  bool SirtContains(jobject obj) const;
5200399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
5212dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  void SirtVisitRoots(RootVisitor* visitor, void* arg);
5220399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
5231f5393447b9f45be7918042d9ee7b521376de866Ian Rogers  void PushSirt(StackIndirectReferenceTable* sirt) {
5241f5393447b9f45be7918042d9ee7b521376de866Ian Rogers    sirt->SetLink(top_sirt_);
5251f5393447b9f45be7918042d9ee7b521376de866Ian Rogers    top_sirt_ = sirt;
5261f5393447b9f45be7918042d9ee7b521376de866Ian Rogers  }
5271f5393447b9f45be7918042d9ee7b521376de866Ian Rogers
5281f5393447b9f45be7918042d9ee7b521376de866Ian Rogers  StackIndirectReferenceTable* PopSirt() {
5291f5393447b9f45be7918042d9ee7b521376de866Ian Rogers    StackIndirectReferenceTable* sirt = top_sirt_;
5301f5393447b9f45be7918042d9ee7b521376de866Ian Rogers    DCHECK(sirt != NULL);
5311f5393447b9f45be7918042d9ee7b521376de866Ian Rogers    top_sirt_ = top_sirt_->GetLink();
5321f5393447b9f45be7918042d9ee7b521376de866Ian Rogers    return sirt;
5331f5393447b9f45be7918042d9ee7b521376de866Ian Rogers  }
53440381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom
535be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  static ThreadOffset TopSirtOffset() {
536be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes    return ThreadOffset(OFFSETOF_MEMBER(Thread, top_sirt_));
537be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  }
538be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes
539475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  DebugInvokeReq* GetInvokeReq() {
540475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes    return debug_invoke_req_;
541475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  }
542475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
54362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  void SetDeoptimizationShadowFrame(ShadowFrame* sf);
54462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  void SetDeoptimizationReturnValue(const JValue& ret_val);
545306057fd278d75bf3794bd5243a3b6652c487d18Ian Rogers
546306057fd278d75bf3794bd5243a3b6652c487d18Ian Rogers  ShadowFrame* GetAndClearDeoptimizationShadowFrame(JValue* ret_val);
547306057fd278d75bf3794bd5243a3b6652c487d18Ian Rogers
54862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  std::deque<instrumentation::InstrumentationStackFrame>* GetInstrumentationStack() {
549725a957985171d712d5c048cc3d00ff14968784bjeffhao    return instrumentation_stack_;
550e343b76af81a005ef64f5e75a555389fd9147dabjeffhao  }
551e343b76af81a005ef64f5e75a555389fd9147dabjeffhao
55281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  BaseMutex* GetHeldMutex(LockLevel level) const {
55300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return held_mutexes_[level];
55400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
55500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
55681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void SetHeldMutex(LockLevel level, BaseMutex* mutex) {
55700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    held_mutexes_[level] = mutex;
55800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
559ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes
560752a0e606afdc7aa4825ebbe62b187b8abba14ccMathieu Chartier  void RunCheckpointFunction();
561858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier
562858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier  bool ReadFlag(ThreadFlag flag) const {
563858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier    return (state_and_flags_.as_struct.flags & flag) != 0;
564858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier  }
565858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier
5669cec247849c248f6c45a14eae5414b69eb8fc67dJeff Hao  bool TestAllFlags() const {
5679cec247849c248f6c45a14eae5414b69eb8fc67dJeff Hao    return (state_and_flags_.as_struct.flags != 0);
5689cec247849c248f6c45a14eae5414b69eb8fc67dJeff Hao  }
5699cec247849c248f6c45a14eae5414b69eb8fc67dJeff Hao
570858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier  void AtomicSetFlag(ThreadFlag flag);
571858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier
572858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier  void AtomicClearFlag(ThreadFlag flag);
573858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier
5740e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro private:
5755d96a7168dd3e2a4acf1a947ef12efa8f82b95c0Elliott Hughes  // We have no control over the size of 'bool', but want our boolean fields
5765d96a7168dd3e2a4acf1a947ef12efa8f82b95c0Elliott Hughes  // to be 4-byte quantities.
5775d96a7168dd3e2a4acf1a947ef12efa8f82b95c0Elliott Hughes  typedef uint32_t bool32_t;
5785d96a7168dd3e2a4acf1a947ef12efa8f82b95c0Elliott Hughes
57952673ffae0025d86f4023735581f19ebcc477487Ian Rogers  explicit Thread(bool daemon);
580b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  ~Thread() LOCKS_EXCLUDED(Locks::mutator_lock_,
581b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers                           Locks::thread_suspend_count_lock_);
582c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  void Destroy();
583c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  friend class ThreadList;  // For ~Thread and Destroy.
5840e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
585365c10235438607541fa2259a5fec48061b90bd8Ian Rogers  void CreatePeer(const char* name, bool as_daemon, jobject thread_group);
5867934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  friend class Runtime;  // For CreatePeer.
5875fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes
58862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  // Avoid use, callers should use SetState. Used only by SignalCatcher::HandleSigQuit, ~Thread and
58962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  // Dbg::Disconnected.
590474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers  ThreadState SetStateUnsafe(ThreadState new_state) {
591474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers    ThreadState old_state = GetState();
59230e173f6da202e4caa37ceb477e2fbbe09cec990Ian Rogers    state_and_flags_.as_struct.state = new_state;
593c747cffa950b8398cb8844d47e2253214c855750Ian Rogers    return old_state;
594c747cffa950b8398cb8844d47e2253214c855750Ian Rogers  }
595474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers  friend class SignalCatcher;  // For SetStateUnsafe.
5967934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  friend class Dbg;  // F or SetStateUnsafe.
597c747cffa950b8398cb8844d47e2253214c855750Ian Rogers
59804d7aa92bc5548bc4d272b9480614f06248194ccIan Rogers  void VerifyStackImpl() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
59904d7aa92bc5548bc4d272b9480614f06248194ccIan Rogers
600cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers  void DumpState(std::ostream& os) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
60100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  void DumpStack(std::ostream& os) const
602b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
603b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
604d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes
605accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes  // Out-of-line conveniences for debugging in gdb.
6067934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  static Thread* CurrentFromGdb();  // Like Thread::Current.
60700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Like Thread::Dump(std::cerr).
608b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  void DumpFromGdb() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
609accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes
61093e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes  static void* CreateCallback(void* arg);
61193e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes
612cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers  void HandleUncaughtExceptions(ScopedObjectAccess& soa)
613cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
614cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers  void RemoveFromThreadGroup(ScopedObjectAccess& soa) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
615accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes
616120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers  void Init(ThreadList*, JavaVMExt*) EXCLUSIVE_LOCKS_REQUIRED(Locks::runtime_shutdown_lock_);
6175d76c435082332ef79a22962386fa92a0870e378Ian Rogers  void InitCardTable();
618b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers  void InitCpu();
619848871b4d8481229c32e0d048a9856e5a9a17ef9Ian Rogers  void InitTlsEntryPoints();
620caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom  void InitTid();
621caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom  void InitPthreadKeySelf();
622be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  void InitStackHwm();
623be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes
624d6a23bd327c38b08aaf6846d426fd6824fe9780bElliott Hughes  void SetUpAlternateSignalStack();
625d6a23bd327c38b08aaf6846d426fd6824fe9780bElliott Hughes  void TearDownAlternateSignalStack();
626d6a23bd327c38b08aaf6846d426fd6824fe9780bElliott Hughes
627c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  void NotifyLocked(Thread* self) EXCLUSIVE_LOCKS_REQUIRED(wait_mutex_);
6285f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
629be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  static void ThreadExitCallback(void* arg);
630b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers
6310878d654e7be8c9666579e22522704d8887415ccIan Rogers  // Has Thread::Startup been called?
6320878d654e7be8c9666579e22522704d8887415ccIan Rogers  static bool is_started_;
6330878d654e7be8c9666579e22522704d8887415ccIan Rogers
6340399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // TLS key used to retrieve the Thread*.
6350399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  static pthread_key_t pthread_key_self_;
6360399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
63700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Used to notify threads that they should attempt to resume, they will suspend again if
63800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // their suspend count is > 0.
639c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  static ConditionVariable* resume_cond_ GUARDED_BY(Locks::thread_suspend_count_lock_);
64000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
6410399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // --- Frequently accessed fields first for short offsets ---
6420399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
643474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers  // 32 bits of atomically changed state and flags. Keeping as 32 bits allows and atomic CAS to
644474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers  // change from being Suspended to Runnable without a suspend request occurring.
64530e173f6da202e4caa37ceb477e2fbbe09cec990Ian Rogers  union StateAndFlags {
646df1ce91ba97bc79a0637e5504b39318fb1c9f577Ian Rogers    struct PACKED(4) {
64730e173f6da202e4caa37ceb477e2fbbe09cec990Ian Rogers      // Bitfield of flag values. Must be changed atomically so that flag values aren't lost. See
64830e173f6da202e4caa37ceb477e2fbbe09cec990Ian Rogers      // ThreadFlags for bit field meanings.
64930e173f6da202e4caa37ceb477e2fbbe09cec990Ian Rogers      volatile uint16_t flags;
65030e173f6da202e4caa37ceb477e2fbbe09cec990Ian Rogers      // Holds the ThreadState. May be changed non-atomically between Suspended (ie not Runnable)
65130e173f6da202e4caa37ceb477e2fbbe09cec990Ian Rogers      // transitions. Changing to Runnable requires that the suspend_request be part of the atomic
65230e173f6da202e4caa37ceb477e2fbbe09cec990Ian Rogers      // operation. If a thread is suspended and a suspend_request is present, a thread may not
65330e173f6da202e4caa37ceb477e2fbbe09cec990Ian Rogers      // change to Runnable as a GC or other operation is in progress.
65401ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers      volatile uint16_t state;
65530e173f6da202e4caa37ceb477e2fbbe09cec990Ian Rogers    } as_struct;
65601ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers    volatile int32_t as_int;
657474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers  };
65830e173f6da202e4caa37ceb477e2fbbe09cec990Ian Rogers  union StateAndFlags state_and_flags_;
65930e173f6da202e4caa37ceb477e2fbbe09cec990Ian Rogers  COMPILE_ASSERT(sizeof(union StateAndFlags) == sizeof(int32_t),
660474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers                 sizeof_state_and_flags_and_int32_are_different);
661474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers
6620399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // A non-zero value is used to tell the current thread to enter a safe point
6630399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // at the next poll.
664b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  int suspend_count_ GUARDED_BY(Locks::thread_suspend_count_lock_);
6650399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
6660399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // The biased card table, see CardTable for details
6670399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  byte* card_table_;
6680399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
6690399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // The pending exception or NULL.
6702dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Throwable* exception_;
6710399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
6720399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // The end of this thread's stack. This is the lowest safely-addressable address on the stack.
6730399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // We leave extra space so there's room for the code that throws StackOverflowError.
6740399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  byte* stack_end_;
6750399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
6760399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // The top of the managed stack often manipulated directly by compiler generated code.
6770399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  ManagedStack managed_stack_;
6780399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
6790399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // Every thread may have an associated JNI environment
6800399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  JNIEnvExt* jni_env_;
6810399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
6820399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // Initialized to "this". On certain architectures (such as x86) reading
6830399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // off of Thread::Current is easy but getting the address of Thread::Current
6840399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // is hard. This field can be read off of Thread::Current to give the address.
6850399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  Thread* self_;
6860399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
687cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers  // Our managed peer (an instance of java.lang.Thread). The jobject version is used during thread
688cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers  // start up, until the thread is registered and the local opeer_ is used.
6892dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* opeer_;
690cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers  jobject jpeer_;
6910399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
6920399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // The "lowest addressable byte" of the stack
6930399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  byte* stack_begin_;
6940399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
6950399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // Size of the stack
6960399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  size_t stack_size_;
6970399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
698dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes  // Thin lock thread id. This is a small integer used by the thin lock implementation.
699dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes  // This is not to be confused with the native thread's tid, nor is it the value returned
700dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes  // by java.lang.Thread.getId --- this is a distinct value, used only for locking. One
701dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes  // important difference between this id and the ids visible to managed code is that these
702dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes  // ones get reused (to ensure that they fit in the number of bits available).
703dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes  uint32_t thin_lock_id_;
704b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers
705d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes  // System thread id.
706d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes  pid_t tid_;
707d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes
70862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  ThrowLocation throw_location_;
70962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
7108daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  // Guards the 'interrupted_' and 'wait_monitor_' members.
71100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  mutable Mutex* wait_mutex_ DEFAULT_MUTEX_ACQUIRED_AFTER;
71200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ConditionVariable* wait_cond_ GUARDED_BY(wait_mutex_);
71300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Pointer to the monitor lock we're currently waiting on (or NULL).
71400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  Monitor* wait_monitor_ GUARDED_BY(wait_mutex_);
71500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Thread "interrupted" status; stays raised until queried or thrown.
71600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  bool32_t interrupted_ GUARDED_BY(wait_mutex_);
7175f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  // The next thread in the wait set this thread is part of.
7185f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  Thread* wait_next_;
7198e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes  // If we're blocked in MonitorEnter, this is the object we're trying to lock.
7202dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* monitor_enter_object_;
7215f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
7225f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  friend class Monitor;
723f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  friend class MonitorInfo;
724dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes
725408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers  // Top of linked list of stack indirect reference tables or NULL for none
726408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers  StackIndirectReferenceTable* top_sirt_;
727b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers
72869759eaa6fd4386f1e6d8748052ad221087b3476Carl Shapiro  Runtime* runtime_;
72969759eaa6fd4386f1e6d8748052ad221087b3476Carl Shapiro
7300399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  RuntimeStats stats_;
73145a76cb99104a222d6a9bd768a084893dcb7cf30Ian Rogers
732edcc09c737b00462881f147602656739d029571eElliott Hughes  // Needed to get the right ClassLoader in JNI_OnLoad, but also
733edcc09c737b00462881f147602656739d029571eElliott Hughes  // useful for testing.
7342dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::ClassLoader* class_loader_override_;
735c143c55718342519db5398e41dda31422cf16c79buzbee
736bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers  // Thread local, lazily allocated, long jump context. Used to deliver exceptions.
73785d1545e985ac689db4bad7849880e843707c862Elliott Hughes  Context* long_jump_context_;
738bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers
739418dfe7849f45535b5388a91bd7a16cfc20a612bElliott Hughes  // A boolean telling us whether we're recursively throwing OOME.
7405d96a7168dd3e2a4acf1a947ef12efa8f82b95c0Elliott Hughes  bool32_t throwing_OutOfMemoryError_;
741726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes
7420399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // How much of 'suspend_count_' is by request of the debugger, used to set things right
7430399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // when the debugger detaches. Must be <= suspend_count_.
744b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  int debug_suspend_count_ GUARDED_BY(Locks::thread_suspend_count_lock_);
7450399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
746475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  // JDWP invoke-during-breakpoint support.
747475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  DebugInvokeReq* debug_invoke_req_;
748475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
749306057fd278d75bf3794bd5243a3b6652c487d18Ian Rogers  // Shadow frame that is used temporarily during the deoptimization of a method.
750306057fd278d75bf3794bd5243a3b6652c487d18Ian Rogers  ShadowFrame* deoptimization_shadow_frame_;
751306057fd278d75bf3794bd5243a3b6652c487d18Ian Rogers  JValue deoptimization_return_value_;
752306057fd278d75bf3794bd5243a3b6652c487d18Ian Rogers
753725a957985171d712d5c048cc3d00ff14968784bjeffhao  // Additional stack used by method instrumentation to store method and return pc values.
754306057fd278d75bf3794bd5243a3b6652c487d18Ian Rogers  // Stored as a pointer since std::deque is not PACKED.
75562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  std::deque<instrumentation::InstrumentationStackFrame>* instrumentation_stack_;
756e343b76af81a005ef64f5e75a555389fd9147dabjeffhao
757899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes  // A cached copy of the java.lang.Thread's name.
758899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes  std::string* name_;
759899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes
76052673ffae0025d86f4023735581f19ebcc477487Ian Rogers  // Is the thread a daemon?
7615d96a7168dd3e2a4acf1a947ef12efa8f82b95c0Elliott Hughes  const bool32_t daemon_;
76252673ffae0025d86f4023735581f19ebcc477487Ian Rogers
7630d39c12238499ca9ccc34d1532c443335e7c1044Elliott Hughes  // A cached pthread_t for the pthread underlying this Thread*.
7640d39c12238499ca9ccc34d1532c443335e7c1044Elliott Hughes  pthread_t pthread_self_;
7650d39c12238499ca9ccc34d1532c443335e7c1044Elliott Hughes
76600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Support for Mutex lock hierarchy bug detection.
7670f827169742aad6209d830db773a101849c32a83Elliott Hughes  BaseMutex* held_mutexes_[kLockLevelCount];
768ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes
7690399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // A positive value implies we're in a region where thread suspension isn't expected.
7700399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  uint32_t no_thread_suspension_;
77152673ffae0025d86f4023735581f19ebcc477487Ian Rogers
77252673ffae0025d86f4023735581f19ebcc477487Ian Rogers  // Cause for last suspension.
77352673ffae0025d86f4023735581f19ebcc477487Ian Rogers  const char* last_no_thread_suspension_cause_;
7745d96a7168dd3e2a4acf1a947ef12efa8f82b95c0Elliott Hughes
775858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier  // Pending checkpoint functions.
7760e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartier  Closure* checkpoint_function_;
777858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier
778a32a6fd4a781262dff4fec102da053d16b7ef6c0Ian Rogers public:
7797655f29fabc0a12765de828914a18314382e5a35Ian Rogers  // Entrypoint function pointers
7806a607ad0902f3b8478e95d0b6b3e63a538571a3fElliott Hughes  // TODO: move this near the top, since changing its offset requires all oats to be recompiled!
781848871b4d8481229c32e0d048a9856e5a9a17ef9Ian Rogers  InterpreterEntryPoints interpreter_entrypoints_;
782848871b4d8481229c32e0d048a9856e5a9a17ef9Ian Rogers  JniEntryPoints jni_entrypoints_;
7837655f29fabc0a12765de828914a18314382e5a35Ian Rogers  PortableEntryPoints portable_entrypoints_;
784848871b4d8481229c32e0d048a9856e5a9a17ef9Ian Rogers  QuickEntryPoints quick_entrypoints_;
785a32a6fd4a781262dff4fec102da053d16b7ef6c0Ian Rogers
786a32a6fd4a781262dff4fec102da053d16b7ef6c0Ian Rogers private:
7876a607ad0902f3b8478e95d0b6b3e63a538571a3fElliott Hughes  // How many times has our pthread key's destructor been called?
7886a607ad0902f3b8478e95d0b6b3e63a538571a3fElliott Hughes  uint32_t thread_exit_check_count_;
7896a607ad0902f3b8478e95d0b6b3e63a538571a3fElliott Hughes
79022f454c7d349c439c774c5d79b32514b190fd531Ian Rogers  friend class ScopedThreadStateChange;
79122f454c7d349c439c774c5d79b32514b190fd531Ian Rogers
7920e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro  DISALLOW_COPY_AND_ASSIGN(Thread);
7930e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro};
794bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers
795330304de14dc7118b45b8e7b5bd11a172fa61701Elliott Hughesstd::ostream& operator<<(std::ostream& os, const Thread& thread);
79634e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughesstd::ostream& operator<<(std::ostream& os, const ThreadState& state);
7970e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
7980e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro}  // namespace art
7990e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
800fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#endif  // ART_RUNTIME_THREAD_H_
801