thread.h revision abbe07d095547ded03c2e9d0d53943d43471278d
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" 38de479be99328d2113bf483e082c9ecf235a34d69TDYa#include "shadow_frame.h" 3968e76526e98432625464022cb26f66b9ef6f5af4Elliott Hughes#include "stack.h" 40e343b76af81a005ef64f5e75a555389fd9147dabjeffhao#include "trace.h" 41bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers#include "UniquePtr.h" 42b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers 430e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapironamespace art { 440e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro 4569f5bc6759f256a146eefd8a7141d39fcc3b0421Elliott Hughesclass Array; 4637f7a40f6789bb287f287a9af00777af9d6428eeElliott Hughesclass Class; 471f87008b165d26541d832ff805250afdc89c253dBrian Carlstromclass ClassLinker; 48edcc09c737b00462881f147602656739d029571eElliott Hughesclass ClassLoader; 49bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogersclass Context; 50475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughesclass DebugInvokeReq; 51a40f9bc48afe3a9d38be2fa298fece13ed82ba28Brian Carlstromclass Method; 528daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughesclass Monitor; 530e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiroclass Object; 54b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiroclass Runtime; 55f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chienclass ShadowFrame; 5668e76526e98432625464022cb26f66b9ef6f5af4Elliott Hughesclass StackIndirectReferenceTable; 5755df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liaoclass StackTraceElement; 581da522de18ac6e4c2913c3233529e9dd115059f8buzbeeclass StaticStorageBase; 5940381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstromclass Thread; 6040381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstromclass ThreadList; 6140381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstromclass Throwable; 621da522de18ac6e4c2913c3233529e9dd115059f8buzbee 6355df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liaotemplate<class T> class ObjectArray; 644417536522fd2a9d8215d8672331984769c9520bShih-wei Liaotemplate<class T> class PrimitiveArray; 654417536522fd2a9d8215d8672331984769c9520bShih-wei Liaotypedef PrimitiveArray<int32_t> IntArray; 660e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro 6734e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes// Thread priorities. These must match the Thread.MIN_PRIORITY, 6834e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes// Thread.NORM_PRIORITY, and Thread.MAX_PRIORITY constants. 6934e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughesenum ThreadPriority { 7034e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes kMinThreadPriority = 1, 7134e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes kNormThreadPriority = 5, 7234e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes kMaxThreadPriority = 10, 7334e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes}; 74fc1d9f581592d54cc14240b9909824af38656931Elliott Hughes 7534e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughesenum ThreadState { 7634e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes kTerminated = 0, // Thread.TERMINATED JDWP TS_ZOMBIE 7734e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes kRunnable = 1, // Thread.RUNNABLE JDWP TS_RUNNING 7834e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes kTimedWaiting = 2, // Thread.TIMED_WAITING JDWP TS_WAIT - in Object.wait() with a timeout 7934e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes kBlocked = 3, // Thread.BLOCKED JDWP TS_MONITOR - blocked on a monitor 8034e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes kWaiting = 4, // Thread.WAITING JDWP TS_WAIT - in Object.wait() 8134e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes kStarting = 5, // Thread.NEW - native thread started, not yet ready to run managed code 8234e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes kNative = 6, // - running in a JNI native method 8334e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes kVmWait = 7, // - waiting on an internal runtime resource 8434e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes kSuspended = 8, // - suspended by GC or debugger 8534e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes}; 86b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 8734e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughesclass PACKED Thread { 8834e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes public: 89932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers // Space to throw a StackOverflowError in. 904165a83d250165c839850651e1b2a69e06128000TDYa#if !defined(ART_USE_LLVM_COMPILER) 91aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom static const size_t kStackOverflowReservedBytes = 4 * KB; 924165a83d250165c839850651e1b2a69e06128000TDYa#else // LLVM_x86 requires more memory to throw stack overflow exception. 934165a83d250165c839850651e1b2a69e06128000TDYa static const size_t kStackOverflowReservedBytes = 8 * KB; 944165a83d250165c839850651e1b2a69e06128000TDYa#endif 95c143c55718342519db5398e41dda31422cf16c79buzbee 966414a97a3c6dc101ae8ebc9480114d0c327e8e8dBrian Carlstrom static const size_t kDefaultStackSize = 16 * KB; 9761e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 989b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao class StackVisitor { 999b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao public: 1000cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers virtual ~StackVisitor() {} 101530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes // Return 'true' if we should continue to visit more frames, 'false' to stop. 102530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes virtual bool VisitFrame(const Frame& frame, uintptr_t pc) = 0; 1039b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao }; 1049b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 105462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes // Creates a new native thread corresponding to the given managed peer. 106462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes // Used to implement Thread.start. 107d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes static void Create(Object* peer, size_t stack_size); 10861e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 109462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes // Attaches the calling native thread to the runtime, returning the new native peer. 110462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes // Used to implement JNI AttachCurrentThread and AttachCurrentThreadAsDaemon calls. 111462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes static Thread* Attach(const char* thread_name, bool as_daemon, Object* thread_group); 112b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 113caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom // Reset internal state of child thread after fork. 114caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom void InitAfterFork(); 115caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom 116b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro static Thread* Current() { 117d0e7e777c412071a911dd2c008b20b7d879fd392Carl Shapiro void* thread = pthread_getspecific(Thread::pthread_key_self_); 118d0e7e777c412071a911dd2c008b20b7d879fd392Carl Shapiro return reinterpret_cast<Thread*>(thread); 1190e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro } 1200e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro 121761928d24e4e7ed7776b52243eaf9095ad35f448Elliott Hughes static Thread* FromManagedThread(Object* thread_peer); 12201158d7a57c8321370667a6045220237d16e0da8Elliott Hughes static Thread* FromManagedThread(JNIEnv* env, jobject thread); 1238e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes static uint32_t LockOwnerFromThreadLock(Object* thread_lock); 1248daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 12528fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes // Translates 172 to pAllocArrayFromCode and so on. 12628fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes static void DumpThreadOffset(std::ostream& os, uint32_t offset, size_t size_of_pointers); 12728fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes 128899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes // When full == true, dumps the detailed thread state and the thread stack (used for SIGQUIT). 129899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes // When full == false, dumps a one-line summary of thread state (used for operator<<). 130899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes void Dump(std::ostream& os, bool full = true) const; 131a09576416788b916095739e43a16917e7948f3a4Elliott Hughes 132abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes // Dumps the SIGQUIT per-thread header. 'thread' can be NULL for a non-attached thread, in which 133abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes // case we use 'tid' to identify the thread, and we'll include as much information as we can. 134abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes static void DumpState(std::ostream& os, const Thread* thread, pid_t tid); 135abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes 13634e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes ThreadState GetState() const { 1370cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers return state_; 1380cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers } 1390cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers 14034e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes ThreadState SetState(ThreadState new_state); 14134e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes void SetStateWithoutSuspendCheck(ThreadState new_state); 1428d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes 143038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes bool IsDaemon(); 144761928d24e4e7ed7776b52243eaf9095ad35f448Elliott Hughes bool IsSuspended(); 145038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes 1468d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes void WaitUntilSuspended(); 1470cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers 1485f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes bool HoldsLock(Object*); 1495f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 1508daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes /* 1518daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * Changes the priority of this thread to match that of the java.lang.Thread object. 1528daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * 1538daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * We map a priority value from 1-10 to Linux "nice" values, where lower 1548daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * numbers indicate higher priority. 1558daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes */ 1568daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes void SetNativePriority(int newPriority); 1578daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 1588daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes /* 1598daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * Returns the thread priority for the current thread by querying the system. 1608daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * This is useful when attaching a thread through JNI. 1618daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * 1628daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * Returns a value from 1 to 10 (compatible with java.lang.Thread values). 1638daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes */ 1648daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes static int GetNativePriority(); 1658daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 166462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes // Returns the "main" ThreadGroup, used when attaching user threads. 167462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes static Object* GetMainThreadGroup(); 168462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes // Returns the "system" ThreadGroup, used when attaching our internal threads. 169462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes static Object* GetSystemThreadGroup(); 170462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes 1710cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers bool CanAccessDirectReferences() const { 172caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom#ifdef MOVING_GARBAGE_COLLECTOR 173a59d1793b89d0fd62c7544c94da4b6e5dac95ad5Elliott Hughes // TODO: when we have a moving collector, we'll need: return state_ == kRunnable; 174caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom#endif 175a59d1793b89d0fd62c7544c94da4b6e5dac95ad5Elliott Hughes return true; 1760cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers } 1770cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers 178dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes uint32_t GetThinLockId() const { 179dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes return thin_lock_id_; 180b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro } 181b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 182d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes pid_t GetTid() const { 183d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes return tid_; 184d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } 185e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes 186ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes // Returns the java.lang.Thread's name, or NULL if this Thread* doesn't have a peer. 187899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes String* GetThreadName() const; 188899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes 189ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes // Sets 'name' to the java.lang.Thread's name. This requires no transition to managed code, 190ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes // allocation, or locking. 191ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes void GetThreadName(std::string& name) const; 192ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes 193899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes // Sets the thread's name. 194899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes void SetThreadName(const char* name); 195fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes 196d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes Object* GetPeer() const { 1978daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes return peer_; 1988daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes } 1998daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 200a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes Object* GetThreadGroup() const; 201a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes 2029d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes RuntimeStats* GetStats() { 2039d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes return &stats_; 2049d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes } 2059d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes 206d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes int GetSuspendCount() const { 207d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes return suspend_count_; 208d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes } 2090cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers 2107dc5166ea740359d381097a7ab382c1dd404055fElliott Hughes bool IsStillStarting() const; 2117dc5166ea740359d381097a7ab382c1dd404055fElliott Hughes 212d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes // Returns the current Method* and native PC (not dex PC) for this thread. 213d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes Method* GetCurrentMethod(uintptr_t* pc = NULL, Method*** sp = NULL) const; 21433dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao 2150e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro bool IsExceptionPending() const { 216b20a554613609dc372073d2ebd9fbc3925a429f5Elliott Hughes return exception_ != NULL; 2170e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro } 2180e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro 219e5b0dc83537bf915c6abe4efeae6e501daf75a27Elliott Hughes Throwable* GetException() const { 2200cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers DCHECK(CanAccessDirectReferences()); 2210e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro return exception_; 2220e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro } 2230e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro 2240cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers void SetException(Throwable* new_exception) { 2250cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers DCHECK(CanAccessDirectReferences()); 2260cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers CHECK(new_exception != NULL); 2270cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers // TODO: CHECK(exception_ == NULL); 2280cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers exception_ = new_exception; // TODO 2290cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers } 2300cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers 2310cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers void ClearException() { 2320cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers exception_ = NULL; 233a09576416788b916095739e43a16917e7948f3a4Elliott Hughes } 234a09576416788b916095739e43a16917e7948f3a4Elliott Hughes 235bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers // Find catch block and perform long jump to appropriate exception handle 236ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers void DeliverException(); 237bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 238bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers Context* GetLongJumpContext(); 239bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 2401a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao Frame GetTopOfStack() const { 2411a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao return top_of_managed_stack_; 2421a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao } 2431a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao 2441a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao // TODO: this is here for testing, remove when we have exception unit tests 2451a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao // that use the real stack 246bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers void SetTopOfStack(void* stack, uintptr_t pc) { 2470cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers top_of_managed_stack_.SetSP(reinterpret_cast<Method**>(stack)); 248bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers top_of_managed_stack_pc_ = pc; 2490e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro } 2500e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro 251bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers void SetTopOfStackPC(uintptr_t pc) { 252bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers top_of_managed_stack_pc_ = pc; 253bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers } 254bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 255a4f94740f9d66b21f4bcd3a225c434aa29cfa323Elliott Hughes // If 'msg' is NULL, no detail message is set. 2565cb5ad27944efb08d4556b3c0d362302e37e832bElliott Hughes void ThrowNewException(const char* exception_class_descriptor, const char* msg); 2575cb5ad27944efb08d4556b3c0d362302e37e832bElliott Hughes 258a4f94740f9d66b21f4bcd3a225c434aa29cfa323Elliott Hughes // If 'msg' is NULL, no detail message is set. An exception must be pending, and will be 259a4f94740f9d66b21f4bcd3a225c434aa29cfa323Elliott Hughes // used as the new exception's cause. 260a4f94740f9d66b21f4bcd3a225c434aa29cfa323Elliott Hughes void ThrowNewWrappedException(const char* exception_class_descriptor, const char* msg); 261a4f94740f9d66b21f4bcd3a225c434aa29cfa323Elliott Hughes 2625cb5ad27944efb08d4556b3c0d362302e37e832bElliott Hughes void ThrowNewExceptionF(const char* exception_class_descriptor, const char* fmt, ...) 263362f9bc807169bcfc8761dde067bbfb79b5ad0fdElliott Hughes __attribute__((format(printf, 3, 4))); 264a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes 2654a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes void ThrowNewExceptionV(const char* exception_class_descriptor, const char* fmt, va_list ap); 2664a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes 2672ced6a534157d5d963693346904389c19775d2daElliott Hughes // OutOfMemoryError is special, because we need to pre-allocate an instance. 2688a8b9cbe158ee13477f2381f164c519762a06fefElliott Hughes // Only the GC should call this. 2692ced6a534157d5d963693346904389c19775d2daElliott Hughes void ThrowOutOfMemoryError(const char* msg); 27079082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughes 2711a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao Frame FindExceptionHandler(void* throw_pc, void** handler_pc); 2721a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao 2731a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao void* FindExceptionHandlerInMethod(const Method* method, 2741a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao void* throw_pc, 2751a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao const DexFile& dex_file, 2761a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao ClassLinker* class_linker); 277c143c55718342519db5398e41dda31422cf16c79buzbee 278be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes static void Startup(); 279038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes static void FinishStartup(); 280c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes static void Shutdown(); 281b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 282b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers // JNI methods 28369f5bc6759f256a146eefd8a7141d39fcc3b0421Elliott Hughes JNIEnvExt* GetJniEnv() const { 284b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers return jni_env_; 285b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers } 286b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers 28728f1a147d77ec772db98bed890b50a9ddcff2365TDYa // Number of references in SIRTs on this thread 288408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers size_t NumSirtReferences(); 289a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers 29028f1a147d77ec772db98bed890b50a9ddcff2365TDYa // Number of references allocated in ShadowFrames on this thread 29128f1a147d77ec772db98bed890b50a9ddcff2365TDYa size_t NumShadowFrameReferences(); 29228f1a147d77ec772db98bed890b50a9ddcff2365TDYa 29328f1a147d77ec772db98bed890b50a9ddcff2365TDYa // Number of references allocated in SIRTs & shadow frames on this thread 29428f1a147d77ec772db98bed890b50a9ddcff2365TDYa size_t NumStackReferences() { 29528f1a147d77ec772db98bed890b50a9ddcff2365TDYa return NumSirtReferences() + NumShadowFrameReferences(); 29628f1a147d77ec772db98bed890b50a9ddcff2365TDYa }; 29728f1a147d77ec772db98bed890b50a9ddcff2365TDYa 298408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers // Is the given obj in this thread's stack indirect reference table? 299408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers bool SirtContains(jobject obj); 300408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers 30128f1a147d77ec772db98bed890b50a9ddcff2365TDYa // Is the given obj in this thread's ShadowFrame? 30228f1a147d77ec772db98bed890b50a9ddcff2365TDYa bool ShadowFrameContains(jobject obj); 30328f1a147d77ec772db98bed890b50a9ddcff2365TDYa 30428f1a147d77ec772db98bed890b50a9ddcff2365TDYa // Is the given obj in this thread's Sirts & ShadowFrames? 30528f1a147d77ec772db98bed890b50a9ddcff2365TDYa bool StackReferencesContain(jobject obj); 30628f1a147d77ec772db98bed890b50a9ddcff2365TDYa 3078dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao void SirtVisitRoots(Heap::RootVisitor* visitor, void* arg); 3088dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao 309f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien void ShadowFrameVisitRoots(Heap::RootVisitor* visitor, void* arg); 310f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien 311408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers // Convert a jobject into a Object* 312408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers Object* DecodeJObject(jobject obj); 313b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers 3148daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes // Implements java.lang.Thread.interrupted. 3158daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes bool Interrupted() { 31685d1545e985ac689db4bad7849880e843707c862Elliott Hughes MutexLock mu(*wait_mutex_); 3178daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes bool interrupted = interrupted_; 3188daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes interrupted_ = false; 3198daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes return interrupted; 3208daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes } 3218daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 3228daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes // Implements java.lang.Thread.isInterrupted. 3238daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes bool IsInterrupted() { 32485d1545e985ac689db4bad7849880e843707c862Elliott Hughes MutexLock mu(*wait_mutex_); 3258daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes return interrupted_; 3268daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes } 3278daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 3285f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes void Interrupt() { 32985d1545e985ac689db4bad7849880e843707c862Elliott Hughes MutexLock mu(*wait_mutex_); 3305f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (interrupted_) { 3315f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 3325f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 3335f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes interrupted_ = true; 3345f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes NotifyLocked(); 3355f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 3365f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 3375f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes void Notify() { 33885d1545e985ac689db4bad7849880e843707c862Elliott Hughes MutexLock mu(*wait_mutex_); 3395f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes NotifyLocked(); 3405f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 3415f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 3426de0860c491d390b7a53be436519ef2d9234c4c9Ian Rogers // Linked list recording transitions from native to managed code 343b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers void PushNativeToManagedRecord(NativeToManagedRecord* record); 344b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers void PopNativeToManagedRecord(const NativeToManagedRecord& record); 3456de0860c491d390b7a53be436519ef2d9234c4c9Ian Rogers 346bffb15585b8fd43d3ca534ddbb85e7f591595951Brian Carlstrom const ClassLoader* GetClassLoaderOverride() { 3470cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers // TODO: need to place the class_loader_override_ in a handle 3480cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers // DCHECK(CanAccessDirectReferences()); 349c143c55718342519db5398e41dda31422cf16c79buzbee return class_loader_override_; 350c143c55718342519db5398e41dda31422cf16c79buzbee } 351c143c55718342519db5398e41dda31422cf16c79buzbee 352bffb15585b8fd43d3ca534ddbb85e7f591595951Brian Carlstrom void SetClassLoaderOverride(const ClassLoader* class_loader_override) { 353c143c55718342519db5398e41dda31422cf16c79buzbee class_loader_override_ = class_loader_override; 354c143c55718342519db5398e41dda31422cf16c79buzbee } 355c143c55718342519db5398e41dda31422cf16c79buzbee 356aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Create the internal representation of a stack trace, that is more time 357aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // and space efficient to compute than the StackTraceElement[] 35801158d7a57c8321370667a6045220237d16e0da8Elliott Hughes jobject CreateInternalStackTrace(JNIEnv* env) const; 35901158d7a57c8321370667a6045220237d16e0da8Elliott Hughes 36001158d7a57c8321370667a6045220237d16e0da8Elliott Hughes // Convert an internal stack trace representation (returned by CreateInternalStackTrace) to a 36101158d7a57c8321370667a6045220237d16e0da8Elliott Hughes // StackTraceElement[]. If output_array is NULL, a new array is created, otherwise as many 36201158d7a57c8321370667a6045220237d16e0da8Elliott Hughes // frames as will fit are written into the given array. If stack_depth is non-NULL, it's updated 36301158d7a57c8321370667a6045220237d16e0da8Elliott Hughes // with the number of valid frames in the returned array. 36401158d7a57c8321370667a6045220237d16e0da8Elliott Hughes static jobjectArray InternalStackTraceToStackTraceElementArray(JNIEnv* env, jobject internal, 36501158d7a57c8321370667a6045220237d16e0da8Elliott Hughes jobjectArray output_array = NULL, int* stack_depth = NULL); 36655df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao 367d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers void VisitRoots(Heap::RootVisitor* visitor, void* arg); 368410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes 369250455229aa0cc07bbd18174efe510bd52631a99jeffhao#if VERIFY_OBJECT_ENABLED 370250455229aa0cc07bbd18174efe510bd52631a99jeffhao void VerifyStack(); 371250455229aa0cc07bbd18174efe510bd52631a99jeffhao#else 372250455229aa0cc07bbd18174efe510bd52631a99jeffhao void VerifyStack() {} 373250455229aa0cc07bbd18174efe510bd52631a99jeffhao#endif 374250455229aa0cc07bbd18174efe510bd52631a99jeffhao 375be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes // 376be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes // Offsets of various members of native Thread class, used by compiled code. 377be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes // 378be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 379be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes static ThreadOffset SelfOffset() { 380be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes return ThreadOffset(OFFSETOF_MEMBER(Thread, self_)); 381be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes } 382be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 383be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes static ThreadOffset ExceptionOffset() { 384be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_)); 385be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes } 386be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 38754e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes static ThreadOffset ThinLockIdOffset() { 388be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes return ThreadOffset(OFFSETOF_MEMBER(Thread, thin_lock_id_)); 389be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes } 390be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 391be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes static ThreadOffset CardTableOffset() { 392be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes return ThreadOffset(OFFSETOF_MEMBER(Thread, card_table_)); 393be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes } 394be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 395be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes static ThreadOffset SuspendCountOffset() { 396be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_)); 397be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes } 398be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 399be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes static ThreadOffset StateOffset() { 40093e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes return ThreadOffset(OFFSETOF_VOLATILE_MEMBER(Thread, state_)); 401be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes } 402be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 403932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers // Size of stack less any space reserved for stack overflow 404932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers size_t GetStackSize() { 40530fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers return stack_size_ - (stack_end_ - stack_begin_); 406932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers } 407932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers 408932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers // Set the stack end to that to be used during a stack overflow 409932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers void SetStackEndForStackOverflow() { 410932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers // During stack overflow we allow use of the full stack 41130fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers if (stack_end_ == stack_begin_) { 4123b6baaa203fa63f1522b2172a1645f90412afdaeElliott Hughes DumpStack(std::cerr); 4133b6baaa203fa63f1522b2172a1645f90412afdaeElliott Hughes LOG(FATAL) << "Need to increase kStackOverflowReservedBytes (currently " 4143b6baaa203fa63f1522b2172a1645f90412afdaeElliott Hughes << kStackOverflowReservedBytes << ")"; 4153b6baaa203fa63f1522b2172a1645f90412afdaeElliott Hughes } 4163b6baaa203fa63f1522b2172a1645f90412afdaeElliott Hughes 41730fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers stack_end_ = stack_begin_; 418932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers } 419932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers 420932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers // Set the stack end to that to be used during regular execution 421932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers void ResetDefaultStackEnd() { 422932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers // Our stacks grow down, so we want stack_end_ to be near there, but reserving enough room 423932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers // to throw a StackOverflowError. 42430fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers stack_end_ = stack_begin_ + kStackOverflowReservedBytes; 425932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers } 426932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers 427449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes static ThreadOffset StackEndOffset() { 428449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes return ThreadOffset(OFFSETOF_MEMBER(Thread, stack_end_)); 429be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes } 430be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 431be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes static ThreadOffset JniEnvOffset() { 432be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes return ThreadOffset(OFFSETOF_MEMBER(Thread, jni_env_)); 433be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes } 434be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 435be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes static ThreadOffset TopOfManagedStackOffset() { 436be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes return ThreadOffset(OFFSETOF_MEMBER(Thread, top_of_managed_stack_) + 437be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes OFFSETOF_MEMBER(Frame, sp_)); 438be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes } 439be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 440bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers static ThreadOffset TopOfManagedStackPcOffset() { 441bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers return ThreadOffset(OFFSETOF_MEMBER(Thread, top_of_managed_stack_pc_)); 442bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers } 443bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 444de479be99328d2113bf483e082c9ecf235a34d69TDYa ShadowFrame* PushShadowFrame(ShadowFrame* frame) { 445de479be99328d2113bf483e082c9ecf235a34d69TDYa ShadowFrame* old_frame = top_shadow_frame_; 446de479be99328d2113bf483e082c9ecf235a34d69TDYa top_shadow_frame_ = frame; 447de479be99328d2113bf483e082c9ecf235a34d69TDYa frame->SetLink(old_frame); 448de479be99328d2113bf483e082c9ecf235a34d69TDYa return old_frame; 449de479be99328d2113bf483e082c9ecf235a34d69TDYa } 450de479be99328d2113bf483e082c9ecf235a34d69TDYa 451de479be99328d2113bf483e082c9ecf235a34d69TDYa ShadowFrame* PopShadowFrame() { 452de479be99328d2113bf483e082c9ecf235a34d69TDYa CHECK(top_shadow_frame_ != NULL); 453de479be99328d2113bf483e082c9ecf235a34d69TDYa ShadowFrame* frame = top_shadow_frame_; 454de479be99328d2113bf483e082c9ecf235a34d69TDYa top_shadow_frame_ = frame->GetLink(); 455de479be99328d2113bf483e082c9ecf235a34d69TDYa return frame; 456de479be99328d2113bf483e082c9ecf235a34d69TDYa } 457f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien 458d668a06b5dcc3b0f7f788da4d756cd4ee6f1d0faTDYa static ThreadOffset TopShadowFrameOffset() { 459d668a06b5dcc3b0f7f788da4d756cd4ee6f1d0faTDYa return ThreadOffset(OFFSETOF_MEMBER(Thread, top_shadow_frame_)); 460d668a06b5dcc3b0f7f788da4d756cd4ee6f1d0faTDYa } 461d668a06b5dcc3b0f7f788da4d756cd4ee6f1d0faTDYa 46240381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom void PushSirt(StackIndirectReferenceTable* sirt); 46340381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom StackIndirectReferenceTable* PopSirt(); 46440381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom 465be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes static ThreadOffset TopSirtOffset() { 466be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes return ThreadOffset(OFFSETOF_MEMBER(Thread, top_sirt_)); 467be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes } 468be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 469530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes void WalkStack(StackVisitor* visitor, bool include_upcalls = false) const; 4709407c60800c95902fba0b3c3265520d47c1e7052Shih-wei Liao 471475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes DebugInvokeReq* GetInvokeReq() { 472475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes return debug_invoke_req_; 473475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes } 474475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes 475c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes void SetDebuggerUpdatesEnabled(bool enabled); 476c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes 477e343b76af81a005ef64f5e75a555389fd9147dabjeffhao bool IsTraceStackEmpty() const { 478e343b76af81a005ef64f5e75a555389fd9147dabjeffhao return trace_stack_->empty(); 479e343b76af81a005ef64f5e75a555389fd9147dabjeffhao } 480e343b76af81a005ef64f5e75a555389fd9147dabjeffhao 481a9ef3fd82bebc6370fc3ddbb094988feb6c83022jeffhao TraceStackFrame GetTraceStackFrame(uint32_t depth) const { 482a9ef3fd82bebc6370fc3ddbb094988feb6c83022jeffhao return trace_stack_->at(trace_stack_->size() - depth - 1); 483a9ef3fd82bebc6370fc3ddbb094988feb6c83022jeffhao } 484a9ef3fd82bebc6370fc3ddbb094988feb6c83022jeffhao 485e343b76af81a005ef64f5e75a555389fd9147dabjeffhao void PushTraceStackFrame(const TraceStackFrame& frame) { 486e343b76af81a005ef64f5e75a555389fd9147dabjeffhao trace_stack_->push_back(frame); 487e343b76af81a005ef64f5e75a555389fd9147dabjeffhao } 488e343b76af81a005ef64f5e75a555389fd9147dabjeffhao 489e343b76af81a005ef64f5e75a555389fd9147dabjeffhao TraceStackFrame PopTraceStackFrame() { 490e343b76af81a005ef64f5e75a555389fd9147dabjeffhao TraceStackFrame frame = trace_stack_->back(); 491e343b76af81a005ef64f5e75a555389fd9147dabjeffhao trace_stack_->pop_back(); 492e343b76af81a005ef64f5e75a555389fd9147dabjeffhao return frame; 493e343b76af81a005ef64f5e75a555389fd9147dabjeffhao } 494e343b76af81a005ef64f5e75a555389fd9147dabjeffhao 495a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughes void CheckSafeToLockOrUnlock(MutexRank rank, bool is_locking); 496a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughes void CheckSafeToWait(MutexRank rank); 497ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes 4980e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro private: 499dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes Thread(); 500c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes ~Thread(); 501c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes void Destroy(); 502c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes friend class ThreadList; // For ~Thread and Destroy. 5030e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro 504462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes void CreatePeer(const char* name, bool as_daemon, Object* thread_group); 5055fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes friend class Runtime; // For CreatePeer. 5065fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes 507d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes void DumpState(std::ostream& os) const; 508d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes void DumpStack(std::ostream& os) const; 509d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 510accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes // Out-of-line conveniences for debugging in gdb. 511498508c1187dc07d3eae5476784cde20f5224d93Elliott Hughes static Thread* CurrentFromGdb(); // Like Thread::Current. 512accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes void DumpFromGdb() const; // Like Thread::Dump(std::cerr). 513accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 51493e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes static void* CreateCallback(void* arg); 51593e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 516accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes void HandleUncaughtExceptions(); 5174514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom void RemoveFromThreadGroup(); 518accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 519462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes void Init(); 5205d76c435082332ef79a22962386fa92a0870e378Ian Rogers void InitCardTable(); 521b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers void InitCpu(); 5223ea4ec5629613013ad9b0d7a69abdb94491ac46fbuzbee void InitFunctionPointers(); 523caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom void InitTid(); 524caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom void InitPthreadKeySelf(); 525be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes void InitStackHwm(); 526be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 5275f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes void NotifyLocked() { 5285f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (wait_monitor_ != NULL) { 52985d1545e985ac689db4bad7849880e843707c862Elliott Hughes wait_cond_->Signal(); 5305f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 5315f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 5325f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 533be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes static void ThreadExitCallback(void* arg); 534b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers 535dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes // Thin lock thread id. This is a small integer used by the thin lock implementation. 536dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes // This is not to be confused with the native thread's tid, nor is it the value returned 537dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes // by java.lang.Thread.getId --- this is a distinct value, used only for locking. One 538dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes // important difference between this id and the ids visible to managed code is that these 539dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes // ones get reused (to ensure that they fit in the number of bits available). 540dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes uint32_t thin_lock_id_; 541b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers 542d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes // System thread id. 543d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes pid_t tid_; 544d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 545dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes // Our managed peer (an instance of java.lang.Thread). 546d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes Object* peer_; 5478daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 5488e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes // The top_of_managed_stack_ and top_of_managed_stack_pc_ fields are accessed from 5498e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes // compiled code, so we keep them early in the structure to (a) avoid having to keep 5508e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes // fixing the assembler offsets and (b) improve the chances that these will still be aligned. 5518e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 5528e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes // Top of the managed stack, written out prior to the state transition from 55368e76526e98432625464022cb26f66b9ef6f5af4Elliott Hughes // kRunnable to kNative. Uses include giving the starting point for scanning 5548e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes // a managed stack when a thread is in native code. 5558e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Frame top_of_managed_stack_; 5568e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes // PC corresponding to the call out of the top_of_managed_stack_ frame 5578e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes uintptr_t top_of_managed_stack_pc_; 5588e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 5598daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes // Guards the 'interrupted_' and 'wait_monitor_' members. 56085d1545e985ac689db4bad7849880e843707c862Elliott Hughes mutable Mutex* wait_mutex_; 56185d1545e985ac689db4bad7849880e843707c862Elliott Hughes ConditionVariable* wait_cond_; 5628daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes // Pointer to the monitor lock we're currently waiting on (or NULL), guarded by wait_mutex_. 5638daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes Monitor* wait_monitor_; 5648daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes // Thread "interrupted" status; stays raised until queried or thrown, guarded by wait_mutex_. 5658e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes uint32_t interrupted_; 5665f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // The next thread in the wait set this thread is part of. 5675f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes Thread* wait_next_; 5688e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes // If we're blocked in MonitorEnter, this is the object we're trying to lock. 5698e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Object* monitor_enter_object_; 5705f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 5715f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes friend class Monitor; 572dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes 5739d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes RuntimeStats stats_; 5749d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes 5755d76c435082332ef79a22962386fa92a0870e378Ian Rogers // The biased card table, see CardTable for details 5765d76c435082332ef79a22962386fa92a0870e378Ian Rogers byte* card_table_; 577c143c55718342519db5398e41dda31422cf16c79buzbee 578449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes // The end of this thread's stack. This is the lowest safely-addressable address on the stack. 579449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes // We leave extra space so there's room for the code that throws StackOverflowError. 580449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes byte* stack_end_; 581be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 582932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers // Size of the stack 583932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers size_t stack_size_; 584932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers 585932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers // The "lowest addressable byte" of the stack 58630fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers byte* stack_begin_; 587932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers 5886de0860c491d390b7a53be436519ef2d9234c4c9Ian Rogers // A linked list (of stack allocated records) recording transitions from 5896de0860c491d390b7a53be436519ef2d9234c4c9Ian Rogers // native to managed code. 5906de0860c491d390b7a53be436519ef2d9234c4c9Ian Rogers NativeToManagedRecord* native_to_managed_record_; 5916de0860c491d390b7a53be436519ef2d9234c4c9Ian Rogers 592408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers // Top of linked list of stack indirect reference tables or NULL for none 593408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers StackIndirectReferenceTable* top_sirt_; 594b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers 595f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien // Top of linked list of shadow stack or NULL for none 596f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien // Some backend may require shadow frame to ease the GC work. 597f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien ShadowFrame* top_shadow_frame_; 598f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien 599b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers // Every thread may have an associated JNI environment 60069f5bc6759f256a146eefd8a7141d39fcc3b0421Elliott Hughes JNIEnvExt* jni_env_; 601b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers 60234e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes volatile ThreadState state_; 603b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 60469759eaa6fd4386f1e6d8748052ad221087b3476Carl Shapiro // Initialized to "this". On certain architectures (such as x86) reading 60569759eaa6fd4386f1e6d8748052ad221087b3476Carl Shapiro // off of Thread::Current is easy but getting the address of Thread::Current 60669759eaa6fd4386f1e6d8748052ad221087b3476Carl Shapiro // is hard. This field can be read off of Thread::Current to give the address. 60769759eaa6fd4386f1e6d8748052ad221087b3476Carl Shapiro Thread* self_; 60869759eaa6fd4386f1e6d8748052ad221087b3476Carl Shapiro 60969759eaa6fd4386f1e6d8748052ad221087b3476Carl Shapiro Runtime* runtime_; 61069759eaa6fd4386f1e6d8748052ad221087b3476Carl Shapiro 61169759eaa6fd4386f1e6d8748052ad221087b3476Carl Shapiro // The pending exception or NULL. 612e5b0dc83537bf915c6abe4efeae6e501daf75a27Elliott Hughes Throwable* exception_; 6130e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro 61445a76cb99104a222d6a9bd768a084893dcb7cf30Ian Rogers // A non-zero value is used to tell the current thread to enter a safe point 61545a76cb99104a222d6a9bd768a084893dcb7cf30Ian Rogers // at the next poll. 61645a76cb99104a222d6a9bd768a084893dcb7cf30Ian Rogers int suspend_count_; 617234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes // How much of 'suspend_count_' is by request of the debugger, used to set things right 618234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes // when the debugger detaches. Must be <= suspend_count_. 619234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes int debug_suspend_count_; 62045a76cb99104a222d6a9bd768a084893dcb7cf30Ian Rogers 621edcc09c737b00462881f147602656739d029571eElliott Hughes // Needed to get the right ClassLoader in JNI_OnLoad, but also 622edcc09c737b00462881f147602656739d029571eElliott Hughes // useful for testing. 623bffb15585b8fd43d3ca534ddbb85e7f591595951Brian Carlstrom const ClassLoader* class_loader_override_; 624c143c55718342519db5398e41dda31422cf16c79buzbee 625bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers // Thread local, lazily allocated, long jump context. Used to deliver exceptions. 62685d1545e985ac689db4bad7849880e843707c862Elliott Hughes Context* long_jump_context_; 627bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 628418dfe7849f45535b5388a91bd7a16cfc20a612bElliott Hughes // A boolean telling us whether we're recursively throwing OOME. 629726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes uint32_t throwing_OutOfMemoryError_; 630726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes 631726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes Throwable* pre_allocated_OutOfMemoryError_; 632418dfe7849f45535b5388a91bd7a16cfc20a612bElliott Hughes 633475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes // JDWP invoke-during-breakpoint support. 634475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes DebugInvokeReq* debug_invoke_req_; 635475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes 63681ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes // TLS key used to retrieve the Thread*. 637b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro static pthread_key_t pthread_key_self_; 638b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 639e343b76af81a005ef64f5e75a555389fd9147dabjeffhao // Additional stack used by method tracer to store method and return pc values. 640e343b76af81a005ef64f5e75a555389fd9147dabjeffhao // Stored as a pointer since std::vector is not PACKED. 641e343b76af81a005ef64f5e75a555389fd9147dabjeffhao std::vector<TraceStackFrame>* trace_stack_; 642e343b76af81a005ef64f5e75a555389fd9147dabjeffhao 643899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes // A cached copy of the java.lang.Thread's name. 644899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes std::string* name_; 645899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes 646ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes uint32_t held_mutexes_[kMaxMutexRank + 1]; 647ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes 648a32a6fd4a781262dff4fec102da053d16b7ef6c0Ian Rogers public: 649a32a6fd4a781262dff4fec102da053d16b7ef6c0Ian Rogers // Runtime support function pointers 65057b86d47b66322693a070185fadfb43cb9c12eabIan Rogers EntryPoints entrypoints_; 651a32a6fd4a781262dff4fec102da053d16b7ef6c0Ian Rogers 652a32a6fd4a781262dff4fec102da053d16b7ef6c0Ian Rogers private: 6530e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro DISALLOW_COPY_AND_ASSIGN(Thread); 6540e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro}; 655bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 656330304de14dc7118b45b8e7b5bd11a172fa61701Elliott Hughesstd::ostream& operator<<(std::ostream& os, const Thread& thread); 65734e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughesstd::ostream& operator<<(std::ostream& os, const ThreadState& state); 6580e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro 659ad7c2a3b4daa2abd707375444f4b0db7d69a3838Elliott Hughesclass ScopedThreadStateChange { 660ad7c2a3b4daa2abd707375444f4b0db7d69a3838Elliott Hughes public: 66134e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes ScopedThreadStateChange(Thread* thread, ThreadState new_state) : thread_(thread) { 662ad7c2a3b4daa2abd707375444f4b0db7d69a3838Elliott Hughes old_thread_state_ = thread_->SetState(new_state); 663ad7c2a3b4daa2abd707375444f4b0db7d69a3838Elliott Hughes } 664ad7c2a3b4daa2abd707375444f4b0db7d69a3838Elliott Hughes 665ad7c2a3b4daa2abd707375444f4b0db7d69a3838Elliott Hughes ~ScopedThreadStateChange() { 666ad7c2a3b4daa2abd707375444f4b0db7d69a3838Elliott Hughes thread_->SetState(old_thread_state_); 667ad7c2a3b4daa2abd707375444f4b0db7d69a3838Elliott Hughes } 668ad7c2a3b4daa2abd707375444f4b0db7d69a3838Elliott Hughes 669ad7c2a3b4daa2abd707375444f4b0db7d69a3838Elliott Hughes private: 670ad7c2a3b4daa2abd707375444f4b0db7d69a3838Elliott Hughes Thread* thread_; 67134e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes ThreadState old_thread_state_; 672ad7c2a3b4daa2abd707375444f4b0db7d69a3838Elliott Hughes DISALLOW_COPY_AND_ASSIGN(ScopedThreadStateChange); 673ad7c2a3b4daa2abd707375444f4b0db7d69a3838Elliott Hughes}; 674ad7c2a3b4daa2abd707375444f4b0db7d69a3838Elliott Hughes 6750e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro} // namespace art 6760e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro 6770e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro#endif // ART_SRC_THREAD_H_ 678