thread.h revision 899e789bd4741c0172268f7838ce8ab220a5f916
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"
35578bbdc684db8ed68e9fedbc678669d27fa68b6eBrian Carlstrom#include "offsets.h"
369d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes#include "runtime_stats.h"
3768e76526e98432625464022cb26f66b9ef6f5af4Elliott Hughes#include "stack.h"
38e343b76af81a005ef64f5e75a555389fd9147dabjeffhao#include "trace.h"
39bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers#include "UniquePtr.h"
40b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers
410e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapironamespace art {
420e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
4369f5bc6759f256a146eefd8a7141d39fcc3b0421Elliott Hughesclass Array;
4437f7a40f6789bb287f287a9af00777af9d6428eeElliott Hughesclass Class;
451f87008b165d26541d832ff805250afdc89c253dBrian Carlstromclass ClassLinker;
46edcc09c737b00462881f147602656739d029571eElliott Hughesclass ClassLoader;
47bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogersclass Context;
48475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughesclass DebugInvokeReq;
49a40f9bc48afe3a9d38be2fa298fece13ed82ba28Brian Carlstromclass Method;
508daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughesclass Monitor;
510e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiroclass Object;
52b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiroclass Runtime;
5368e76526e98432625464022cb26f66b9ef6f5af4Elliott Hughesclass StackIndirectReferenceTable;
5455df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liaoclass StackTraceElement;
551da522de18ac6e4c2913c3233529e9dd115059f8buzbeeclass StaticStorageBase;
5640381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstromclass Thread;
5740381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstromclass ThreadList;
5840381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstromclass Throwable;
591da522de18ac6e4c2913c3233529e9dd115059f8buzbee
6055df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liaotemplate<class T> class ObjectArray;
614417536522fd2a9d8215d8672331984769c9520bShih-wei Liaotemplate<class T> class PrimitiveArray;
624417536522fd2a9d8215d8672331984769c9520bShih-wei Liaotypedef PrimitiveArray<int32_t> IntArray;
630e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
6485d1545e985ac689db4bad7849880e843707c862Elliott Hughesclass PACKED Thread {
650e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro public:
668daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  /* thread priorities, from java.lang.Thread */
678daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  enum Priority {
688daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes    kMinPriority = 1,
698daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes    kNormPriority = 5,
708daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes    kMaxPriority = 10,
718daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  };
72b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro  enum State {
73499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes    // These correspond to JDWP states (but needn't share the same values).
74499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes    kTerminated   = 0,        // TS_ZOMBIE
75499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes    kRunnable     = 1,        // TS_RUNNING
76499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes    kTimedWaiting = 2,        // TS_SLEEPING in Object.wait()
77499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes    kBlocked      = 3,        // TS_MONITOR on a monitor
78499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes    kWaiting      = 4,        // TS_WAIT in Object.wait()
7993e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes    // Non-JDWP states.
8093e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes    kInitializing = 5,        // allocated, not yet running --- TODO: unnecessary?
8193e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes    kStarting     = 6,        // native thread started, not yet ready to run managed code
8293e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes    kNative       = 7,        // off in a JNI native method
8393e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes    kVmWait       = 8,        // waiting on a VM resource
8493e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes    kSuspended    = 9,        // suspended, usually by GC or debugger
85b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro  };
86b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro
87932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers  // Space to throw a StackOverflowError in.
88aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  static const size_t kStackOverflowReservedBytes = 4 * KB;
89c143c55718342519db5398e41dda31422cf16c79buzbee
9072db0d77d6c476c71c5bbaa14a80ca77f23a47f3Brian Carlstrom  static const size_t kDefaultStackSize = 96 * KB;
9161e019d291583029c01b61b93bea750f2b663c37Carl Shapiro
92c143c55718342519db5398e41dda31422cf16c79buzbee  // Runtime support function pointers
934a3164faefd255b1c1e911e7ad7c3d57749caaf6buzbee  void (*pDebugMe)(Method*, uint32_t);
94c143c55718342519db5398e41dda31422cf16c79buzbee  void* (*pMemcpy)(void*, const void*, size_t);
955433072f589b61413e042eddf76e8190a048f71dbuzbee  uint64_t (*pShlLong)(uint64_t, uint32_t);
965433072f589b61413e042eddf76e8190a048f71dbuzbee  uint64_t (*pShrLong)(uint64_t, uint32_t);
975433072f589b61413e042eddf76e8190a048f71dbuzbee  uint64_t (*pUshrLong)(uint64_t, uint32_t);
98c143c55718342519db5398e41dda31422cf16c79buzbee  float (*pI2f)(int);
99c143c55718342519db5398e41dda31422cf16c79buzbee  int (*pF2iz)(float);
100c143c55718342519db5398e41dda31422cf16c79buzbee  float (*pD2f)(double);
101c143c55718342519db5398e41dda31422cf16c79buzbee  double (*pF2d)(float);
102c143c55718342519db5398e41dda31422cf16c79buzbee  double (*pI2d)(int);
103c143c55718342519db5398e41dda31422cf16c79buzbee  int (*pD2iz)(double);
104c143c55718342519db5398e41dda31422cf16c79buzbee  float (*pL2f)(long);
105c143c55718342519db5398e41dda31422cf16c79buzbee  double (*pL2d)(long);
1061b4c85959b3d9a4a33bc2160c46c1bbde67350c7buzbee  long long (*pF2l)(float);
1071b4c85959b3d9a4a33bc2160c46c1bbde67350c7buzbee  long long (*pD2l)(double);
108c143c55718342519db5398e41dda31422cf16c79buzbee  float (*pFadd)(float, float);
109c143c55718342519db5398e41dda31422cf16c79buzbee  float (*pFsub)(float, float);
110c143c55718342519db5398e41dda31422cf16c79buzbee  float (*pFdiv)(float, float);
111c143c55718342519db5398e41dda31422cf16c79buzbee  float (*pFmul)(float, float);
112c143c55718342519db5398e41dda31422cf16c79buzbee  float (*pFmodf)(float, float);
113c143c55718342519db5398e41dda31422cf16c79buzbee  double (*pDadd)(double, double);
114c143c55718342519db5398e41dda31422cf16c79buzbee  double (*pDsub)(double, double);
115c143c55718342519db5398e41dda31422cf16c79buzbee  double (*pDdiv)(double, double);
116c143c55718342519db5398e41dda31422cf16c79buzbee  double (*pDmul)(double, double);
117c143c55718342519db5398e41dda31422cf16c79buzbee  double (*pFmod)(double, double);
118c143c55718342519db5398e41dda31422cf16c79buzbee  int (*pIdivmod)(int, int);
119c143c55718342519db5398e41dda31422cf16c79buzbee  int (*pIdiv)(int, int);
120439c4fa0db980fb19e4a585723a64a3461e4c278buzbee  long long (*pLmul)(long long, long long);
121c143c55718342519db5398e41dda31422cf16c79buzbee  long long (*pLdivmod)(long long, long long);
1224a510d8750d50b77353d0a5a891d1b9b3a48ecd5Ian Rogers  void (*pCheckSuspendFromCode)(Thread*);  // Stub that is called when the suspend count is non-zero
1234a510d8750d50b77353d0a5a891d1b9b3a48ecd5Ian Rogers  void (*pTestSuspendFromCode)();  // Stub that is periodically called to test the suspend count
12421d9e8323124a832a21679ca83808bc9c68ed365Ian Rogers  void* (*pAllocObjectFromCode)(uint32_t, void*);
12528ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers  void* (*pAllocObjectFromCodeWithAccessCheck)(uint32_t, void*);
126b408de744566a5c5a80be1ba7f5c88407e816945Elliott Hughes  void* (*pAllocArrayFromCode)(uint32_t, void*, int32_t);
127e51a511ccee3f3c0120807321bcc160fcaa664beIan Rogers  void (*pCanPutArrayElementFromCode)(void*, void*);
128ce9eca6de042f26e9eebc41c9bee8b4d14f753aaIan Rogers  void* (*pCheckAndAllocArrayFromCode)(uint32_t, void*, int32_t);
129362f9bc807169bcfc8761dde067bbfb79b5ad0fdElliott Hughes  void (*pCheckCastFromCode)(void*, void*);
130ce9eca6de042f26e9eebc41c9bee8b4d14f753aaIan Rogers  Object* (*pDecodeJObjectInThread)(Thread* thread, jobject obj);
13167375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers  void (*pDeliverException)(void*);
132ce9eca6de042f26e9eebc41c9bee8b4d14f753aaIan Rogers  void* (*pFindInstanceFieldFromCode)(uint32_t, void*);
133ce9eca6de042f26e9eebc41c9bee8b4d14f753aaIan Rogers  Method* (*pFindInterfaceMethodInCache)(Class*, uint32_t, const Method*, struct DvmDex*);
134ce9eca6de042f26e9eebc41c9bee8b4d14f753aaIan Rogers  void* (*pFindNativeMethod)(Thread* thread);
135ce9eca6de042f26e9eebc41c9bee8b4d14f753aaIan Rogers  int32_t (*pGet32Static)(uint32_t, void*);
136ce9eca6de042f26e9eebc41c9bee8b4d14f753aaIan Rogers  int64_t (*pGet64Static)(uint32_t, void*);
137ce9eca6de042f26e9eebc41c9bee8b4d14f753aaIan Rogers  void* (*pGetObjStatic)(uint32_t, void*);
138ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers  void (*pHandleFillArrayDataFromCode)(void*, void*);
139ce9eca6de042f26e9eebc41c9bee8b4d14f753aaIan Rogers  void* (*pInitializeStaticStorage)(uint32_t, void*);
140362f9bc807169bcfc8761dde067bbfb79b5ad0fdElliott Hughes  uint32_t (*pInstanceofNonTrivialFromCode)(const Class*, const Class*);
141ce9eca6de042f26e9eebc41c9bee8b4d14f753aaIan Rogers  void (*pInvokeInterfaceTrampoline)(uint32_t, void*);
14228ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers  void* (*pInitializeTypeFromCode)(uint32_t, void*);
143b093c6b27f8ea9bbe2d49c03ebe345203a121199Ian Rogers  void* (*pInitializeTypeAndVerifyAccessFromCode)(uint32_t, void*);
144ce9eca6de042f26e9eebc41c9bee8b4d14f753aaIan Rogers  void (*pLockObjectFromCode)(void*);
1456fd03fb67e4628689f0abf34edeacc0e35b6295eBrian Carlstrom  void (*pObjectInit)(void*);
14648d7222d2f543752ffb17db05e0b9e891061135cbuzbee  void* (*pResolveMethodFromCode)(void*, uint32_t, bool);
147aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  void* (*pResolveStringFromCode)(void*, uint32_t);
148ce9eca6de042f26e9eebc41c9bee8b4d14f753aaIan Rogers  int (*pSet32Static)(uint32_t, void*, int32_t);
149ce9eca6de042f26e9eebc41c9bee8b4d14f753aaIan Rogers  int (*pSet64Static)(uint32_t, void*, int64_t);
150ce9eca6de042f26e9eebc41c9bee8b4d14f753aaIan Rogers  int (*pSetObjStatic)(uint32_t, void*, void*);
151932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers  void (*pThrowStackOverflowFromCode)(void*);
1525ade1d255ef1b5022321ac20493208703b34d2b1buzbee  void (*pThrowNullPointerFromCode)();
1535ade1d255ef1b5022321ac20493208703b34d2b1buzbee  void (*pThrowArrayBoundsFromCode)(int32_t, int32_t);
1545ade1d255ef1b5022321ac20493208703b34d2b1buzbee  void (*pThrowDivZeroFromCode)();
1555ade1d255ef1b5022321ac20493208703b34d2b1buzbee  void (*pThrowVerificationErrorFromCode)(int32_t, int32_t);
1565ade1d255ef1b5022321ac20493208703b34d2b1buzbee  void (*pThrowNegArraySizeFromCode)(int32_t);
1575ade1d255ef1b5022321ac20493208703b34d2b1buzbee  void (*pThrowNoSuchMethodFromCode)(int32_t);
158ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers  void (*pThrowAbstractMethodErrorFromCode)(Method* method, Thread* thread, Method** sp);
159ce9eca6de042f26e9eebc41c9bee8b4d14f753aaIan Rogers  void (*pUnlockObjectFromCode)(void*);
1606a4be3a6226cec645cf905dd352e44f7968a7fa4Brian Carlstrom  void* (*pUnresolvedDirectMethodTrampolineFromCode)(int32_t, Method**, Thread*,
161ce9eca6de042f26e9eebc41c9bee8b4d14f753aaIan Rogers                                                     Runtime::TrampolineType);
162c143c55718342519db5398e41dda31422cf16c79buzbee
1639b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao  class StackVisitor {
1649b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao   public:
1650cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers    virtual ~StackVisitor() {}
166bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers    virtual void VisitFrame(const Frame& frame, uintptr_t pc) = 0;
1679b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao  };
1689b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao
16961e019d291583029c01b61b93bea750f2b663c37Carl Shapiro  // Creates a new thread.
170d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes  static void Create(Object* peer, size_t stack_size);
17161e019d291583029c01b61b93bea750f2b663c37Carl Shapiro
17261e019d291583029c01b61b93bea750f2b663c37Carl Shapiro  // Creates a new thread from the calling thread.
173dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes  static Thread* Attach(const Runtime* runtime, const char* name, bool as_daemon);
174b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro
175caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom  // Reset internal state of child thread after fork.
176caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom  void InitAfterFork();
177caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom
178b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro  static Thread* Current() {
179d0e7e777c412071a911dd2c008b20b7d879fd392Carl Shapiro    void* thread = pthread_getspecific(Thread::pthread_key_self_);
180d0e7e777c412071a911dd2c008b20b7d879fd392Carl Shapiro    return reinterpret_cast<Thread*>(thread);
1810e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro  }
1820e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
183761928d24e4e7ed7776b52243eaf9095ad35f448Elliott Hughes  static Thread* FromManagedThread(Object* thread_peer);
18401158d7a57c8321370667a6045220237d16e0da8Elliott Hughes  static Thread* FromManagedThread(JNIEnv* env, jobject thread);
1858e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes  static uint32_t LockOwnerFromThreadLock(Object* thread_lock);
1868daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
187899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes  // When full == true, dumps the detailed thread state and the thread stack (used for SIGQUIT).
188899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes  // When full == false, dumps a one-line summary of thread state (used for operator<<).
189899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes  void Dump(std::ostream& os, bool full = true) const;
190a09576416788b916095739e43a16917e7948f3a4Elliott Hughes
1910cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers  State GetState() const {
1920cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers    return state_;
1930cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers  }
1940cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers
1958d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes  State SetState(State new_state);
1968d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes
197038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes  bool IsDaemon();
198761928d24e4e7ed7776b52243eaf9095ad35f448Elliott Hughes  bool IsSuspended();
199038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes
2008d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes  void WaitUntilSuspended();
2010cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers
2025f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  bool HoldsLock(Object*);
2035f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
2048daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  /*
2058daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes   * Changes the priority of this thread to match that of the java.lang.Thread object.
2068daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes   *
2078daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes   * We map a priority value from 1-10 to Linux "nice" values, where lower
2088daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes   * numbers indicate higher priority.
2098daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes   */
2108daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  void SetNativePriority(int newPriority);
2118daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
2128daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  /*
2138daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes   * Returns the thread priority for the current thread by querying the system.
2148daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes   * This is useful when attaching a thread through JNI.
2158daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes   *
2168daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes   * Returns a value from 1 to 10 (compatible with java.lang.Thread values).
2178daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes   */
2188daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  static int GetNativePriority();
2198daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
2200cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers  bool CanAccessDirectReferences() const {
221caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom#ifdef MOVING_GARBAGE_COLLECTOR
222a59d1793b89d0fd62c7544c94da4b6e5dac95ad5Elliott Hughes    // TODO: when we have a moving collector, we'll need: return state_ == kRunnable;
223caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom#endif
224a59d1793b89d0fd62c7544c94da4b6e5dac95ad5Elliott Hughes    return true;
2250cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers  }
2260cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers
227dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes  uint32_t GetThinLockId() const {
228dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes    return thin_lock_id_;
229b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro  }
230b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro
231d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes  pid_t GetTid() const {
232d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes    return tid_;
233d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes  }
234e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes
235fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes  // Returns the java.lang.Thread's name, or NULL.
236899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes  String* GetThreadName() const;
237899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes
238899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes  // Sets the thread's name.
239899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes  void SetThreadName(const char* name);
240fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes
241d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes  Object* GetPeer() const {
2428daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes    return peer_;
2438daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  }
2448daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
245a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  Object* GetThreadGroup() const;
246a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
2479d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  RuntimeStats* GetStats() {
2489d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    return &stats_;
2499d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  }
2509d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
251d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  int GetSuspendCount() const {
252d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    return suspend_count_;
253d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
2540cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers
255d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // Returns the current Method* and native PC (not dex PC) for this thread.
256d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  Method* GetCurrentMethod(uintptr_t* pc = NULL, Method*** sp = NULL) const;
25733dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao
2580e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro  bool IsExceptionPending() const {
259b20a554613609dc372073d2ebd9fbc3925a429f5Elliott Hughes    return exception_ != NULL;
2600e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro  }
2610e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
262e5b0dc83537bf915c6abe4efeae6e501daf75a27Elliott Hughes  Throwable* GetException() const {
2630cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers    DCHECK(CanAccessDirectReferences());
2640e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro    return exception_;
2650e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro  }
2660e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
2670cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers  void SetException(Throwable* new_exception) {
2680cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers    DCHECK(CanAccessDirectReferences());
2690cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers    CHECK(new_exception != NULL);
2700cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers    // TODO: CHECK(exception_ == NULL);
2710cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers    exception_ = new_exception;  // TODO
2720cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers  }
2730cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers
2740cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers  void ClearException() {
2750cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers    exception_ = NULL;
276a09576416788b916095739e43a16917e7948f3a4Elliott Hughes  }
277a09576416788b916095739e43a16917e7948f3a4Elliott Hughes
278bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers  // Find catch block and perform long jump to appropriate exception handle
279ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers  void DeliverException();
280bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers
281bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers  Context* GetLongJumpContext();
282bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers
2831a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao  Frame GetTopOfStack() const {
2841a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao    return top_of_managed_stack_;
2851a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao  }
2861a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao
2871a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao  // TODO: this is here for testing, remove when we have exception unit tests
2881a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao  // that use the real stack
289bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers  void SetTopOfStack(void* stack, uintptr_t pc) {
2900cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers    top_of_managed_stack_.SetSP(reinterpret_cast<Method**>(stack));
291bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers    top_of_managed_stack_pc_ = pc;
2920e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro  }
2930e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
294bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers  void SetTopOfStackPC(uintptr_t pc) {
295bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers    top_of_managed_stack_pc_ = pc;
296bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers  }
297bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers
2985cb5ad27944efb08d4556b3c0d362302e37e832bElliott Hughes  // 'msg' may be NULL.
2995cb5ad27944efb08d4556b3c0d362302e37e832bElliott Hughes  void ThrowNewException(const char* exception_class_descriptor, const char* msg);
3005cb5ad27944efb08d4556b3c0d362302e37e832bElliott Hughes
3015cb5ad27944efb08d4556b3c0d362302e37e832bElliott Hughes  void ThrowNewExceptionF(const char* exception_class_descriptor, const char* fmt, ...)
302362f9bc807169bcfc8761dde067bbfb79b5ad0fdElliott Hughes      __attribute__((format(printf, 3, 4)));
303a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes
3044a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes  void ThrowNewExceptionV(const char* exception_class_descriptor, const char* fmt, va_list ap);
3054a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes
3062ced6a534157d5d963693346904389c19775d2daElliott Hughes  // OutOfMemoryError is special, because we need to pre-allocate an instance.
3072ced6a534157d5d963693346904389c19775d2daElliott Hughes  void ThrowOutOfMemoryError(const char* msg);
308418dfe7849f45535b5388a91bd7a16cfc20a612bElliott Hughes  void ThrowOutOfMemoryError(Class* c, size_t byte_count);
30979082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughes
3101a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao  Frame FindExceptionHandler(void* throw_pc, void** handler_pc);
3111a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao
3121a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao  void* FindExceptionHandlerInMethod(const Method* method,
3131a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao                                     void* throw_pc,
3141a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao                                     const DexFile& dex_file,
3151a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao                                     ClassLinker* class_linker);
316c143c55718342519db5398e41dda31422cf16c79buzbee
317be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  static void Startup();
318038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes  static void FinishStartup();
319c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes  static void Shutdown();
320b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro
321b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers  // JNI methods
32269f5bc6759f256a146eefd8a7141d39fcc3b0421Elliott Hughes  JNIEnvExt* GetJniEnv() const {
323b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers    return jni_env_;
324b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers  }
325b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers
326408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers  // Number of references allocated in SIRTs on this thread
327408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers  size_t NumSirtReferences();
328a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers
329408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers  // Is the given obj in this thread's stack indirect reference table?
330408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers  bool SirtContains(jobject obj);
331408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers
3328dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao  void SirtVisitRoots(Heap::RootVisitor* visitor, void* arg);
3338dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao
334408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers  // Convert a jobject into a Object*
335408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers  Object* DecodeJObject(jobject obj);
336b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers
3378daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  // Implements java.lang.Thread.interrupted.
3388daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  bool Interrupted() {
33985d1545e985ac689db4bad7849880e843707c862Elliott Hughes    MutexLock mu(*wait_mutex_);
3408daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes    bool interrupted = interrupted_;
3418daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes    interrupted_ = false;
3428daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes    return interrupted;
3438daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  }
3448daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
3458daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  // Implements java.lang.Thread.isInterrupted.
3468daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  bool IsInterrupted() {
34785d1545e985ac689db4bad7849880e843707c862Elliott Hughes    MutexLock mu(*wait_mutex_);
3488daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes    return interrupted_;
3498daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  }
3508daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
3515f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  void Interrupt() {
35285d1545e985ac689db4bad7849880e843707c862Elliott Hughes    MutexLock mu(*wait_mutex_);
3535f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    if (interrupted_) {
3545f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes      return;
3555f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    }
3565f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    interrupted_ = true;
3575f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    NotifyLocked();
3585f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
3595f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
3605f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  void Notify() {
36185d1545e985ac689db4bad7849880e843707c862Elliott Hughes    MutexLock mu(*wait_mutex_);
3625f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    NotifyLocked();
3635f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
3645f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
3656de0860c491d390b7a53be436519ef2d9234c4c9Ian Rogers  // Linked list recording transitions from native to managed code
366b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers  void PushNativeToManagedRecord(NativeToManagedRecord* record);
367b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers  void PopNativeToManagedRecord(const NativeToManagedRecord& record);
3686de0860c491d390b7a53be436519ef2d9234c4c9Ian Rogers
369bffb15585b8fd43d3ca534ddbb85e7f591595951Brian Carlstrom  const ClassLoader* GetClassLoaderOverride() {
3700cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers    // TODO: need to place the class_loader_override_ in a handle
3710cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers    // DCHECK(CanAccessDirectReferences());
372c143c55718342519db5398e41dda31422cf16c79buzbee    return class_loader_override_;
373c143c55718342519db5398e41dda31422cf16c79buzbee  }
374c143c55718342519db5398e41dda31422cf16c79buzbee
375bffb15585b8fd43d3ca534ddbb85e7f591595951Brian Carlstrom  void SetClassLoaderOverride(const ClassLoader* class_loader_override) {
376c143c55718342519db5398e41dda31422cf16c79buzbee    class_loader_override_ = class_loader_override;
377c143c55718342519db5398e41dda31422cf16c79buzbee  }
378c143c55718342519db5398e41dda31422cf16c79buzbee
379aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers  // Create the internal representation of a stack trace, that is more time
380aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers  // and space efficient to compute than the StackTraceElement[]
38101158d7a57c8321370667a6045220237d16e0da8Elliott Hughes  jobject CreateInternalStackTrace(JNIEnv* env) const;
38201158d7a57c8321370667a6045220237d16e0da8Elliott Hughes
38301158d7a57c8321370667a6045220237d16e0da8Elliott Hughes  // Convert an internal stack trace representation (returned by CreateInternalStackTrace) to a
38401158d7a57c8321370667a6045220237d16e0da8Elliott Hughes  // StackTraceElement[]. If output_array is NULL, a new array is created, otherwise as many
38501158d7a57c8321370667a6045220237d16e0da8Elliott Hughes  // frames as will fit are written into the given array. If stack_depth is non-NULL, it's updated
38601158d7a57c8321370667a6045220237d16e0da8Elliott Hughes  // with the number of valid frames in the returned array.
38701158d7a57c8321370667a6045220237d16e0da8Elliott Hughes  static jobjectArray InternalStackTraceToStackTraceElementArray(JNIEnv* env, jobject internal,
38801158d7a57c8321370667a6045220237d16e0da8Elliott Hughes      jobjectArray output_array = NULL, int* stack_depth = NULL);
38955df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao
390d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers  void VisitRoots(Heap::RootVisitor* visitor, void* arg);
391410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes
392be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  //
393be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  // Offsets of various members of native Thread class, used by compiled code.
394be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  //
395be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes
396be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  static ThreadOffset SelfOffset() {
397be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes    return ThreadOffset(OFFSETOF_MEMBER(Thread, self_));
398be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  }
399be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes
400be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  static ThreadOffset ExceptionOffset() {
401be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes    return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_));
402be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  }
403be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes
40454e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes  static ThreadOffset ThinLockIdOffset() {
405be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes    return ThreadOffset(OFFSETOF_MEMBER(Thread, thin_lock_id_));
406be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  }
407be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes
408be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  static ThreadOffset CardTableOffset() {
409be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes    return ThreadOffset(OFFSETOF_MEMBER(Thread, card_table_));
410be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  }
411be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes
412be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  static ThreadOffset SuspendCountOffset() {
413be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes    return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_));
414be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  }
415be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes
416be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  static ThreadOffset StateOffset() {
41793e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes    return ThreadOffset(OFFSETOF_VOLATILE_MEMBER(Thread, state_));
418be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  }
419be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes
420932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers  // Size of stack less any space reserved for stack overflow
421932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers  size_t GetStackSize() {
422932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers    return stack_size_ - (stack_end_ - stack_base_);
423932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers  }
424932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers
425932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers  // Set the stack end to that to be used during a stack overflow
426932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers  void SetStackEndForStackOverflow() {
427932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers    // During stack overflow we allow use of the full stack
4283b6baaa203fa63f1522b2172a1645f90412afdaeElliott Hughes    if (stack_end_ == stack_base_) {
4293b6baaa203fa63f1522b2172a1645f90412afdaeElliott Hughes      DumpStack(std::cerr);
4303b6baaa203fa63f1522b2172a1645f90412afdaeElliott Hughes      LOG(FATAL) << "Need to increase kStackOverflowReservedBytes (currently "
4313b6baaa203fa63f1522b2172a1645f90412afdaeElliott Hughes                 << kStackOverflowReservedBytes << ")";
4323b6baaa203fa63f1522b2172a1645f90412afdaeElliott Hughes    }
4333b6baaa203fa63f1522b2172a1645f90412afdaeElliott Hughes
434932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers    stack_end_ = stack_base_;
435932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers  }
436932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers
437932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers  // Set the stack end to that to be used during regular execution
438932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers  void ResetDefaultStackEnd() {
439932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers    // Our stacks grow down, so we want stack_end_ to be near there, but reserving enough room
440932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers    // to throw a StackOverflowError.
441932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers    stack_end_ = stack_base_ + kStackOverflowReservedBytes;
442932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers  }
443932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers
444449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes  static ThreadOffset StackEndOffset() {
445449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes    return ThreadOffset(OFFSETOF_MEMBER(Thread, stack_end_));
446be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  }
447be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes
448be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  static ThreadOffset JniEnvOffset() {
449be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes    return ThreadOffset(OFFSETOF_MEMBER(Thread, jni_env_));
450be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  }
451be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes
452be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  static ThreadOffset TopOfManagedStackOffset() {
453be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes    return ThreadOffset(OFFSETOF_MEMBER(Thread, top_of_managed_stack_) +
454be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes        OFFSETOF_MEMBER(Frame, sp_));
455be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  }
456be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes
457bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers  static ThreadOffset TopOfManagedStackPcOffset() {
458bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers    return ThreadOffset(OFFSETOF_MEMBER(Thread, top_of_managed_stack_pc_));
459bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers  }
460bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers
46140381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom  void PushSirt(StackIndirectReferenceTable* sirt);
46240381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom  StackIndirectReferenceTable* PopSirt();
46340381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom
464be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  static ThreadOffset TopSirtOffset() {
465be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes    return ThreadOffset(OFFSETOF_MEMBER(Thread, top_sirt_));
466be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  }
467be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes
4689407c60800c95902fba0b3c3265520d47c1e7052Shih-wei Liao  void WalkStack(StackVisitor* visitor) const;
4699407c60800c95902fba0b3c3265520d47c1e7052Shih-wei Liao
470475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  DebugInvokeReq* GetInvokeReq() {
471475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes    return debug_invoke_req_;
472475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  }
473475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
474e343b76af81a005ef64f5e75a555389fd9147dabjeffhao  bool IsTraceStackEmpty() const {
475e343b76af81a005ef64f5e75a555389fd9147dabjeffhao    return trace_stack_->empty();
476e343b76af81a005ef64f5e75a555389fd9147dabjeffhao  }
477e343b76af81a005ef64f5e75a555389fd9147dabjeffhao
478a9ef3fd82bebc6370fc3ddbb094988feb6c83022jeffhao  TraceStackFrame GetTraceStackFrame(uint32_t depth) const {
479a9ef3fd82bebc6370fc3ddbb094988feb6c83022jeffhao    return trace_stack_->at(trace_stack_->size() - depth - 1);
480a9ef3fd82bebc6370fc3ddbb094988feb6c83022jeffhao  }
481a9ef3fd82bebc6370fc3ddbb094988feb6c83022jeffhao
482e343b76af81a005ef64f5e75a555389fd9147dabjeffhao  void PushTraceStackFrame(const TraceStackFrame& frame) {
483e343b76af81a005ef64f5e75a555389fd9147dabjeffhao    trace_stack_->push_back(frame);
484e343b76af81a005ef64f5e75a555389fd9147dabjeffhao  }
485e343b76af81a005ef64f5e75a555389fd9147dabjeffhao
486e343b76af81a005ef64f5e75a555389fd9147dabjeffhao  TraceStackFrame PopTraceStackFrame() {
487e343b76af81a005ef64f5e75a555389fd9147dabjeffhao    TraceStackFrame frame = trace_stack_->back();
488e343b76af81a005ef64f5e75a555389fd9147dabjeffhao    trace_stack_->pop_back();
489e343b76af81a005ef64f5e75a555389fd9147dabjeffhao    return frame;
490e343b76af81a005ef64f5e75a555389fd9147dabjeffhao  }
491e343b76af81a005ef64f5e75a555389fd9147dabjeffhao
4920e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro private:
493dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes  Thread();
494c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes  ~Thread();
49502b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes  friend class ThreadList;  // For ~Thread.
4960e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
4975fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes  void CreatePeer(const char* name, bool as_daemon);
4985fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes  friend class Runtime; // For CreatePeer.
4995fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes
500d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes  void DumpState(std::ostream& os) const;
501d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes  void DumpStack(std::ostream& os) const;
502d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes
503accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes  // Out-of-line conveniences for debugging in gdb.
504498508c1187dc07d3eae5476784cde20f5224d93Elliott Hughes  static Thread* CurrentFromGdb(); // Like Thread::Current.
505accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes  void DumpFromGdb() const; // Like Thread::Dump(std::cerr).
506accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes
50793e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes  void Attach(const Runtime* runtime);
50893e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes  static void* CreateCallback(void* arg);
50993e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes
510accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes  void HandleUncaughtExceptions();
5114514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom  void RemoveFromThreadGroup();
512accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes
5135d76c435082332ef79a22962386fa92a0870e378Ian Rogers  void InitCardTable();
514b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers  void InitCpu();
5153ea4ec5629613013ad9b0d7a69abdb94491ac46fbuzbee  void InitFunctionPointers();
516caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom  void InitTid();
517caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom  void InitPthreadKeySelf();
518be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  void InitStackHwm();
519be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes
5205f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  void NotifyLocked() {
5215f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    if (wait_monitor_ != NULL) {
52285d1545e985ac689db4bad7849880e843707c862Elliott Hughes      wait_cond_->Signal();
5235f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    }
5245f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
5255f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
526be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  static void ThreadExitCallback(void* arg);
527b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers
52867375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers  void WalkStackUntilUpCall(StackVisitor* visitor, bool include_upcall) const;
529bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers
530dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes  // Thin lock thread id. This is a small integer used by the thin lock implementation.
531dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes  // This is not to be confused with the native thread's tid, nor is it the value returned
532dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes  // by java.lang.Thread.getId --- this is a distinct value, used only for locking. One
533dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes  // important difference between this id and the ids visible to managed code is that these
534dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes  // ones get reused (to ensure that they fit in the number of bits available).
535dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes  uint32_t thin_lock_id_;
536b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers
537d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes  // System thread id.
538d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes  pid_t tid_;
539d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes
540dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes  // Our managed peer (an instance of java.lang.Thread).
541d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes  Object* peer_;
5428daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
5438e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes  // The top_of_managed_stack_ and top_of_managed_stack_pc_ fields are accessed from
5448e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes  // compiled code, so we keep them early in the structure to (a) avoid having to keep
5458e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes  // fixing the assembler offsets and (b) improve the chances that these will still be aligned.
5468e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes
5478e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes  // Top of the managed stack, written out prior to the state transition from
54868e76526e98432625464022cb26f66b9ef6f5af4Elliott Hughes  // kRunnable to kNative. Uses include giving the starting point for scanning
5498e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes  // a managed stack when a thread is in native code.
5508e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes  Frame top_of_managed_stack_;
5518e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes  // PC corresponding to the call out of the top_of_managed_stack_ frame
5528e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes  uintptr_t top_of_managed_stack_pc_;
5538e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes
5548daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  // Guards the 'interrupted_' and 'wait_monitor_' members.
55585d1545e985ac689db4bad7849880e843707c862Elliott Hughes  mutable Mutex* wait_mutex_;
55685d1545e985ac689db4bad7849880e843707c862Elliott Hughes  ConditionVariable* wait_cond_;
5578daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  // Pointer to the monitor lock we're currently waiting on (or NULL), guarded by wait_mutex_.
5588daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  Monitor* wait_monitor_;
5598daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  // Thread "interrupted" status; stays raised until queried or thrown, guarded by wait_mutex_.
5608e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes  uint32_t interrupted_;
5615f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  // The next thread in the wait set this thread is part of.
5625f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  Thread* wait_next_;
5638e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes  // If we're blocked in MonitorEnter, this is the object we're trying to lock.
5648e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes  Object* monitor_enter_object_;
5655f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
5665f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  friend class Monitor;
567dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes
5689d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  RuntimeStats stats_;
5699d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
5705d76c435082332ef79a22962386fa92a0870e378Ian Rogers  // The biased card table, see CardTable for details
5715d76c435082332ef79a22962386fa92a0870e378Ian Rogers  byte* card_table_;
572c143c55718342519db5398e41dda31422cf16c79buzbee
573449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes  // The end of this thread's stack. This is the lowest safely-addressable address on the stack.
574449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes  // We leave extra space so there's room for the code that throws StackOverflowError.
575449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes  byte* stack_end_;
576be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes
577932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers  // Size of the stack
578932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers  size_t stack_size_;
579932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers
580932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers  // The "lowest addressable byte" of the stack
581932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers  byte* stack_base_;
582932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers
5836de0860c491d390b7a53be436519ef2d9234c4c9Ian Rogers  // A linked list (of stack allocated records) recording transitions from
5846de0860c491d390b7a53be436519ef2d9234c4c9Ian Rogers  // native to managed code.
5856de0860c491d390b7a53be436519ef2d9234c4c9Ian Rogers  NativeToManagedRecord* native_to_managed_record_;
5866de0860c491d390b7a53be436519ef2d9234c4c9Ian Rogers
587408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers  // Top of linked list of stack indirect reference tables or NULL for none
588408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers  StackIndirectReferenceTable* top_sirt_;
589b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers
590b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers  // Every thread may have an associated JNI environment
59169f5bc6759f256a146eefd8a7141d39fcc3b0421Elliott Hughes  JNIEnvExt* jni_env_;
592b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers
59393e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes  volatile State state_;
594b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro
59569759eaa6fd4386f1e6d8748052ad221087b3476Carl Shapiro  // Initialized to "this". On certain architectures (such as x86) reading
59669759eaa6fd4386f1e6d8748052ad221087b3476Carl Shapiro  // off of Thread::Current is easy but getting the address of Thread::Current
59769759eaa6fd4386f1e6d8748052ad221087b3476Carl Shapiro  // is hard. This field can be read off of Thread::Current to give the address.
59869759eaa6fd4386f1e6d8748052ad221087b3476Carl Shapiro  Thread* self_;
59969759eaa6fd4386f1e6d8748052ad221087b3476Carl Shapiro
60069759eaa6fd4386f1e6d8748052ad221087b3476Carl Shapiro  Runtime* runtime_;
60169759eaa6fd4386f1e6d8748052ad221087b3476Carl Shapiro
60269759eaa6fd4386f1e6d8748052ad221087b3476Carl Shapiro  // The pending exception or NULL.
603e5b0dc83537bf915c6abe4efeae6e501daf75a27Elliott Hughes  Throwable* exception_;
6040e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
60545a76cb99104a222d6a9bd768a084893dcb7cf30Ian Rogers  // A non-zero value is used to tell the current thread to enter a safe point
60645a76cb99104a222d6a9bd768a084893dcb7cf30Ian Rogers  // at the next poll.
60745a76cb99104a222d6a9bd768a084893dcb7cf30Ian Rogers  int suspend_count_;
608234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes  // How much of 'suspend_count_' is by request of the debugger, used to set things right
609234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes  // when the debugger detaches. Must be <= suspend_count_.
610234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes  int debug_suspend_count_;
61145a76cb99104a222d6a9bd768a084893dcb7cf30Ian Rogers
612edcc09c737b00462881f147602656739d029571eElliott Hughes  // Needed to get the right ClassLoader in JNI_OnLoad, but also
613edcc09c737b00462881f147602656739d029571eElliott Hughes  // useful for testing.
614bffb15585b8fd43d3ca534ddbb85e7f591595951Brian Carlstrom  const ClassLoader* class_loader_override_;
615c143c55718342519db5398e41dda31422cf16c79buzbee
616bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers  // Thread local, lazily allocated, long jump context. Used to deliver exceptions.
61785d1545e985ac689db4bad7849880e843707c862Elliott Hughes  Context* long_jump_context_;
618bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers
619418dfe7849f45535b5388a91bd7a16cfc20a612bElliott Hughes  // A boolean telling us whether we're recursively throwing OOME.
620726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes  uint32_t throwing_OutOfMemoryError_;
621726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes
622726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes  Throwable* pre_allocated_OutOfMemoryError_;
623418dfe7849f45535b5388a91bd7a16cfc20a612bElliott Hughes
624475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  // JDWP invoke-during-breakpoint support.
625475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  DebugInvokeReq* debug_invoke_req_;
626475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
62769759eaa6fd4386f1e6d8748052ad221087b3476Carl Shapiro  // TLS key used to retrieve the VM thread object.
628b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro  static pthread_key_t pthread_key_self_;
629b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro
630e343b76af81a005ef64f5e75a555389fd9147dabjeffhao  // Additional stack used by method tracer to store method and return pc values.
631e343b76af81a005ef64f5e75a555389fd9147dabjeffhao  // Stored as a pointer since std::vector is not PACKED.
632e343b76af81a005ef64f5e75a555389fd9147dabjeffhao  std::vector<TraceStackFrame>* trace_stack_;
633e343b76af81a005ef64f5e75a555389fd9147dabjeffhao
634899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes  // A cached copy of the java.lang.Thread's name.
635899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes  std::string* name_;
636899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes
6370e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro  DISALLOW_COPY_AND_ASSIGN(Thread);
6380e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro};
639bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers
640330304de14dc7118b45b8e7b5bd11a172fa61701Elliott Hughesstd::ostream& operator<<(std::ostream& os, const Thread& thread);
641b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogersstd::ostream& operator<<(std::ostream& os, const Thread::State& state);
6420e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
643ad7c2a3b4daa2abd707375444f4b0db7d69a3838Elliott Hughesclass ScopedThreadStateChange {
644ad7c2a3b4daa2abd707375444f4b0db7d69a3838Elliott Hughes public:
645ad7c2a3b4daa2abd707375444f4b0db7d69a3838Elliott Hughes  ScopedThreadStateChange(Thread* thread, Thread::State new_state) : thread_(thread) {
646ad7c2a3b4daa2abd707375444f4b0db7d69a3838Elliott Hughes    old_thread_state_ = thread_->SetState(new_state);
647ad7c2a3b4daa2abd707375444f4b0db7d69a3838Elliott Hughes  }
648ad7c2a3b4daa2abd707375444f4b0db7d69a3838Elliott Hughes
649ad7c2a3b4daa2abd707375444f4b0db7d69a3838Elliott Hughes  ~ScopedThreadStateChange() {
650ad7c2a3b4daa2abd707375444f4b0db7d69a3838Elliott Hughes    thread_->SetState(old_thread_state_);
651ad7c2a3b4daa2abd707375444f4b0db7d69a3838Elliott Hughes  }
652ad7c2a3b4daa2abd707375444f4b0db7d69a3838Elliott Hughes
653ad7c2a3b4daa2abd707375444f4b0db7d69a3838Elliott Hughes private:
654ad7c2a3b4daa2abd707375444f4b0db7d69a3838Elliott Hughes  Thread* thread_;
655ad7c2a3b4daa2abd707375444f4b0db7d69a3838Elliott Hughes  Thread::State old_thread_state_;
656ad7c2a3b4daa2abd707375444f4b0db7d69a3838Elliott Hughes  DISALLOW_COPY_AND_ASSIGN(ScopedThreadStateChange);
657ad7c2a3b4daa2abd707375444f4b0db7d69a3838Elliott Hughes};
658ad7c2a3b4daa2abd707375444f4b0db7d69a3838Elliott Hughes
6590e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro}  // namespace art
6600e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
6610e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro#endif  // ART_SRC_THREAD_H_
662