thread.h revision 474b6da273c7ce6df50a4e51eb9929a77e1611c3
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
170e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro#ifndef ART_SRC_THREAD_H_
180e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro#define ART_SRC_THREAD_H_
190e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
20b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro#include <pthread.h>
21a09576416788b916095739e43a16917e7948f3a4Elliott Hughes
2202b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes#include <bitset>
23a09576416788b916095739e43a16917e7948f3a4Elliott Hughes#include <iosfwd>
24b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers#include <list>
258daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes#include <string>
26e343b76af81a005ef64f5e75a555389fd9147dabjeffhao#include <vector>
27b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro
281f87008b165d26541d832ff805250afdc89c253dBrian Carlstrom#include "dex_file.h"
29578bbdc684db8ed68e9fedbc678669d27fa68b6eBrian Carlstrom#include "globals.h"
3069f5bc6759f256a146eefd8a7141d39fcc3b0421Elliott Hughes#include "jni_internal.h"
31578bbdc684db8ed68e9fedbc678669d27fa68b6eBrian Carlstrom#include "logging.h"
32578bbdc684db8ed68e9fedbc678669d27fa68b6eBrian Carlstrom#include "macros.h"
338daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes#include "mutex.h"
34b765be0d656c3073402693aeaf64e95a0e49f218Brian Carlstrom#include "mem_map.h"
3557b86d47b66322693a070185fadfb43cb9c12eabIan Rogers#include "oat/runtime/oat_support_entrypoints.h"
36578bbdc684db8ed68e9fedbc678669d27fa68b6eBrian Carlstrom#include "offsets.h"
379d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes#include "runtime_stats.h"
3868e76526e98432625464022cb26f66b9ef6f5af4Elliott Hughes#include "stack.h"
39e343b76af81a005ef64f5e75a555389fd9147dabjeffhao#include "trace.h"
40bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers#include "UniquePtr.h"
4121d28f510eb590f52810c83f1f3f37fe5f4adf46Shih-wei Liao#ifdef ART_USE_GREENLAND_COMPILER
4221d28f510eb590f52810c83f1f3f37fe5f4adf46Shih-wei Liao#include "greenland/runtime_entry_points.h"
4321d28f510eb590f52810c83f1f3f37fe5f4adf46Shih-wei Liao#endif
44b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers
450e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapironamespace art {
460e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
4769f5bc6759f256a146eefd8a7141d39fcc3b0421Elliott Hughesclass Array;
4837f7a40f6789bb287f287a9af00777af9d6428eeElliott Hughesclass Class;
491f87008b165d26541d832ff805250afdc89c253dBrian Carlstromclass ClassLinker;
50edcc09c737b00462881f147602656739d029571eElliott Hughesclass ClassLoader;
51bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogersclass Context;
521b09b094a85e03f6ef5f687f58bb91c433273ba1Ian Rogersstruct DebugInvokeReq;
5366f19258f9728d4ffe026074d8fd429d639802faMathieu Chartierclass AbstractMethod;
548daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughesclass Monitor;
550e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiroclass Object;
56b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiroclass Runtime;
5700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersclass ScopedObjectAccess;
5800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersclass ScopedObjectAccessUnchecked;
59f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chienclass ShadowFrame;
6068e76526e98432625464022cb26f66b9ef6f5af4Elliott Hughesclass StackIndirectReferenceTable;
6155df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liaoclass StackTraceElement;
621da522de18ac6e4c2913c3233529e9dd115059f8buzbeeclass StaticStorageBase;
6340381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstromclass Thread;
6440381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstromclass ThreadList;
6540381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstromclass Throwable;
661da522de18ac6e4c2913c3233529e9dd115059f8buzbee
6755df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liaotemplate<class T> class ObjectArray;
684417536522fd2a9d8215d8672331984769c9520bShih-wei Liaotemplate<class T> class PrimitiveArray;
694417536522fd2a9d8215d8672331984769c9520bShih-wei Liaotypedef PrimitiveArray<int32_t> IntArray;
700e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
7134e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes// Thread priorities. These must match the Thread.MIN_PRIORITY,
7234e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes// Thread.NORM_PRIORITY, and Thread.MAX_PRIORITY constants.
7334e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughesenum ThreadPriority {
7434e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes  kMinThreadPriority = 1,
7534e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes  kNormThreadPriority = 5,
7634e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes  kMaxThreadPriority = 10,
7734e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes};
78fc1d9f581592d54cc14240b9909824af38656931Elliott Hughes
7934e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughesenum ThreadState {
8000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  kTerminated                     = 0,   // Thread.TERMINATED     JDWP TS_ZOMBIE
8100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  kRunnable                       = 1,   // Thread.RUNNABLE       JDWP TS_RUNNING
8200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  kTimedWaiting                   = 2,   // Thread.TIMED_WAITING  JDWP TS_WAIT    - in Object.wait() with a timeout
8300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  kBlocked                        = 3,   // Thread.BLOCKED        JDWP TS_MONITOR - blocked on a monitor
8400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  kWaiting                        = 4,   // Thread.WAITING        JDWP TS_WAIT    - in Object.wait()
8500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  kWaitingForGcToComplete         = 5,   // Thread.WAITING        JDWP TS_WAIT    - blocked waiting for GC
8600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  kWaitingPerformingGc            = 6,   // Thread.WAITING        JDWP TS_WAIT    - performing GC
8700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  kWaitingForDebuggerSend         = 7,   // Thread.WAITING        JDWP TS_WAIT    - blocked waiting for events to be sent
8800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  kWaitingForDebuggerToAttach     = 8,   // Thread.WAITING        JDWP TS_WAIT    - blocked waiting for debugger to attach
8900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  kWaitingInMainDebuggerLoop      = 9,   // Thread.WAITING        JDWP TS_WAIT    - blocking/reading/processing debugger events
9000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  kWaitingForDebuggerSuspension   = 10,  // Thread.WAITING        JDWP TS_WAIT    - waiting for debugger suspend all
9100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  kWaitingForJniOnLoad            = 11,  // Thread.WAITING        JDWP TS_WAIT    - waiting for execution of dlopen and JNI on load code
9200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  kWaitingForSignalCatcherOutput  = 12,  // Thread.WAITING        JDWP TS_WAIT    - waiting for signal catcher IO to complete
9300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  kWaitingInMainSignalCatcherLoop = 13,  // Thread.WAITING        JDWP TS_WAIT    - blocking/reading/processing signals
9400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  kStarting                       = 14,  // Thread.NEW            JDWP TS_WAIT    - native thread started, not yet ready to run managed code
9500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  kNative                         = 15,  // Thread.RUNNABLE       JDWP TS_RUNNING - running in a JNI native method
9600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  kSuspended                      = 16,  // Thread.RUNNABLE       JDWP TS_RUNNING - suspended by GC or debugger
9734e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes};
98b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro
99474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogersenum ThreadFlag {
100474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers  kSuspendRequest   = 1,  // If set implies that suspend_count_ > 0.
101474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers  kExceptionPending = 2,  // If set implies that exception_ != NULL.
102474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers  kEnterInterpreter = 4,  // Instruct managed code it should enter the interpreter.
103474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers};
104474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers
10534e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughesclass PACKED Thread {
10634e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes public:
107932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers  // Space to throw a StackOverflowError in.
1084165a83d250165c839850651e1b2a69e06128000TDYa#if !defined(ART_USE_LLVM_COMPILER)
109aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  static const size_t kStackOverflowReservedBytes = 4 * KB;
1104165a83d250165c839850651e1b2a69e06128000TDYa#else  // LLVM_x86 requires more memory to throw stack overflow exception.
1114165a83d250165c839850651e1b2a69e06128000TDYa  static const size_t kStackOverflowReservedBytes = 8 * KB;
1124165a83d250165c839850651e1b2a69e06128000TDYa#endif
113c143c55718342519db5398e41dda31422cf16c79buzbee
114462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes  // Creates a new native thread corresponding to the given managed peer.
115462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes  // Used to implement Thread.start.
11652673ffae0025d86f4023735581f19ebcc477487Ian Rogers  static void CreateNativeThread(JNIEnv* env, jobject peer, size_t stack_size, bool daemon);
11761e019d291583029c01b61b93bea750f2b663c37Carl Shapiro
118462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes  // Attaches the calling native thread to the runtime, returning the new native peer.
119462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes  // Used to implement JNI AttachCurrentThread and AttachCurrentThreadAsDaemon calls.
120365c10235438607541fa2259a5fec48061b90bd8Ian Rogers  static Thread* Attach(const char* thread_name, bool as_daemon, jobject thread_group);
121b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro
122caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom  // Reset internal state of child thread after fork.
123caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom  void InitAfterFork();
124caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom
1250399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  static Thread* Current() __attribute__ ((pure)) {
126e0447357fa8dece635c213d360cd03160cc2d8a0Elliott Hughes    // We rely on Thread::Current returning NULL for a detached thread, so it's not obvious
127e0447357fa8dece635c213d360cd03160cc2d8a0Elliott Hughes    // that we can replace this with a direct %fs access on x86.
128d0e7e777c412071a911dd2c008b20b7d879fd392Carl Shapiro    void* thread = pthread_getspecific(Thread::pthread_key_self_);
129d0e7e777c412071a911dd2c008b20b7d879fd392Carl Shapiro    return reinterpret_cast<Thread*>(thread);
1300e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro  }
1310e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
13200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  static Thread* FromManagedThread(const ScopedObjectAccessUnchecked& ts, Object* thread_peer)
133b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
134b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
13500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  static Thread* FromManagedThread(const ScopedObjectAccessUnchecked& ts, jobject thread)
136b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
137b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1388daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
13928fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes  // Translates 172 to pAllocArrayFromCode and so on.
14028fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes  static void DumpThreadOffset(std::ostream& os, uint32_t offset, size_t size_of_pointers);
14128fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes
14200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Dumps a one-line summary of thread state (used for operator<<).
14300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  void ShortDump(std::ostream& os) const;
14400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
14500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Dumps the detailed thread state and the thread stack (used for SIGQUIT).
14600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  void Dump(std::ostream& os) const
147b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
148b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
149a09576416788b916095739e43a16917e7948f3a4Elliott Hughes
150abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes  // Dumps the SIGQUIT per-thread header. 'thread' can be NULL for a non-attached thread, in which
151abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes  // case we use 'tid' to identify the thread, and we'll include as much information as we can.
15200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  static void DumpState(std::ostream& os, const Thread* thread, pid_t tid)
153b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_);
154abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes
155474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers  ThreadState GetState() const {
156474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers    return static_cast<ThreadState>(state_and_flags_.state);
1570cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers  }
1580cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers
159474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers  ThreadState SetState(ThreadState new_state);
1608d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes
16100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  int GetSuspendCount() const
162b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_suspend_count_lock_) {
163b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    Locks::thread_suspend_count_lock_->AssertHeld();
16400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return suspend_count_;
16552673ffae0025d86f4023735581f19ebcc477487Ian Rogers  }
16652673ffae0025d86f4023735581f19ebcc477487Ian Rogers
16700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  int GetDebugSuspendCount() const
168b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_suspend_count_lock_) {
169b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    Locks::thread_suspend_count_lock_->AssertHeld();
17000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return debug_suspend_count_;
17100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
17200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
17300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  bool IsSuspended() const
174b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_suspend_count_lock_) {
17500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    int suspend_count = GetSuspendCount();
17600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return suspend_count != 0 && GetState() != kRunnable;
17700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
17800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
17900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  void ModifySuspendCount(int delta, bool for_debugger)
180b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_suspend_count_lock_);
18100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
18200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Called when thread detected that the thread_suspend_count_ was non-zero. Gives up share of
18300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // mutator_lock_ and waits until it is resumed and thread_suspend_count_ is zero.
1849da7f59c9059397182b9a97e898a42ec06d4d646Ian Rogers  void FullSuspendCheck()
185b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
186b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
18700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
18800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Transition from non-runnable to runnable state acquiring share on mutator_lock_.
18900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ThreadState TransitionFromSuspendedToRunnable()
190b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
191b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCK_FUNCTION(Locks::mutator_lock_);
19200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
19300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Transition from runnable into a state where mutator privileges are denied. Releases share of
19400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // mutator lock.
19500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  void TransitionFromRunnableToSuspended(ThreadState new_state)
196b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
197b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      UNLOCK_FUNCTION(Locks::mutator_lock_);
198038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes
19900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Wait for a debugger suspension on the thread associated with the given peer. Returns the
20000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // thread on success, else NULL. If the thread should be suspended then request_suspension should
20100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // be true on entry. If the suspension times out then *timeout is set to true.
20200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  static Thread* SuspendForDebugger(jobject peer,  bool request_suspension, bool* timeout)
203b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      LOCKS_EXCLUDED(Locks::mutator_lock_,
204b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers                     Locks::thread_list_lock_,
205b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers                     Locks::thread_suspend_count_lock_);
2060cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers
2070399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // Once called thread suspension will cause an assertion failure.
2080399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers#ifndef NDEBUG
20952673ffae0025d86f4023735581f19ebcc477487Ian Rogers  const char* StartAssertNoThreadSuspension(const char* cause) {
21052673ffae0025d86f4023735581f19ebcc477487Ian Rogers    CHECK(cause != NULL);
21152673ffae0025d86f4023735581f19ebcc477487Ian Rogers    const char* previous_cause = last_no_thread_suspension_cause_;
2120399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    no_thread_suspension_++;
21352673ffae0025d86f4023735581f19ebcc477487Ian Rogers    last_no_thread_suspension_cause_ = cause;
21452673ffae0025d86f4023735581f19ebcc477487Ian Rogers    return previous_cause;
2150399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  }
21652673ffae0025d86f4023735581f19ebcc477487Ian Rogers#else
21752673ffae0025d86f4023735581f19ebcc477487Ian Rogers  const char* StartAssertNoThreadSuspension(const char* cause) {
21852673ffae0025d86f4023735581f19ebcc477487Ian Rogers    CHECK(cause != NULL);
21952673ffae0025d86f4023735581f19ebcc477487Ian Rogers    return NULL;
22052673ffae0025d86f4023735581f19ebcc477487Ian Rogers  }
22152673ffae0025d86f4023735581f19ebcc477487Ian Rogers#endif
22252673ffae0025d86f4023735581f19ebcc477487Ian Rogers
2230399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // End region where no thread suspension is expected.
2240399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers#ifndef NDEBUG
22552673ffae0025d86f4023735581f19ebcc477487Ian Rogers  void EndAssertNoThreadSuspension(const char* old_cause) {
22652673ffae0025d86f4023735581f19ebcc477487Ian Rogers    CHECK(old_cause != NULL || no_thread_suspension_ == 1);
22752673ffae0025d86f4023735581f19ebcc477487Ian Rogers    CHECK_GT(no_thread_suspension_, 0U);
2280399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    no_thread_suspension_--;
22952673ffae0025d86f4023735581f19ebcc477487Ian Rogers    last_no_thread_suspension_cause_ = old_cause;
23052673ffae0025d86f4023735581f19ebcc477487Ian Rogers  }
23152673ffae0025d86f4023735581f19ebcc477487Ian Rogers#else
23252673ffae0025d86f4023735581f19ebcc477487Ian Rogers  void EndAssertNoThreadSuspension(const char*) {
2330399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  }
23452673ffae0025d86f4023735581f19ebcc477487Ian Rogers#endif
2350399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
23600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
23700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#ifndef NDEBUG
23800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  void AssertThreadSuspensionIsAllowable(bool check_locks = true) const;
23900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#else
24000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  void AssertThreadSuspensionIsAllowable(bool check_locks = true) const {
241e0f0cb3d855cb5e926452b5e1ec8457adc4e454eMathieu Chartier    UNUSED(check_locks);  // Keep GCC happy about unused parameters.
2420399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  }
24300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#endif
2440399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
24500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  bool IsDaemon() const {
24600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return daemon_;
24700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
24800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
2495f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  bool HoldsLock(Object*);
2505f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
2518daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  /*
2528daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes   * Changes the priority of this thread to match that of the java.lang.Thread object.
2538daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes   *
2548daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes   * We map a priority value from 1-10 to Linux "nice" values, where lower
2558daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes   * numbers indicate higher priority.
2568daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes   */
2578daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  void SetNativePriority(int newPriority);
2588daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
2598daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  /*
2608daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes   * Returns the thread priority for the current thread by querying the system.
2618daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes   * This is useful when attaching a thread through JNI.
2628daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes   *
2638daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes   * Returns a value from 1 to 10 (compatible with java.lang.Thread values).
2648daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes   */
2658daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  static int GetNativePriority();
2668daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
267dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes  uint32_t GetThinLockId() const {
268dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes    return thin_lock_id_;
269b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro  }
270b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro
271d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes  pid_t GetTid() const {
272d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes    return tid_;
273d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes  }
274e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes
275ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes  // Returns the java.lang.Thread's name, or NULL if this Thread* doesn't have a peer.
27600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  String* GetThreadName(const ScopedObjectAccessUnchecked& ts) const
277b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
278899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes
279ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes  // Sets 'name' to the java.lang.Thread's name. This requires no transition to managed code,
280ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes  // allocation, or locking.
281ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes  void GetThreadName(std::string& name) const;
282ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes
283899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes  // Sets the thread's name.
284b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  void SetThreadName(const char* name) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
285fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes
286b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  Object* GetPeer() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2878daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes    return peer_;
2888daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  }
2898daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
29000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  bool HasPeer() const {
29100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return peer_ != NULL;
29200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
29300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
29400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  Object* GetThreadGroup(const ScopedObjectAccessUnchecked& ts) const
295b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
296a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
2979d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  RuntimeStats* GetStats() {
2989d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    return &stats_;
2999d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  }
3009d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
3017dc5166ea740359d381097a7ab382c1dd404055fElliott Hughes  bool IsStillStarting() const;
3027dc5166ea740359d381097a7ab382c1dd404055fElliott Hughes
3030e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro  bool IsExceptionPending() const {
304474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers    bool result = ReadFlag(kExceptionPending);
305474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers    DCHECK_EQ(result, exception_ != NULL);
306474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers    return result;
3070e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro  }
3080e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
309b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  Throwable* GetException() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3100e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro    return exception_;
3110e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro  }
3120e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
31300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  void AssertNoPendingException() const;
31400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
315474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers  void SetException(Throwable* new_exception) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3160cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers    CHECK(new_exception != NULL);
317474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers    // TODO: DCHECK(!IsExceptionPending());
318474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers    exception_ = new_exception;
319474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers    AtomicSetFlag(kExceptionPending);
320474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers    DCHECK(IsExceptionPending());
3210cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers  }
3220cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers
3230cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers  void ClearException() {
3240cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers    exception_ = NULL;
325474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers    AtomicClearFlag(kExceptionPending);
326474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers    DCHECK(!IsExceptionPending());
327a09576416788b916095739e43a16917e7948f3a4Elliott Hughes  }
328a09576416788b916095739e43a16917e7948f3a4Elliott Hughes
329bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers  // Find catch block and perform long jump to appropriate exception handle
330b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  void DeliverException() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
331bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers
332bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers  Context* GetLongJumpContext();
3330399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  void ReleaseLongJumpContext(Context* context) {
3340399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    DCHECK(long_jump_context_ == NULL);
3350399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    long_jump_context_ = context;
3361a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao  }
3371a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao
33866f19258f9728d4ffe026074d8fd429d639802faMathieu Chartier  AbstractMethod* GetCurrentMethod(uint32_t* dex_pc = NULL, size_t* frame_id = NULL) const
339b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
3400399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
341bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers  void SetTopOfStack(void* stack, uintptr_t pc) {
34266f19258f9728d4ffe026074d8fd429d639802faMathieu Chartier    AbstractMethod** top_method = reinterpret_cast<AbstractMethod**>(stack);
3430399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    managed_stack_.SetTopQuickFrame(top_method);
3440399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    managed_stack_.SetTopQuickFramePc(pc);
3450e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro  }
3460e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
3470399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  bool HasManagedStack() const {
3480399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    return managed_stack_.GetTopQuickFrame() != NULL || managed_stack_.GetTopShadowFrame() != NULL;
349bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers  }
350bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers
351a4f94740f9d66b21f4bcd3a225c434aa29cfa323Elliott Hughes  // If 'msg' is NULL, no detail message is set.
35200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  void ThrowNewException(const char* exception_class_descriptor, const char* msg)
353b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
3545cb5ad27944efb08d4556b3c0d362302e37e832bElliott Hughes
355a4f94740f9d66b21f4bcd3a225c434aa29cfa323Elliott Hughes  // If 'msg' is NULL, no detail message is set. An exception must be pending, and will be
356a4f94740f9d66b21f4bcd3a225c434aa29cfa323Elliott Hughes  // used as the new exception's cause.
35700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  void ThrowNewWrappedException(const char* exception_class_descriptor, const char* msg)
358b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
359a4f94740f9d66b21f4bcd3a225c434aa29cfa323Elliott Hughes
3605cb5ad27944efb08d4556b3c0d362302e37e832bElliott Hughes  void ThrowNewExceptionF(const char* exception_class_descriptor, const char* fmt, ...)
36100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      __attribute__((format(printf, 3, 4)))
362b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
363a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes
36400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  void ThrowNewExceptionV(const char* exception_class_descriptor, const char* fmt, va_list ap)
365b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
3664a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes
3672ced6a534157d5d963693346904389c19775d2daElliott Hughes  // OutOfMemoryError is special, because we need to pre-allocate an instance.
3688a8b9cbe158ee13477f2381f164c519762a06fefElliott Hughes  // Only the GC should call this.
36900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  void ThrowOutOfMemoryError(const char* msg)
370b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
37179082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughes
3720399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  //QuickFrameIterator FindExceptionHandler(void* throw_pc, void** handler_pc);
3731a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao
37466f19258f9728d4ffe026074d8fd429d639802faMathieu Chartier  void* FindExceptionHandlerInMethod(const AbstractMethod* method,
3751a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao                                     void* throw_pc,
3761a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao                                     const DexFile& dex_file,
3771a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao                                     ClassLinker* class_linker);
378c143c55718342519db5398e41dda31422cf16c79buzbee
379be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  static void Startup();
380038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes  static void FinishStartup();
381c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes  static void Shutdown();
382b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro
383b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers  // JNI methods
38469f5bc6759f256a146eefd8a7141d39fcc3b0421Elliott Hughes  JNIEnvExt* GetJniEnv() const {
385b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers    return jni_env_;
386b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers  }
387b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers
388408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers  // Convert a jobject into a Object*
38900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  Object* DecodeJObject(jobject obj)
39000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      LOCKS_EXCLUDED(JavaVMExt::globals_lock,
39100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                     JavaVMExt::weak_globals_lock)
392b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
393b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers
3948daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  // Implements java.lang.Thread.interrupted.
3958daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  bool Interrupted() {
39685d1545e985ac689db4bad7849880e843707c862Elliott Hughes    MutexLock mu(*wait_mutex_);
3978daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes    bool interrupted = interrupted_;
3988daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes    interrupted_ = false;
3998daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes    return interrupted;
4008daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  }
4018daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
4028daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  // Implements java.lang.Thread.isInterrupted.
4038daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  bool IsInterrupted() {
40485d1545e985ac689db4bad7849880e843707c862Elliott Hughes    MutexLock mu(*wait_mutex_);
4058daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes    return interrupted_;
4068daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  }
4078daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
4085f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  void Interrupt() {
40985d1545e985ac689db4bad7849880e843707c862Elliott Hughes    MutexLock mu(*wait_mutex_);
4105f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    if (interrupted_) {
4115f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes      return;
4125f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    }
4135f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    interrupted_ = true;
4145f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    NotifyLocked();
4155f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
4165f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
4175f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  void Notify() {
41885d1545e985ac689db4bad7849880e843707c862Elliott Hughes    MutexLock mu(*wait_mutex_);
4195f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    NotifyLocked();
4205f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
4215f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
422474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers  ClassLoader* GetClassLoaderOverride() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
423c143c55718342519db5398e41dda31422cf16c79buzbee    return class_loader_override_;
424c143c55718342519db5398e41dda31422cf16c79buzbee  }
425c143c55718342519db5398e41dda31422cf16c79buzbee
426365c10235438607541fa2259a5fec48061b90bd8Ian Rogers  void SetClassLoaderOverride(ClassLoader* class_loader_override) {
427c143c55718342519db5398e41dda31422cf16c79buzbee    class_loader_override_ = class_loader_override;
428c143c55718342519db5398e41dda31422cf16c79buzbee  }
429c143c55718342519db5398e41dda31422cf16c79buzbee
430aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers  // Create the internal representation of a stack trace, that is more time
431aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers  // and space efficient to compute than the StackTraceElement[]
43200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  jobject CreateInternalStackTrace(const ScopedObjectAccess& soa) const
433b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
43401158d7a57c8321370667a6045220237d16e0da8Elliott Hughes
43501158d7a57c8321370667a6045220237d16e0da8Elliott Hughes  // Convert an internal stack trace representation (returned by CreateInternalStackTrace) to a
43601158d7a57c8321370667a6045220237d16e0da8Elliott Hughes  // StackTraceElement[]. If output_array is NULL, a new array is created, otherwise as many
43701158d7a57c8321370667a6045220237d16e0da8Elliott Hughes  // frames as will fit are written into the given array. If stack_depth is non-NULL, it's updated
43801158d7a57c8321370667a6045220237d16e0da8Elliott Hughes  // with the number of valid frames in the returned array.
43901158d7a57c8321370667a6045220237d16e0da8Elliott Hughes  static jobjectArray InternalStackTraceToStackTraceElementArray(JNIEnv* env, jobject internal,
44001158d7a57c8321370667a6045220237d16e0da8Elliott Hughes      jobjectArray output_array = NULL, int* stack_depth = NULL);
44155df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao
44200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  void VisitRoots(Heap::RootVisitor* visitor, void* arg)
443b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
444410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes
445250455229aa0cc07bbd18174efe510bd52631a99jeffhao#if VERIFY_OBJECT_ENABLED
446b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  void VerifyStack() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
447250455229aa0cc07bbd18174efe510bd52631a99jeffhao#else
448b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  void VerifyStack() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_){}
449250455229aa0cc07bbd18174efe510bd52631a99jeffhao#endif
450250455229aa0cc07bbd18174efe510bd52631a99jeffhao
451be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  //
452be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  // Offsets of various members of native Thread class, used by compiled code.
453be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  //
454be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes
455be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  static ThreadOffset SelfOffset() {
456be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes    return ThreadOffset(OFFSETOF_MEMBER(Thread, self_));
457be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  }
458be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes
459be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  static ThreadOffset ExceptionOffset() {
460be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes    return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_));
461be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  }
462be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes
46354e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes  static ThreadOffset ThinLockIdOffset() {
464be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes    return ThreadOffset(OFFSETOF_MEMBER(Thread, thin_lock_id_));
465be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  }
466be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes
467be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  static ThreadOffset CardTableOffset() {
468be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes    return ThreadOffset(OFFSETOF_MEMBER(Thread, card_table_));
469be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  }
470be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes
471474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers  static ThreadOffset ThreadFlagsOffset() {
472474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers    return ThreadOffset(OFFSETOF_MEMBER(Thread, state_and_flags_));
473be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  }
474be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes
475932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers  // Size of stack less any space reserved for stack overflow
476932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers  size_t GetStackSize() {
47730fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    return stack_size_ - (stack_end_ - stack_begin_);
478932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers  }
479932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers
480932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers  // Set the stack end to that to be used during a stack overflow
481b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  void SetStackEndForStackOverflow() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
482932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers
483932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers  // Set the stack end to that to be used during regular execution
484932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers  void ResetDefaultStackEnd() {
485932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers    // Our stacks grow down, so we want stack_end_ to be near there, but reserving enough room
486932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers    // to throw a StackOverflowError.
48730fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    stack_end_ = stack_begin_ + kStackOverflowReservedBytes;
488932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers  }
489932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers
490449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes  static ThreadOffset StackEndOffset() {
491449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes    return ThreadOffset(OFFSETOF_MEMBER(Thread, stack_end_));
492be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  }
493be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes
494be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  static ThreadOffset JniEnvOffset() {
495be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes    return ThreadOffset(OFFSETOF_MEMBER(Thread, jni_env_));
496be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  }
497be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes
498be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  static ThreadOffset TopOfManagedStackOffset() {
4990399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    return ThreadOffset(OFFSETOF_MEMBER(Thread, managed_stack_) +
5000399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers                        ManagedStack::TopQuickFrameOffset());
501be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  }
502be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes
503bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers  static ThreadOffset TopOfManagedStackPcOffset() {
5040399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    return ThreadOffset(OFFSETOF_MEMBER(Thread, managed_stack_) +
5050399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers                        ManagedStack::TopQuickFramePcOffset());
5060399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  }
5070399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
5080399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  const ManagedStack* GetManagedStack() const {
5090399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    return &managed_stack_;
5100399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  }
5110399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
5120399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // Linked list recording fragments of managed stack.
5130399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  void PushManagedStackFragment(ManagedStack* fragment) {
5140399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    managed_stack_.PushManagedStackFragment(fragment);
5150399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  }
5160399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  void PopManagedStackFragment(const ManagedStack& fragment) {
5170399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    managed_stack_.PopManagedStackFragment(fragment);
518bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers  }
519bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers
5200399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  ShadowFrame* PushShadowFrame(ShadowFrame* new_top_frame) {
5210399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    return managed_stack_.PushShadowFrame(new_top_frame);
522de479be99328d2113bf483e082c9ecf235a34d69TDYa  }
523de479be99328d2113bf483e082c9ecf235a34d69TDYa
524de479be99328d2113bf483e082c9ecf235a34d69TDYa  ShadowFrame* PopShadowFrame() {
5250399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    return managed_stack_.PopShadowFrame();
526de479be99328d2113bf483e082c9ecf235a34d69TDYa  }
527f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien
528d668a06b5dcc3b0f7f788da4d756cd4ee6f1d0faTDYa  static ThreadOffset TopShadowFrameOffset() {
5290399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    return ThreadOffset(OFFSETOF_MEMBER(Thread, managed_stack_) +
5300399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers                        ManagedStack::TopShadowFrameOffset());
531d668a06b5dcc3b0f7f788da4d756cd4ee6f1d0faTDYa  }
532d668a06b5dcc3b0f7f788da4d756cd4ee6f1d0faTDYa
5330399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // Number of references allocated in ShadowFrames on this thread
5340399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  size_t NumShadowFrameReferences() const {
5350399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    return managed_stack_.NumShadowFrameReferences();
5360399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  }
5370399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
5380399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // Number of references in SIRTs on this thread
5390399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  size_t NumSirtReferences();
5400399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
5410399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // Number of references allocated in SIRTs & shadow frames on this thread
5420399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  size_t NumStackReferences() {
5430399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    return NumSirtReferences() + NumShadowFrameReferences();
5440399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  };
5450399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
5460399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // Is the given obj in this thread's stack indirect reference table?
5470399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  bool SirtContains(jobject obj);
5480399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
5490399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  void SirtVisitRoots(Heap::RootVisitor* visitor, void* arg);
5500399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
55140381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom  void PushSirt(StackIndirectReferenceTable* sirt);
55240381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom  StackIndirectReferenceTable* PopSirt();
55340381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom
554be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  static ThreadOffset TopSirtOffset() {
555be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes    return ThreadOffset(OFFSETOF_MEMBER(Thread, top_sirt_));
556be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  }
557be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes
558475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  DebugInvokeReq* GetInvokeReq() {
559475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes    return debug_invoke_req_;
560475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  }
561475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
562c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  void SetDebuggerUpdatesEnabled(bool enabled);
563c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes
5640399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  const std::vector<TraceStackFrame>* GetTraceStack() const {
5650399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    return trace_stack_;
566e343b76af81a005ef64f5e75a555389fd9147dabjeffhao  }
567e343b76af81a005ef64f5e75a555389fd9147dabjeffhao
5680399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  bool IsTraceStackEmpty() const {
5690399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    return trace_stack_->empty();
570a9ef3fd82bebc6370fc3ddbb094988feb6c83022jeffhao  }
571a9ef3fd82bebc6370fc3ddbb094988feb6c83022jeffhao
572e343b76af81a005ef64f5e75a555389fd9147dabjeffhao  void PushTraceStackFrame(const TraceStackFrame& frame) {
573e343b76af81a005ef64f5e75a555389fd9147dabjeffhao    trace_stack_->push_back(frame);
574e343b76af81a005ef64f5e75a555389fd9147dabjeffhao  }
575e343b76af81a005ef64f5e75a555389fd9147dabjeffhao
576e343b76af81a005ef64f5e75a555389fd9147dabjeffhao  TraceStackFrame PopTraceStackFrame() {
577e343b76af81a005ef64f5e75a555389fd9147dabjeffhao    TraceStackFrame frame = trace_stack_->back();
578e343b76af81a005ef64f5e75a555389fd9147dabjeffhao    trace_stack_->pop_back();
579e343b76af81a005ef64f5e75a555389fd9147dabjeffhao    return frame;
580e343b76af81a005ef64f5e75a555389fd9147dabjeffhao  }
581e343b76af81a005ef64f5e75a555389fd9147dabjeffhao
58200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  BaseMutex* GetHeldMutex(MutexLevel level) const {
58300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return held_mutexes_[level];
58400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
58500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
58600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  void SetHeldMutex(MutexLevel level, BaseMutex* mutex) {
58700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    held_mutexes_[level] = mutex;
58800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
589ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes
5900e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro private:
5915d96a7168dd3e2a4acf1a947ef12efa8f82b95c0Elliott Hughes  // We have no control over the size of 'bool', but want our boolean fields
5925d96a7168dd3e2a4acf1a947ef12efa8f82b95c0Elliott Hughes  // to be 4-byte quantities.
5935d96a7168dd3e2a4acf1a947ef12efa8f82b95c0Elliott Hughes  typedef uint32_t bool32_t;
5945d96a7168dd3e2a4acf1a947ef12efa8f82b95c0Elliott Hughes
59552673ffae0025d86f4023735581f19ebcc477487Ian Rogers  explicit Thread(bool daemon);
596b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  ~Thread() LOCKS_EXCLUDED(Locks::mutator_lock_,
597b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers                           Locks::thread_suspend_count_lock_);
598c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  void Destroy();
599c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  friend class ThreadList;  // For ~Thread and Destroy.
6000e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
601365c10235438607541fa2259a5fec48061b90bd8Ian Rogers  void CreatePeer(const char* name, bool as_daemon, jobject thread_group);
6025fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes  friend class Runtime; // For CreatePeer.
6035fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes
604474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers  // Avoid use, callers should use SetState. Used only by SignalCatcher::HandleSigQuit and ~Thread.
605474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers  ThreadState SetStateUnsafe(ThreadState new_state) {
606474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers    ThreadState old_state = GetState();
607474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers    state_and_flags_.state = new_state;
608c747cffa950b8398cb8844d47e2253214c855750Ian Rogers    return old_state;
609c747cffa950b8398cb8844d47e2253214c855750Ian Rogers  }
610474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers  friend class SignalCatcher;  // For SetStateUnsafe.
611c747cffa950b8398cb8844d47e2253214c855750Ian Rogers
612d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes  void DumpState(std::ostream& os) const;
61300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  void DumpStack(std::ostream& os) const
614b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
615b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
616d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes
617accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes  // Out-of-line conveniences for debugging in gdb.
618498508c1187dc07d3eae5476784cde20f5224d93Elliott Hughes  static Thread* CurrentFromGdb(); // Like Thread::Current.
61900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Like Thread::Dump(std::cerr).
620b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  void DumpFromGdb() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
621accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes
62293e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes  static void* CreateCallback(void* arg);
62393e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes
62400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  void HandleUncaughtExceptions(const ScopedObjectAccess& soa)
625b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
62600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  void RemoveFromThreadGroup(const ScopedObjectAccess& soa)
627b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
628accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes
629462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes  void Init();
6305d76c435082332ef79a22962386fa92a0870e378Ian Rogers  void InitCardTable();
631b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers  void InitCpu();
6323ea4ec5629613013ad9b0d7a69abdb94491ac46fbuzbee  void InitFunctionPointers();
633caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom  void InitTid();
634caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom  void InitPthreadKeySelf();
635be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  void InitStackHwm();
636be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes
63700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  void NotifyLocked() EXCLUSIVE_LOCKS_REQUIRED(wait_mutex_) {
6385f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    if (wait_monitor_ != NULL) {
63985d1545e985ac689db4bad7849880e843707c862Elliott Hughes      wait_cond_->Signal();
6405f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    }
6415f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
6425f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
643474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers  bool ReadFlag(ThreadFlag flag) const {
644474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers    return (state_and_flags_.flags & flag) != 0;
645474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers  }
646474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers
647474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers  void AtomicSetFlag(ThreadFlag flag);
648474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers
649474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers  void AtomicClearFlag(ThreadFlag flag);
650474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers
651be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  static void ThreadExitCallback(void* arg);
652b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers
6530399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // TLS key used to retrieve the Thread*.
6540399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  static pthread_key_t pthread_key_self_;
6550399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
65600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Used to notify threads that they should attempt to resume, they will suspend again if
65700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // their suspend count is > 0.
65800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  static ConditionVariable* resume_cond_
659b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      GUARDED_BY(Locks::thread_suspend_count_lock_);
66000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
6610399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // --- Frequently accessed fields first for short offsets ---
6620399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
663474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers  // 32 bits of atomically changed state and flags. Keeping as 32 bits allows and atomic CAS to
664474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers  // change from being Suspended to Runnable without a suspend request occurring.
665474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers  struct PACKED StateAndFlags {
666474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers    // Bitfield of flag values. Must be changed atomically so that flag values aren't lost. See
667474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers    // ThreadFlags for bit field meanings.
668474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers    volatile uint16_t flags;
669474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers    // Holds the ThreadState. May be changed non-atomically between Suspended (ie not Runnable)
670474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers    // transitions. Changing to Runnable requires that the suspend_request be part of the atomic
671474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers    // operation. If a thread is suspended and a suspend_request is present, a thread may not
672474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers    // change to Runnable as a GC or other operation is in progress.
673474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers    uint16_t state;
674474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers  };
675474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers  struct StateAndFlags state_and_flags_;
676474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers  COMPILE_ASSERT(sizeof(struct StateAndFlags) == sizeof(int32_t),
677474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers                 sizeof_state_and_flags_and_int32_are_different);
678474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers
6790399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // A non-zero value is used to tell the current thread to enter a safe point
6800399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // at the next poll.
681b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  int suspend_count_ GUARDED_BY(Locks::thread_suspend_count_lock_);
6820399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
6830399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // The biased card table, see CardTable for details
6840399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  byte* card_table_;
6850399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
6860399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // The pending exception or NULL.
6870399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  Throwable* exception_;
6880399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
6890399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // The end of this thread's stack. This is the lowest safely-addressable address on the stack.
6900399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // We leave extra space so there's room for the code that throws StackOverflowError.
6910399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  byte* stack_end_;
6920399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
6930399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // The top of the managed stack often manipulated directly by compiler generated code.
6940399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  ManagedStack managed_stack_;
6950399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
6960399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // Every thread may have an associated JNI environment
6970399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  JNIEnvExt* jni_env_;
6980399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
6990399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // Initialized to "this". On certain architectures (such as x86) reading
7000399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // off of Thread::Current is easy but getting the address of Thread::Current
7010399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // is hard. This field can be read off of Thread::Current to give the address.
7020399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  Thread* self_;
7030399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
7040399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // Our managed peer (an instance of java.lang.Thread).
7050399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  Object* peer_;
7060399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
7070399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // The "lowest addressable byte" of the stack
7080399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  byte* stack_begin_;
7090399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
7100399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // Size of the stack
7110399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  size_t stack_size_;
7120399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
713dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes  // Thin lock thread id. This is a small integer used by the thin lock implementation.
714dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes  // This is not to be confused with the native thread's tid, nor is it the value returned
715dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes  // by java.lang.Thread.getId --- this is a distinct value, used only for locking. One
716dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes  // important difference between this id and the ids visible to managed code is that these
717dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes  // ones get reused (to ensure that they fit in the number of bits available).
718dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes  uint32_t thin_lock_id_;
719b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers
720d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes  // System thread id.
721d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes  pid_t tid_;
722d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes
7238daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  // Guards the 'interrupted_' and 'wait_monitor_' members.
72400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  mutable Mutex* wait_mutex_ DEFAULT_MUTEX_ACQUIRED_AFTER;
72500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ConditionVariable* wait_cond_ GUARDED_BY(wait_mutex_);
72600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Pointer to the monitor lock we're currently waiting on (or NULL).
72700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  Monitor* wait_monitor_ GUARDED_BY(wait_mutex_);
72800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Thread "interrupted" status; stays raised until queried or thrown.
72900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  bool32_t interrupted_ GUARDED_BY(wait_mutex_);
7305f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  // The next thread in the wait set this thread is part of.
7315f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  Thread* wait_next_;
7328e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes  // If we're blocked in MonitorEnter, this is the object we're trying to lock.
7338e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes  Object* monitor_enter_object_;
7345f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
7355f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  friend class Monitor;
736dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes
737408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers  // Top of linked list of stack indirect reference tables or NULL for none
738408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers  StackIndirectReferenceTable* top_sirt_;
739b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers
74069759eaa6fd4386f1e6d8748052ad221087b3476Carl Shapiro  Runtime* runtime_;
74169759eaa6fd4386f1e6d8748052ad221087b3476Carl Shapiro
7420399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  RuntimeStats stats_;
74345a76cb99104a222d6a9bd768a084893dcb7cf30Ian Rogers
744edcc09c737b00462881f147602656739d029571eElliott Hughes  // Needed to get the right ClassLoader in JNI_OnLoad, but also
745edcc09c737b00462881f147602656739d029571eElliott Hughes  // useful for testing.
746365c10235438607541fa2259a5fec48061b90bd8Ian Rogers  ClassLoader* class_loader_override_;
747c143c55718342519db5398e41dda31422cf16c79buzbee
748bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers  // Thread local, lazily allocated, long jump context. Used to deliver exceptions.
74985d1545e985ac689db4bad7849880e843707c862Elliott Hughes  Context* long_jump_context_;
750bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers
751418dfe7849f45535b5388a91bd7a16cfc20a612bElliott Hughes  // A boolean telling us whether we're recursively throwing OOME.
7525d96a7168dd3e2a4acf1a947ef12efa8f82b95c0Elliott Hughes  bool32_t throwing_OutOfMemoryError_;
753726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes
7540399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // How much of 'suspend_count_' is by request of the debugger, used to set things right
7550399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // when the debugger detaches. Must be <= suspend_count_.
756b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  int debug_suspend_count_ GUARDED_BY(Locks::thread_suspend_count_lock_);
7570399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
758475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  // JDWP invoke-during-breakpoint support.
759475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  DebugInvokeReq* debug_invoke_req_;
760475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
761e343b76af81a005ef64f5e75a555389fd9147dabjeffhao  // Additional stack used by method tracer to store method and return pc values.
762e343b76af81a005ef64f5e75a555389fd9147dabjeffhao  // Stored as a pointer since std::vector is not PACKED.
763e343b76af81a005ef64f5e75a555389fd9147dabjeffhao  std::vector<TraceStackFrame>* trace_stack_;
764e343b76af81a005ef64f5e75a555389fd9147dabjeffhao
765899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes  // A cached copy of the java.lang.Thread's name.
766899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes  std::string* name_;
767899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes
76852673ffae0025d86f4023735581f19ebcc477487Ian Rogers  // Is the thread a daemon?
7695d96a7168dd3e2a4acf1a947ef12efa8f82b95c0Elliott Hughes  const bool32_t daemon_;
77052673ffae0025d86f4023735581f19ebcc477487Ian Rogers
7710d39c12238499ca9ccc34d1532c443335e7c1044Elliott Hughes  // A cached pthread_t for the pthread underlying this Thread*.
7720d39c12238499ca9ccc34d1532c443335e7c1044Elliott Hughes  pthread_t pthread_self_;
7730d39c12238499ca9ccc34d1532c443335e7c1044Elliott Hughes
77400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Support for Mutex lock hierarchy bug detection.
77500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  BaseMutex* held_mutexes_[kMaxMutexLevel + 1];
776ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes
7770399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  // A positive value implies we're in a region where thread suspension isn't expected.
7780399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  uint32_t no_thread_suspension_;
77952673ffae0025d86f4023735581f19ebcc477487Ian Rogers
78052673ffae0025d86f4023735581f19ebcc477487Ian Rogers  // Cause for last suspension.
78152673ffae0025d86f4023735581f19ebcc477487Ian Rogers  const char* last_no_thread_suspension_cause_;
7825d96a7168dd3e2a4acf1a947ef12efa8f82b95c0Elliott Hughes
783a32a6fd4a781262dff4fec102da053d16b7ef6c0Ian Rogers public:
784a32a6fd4a781262dff4fec102da053d16b7ef6c0Ian Rogers  // Runtime support function pointers
7856a607ad0902f3b8478e95d0b6b3e63a538571a3fElliott Hughes  // TODO: move this near the top, since changing its offset requires all oats to be recompiled!
78657b86d47b66322693a070185fadfb43cb9c12eabIan Rogers  EntryPoints entrypoints_;
78721d28f510eb590f52810c83f1f3f37fe5f4adf46Shih-wei Liao#ifdef ART_USE_GREENLAND_COMPILER
78821d28f510eb590f52810c83f1f3f37fe5f4adf46Shih-wei Liao  RuntimeEntryPoints runtime_entry_points_;
78921d28f510eb590f52810c83f1f3f37fe5f4adf46Shih-wei Liao#endif
790a32a6fd4a781262dff4fec102da053d16b7ef6c0Ian Rogers
791a32a6fd4a781262dff4fec102da053d16b7ef6c0Ian Rogers private:
7926a607ad0902f3b8478e95d0b6b3e63a538571a3fElliott Hughes  // How many times has our pthread key's destructor been called?
7936a607ad0902f3b8478e95d0b6b3e63a538571a3fElliott Hughes  uint32_t thread_exit_check_count_;
7946a607ad0902f3b8478e95d0b6b3e63a538571a3fElliott Hughes
79522f454c7d349c439c774c5d79b32514b190fd531Ian Rogers  friend class ScopedThreadStateChange;
79622f454c7d349c439c774c5d79b32514b190fd531Ian Rogers
7970e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro  DISALLOW_COPY_AND_ASSIGN(Thread);
7980e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro};
799bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers
800330304de14dc7118b45b8e7b5bd11a172fa61701Elliott Hughesstd::ostream& operator<<(std::ostream& os, const Thread& thread);
80134e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughesstd::ostream& operator<<(std::ostream& os, const ThreadState& state);
8020e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
8030e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro}  // namespace art
8040e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
8050e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro#endif  // ART_SRC_THREAD_H_
806