src/hotspot/share/runtime/interfaceSupport.inline.hpp
changeset 54495 941db9c0b5b5
parent 54347 235883996bc7
child 54523 5df03f58d25b
equal deleted inserted replaced
54494:1bbce3048d20 54495:941db9c0b5b5
    64   static void deoptimizeAll();
    64   static void deoptimizeAll();
    65   static void stress_derived_pointers();
    65   static void stress_derived_pointers();
    66   static void verify_stack();
    66   static void verify_stack();
    67   static void verify_last_frame();
    67   static void verify_last_frame();
    68 # endif
    68 # endif
    69 
       
    70  public:
       
    71   static void serialize_thread_state_with_handler(JavaThread* thread) {
       
    72     serialize_thread_state_internal(thread, true);
       
    73   }
       
    74 
       
    75   // Should only call this if we know that we have a proper SEH set up.
       
    76   static void serialize_thread_state(JavaThread* thread) {
       
    77     serialize_thread_state_internal(thread, false);
       
    78   }
       
    79 
       
    80  private:
       
    81   static void serialize_thread_state_internal(JavaThread* thread, bool needs_exception_handler) {
       
    82     // Make sure new state is seen by VM thread
       
    83     OrderAccess::fence();
       
    84   }
       
    85 };
    69 };
    86 
    70 
    87 
    71 
    88 // Basic class for all thread transition classes.
    72 // Basic class for all thread transition classes.
    89 
    73 
   101   static inline void transition(JavaThread *thread, JavaThreadState from, JavaThreadState to) {
    85   static inline void transition(JavaThread *thread, JavaThreadState from, JavaThreadState to) {
   102     assert(from != _thread_in_Java, "use transition_from_java");
    86     assert(from != _thread_in_Java, "use transition_from_java");
   103     assert(from != _thread_in_native, "use transition_from_native");
    87     assert(from != _thread_in_native, "use transition_from_native");
   104     assert((from & 1) == 0 && (to & 1) == 0, "odd numbers are transitions states");
    88     assert((from & 1) == 0 && (to & 1) == 0, "odd numbers are transitions states");
   105     assert(thread->thread_state() == from, "coming from wrong thread state");
    89     assert(thread->thread_state() == from, "coming from wrong thread state");
   106     // Change to transition state
    90     // Change to transition state and ensure it is seen by the VM thread.
   107     thread->set_thread_state((JavaThreadState)(from + 1));
    91     thread->set_thread_state_fence((JavaThreadState)(from + 1));
   108 
       
   109     InterfaceSupport::serialize_thread_state(thread);
       
   110 
       
   111     SafepointMechanism::block_if_requested(thread);
       
   112     thread->set_thread_state(to);
       
   113 
       
   114     CHECK_UNHANDLED_OOPS_ONLY(thread->clear_unhandled_oops();)
       
   115   }
       
   116 
       
   117   // transition_and_fence must be used on any thread state transition
       
   118   // where there might not be a Java call stub on the stack, in
       
   119   // particular on Windows where the Structured Exception Handler is
       
   120   // set up in the call stub.
       
   121   static inline void transition_and_fence(JavaThread *thread, JavaThreadState from, JavaThreadState to) {
       
   122     assert(thread->thread_state() == from, "coming from wrong thread state");
       
   123     assert((from & 1) == 0 && (to & 1) == 0, "odd numbers are transitions states");
       
   124     // Change to transition state
       
   125     thread->set_thread_state((JavaThreadState)(from + 1));
       
   126 
       
   127     InterfaceSupport::serialize_thread_state_with_handler(thread);
       
   128 
    92 
   129     SafepointMechanism::block_if_requested(thread);
    93     SafepointMechanism::block_if_requested(thread);
   130     thread->set_thread_state(to);
    94     thread->set_thread_state(to);
   131 
    95 
   132     CHECK_UNHANDLED_OOPS_ONLY(thread->clear_unhandled_oops();)
    96     CHECK_UNHANDLED_OOPS_ONLY(thread->clear_unhandled_oops();)
   141   }
   105   }
   142 
   106 
   143   static inline void transition_from_native(JavaThread *thread, JavaThreadState to) {
   107   static inline void transition_from_native(JavaThread *thread, JavaThreadState to) {
   144     assert((to & 1) == 0, "odd numbers are transitions states");
   108     assert((to & 1) == 0, "odd numbers are transitions states");
   145     assert(thread->thread_state() == _thread_in_native, "coming from wrong thread state");
   109     assert(thread->thread_state() == _thread_in_native, "coming from wrong thread state");
   146     // Change to transition state
   110     // Change to transition state and ensure it is seen by the VM thread.
   147     thread->set_thread_state(_thread_in_native_trans);
   111     thread->set_thread_state_fence(_thread_in_native_trans);
   148 
       
   149     InterfaceSupport::serialize_thread_state_with_handler(thread);
       
   150 
   112 
   151     // We never install asynchronous exceptions when coming (back) in
   113     // We never install asynchronous exceptions when coming (back) in
   152     // to the runtime from native code because the runtime is not set
   114     // to the runtime from native code because the runtime is not set
   153     // up to handle exceptions floating around at arbitrary points.
   115     // up to handle exceptions floating around at arbitrary points.
   154     if (SafepointMechanism::should_block(thread) || thread->is_suspend_after_native()) {
   116     if (SafepointMechanism::should_block(thread) || thread->is_suspend_after_native()) {
   155       JavaThread::check_safepoint_and_suspend_for_native_trans(thread);
   117       JavaThread::check_safepoint_and_suspend_for_native_trans(thread);
   156 
       
   157       // Clear unhandled oops anywhere where we could block, even if we don't.
       
   158       CHECK_UNHANDLED_OOPS_ONLY(thread->clear_unhandled_oops();)
       
   159     }
   118     }
   160 
   119 
   161     thread->set_thread_state(to);
   120     thread->set_thread_state(to);
   162   }
   121   }
   163  protected:
   122  protected:
   164    void trans(JavaThreadState from, JavaThreadState to)  { transition(_thread, from, to); }
   123    void trans(JavaThreadState from, JavaThreadState to)  { transition(_thread, from, to); }
   165    void trans_from_java(JavaThreadState to)              { transition_from_java(_thread, to); }
   124    void trans_from_java(JavaThreadState to)              { transition_from_java(_thread, to); }
   166    void trans_from_native(JavaThreadState to)            { transition_from_native(_thread, to); }
   125    void trans_from_native(JavaThreadState to)            { transition_from_native(_thread, to); }
   167    void trans_and_fence(JavaThreadState from, JavaThreadState to) { transition_and_fence(_thread, from, to); }
       
   168 };
   126 };
   169 
   127 
   170 class ThreadInVMForHandshake : public ThreadStateTransition {
   128 class ThreadInVMForHandshake : public ThreadStateTransition {
   171   const JavaThreadState _original_state;
   129   const JavaThreadState _original_state;
   172 
   130 
   173   void transition_back() {
   131   void transition_back() {
   174     // This can be invoked from transition states and must return to the original state properly
   132     // This can be invoked from transition states and must return to the original state properly
   175     assert(_thread->thread_state() == _thread_in_vm, "should only call when leaving VM after handshake");
   133     assert(_thread->thread_state() == _thread_in_vm, "should only call when leaving VM after handshake");
   176     _thread->set_thread_state(_thread_in_vm_trans);
   134     // Change to transition state and ensure it is seen by the VM thread.
   177 
   135     _thread->set_thread_state_fence(_thread_in_vm_trans);
   178     InterfaceSupport::serialize_thread_state(_thread);
       
   179 
   136 
   180     SafepointMechanism::block_if_requested(_thread);
   137     SafepointMechanism::block_if_requested(_thread);
   181 
   138 
   182     _thread->set_thread_state(_original_state);
   139     _thread->set_thread_state(_original_state);
   183   }
   140   }
   215   }
   172   }
   216 };
   173 };
   217 
   174 
   218 
   175 
   219 class ThreadInVMfromUnknown {
   176 class ThreadInVMfromUnknown {
   220  private:
       
   221   JavaThread* _thread;
   177   JavaThread* _thread;
   222  public:
   178  public:
   223   ThreadInVMfromUnknown() : _thread(NULL) {
   179   ThreadInVMfromUnknown() : _thread(NULL) {
   224     Thread* t = Thread::current();
   180     Thread* t = Thread::current();
   225     if (t->is_Java_thread()) {
   181     if (t->is_Java_thread()) {
   234       }
   190       }
   235     }
   191     }
   236   }
   192   }
   237   ~ThreadInVMfromUnknown()  {
   193   ~ThreadInVMfromUnknown()  {
   238     if (_thread) {
   194     if (_thread) {
   239       ThreadStateTransition::transition_and_fence(_thread, _thread_in_vm, _thread_in_native);
   195       ThreadStateTransition::transition(_thread, _thread_in_vm, _thread_in_native);
   240     }
   196     }
   241   }
   197   }
   242 };
   198 };
   243 
   199 
   244 
   200 
   246  public:
   202  public:
   247   ThreadInVMfromNative(JavaThread* thread) : ThreadStateTransition(thread) {
   203   ThreadInVMfromNative(JavaThread* thread) : ThreadStateTransition(thread) {
   248     trans_from_native(_thread_in_vm);
   204     trans_from_native(_thread_in_vm);
   249   }
   205   }
   250   ~ThreadInVMfromNative() {
   206   ~ThreadInVMfromNative() {
   251     trans_and_fence(_thread_in_vm, _thread_in_native);
   207     trans(_thread_in_vm, _thread_in_native);
   252   }
   208   }
   253 };
   209 };
   254 
   210 
   255 
   211 
   256 class ThreadToNativeFromVM : public ThreadStateTransition {
   212 class ThreadToNativeFromVM : public ThreadStateTransition {
   258   ThreadToNativeFromVM(JavaThread *thread) : ThreadStateTransition(thread) {
   214   ThreadToNativeFromVM(JavaThread *thread) : ThreadStateTransition(thread) {
   259     // We are leaving the VM at this point and going directly to native code.
   215     // We are leaving the VM at this point and going directly to native code.
   260     // Block, if we are in the middle of a safepoint synchronization.
   216     // Block, if we are in the middle of a safepoint synchronization.
   261     assert(!thread->owns_locks(), "must release all locks when leaving VM");
   217     assert(!thread->owns_locks(), "must release all locks when leaving VM");
   262     thread->frame_anchor()->make_walkable(thread);
   218     thread->frame_anchor()->make_walkable(thread);
   263     trans_and_fence(_thread_in_vm, _thread_in_native);
   219     trans(_thread_in_vm, _thread_in_native);
   264     // Check for pending. async. exceptions or suspends.
   220     // Check for pending. async. exceptions or suspends.
   265     if (_thread->has_special_runtime_exit_condition()) _thread->handle_special_runtime_exit_condition(false);
   221     if (_thread->has_special_runtime_exit_condition()) _thread->handle_special_runtime_exit_condition(false);
   266   }
   222   }
   267 
   223 
   268   ~ThreadToNativeFromVM() {
   224   ~ThreadToNativeFromVM() {
   277  public:
   233  public:
   278   ThreadBlockInVM(JavaThread *thread)
   234   ThreadBlockInVM(JavaThread *thread)
   279   : ThreadStateTransition(thread) {
   235   : ThreadStateTransition(thread) {
   280     // Once we are blocked vm expects stack to be walkable
   236     // Once we are blocked vm expects stack to be walkable
   281     thread->frame_anchor()->make_walkable(thread);
   237     thread->frame_anchor()->make_walkable(thread);
   282     trans_and_fence(_thread_in_vm, _thread_blocked);
   238     trans(_thread_in_vm, _thread_blocked);
   283   }
   239   }
   284   ~ThreadBlockInVM() {
   240   ~ThreadBlockInVM() {
   285     trans_and_fence(_thread_blocked, _thread_in_vm);
   241     trans(_thread_blocked, _thread_in_vm);
   286     OrderAccess::cross_modify_fence();
   242     OrderAccess::cross_modify_fence();
   287     // We don't need to clear_walkable because it will happen automagically when we return to java
   243     // We don't need to clear_walkable because it will happen automagically when we return to java
   288   }
   244   }
   289 };
   245 };
   290 
   246 
   320     // so we can skip the _thread_in_vm_trans state here. Since
   276     // so we can skip the _thread_in_vm_trans state here. Since
   321     // we don't read poll, it's enough to order the stores.
   277     // we don't read poll, it's enough to order the stores.
   322     OrderAccess::storestore();
   278     OrderAccess::storestore();
   323 
   279 
   324     thread->set_thread_state(_thread_blocked);
   280     thread->set_thread_state(_thread_blocked);
   325 
       
   326     CHECK_UNHANDLED_OOPS_ONLY(_thread->clear_unhandled_oops();)
       
   327   }
   281   }
   328   ~ThreadBlockInVMWithDeadlockCheck() {
   282   ~ThreadBlockInVMWithDeadlockCheck() {
   329     // Change to transition state
   283     // Change to transition state and ensure it is seen by the VM thread.
   330     _thread->set_thread_state((JavaThreadState)(_thread_blocked_trans));
   284     _thread->set_thread_state_fence((JavaThreadState)(_thread_blocked_trans));
   331 
       
   332     InterfaceSupport::serialize_thread_state_with_handler(_thread);
       
   333 
   285 
   334     if (SafepointMechanism::should_block(_thread)) {
   286     if (SafepointMechanism::should_block(_thread)) {
   335       release_monitor();
   287       release_monitor();
   336       SafepointMechanism::block_if_requested(_thread);
   288       SafepointMechanism::block_if_requested(_thread);
   337     }
   289     }
   338 
   290 
   339     _thread->set_thread_state(_thread_in_vm);
   291     _thread->set_thread_state(_thread_in_vm);
   340     CHECK_UNHANDLED_OOPS_ONLY(_thread->clear_unhandled_oops();)
       
   341 
       
   342     OrderAccess::cross_modify_fence();
   292     OrderAccess::cross_modify_fence();
   343   }
   293   }
   344 };
   294 };
   345 
   295 
   346 
   296