src/hotspot/share/runtime/safepoint.hpp
changeset 55514 03468b206457
parent 54807 33fe50b6d707
child 55569 8e3a0ebf3497
equal deleted inserted replaced
55505:0bf9477626ef 55514:03468b206457
    45 // The Mutex/Condition variable and ObjectLocker classes calls the enter/
    45 // The Mutex/Condition variable and ObjectLocker classes calls the enter/
    46 // exit safepoint methods, when a thread is blocked/restarted. Hence, all mutex exter/
    46 // exit safepoint methods, when a thread is blocked/restarted. Hence, all mutex exter/
    47 // exit points *must* be at a safepoint.
    47 // exit points *must* be at a safepoint.
    48 
    48 
    49 class ThreadSafepointState;
    49 class ThreadSafepointState;
       
    50 
       
    51 class SafepointStateTracker {
       
    52   uint64_t _safepoint_id;
       
    53   bool     _at_safepoint;
       
    54 public:
       
    55   SafepointStateTracker(uint64_t safepoint_id, bool at_safepoint);
       
    56   bool safepoint_state_changed();
       
    57 };
    50 
    58 
    51 //
    59 //
    52 // Implements roll-forward to safepoint (safepoint synchronization)
    60 // Implements roll-forward to safepoint (safepoint synchronization)
    53 //
    61 //
    54 class SafepointSynchronize : AllStatic {
    62 class SafepointSynchronize : AllStatic {
    75 
    83 
    76  private:
    84  private:
    77   friend class SafepointMechanism;
    85   friend class SafepointMechanism;
    78   friend class ThreadSafepointState;
    86   friend class ThreadSafepointState;
    79   friend class HandshakeState;
    87   friend class HandshakeState;
       
    88   friend class SafepointStateTracker;
    80 
    89 
    81   // Threads might read this flag directly, without acquiring the Threads_lock:
    90   // Threads might read this flag directly, without acquiring the Threads_lock:
    82   static volatile SynchronizeState _state;
    91   static volatile SynchronizeState _state;
    83   // Number of threads we are waiting for to block:
    92   // Number of threads we are waiting for to block:
    84   static int              _waiting_to_block;
    93   static int              _waiting_to_block;
    88   // This counter is used for fast versions of jni_Get<Primitive>Field.
    97   // This counter is used for fast versions of jni_Get<Primitive>Field.
    89   // An even value means there are no ongoing safepoint operations.
    98   // An even value means there are no ongoing safepoint operations.
    90   // The counter is incremented ONLY at the beginning and end of each
    99   // The counter is incremented ONLY at the beginning and end of each
    91   // safepoint.
   100   // safepoint.
    92   static volatile uint64_t _safepoint_counter;
   101   static volatile uint64_t _safepoint_counter;
       
   102 
       
   103   // A change in this counter or a change in the result of
       
   104   // is_at_safepoint() are used by SafepointStateTracker::
       
   105   // safepoint_state_changed() to determine its answer.
       
   106   static uint64_t _safepoint_id;
    93 
   107 
    94   // JavaThreads that need to block for the safepoint will stop on the
   108   // JavaThreads that need to block for the safepoint will stop on the
    95   // _wait_barrier, where they can quickly be started again.
   109   // _wait_barrier, where they can quickly be started again.
    96   static WaitBarrier* _wait_barrier;
   110   static WaitBarrier* _wait_barrier;
    97   static long         _end_of_last_safepoint;     // Time of last safepoint in milliseconds
   111   static long         _end_of_last_safepoint;     // Time of last safepoint in milliseconds
   112   static void arm_safepoint();
   126   static void arm_safepoint();
   113   static int synchronize_threads(jlong safepoint_limit_time, int nof_threads, int* initial_running);
   127   static int synchronize_threads(jlong safepoint_limit_time, int nof_threads, int* initial_running);
   114   static void disarm_safepoint();
   128   static void disarm_safepoint();
   115   static void increment_jni_active_count();
   129   static void increment_jni_active_count();
   116   static void decrement_waiting_to_block();
   130   static void decrement_waiting_to_block();
       
   131   static bool thread_not_running(ThreadSafepointState *cur_state);
   117 
   132 
   118   // Used in safepoint_safe to do a stable load of the thread state.
   133   // Used in safepoint_safe to do a stable load of the thread state.
   119   static bool try_stable_load_state(JavaThreadState *state,
   134   static bool try_stable_load_state(JavaThreadState *state,
   120                                     JavaThread *thread,
   135                                     JavaThread *thread,
   121                                     uint64_t safepoint_count);
   136                                     uint64_t safepoint_count);
   125 
   140 
   126   // Called from VMThread during handshakes.
   141   // Called from VMThread during handshakes.
   127   // If true the VMThread may safely process the handshake operation for the JavaThread.
   142   // If true the VMThread may safely process the handshake operation for the JavaThread.
   128   static bool handshake_safe(JavaThread *thread);
   143   static bool handshake_safe(JavaThread *thread);
   129 
   144 
       
   145   static uint64_t safepoint_counter()             { return _safepoint_counter; }
       
   146 
   130 public:
   147 public:
   131 
   148 
   132   static void init(Thread* vmthread);
   149   static void init(Thread* vmthread);
   133 
   150 
   134   // Roll all threads forward to safepoint. Must be called by the VMThread.
   151   // Roll all threads forward to safepoint. Must be called by the VMThread.
   139   static const uint64_t InactiveSafepointCounter;
   156   static const uint64_t InactiveSafepointCounter;
   140 
   157 
   141   // Query
   158   // Query
   142   static bool is_at_safepoint()                   { return _state == _synchronized; }
   159   static bool is_at_safepoint()                   { return _state == _synchronized; }
   143   static bool is_synchronizing()                  { return _state == _synchronizing; }
   160   static bool is_synchronizing()                  { return _state == _synchronizing; }
   144   static uint64_t safepoint_counter()             { return _safepoint_counter; }
   161 
   145   static bool is_same_safepoint(uint64_t counter) { return (SafepointSynchronize::safepoint_counter() - counter) < 2; }
   162   static uint64_t safepoint_id() {
       
   163     return _safepoint_id;
       
   164   }
       
   165 
       
   166   static SafepointStateTracker safepoint_state_tracker() {
       
   167     return SafepointStateTracker(safepoint_id(), is_at_safepoint());
       
   168   }
       
   169 
   146   // Exception handling for page polling
   170   // Exception handling for page polling
   147   static void handle_polling_page_exception(JavaThread *thread);
   171   static void handle_polling_page_exception(JavaThread *thread);
   148 
   172 
   149   static bool is_cleanup_needed();
   173   static bool is_cleanup_needed();
   150   static void do_cleanup_tasks();
   174   static void do_cleanup_tasks();