< prev index next >

src/hotspot/share/runtime/thread.hpp

Print this page




  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_RUNTIME_THREAD_HPP
  26 #define SHARE_RUNTIME_THREAD_HPP
  27 
  28 #include "jni.h"
  29 #include "code/compiledMethod.hpp"
  30 #include "gc/shared/gcThreadLocalData.hpp"
  31 #include "gc/shared/threadLocalAllocBuffer.hpp"
  32 #include "memory/allocation.hpp"
  33 #include "oops/oop.hpp"
  34 #include "prims/jvmtiExport.hpp"

  35 #include "runtime/frame.hpp"
  36 #include "runtime/globals.hpp"
  37 #include "runtime/handshake.hpp"
  38 #include "runtime/javaFrameAnchor.hpp"
  39 #include "runtime/jniHandles.hpp"
  40 #include "runtime/mutexLocker.hpp"
  41 #include "runtime/os.hpp"
  42 #include "runtime/osThread.hpp"
  43 #include "runtime/park.hpp"
  44 #include "runtime/stubRoutines.hpp"
  45 #include "runtime/threadHeapSampler.hpp"
  46 #include "runtime/threadLocalStorage.hpp"
  47 #include "runtime/threadStatisticalInfo.hpp"
  48 #include "runtime/unhandledOops.hpp"
  49 #include "utilities/align.hpp"
  50 #include "utilities/exceptions.hpp"
  51 #include "utilities/macros.hpp"
  52 #ifdef ZERO
  53 # include "stack_zero.hpp"
  54 #endif


 969 
 970   // Create and start the single instance of WatcherThread, or stop it on shutdown
 971   static void start();
 972   static void stop();
 973   // Only allow start once the VM is sufficiently initialized
 974   // Otherwise the first task to enroll will trigger the start
 975   static void make_startable();
 976  private:
 977   int sleep() const;
 978 };
 979 
 980 
 981 class CompilerThread;
 982 
 983 typedef void (*ThreadFunction)(JavaThread*, TRAPS);
 984 
 985 class JavaThread: public Thread {
 986   friend class VMStructs;
 987   friend class JVMCIVMStructs;
 988   friend class WhiteBox;

 989  private:
 990   bool           _on_thread_list;                // Is set when this JavaThread is added to the Threads list
 991   oop            _threadObj;                     // The Java level thread object
 992 
 993 #ifdef ASSERT
 994  private:
 995   int _java_call_counter;
 996 
 997  public:
 998   int  java_call_counter()                       { return _java_call_counter; }
 999   void inc_java_call_counter()                   { _java_call_counter++; }
1000   void dec_java_call_counter() {
1001     assert(_java_call_counter > 0, "Invalid nesting of JavaCallWrapper");
1002     _java_call_counter--;
1003   }
1004  private:  // restore original namespace restriction
1005 #endif  // ifdef ASSERT
1006 
1007 #ifndef PRODUCT
1008  public:


1014 
1015   JavaFrameAnchor _anchor;                       // Encapsulation of current java frame and it state
1016 
1017   ThreadFunction _entry_point;
1018 
1019   JNIEnv        _jni_environment;
1020 
1021   // Deopt support
1022   DeoptResourceMark*  _deopt_mark;               // Holds special ResourceMark for deoptimization
1023 
1024   intptr_t*      _must_deopt_id;                 // id of frame that needs to be deopted once we
1025                                                  // transition out of native
1026   CompiledMethod*       _deopt_nmethod;         // CompiledMethod that is currently being deoptimized
1027   vframeArray*  _vframe_array_head;              // Holds the heap of the active vframeArrays
1028   vframeArray*  _vframe_array_last;              // Holds last vFrameArray we popped
1029   // Because deoptimization is lazy we must save jvmti requests to set locals
1030   // in compiled frames until we deoptimize and we have an interpreter frame.
1031   // This holds the pointer to array (yeah like there might be more than one) of
1032   // description of compiled vframes that have locals that need to be updated.
1033   GrowableArray<jvmtiDeferredLocalVariableSet*>* _deferred_locals_updates;

1034 
1035   // Handshake value for fixing 6243940. We need a place for the i2c
1036   // adapter to store the callee Method*. This value is NEVER live
1037   // across a gc point so it does NOT have to be gc'd
1038   // The handshake is open ended since we can't be certain that it will
1039   // be NULLed. This is because we rarely ever see the race and end up
1040   // in handle_wrong_method which is the backend of the handshake. See
1041   // code in i2c adapters and handle_wrong_method.
1042 
1043   Method*       _callee_target;
1044 
1045   // Used to pass back results to the interpreter or generated code running Java code.
1046   oop           _vm_result;    // oop result is GC-preserved
1047   Metadata*     _vm_result_2;  // non-oop result
1048 
1049   // See ReduceInitialCardMarks: this holds the precise space interval of
1050   // the most recent slow path allocation for which compiled code has
1051   // elided card-marks for performance along the fast-path.
1052   MemRegion     _deferred_card_mark;
1053 


1091   volatile bool         _suspend_equivalent;     // Suspend equivalent condition
1092   jint                  _in_deopt_handler;       // count of deoptimization
1093                                                  // handlers thread is in
1094   volatile bool         _doing_unsafe_access;    // Thread may fault due to unsafe access
1095   bool                  _do_not_unlock_if_synchronized;  // Do not unlock the receiver of a synchronized method (since it was
1096                                                          // never locked) when throwing an exception. Used by interpreter only.
1097 
1098   // JNI attach states:
1099   enum JNIAttachStates {
1100     _not_attaching_via_jni = 1,  // thread is not attaching via JNI
1101     _attaching_via_jni,          // thread is attaching via JNI
1102     _attached_via_jni            // thread has attached via JNI
1103   };
1104 
1105   // A regular JavaThread's _jni_attach_state is _not_attaching_via_jni.
1106   // A native thread that is attaching via JNI starts with a value
1107   // of _attaching_via_jni and transitions to _attached_via_jni.
1108   volatile JNIAttachStates _jni_attach_state;
1109 
1110  public:


1111   // State of the stack guard pages for this thread.
1112   enum StackGuardState {
1113     stack_guard_unused,         // not needed
1114     stack_guard_reserved_disabled,
1115     stack_guard_yellow_reserved_disabled,// disabled (temporarily) after stack overflow
1116     stack_guard_enabled         // enabled
1117   };
1118 
1119  private:
1120 
1121 #if INCLUDE_JVMCI
1122   // The _pending_* fields below are used to communicate extra information
1123   // from an uncommon trap in JVMCI compiled code to the uncommon trap handler.
1124 
1125   // Communicates the DeoptReason and DeoptAction of the uncommon trap
1126   int       _pending_deoptimization;
1127 
1128   // Specifies whether the uncommon trap is to bci 0 of a synchronized method
1129   // before the monitor has been acquired.
1130   bool      _pending_monitorenter;


1182  private:
1183   // support for JNI critical regions
1184   jint    _jni_active_critical;                  // count of entries into JNI critical region
1185 
1186   // Checked JNI: function name requires exception check
1187   char* _pending_jni_exception_check_fn;
1188 
1189   // For deadlock detection.
1190   int _depth_first_number;
1191 
1192   // JVMTI PopFrame support
1193   // This is set to popframe_pending to signal that top Java frame should be popped immediately
1194   int _popframe_condition;
1195 
1196   // If reallocation of scalar replaced objects fails, we throw OOM
1197   // and during exception propagation, pop the top
1198   // _frames_to_pop_failed_realloc frames, the ones that reference
1199   // failed reallocations.
1200   int _frames_to_pop_failed_realloc;
1201 





1202 #ifndef PRODUCT
1203   int _jmp_ring_index;
1204   struct {
1205     // We use intptr_t instead of address so debugger doesn't try and display strings
1206     intptr_t _target;
1207     intptr_t _instruction;
1208     const char*  _file;
1209     int _line;
1210   }   _jmp_ring[jump_ring_buffer_size];
1211 #endif // PRODUCT
1212 
1213   friend class VMThread;
1214   friend class ThreadWaitTransition;
1215   friend class VM_Exit;
1216 
1217   void initialize();                             // Initialized the instance variables
1218 
1219  public:
1220   // Constructor
1221   JavaThread(bool is_attaching_via_jni = false); // for main thread and JNI attached threads


1312   // will also be considered terminated.
1313   bool check_is_terminated(TerminatedTypes l_terminated) const {
1314     return l_terminated != _not_terminated && l_terminated != _thread_exiting;
1315   }
1316   bool is_terminated() const;
1317   void set_terminated(TerminatedTypes t);
1318   // special for Threads::remove() which is static:
1319   void set_terminated_value();
1320   void block_if_vm_exited();
1321 
1322   bool doing_unsafe_access()                     { return _doing_unsafe_access; }
1323   void set_doing_unsafe_access(bool val)         { _doing_unsafe_access = val; }
1324 
1325   bool do_not_unlock_if_synchronized()             { return _do_not_unlock_if_synchronized; }
1326   void set_do_not_unlock_if_synchronized(bool val) { _do_not_unlock_if_synchronized = val; }
1327 
1328   inline void set_polling_page_release(void* poll_value);
1329   inline void set_polling_page(void* poll_value);
1330   inline volatile void* get_polling_page();
1331 









1332  private:
1333   // Support for thread handshake operations
1334   HandshakeState _handshake;
1335  public:
1336   void set_handshake_operation(HandshakeOperation* op) {
1337     _handshake.set_operation(this, op);
1338   }
1339 
1340   bool has_handshake() const {
1341     return _handshake.has_operation();
1342   }
1343 
1344   void handshake_process_by_self() {
1345     _handshake.process_by_self(this);
1346   }
1347 
1348   void handshake_process_by_vmthread() {
1349     _handshake.process_by_vmthread(this);
1350   }
1351 


1453 
1454   // utility methods to see if we are doing some kind of suspension
1455   bool is_being_ext_suspended() const            {
1456     MutexLocker ml(SR_lock(), Mutex::_no_safepoint_check_flag);
1457     return is_ext_suspended() || is_external_suspend();
1458   }
1459 
1460   bool is_suspend_equivalent() const             { return _suspend_equivalent; }
1461 
1462   void set_suspend_equivalent()                  { _suspend_equivalent = true; }
1463   void clear_suspend_equivalent()                { _suspend_equivalent = false; }
1464 
1465   // Thread.stop support
1466   void send_thread_stop(oop throwable);
1467   AsyncRequests clear_special_runtime_exit_condition() {
1468     AsyncRequests x = _special_runtime_exit_condition;
1469     _special_runtime_exit_condition = _no_async_condition;
1470     return x;
1471   }
1472 

1473   // Are any async conditions present?
1474   bool has_async_condition() { return (_special_runtime_exit_condition != _no_async_condition); }
1475 
1476   void check_and_handle_async_exceptions(bool check_unsafe_error = true);
1477 
1478   // these next two are also used for self-suspension and async exception support
1479   void handle_special_runtime_exit_condition(bool check_asyncs = true);
1480 
1481   // Return true if JavaThread has an asynchronous condition or
1482   // if external suspension is requested.
1483   bool has_special_runtime_exit_condition() {
1484     // Because we don't use is_external_suspend_with_lock
1485     // it is possible that we won't see an asynchronous external suspend
1486     // request that has just gotten started, i.e., SR_lock grabbed but
1487     // _external_suspend field change either not made yet or not visible
1488     // yet. However, this is okay because the request is asynchronous and
1489     // we will see the new flag value the next time through. It's also
1490     // possible that the external suspend request is dropped after
1491     // we have checked is_external_suspend(), we will recheck its value
1492     // under SR_lock in java_suspend_self().
1493     return (_special_runtime_exit_condition != _no_async_condition) ||
1494             is_external_suspend() || is_trace_suspend();
1495   }
1496 
1497   void set_pending_unsafe_access_error()          { _special_runtime_exit_condition = _async_unsafe_access_error; }
1498 
1499   inline void set_pending_async_exception(oop e);
1500 
1501   // Fast-locking support
1502   bool is_lock_owned(address adr) const;
1503 
1504   // Accessors for vframe array top
1505   // The linked list of vframe arrays are sorted on sp. This means when we
1506   // unpack the head must contain the vframe array to unpack.
1507   void set_vframe_array_head(vframeArray* value) { _vframe_array_head = value; }
1508   vframeArray* vframe_array_head() const         { return _vframe_array_head;  }
1509 
1510   // Side structure for deferring update of java frame locals until deopt occurs
1511   GrowableArray<jvmtiDeferredLocalVariableSet*>* deferred_locals() const { return _deferred_locals_updates; }
1512   void set_deferred_locals(GrowableArray<jvmtiDeferredLocalVariableSet *>* vf) { _deferred_locals_updates = vf; }
1513 



1514   // These only really exist to make debugging deopt problems simpler
1515 
1516   void set_vframe_array_last(vframeArray* value) { _vframe_array_last = value; }
1517   vframeArray* vframe_array_last() const         { return _vframe_array_last;  }
1518 
1519   // The special resourceMark used during deoptimization
1520 
1521   void set_deopt_mark(DeoptResourceMark* value)  { _deopt_mark = value; }
1522   DeoptResourceMark* deopt_mark(void)            { return _deopt_mark; }
1523 
1524   intptr_t* must_deopt_id()                      { return _must_deopt_id; }
1525   void     set_must_deopt_id(intptr_t* id)       { _must_deopt_id = id; }
1526   void     clear_must_deopt_id()                 { _must_deopt_id = NULL; }
1527 
1528   void set_deopt_compiled_method(CompiledMethod* nm)  { _deopt_nmethod = nm; }
1529   CompiledMethod* deopt_compiled_method()        { return _deopt_nmethod; }
1530 
1531   Method*    callee_target() const               { return _callee_target; }
1532   void set_callee_target  (Method* x)          { _callee_target   = x; }
1533 


1787   static ByteSize pending_failed_speculation_offset() { return byte_offset_of(JavaThread, _pending_failed_speculation); }
1788   static ByteSize jvmci_alternate_call_target_offset() { return byte_offset_of(JavaThread, _jvmci._alternate_call_target); }
1789   static ByteSize jvmci_implicit_exception_pc_offset() { return byte_offset_of(JavaThread, _jvmci._implicit_exception_pc); }
1790   static ByteSize jvmci_counters_offset()        { return byte_offset_of(JavaThread, _jvmci_counters); }
1791 #endif // INCLUDE_JVMCI
1792   static ByteSize exception_oop_offset()         { return byte_offset_of(JavaThread, _exception_oop); }
1793   static ByteSize exception_pc_offset()          { return byte_offset_of(JavaThread, _exception_pc); }
1794   static ByteSize exception_handler_pc_offset()  { return byte_offset_of(JavaThread, _exception_handler_pc); }
1795   static ByteSize stack_overflow_limit_offset()  { return byte_offset_of(JavaThread, _stack_overflow_limit); }
1796   static ByteSize is_method_handle_return_offset() { return byte_offset_of(JavaThread, _is_method_handle_return); }
1797   static ByteSize stack_guard_state_offset()     { return byte_offset_of(JavaThread, _stack_guard_state); }
1798   static ByteSize reserved_stack_activation_offset() { return byte_offset_of(JavaThread, _reserved_stack_activation); }
1799   static ByteSize suspend_flags_offset()         { return byte_offset_of(JavaThread, _suspend_flags); }
1800 
1801   static ByteSize do_not_unlock_if_synchronized_offset() { return byte_offset_of(JavaThread, _do_not_unlock_if_synchronized); }
1802   static ByteSize should_post_on_exceptions_flag_offset() {
1803     return byte_offset_of(JavaThread, _should_post_on_exceptions_flag);
1804   }
1805   static ByteSize doing_unsafe_access_offset() { return byte_offset_of(JavaThread, _doing_unsafe_access); }
1806 





1807   // Returns the jni environment for this thread
1808   JNIEnv* jni_environment()                      { return &_jni_environment; }
1809 
1810   static JavaThread* thread_from_jni_environment(JNIEnv* env) {
1811     JavaThread *thread_from_jni_env = (JavaThread*)((intptr_t)env - in_bytes(jni_environment_offset()));
1812     // Only return NULL if thread is off the thread list; starting to
1813     // exit should not return NULL.
1814     if (thread_from_jni_env->is_terminated()) {
1815       thread_from_jni_env->block_if_vm_exited();
1816       return NULL;
1817     } else {
1818       return thread_from_jni_env;
1819     }
1820   }
1821 
1822   // JNI critical regions. These can nest.
1823   bool in_critical()    { return _jni_active_critical > 0; }
1824   bool in_last_critical()  { return _jni_active_critical == 1; }
1825   inline void enter_critical();
1826   void exit_critical() {


1870   // Memory operations
1871   void oops_do(OopClosure* f, CodeBlobClosure* cf);
1872 
1873   // Sweeper operations
1874   virtual void nmethods_do(CodeBlobClosure* cf);
1875 
1876   // RedefineClasses Support
1877   void metadata_do(MetadataClosure* f);
1878 
1879   // Debug method asserting thread states are correct during a handshake operation.
1880   DEBUG_ONLY(void verify_states_for_handshake();)
1881 
1882   // Misc. operations
1883   char* name() const { return (char*)get_thread_name(); }
1884   void print_on(outputStream* st, bool print_extended_info) const;
1885   void print_on(outputStream* st) const { print_on(st, false); }
1886   void print() const;
1887   void print_value();
1888   void print_thread_state_on(outputStream*) const      PRODUCT_RETURN;
1889   void print_thread_state() const                      PRODUCT_RETURN;

1890   void print_on_error(outputStream* st, char* buf, int buflen) const;
1891   void print_name_on_error(outputStream* st, char* buf, int buflen) const;
1892   void verify();
1893   const char* get_thread_name() const;
1894  protected:
1895   // factor out low-level mechanics for use in both normal and error cases
1896   virtual const char* get_thread_name_string(char* buf = NULL, int buflen = 0) const;
1897  public:
1898   const char* get_threadgroup_name() const;
1899   const char* get_parent_name() const;
1900 
1901   // Accessing frames
1902   frame last_frame() {
1903     _anchor.make_walkable(this);
1904     return pd_last_frame();
1905   }
1906   javaVFrame* last_java_vframe(RegisterMap* reg_map);
1907 


1908   // Returns method at 'depth' java or native frames down the stack
1909   // Used for security checks
1910   Klass* security_get_caller_class(int depth);
1911 
1912   // Print stack trace in external format
1913   void print_stack_on(outputStream* st);
1914   void print_stack() { print_stack_on(tty); }
1915 
1916   // Print stack traces in various internal formats
1917   void trace_stack()                             PRODUCT_RETURN;
1918   void trace_stack_from(vframe* start_vf)        PRODUCT_RETURN;
1919   void trace_frames()                            PRODUCT_RETURN;
1920   void trace_oops()                              PRODUCT_RETURN;
1921 
1922   // Print an annotated view of the stack frames
1923   void print_frame_layout(int depth = 0, bool validate_only = false) NOT_DEBUG_RETURN;
1924   void validate_frame_layout() {
1925     print_frame_layout(0, true);
1926   }
1927 




  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_RUNTIME_THREAD_HPP
  26 #define SHARE_RUNTIME_THREAD_HPP
  27 
  28 #include "jni.h"
  29 #include "code/compiledMethod.hpp"
  30 #include "gc/shared/gcThreadLocalData.hpp"
  31 #include "gc/shared/threadLocalAllocBuffer.hpp"
  32 #include "memory/allocation.hpp"
  33 #include "oops/oop.hpp"
  34 #include "prims/jvmtiExport.hpp"
  35 #include "runtime/continuation.hpp"
  36 #include "runtime/frame.hpp"
  37 #include "runtime/globals.hpp"
  38 #include "runtime/handshake.hpp"
  39 #include "runtime/javaFrameAnchor.hpp"
  40 #include "runtime/jniHandles.hpp"
  41 #include "runtime/mutexLocker.hpp"
  42 #include "runtime/os.hpp"
  43 #include "runtime/osThread.hpp"
  44 #include "runtime/park.hpp"
  45 #include "runtime/stubRoutines.hpp"
  46 #include "runtime/threadHeapSampler.hpp"
  47 #include "runtime/threadLocalStorage.hpp"
  48 #include "runtime/threadStatisticalInfo.hpp"
  49 #include "runtime/unhandledOops.hpp"
  50 #include "utilities/align.hpp"
  51 #include "utilities/exceptions.hpp"
  52 #include "utilities/macros.hpp"
  53 #ifdef ZERO
  54 # include "stack_zero.hpp"
  55 #endif


 970 
 971   // Create and start the single instance of WatcherThread, or stop it on shutdown
 972   static void start();
 973   static void stop();
 974   // Only allow start once the VM is sufficiently initialized
 975   // Otherwise the first task to enroll will trigger the start
 976   static void make_startable();
 977  private:
 978   int sleep() const;
 979 };
 980 
 981 
 982 class CompilerThread;
 983 
 984 typedef void (*ThreadFunction)(JavaThread*, TRAPS);
 985 
 986 class JavaThread: public Thread {
 987   friend class VMStructs;
 988   friend class JVMCIVMStructs;
 989   friend class WhiteBox;
 990   friend class Continuation;
 991  private:
 992   bool           _on_thread_list;                // Is set when this JavaThread is added to the Threads list
 993   oop            _threadObj;                     // The Java level thread object
 994 
 995 #ifdef ASSERT
 996  private:
 997   int _java_call_counter;
 998 
 999  public:
1000   int  java_call_counter()                       { return _java_call_counter; }
1001   void inc_java_call_counter()                   { _java_call_counter++; }
1002   void dec_java_call_counter() {
1003     assert(_java_call_counter > 0, "Invalid nesting of JavaCallWrapper");
1004     _java_call_counter--;
1005   }
1006  private:  // restore original namespace restriction
1007 #endif  // ifdef ASSERT
1008 
1009 #ifndef PRODUCT
1010  public:


1016 
1017   JavaFrameAnchor _anchor;                       // Encapsulation of current java frame and it state
1018 
1019   ThreadFunction _entry_point;
1020 
1021   JNIEnv        _jni_environment;
1022 
1023   // Deopt support
1024   DeoptResourceMark*  _deopt_mark;               // Holds special ResourceMark for deoptimization
1025 
1026   intptr_t*      _must_deopt_id;                 // id of frame that needs to be deopted once we
1027                                                  // transition out of native
1028   CompiledMethod*       _deopt_nmethod;         // CompiledMethod that is currently being deoptimized
1029   vframeArray*  _vframe_array_head;              // Holds the heap of the active vframeArrays
1030   vframeArray*  _vframe_array_last;              // Holds last vFrameArray we popped
1031   // Because deoptimization is lazy we must save jvmti requests to set locals
1032   // in compiled frames until we deoptimize and we have an interpreter frame.
1033   // This holds the pointer to array (yeah like there might be more than one) of
1034   // description of compiled vframes that have locals that need to be updated.
1035   GrowableArray<jvmtiDeferredLocalVariableSet*>* _deferred_locals_updates;
1036   GrowableArray<WeakHandle<vm_nmethod_keepalive_data> >* _keepalive_cleanup;
1037 
1038   // Handshake value for fixing 6243940. We need a place for the i2c
1039   // adapter to store the callee Method*. This value is NEVER live
1040   // across a gc point so it does NOT have to be gc'd
1041   // The handshake is open ended since we can't be certain that it will
1042   // be NULLed. This is because we rarely ever see the race and end up
1043   // in handle_wrong_method which is the backend of the handshake. See
1044   // code in i2c adapters and handle_wrong_method.
1045 
1046   Method*       _callee_target;
1047 
1048   // Used to pass back results to the interpreter or generated code running Java code.
1049   oop           _vm_result;    // oop result is GC-preserved
1050   Metadata*     _vm_result_2;  // non-oop result
1051 
1052   // See ReduceInitialCardMarks: this holds the precise space interval of
1053   // the most recent slow path allocation for which compiled code has
1054   // elided card-marks for performance along the fast-path.
1055   MemRegion     _deferred_card_mark;
1056 


1094   volatile bool         _suspend_equivalent;     // Suspend equivalent condition
1095   jint                  _in_deopt_handler;       // count of deoptimization
1096                                                  // handlers thread is in
1097   volatile bool         _doing_unsafe_access;    // Thread may fault due to unsafe access
1098   bool                  _do_not_unlock_if_synchronized;  // Do not unlock the receiver of a synchronized method (since it was
1099                                                          // never locked) when throwing an exception. Used by interpreter only.
1100 
1101   // JNI attach states:
1102   enum JNIAttachStates {
1103     _not_attaching_via_jni = 1,  // thread is not attaching via JNI
1104     _attaching_via_jni,          // thread is attaching via JNI
1105     _attached_via_jni            // thread has attached via JNI
1106   };
1107 
1108   // A regular JavaThread's _jni_attach_state is _not_attaching_via_jni.
1109   // A native thread that is attaching via JNI starts with a value
1110   // of _attaching_via_jni and transitions to _attached_via_jni.
1111   volatile JNIAttachStates _jni_attach_state;
1112 
1113  public:
1114   DEBUG_ONLY(oopDesc* _continuation;)
1115 
1116   // State of the stack guard pages for this thread.
1117   enum StackGuardState {
1118     stack_guard_unused,         // not needed
1119     stack_guard_reserved_disabled,
1120     stack_guard_yellow_reserved_disabled,// disabled (temporarily) after stack overflow
1121     stack_guard_enabled         // enabled
1122   };
1123 
1124  private:
1125 
1126 #if INCLUDE_JVMCI
1127   // The _pending_* fields below are used to communicate extra information
1128   // from an uncommon trap in JVMCI compiled code to the uncommon trap handler.
1129 
1130   // Communicates the DeoptReason and DeoptAction of the uncommon trap
1131   int       _pending_deoptimization;
1132 
1133   // Specifies whether the uncommon trap is to bci 0 of a synchronized method
1134   // before the monitor has been acquired.
1135   bool      _pending_monitorenter;


1187  private:
1188   // support for JNI critical regions
1189   jint    _jni_active_critical;                  // count of entries into JNI critical region
1190 
1191   // Checked JNI: function name requires exception check
1192   char* _pending_jni_exception_check_fn;
1193 
1194   // For deadlock detection.
1195   int _depth_first_number;
1196 
1197   // JVMTI PopFrame support
1198   // This is set to popframe_pending to signal that top Java frame should be popped immediately
1199   int _popframe_condition;
1200 
1201   // If reallocation of scalar replaced objects fails, we throw OOM
1202   // and during exception propagation, pop the top
1203   // _frames_to_pop_failed_realloc frames, the ones that reference
1204   // failed reallocations.
1205   int _frames_to_pop_failed_realloc;
1206 
1207   bool _cont_yield; // a continuation yield is in progress
1208   bool _cont_preempt;
1209   FrameInfo _cont_frame;
1210   int _cont_fastpath;
1211 
1212 #ifndef PRODUCT
1213   int _jmp_ring_index;
1214   struct {
1215     // We use intptr_t instead of address so debugger doesn't try and display strings
1216     intptr_t _target;
1217     intptr_t _instruction;
1218     const char*  _file;
1219     int _line;
1220   }   _jmp_ring[jump_ring_buffer_size];
1221 #endif // PRODUCT
1222 
1223   friend class VMThread;
1224   friend class ThreadWaitTransition;
1225   friend class VM_Exit;
1226 
1227   void initialize();                             // Initialized the instance variables
1228 
1229  public:
1230   // Constructor
1231   JavaThread(bool is_attaching_via_jni = false); // for main thread and JNI attached threads


1322   // will also be considered terminated.
1323   bool check_is_terminated(TerminatedTypes l_terminated) const {
1324     return l_terminated != _not_terminated && l_terminated != _thread_exiting;
1325   }
1326   bool is_terminated() const;
1327   void set_terminated(TerminatedTypes t);
1328   // special for Threads::remove() which is static:
1329   void set_terminated_value();
1330   void block_if_vm_exited();
1331 
1332   bool doing_unsafe_access()                     { return _doing_unsafe_access; }
1333   void set_doing_unsafe_access(bool val)         { _doing_unsafe_access = val; }
1334 
1335   bool do_not_unlock_if_synchronized()             { return _do_not_unlock_if_synchronized; }
1336   void set_do_not_unlock_if_synchronized(bool val) { _do_not_unlock_if_synchronized = val; }
1337 
1338   inline void set_polling_page_release(void* poll_value);
1339   inline void set_polling_page(void* poll_value);
1340   inline volatile void* get_polling_page();
1341 
1342   // Continuation support
1343   bool cont_yield() { return _cont_yield; }
1344   void set_cont_yield(bool x) { _cont_yield = x; }
1345   bool cont_fastpath() { return _cont_fastpath != 0; }
1346   void set_cont_fastpath(bool x) { _cont_fastpath = (int)x; }
1347   bool cont_preempt() { return _cont_preempt; }
1348   void set_cont_preempt(bool x) { _cont_preempt = x; }
1349   FrameInfo* cont_frame() { return &_cont_frame; }
1350 
1351  private:
1352   // Support for thread handshake operations
1353   HandshakeState _handshake;
1354  public:
1355   void set_handshake_operation(HandshakeOperation* op) {
1356     _handshake.set_operation(this, op);
1357   }
1358 
1359   bool has_handshake() const {
1360     return _handshake.has_operation();
1361   }
1362 
1363   void handshake_process_by_self() {
1364     _handshake.process_by_self(this);
1365   }
1366 
1367   void handshake_process_by_vmthread() {
1368     _handshake.process_by_vmthread(this);
1369   }
1370 


1472 
1473   // utility methods to see if we are doing some kind of suspension
1474   bool is_being_ext_suspended() const            {
1475     MutexLocker ml(SR_lock(), Mutex::_no_safepoint_check_flag);
1476     return is_ext_suspended() || is_external_suspend();
1477   }
1478 
1479   bool is_suspend_equivalent() const             { return _suspend_equivalent; }
1480 
1481   void set_suspend_equivalent()                  { _suspend_equivalent = true; }
1482   void clear_suspend_equivalent()                { _suspend_equivalent = false; }
1483 
1484   // Thread.stop support
1485   void send_thread_stop(oop throwable);
1486   AsyncRequests clear_special_runtime_exit_condition() {
1487     AsyncRequests x = _special_runtime_exit_condition;
1488     _special_runtime_exit_condition = _no_async_condition;
1489     return x;
1490   }
1491 
1492   bool is_cont_force_yield() { return cont_preempt(); }
1493   // Are any async conditions present?
1494   bool has_async_condition() { return (_special_runtime_exit_condition != _no_async_condition); }
1495 
1496   void check_and_handle_async_exceptions(bool check_unsafe_error = true);
1497 
1498   // these next two are also used for self-suspension and async exception support
1499   void handle_special_runtime_exit_condition(bool check_asyncs = true);
1500 
1501   // Return true if JavaThread has an asynchronous condition or
1502   // if external suspension is requested.
1503   bool has_special_runtime_exit_condition() {
1504     // Because we don't use is_external_suspend_with_lock
1505     // it is possible that we won't see an asynchronous external suspend
1506     // request that has just gotten started, i.e., SR_lock grabbed but
1507     // _external_suspend field change either not made yet or not visible
1508     // yet. However, this is okay because the request is asynchronous and
1509     // we will see the new flag value the next time through. It's also
1510     // possible that the external suspend request is dropped after
1511     // we have checked is_external_suspend(), we will recheck its value
1512     // under SR_lock in java_suspend_self().
1513     return (_special_runtime_exit_condition != _no_async_condition) ||
1514             is_external_suspend() || is_trace_suspend() || is_cont_force_yield();
1515   }
1516 
1517   void set_pending_unsafe_access_error()          { _special_runtime_exit_condition = _async_unsafe_access_error; }
1518 
1519   inline void set_pending_async_exception(oop e);
1520 
1521   // Fast-locking support
1522   bool is_lock_owned(address adr) const;
1523 
1524   // Accessors for vframe array top
1525   // The linked list of vframe arrays are sorted on sp. This means when we
1526   // unpack the head must contain the vframe array to unpack.
1527   void set_vframe_array_head(vframeArray* value) { _vframe_array_head = value; }
1528   vframeArray* vframe_array_head() const         { return _vframe_array_head;  }
1529 
1530   // Side structure for deferring update of java frame locals until deopt occurs
1531   GrowableArray<jvmtiDeferredLocalVariableSet*>* deferred_locals() const { return _deferred_locals_updates; }
1532   void set_deferred_locals(GrowableArray<jvmtiDeferredLocalVariableSet *>* vf) { _deferred_locals_updates = vf; }
1533 
1534   void set_keepalive_cleanup(GrowableArray<WeakHandle<vm_nmethod_keepalive_data> >* lst) { _keepalive_cleanup = lst; }
1535   GrowableArray<WeakHandle<vm_nmethod_keepalive_data> >* keepalive_cleanup() const { return _keepalive_cleanup; }
1536 
1537   // These only really exist to make debugging deopt problems simpler
1538 
1539   void set_vframe_array_last(vframeArray* value) { _vframe_array_last = value; }
1540   vframeArray* vframe_array_last() const         { return _vframe_array_last;  }
1541 
1542   // The special resourceMark used during deoptimization
1543 
1544   void set_deopt_mark(DeoptResourceMark* value)  { _deopt_mark = value; }
1545   DeoptResourceMark* deopt_mark(void)            { return _deopt_mark; }
1546 
1547   intptr_t* must_deopt_id()                      { return _must_deopt_id; }
1548   void     set_must_deopt_id(intptr_t* id)       { _must_deopt_id = id; }
1549   void     clear_must_deopt_id()                 { _must_deopt_id = NULL; }
1550 
1551   void set_deopt_compiled_method(CompiledMethod* nm)  { _deopt_nmethod = nm; }
1552   CompiledMethod* deopt_compiled_method()        { return _deopt_nmethod; }
1553 
1554   Method*    callee_target() const               { return _callee_target; }
1555   void set_callee_target  (Method* x)          { _callee_target   = x; }
1556 


1810   static ByteSize pending_failed_speculation_offset() { return byte_offset_of(JavaThread, _pending_failed_speculation); }
1811   static ByteSize jvmci_alternate_call_target_offset() { return byte_offset_of(JavaThread, _jvmci._alternate_call_target); }
1812   static ByteSize jvmci_implicit_exception_pc_offset() { return byte_offset_of(JavaThread, _jvmci._implicit_exception_pc); }
1813   static ByteSize jvmci_counters_offset()        { return byte_offset_of(JavaThread, _jvmci_counters); }
1814 #endif // INCLUDE_JVMCI
1815   static ByteSize exception_oop_offset()         { return byte_offset_of(JavaThread, _exception_oop); }
1816   static ByteSize exception_pc_offset()          { return byte_offset_of(JavaThread, _exception_pc); }
1817   static ByteSize exception_handler_pc_offset()  { return byte_offset_of(JavaThread, _exception_handler_pc); }
1818   static ByteSize stack_overflow_limit_offset()  { return byte_offset_of(JavaThread, _stack_overflow_limit); }
1819   static ByteSize is_method_handle_return_offset() { return byte_offset_of(JavaThread, _is_method_handle_return); }
1820   static ByteSize stack_guard_state_offset()     { return byte_offset_of(JavaThread, _stack_guard_state); }
1821   static ByteSize reserved_stack_activation_offset() { return byte_offset_of(JavaThread, _reserved_stack_activation); }
1822   static ByteSize suspend_flags_offset()         { return byte_offset_of(JavaThread, _suspend_flags); }
1823 
1824   static ByteSize do_not_unlock_if_synchronized_offset() { return byte_offset_of(JavaThread, _do_not_unlock_if_synchronized); }
1825   static ByteSize should_post_on_exceptions_flag_offset() {
1826     return byte_offset_of(JavaThread, _should_post_on_exceptions_flag);
1827   }
1828   static ByteSize doing_unsafe_access_offset() { return byte_offset_of(JavaThread, _doing_unsafe_access); }
1829 
1830   DEBUG_ONLY(static ByteSize continuation_offset() { return byte_offset_of(JavaThread, _continuation); })
1831   static ByteSize cont_fastpath_offset()      { return byte_offset_of(JavaThread, _cont_fastpath); }
1832   static ByteSize cont_frame_offset()         { return byte_offset_of(JavaThread, _cont_frame); }
1833   static ByteSize cont_preempt_offset()       { return byte_offset_of(JavaThread, _cont_preempt); }
1834 
1835   // Returns the jni environment for this thread
1836   JNIEnv* jni_environment()                      { return &_jni_environment; }
1837 
1838   static JavaThread* thread_from_jni_environment(JNIEnv* env) {
1839     JavaThread *thread_from_jni_env = (JavaThread*)((intptr_t)env - in_bytes(jni_environment_offset()));
1840     // Only return NULL if thread is off the thread list; starting to
1841     // exit should not return NULL.
1842     if (thread_from_jni_env->is_terminated()) {
1843       thread_from_jni_env->block_if_vm_exited();
1844       return NULL;
1845     } else {
1846       return thread_from_jni_env;
1847     }
1848   }
1849 
1850   // JNI critical regions. These can nest.
1851   bool in_critical()    { return _jni_active_critical > 0; }
1852   bool in_last_critical()  { return _jni_active_critical == 1; }
1853   inline void enter_critical();
1854   void exit_critical() {


1898   // Memory operations
1899   void oops_do(OopClosure* f, CodeBlobClosure* cf);
1900 
1901   // Sweeper operations
1902   virtual void nmethods_do(CodeBlobClosure* cf);
1903 
1904   // RedefineClasses Support
1905   void metadata_do(MetadataClosure* f);
1906 
1907   // Debug method asserting thread states are correct during a handshake operation.
1908   DEBUG_ONLY(void verify_states_for_handshake();)
1909 
1910   // Misc. operations
1911   char* name() const { return (char*)get_thread_name(); }
1912   void print_on(outputStream* st, bool print_extended_info) const;
1913   void print_on(outputStream* st) const { print_on(st, false); }
1914   void print() const;
1915   void print_value();
1916   void print_thread_state_on(outputStream*) const      PRODUCT_RETURN;
1917   void print_thread_state() const                      PRODUCT_RETURN;
1918   DEBUG_ONLY(const char* thread_state_name() const;)
1919   void print_on_error(outputStream* st, char* buf, int buflen) const;
1920   void print_name_on_error(outputStream* st, char* buf, int buflen) const;
1921   void verify();
1922   const char* get_thread_name() const;
1923  protected:
1924   // factor out low-level mechanics for use in both normal and error cases
1925   virtual const char* get_thread_name_string(char* buf = NULL, int buflen = 0) const;
1926  public:
1927   const char* get_threadgroup_name() const;
1928   const char* get_parent_name() const;
1929 
1930   // Accessing frames
1931   frame last_frame() {
1932     _anchor.make_walkable(this);
1933     return pd_last_frame();
1934   }
1935   javaVFrame* last_java_vframe(RegisterMap* reg_map);
1936 
1937   oop last_continuation();
1938   
1939   // Returns method at 'depth' java or native frames down the stack
1940   // Used for security checks
1941   Klass* security_get_caller_class(int depth);
1942 
1943   // Print stack trace in external format
1944   void print_stack_on(outputStream* st);
1945   void print_stack() { print_stack_on(tty); }
1946 
1947   // Print stack traces in various internal formats
1948   void trace_stack()                             PRODUCT_RETURN;
1949   void trace_stack_from(vframe* start_vf)        PRODUCT_RETURN;
1950   void trace_frames()                            PRODUCT_RETURN;
1951   void trace_oops()                              PRODUCT_RETURN;
1952 
1953   // Print an annotated view of the stack frames
1954   void print_frame_layout(int depth = 0, bool validate_only = false) NOT_DEBUG_RETURN;
1955   void validate_frame_layout() {
1956     print_frame_layout(0, true);
1957   }
1958 


< prev index next >