1 /*
   2  * Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  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_GC_G1_G1CONCURRENTHEAPRESIZETHREAD_HPP
  26 #define SHARE_GC_G1_G1CONCURRENTHEAPRESIZETHREAD_HPP
  27 
  28 #include "gc/shared/concurrentGCThread.hpp"
  29 
  30 class G1Policy;
  31 class G1ConcurrentHeapResize;
  32 
  33 // The concurrent heap resize thread helps to commit/uncommit heap region memory
  34 // to avoid the time spend in STW pause
  35 class G1ConcurrentHeapResizeThread : public ConcurrentGCThread {
  36 
  37   G1ConcurrentHeapResize* _concurrent_heap_resize;
  38 
  39   // The concurrent resizing thread has 3 states:
  40   // Idle: thread is waiting for start
  41   // Working: thread has started and is doing the work of memory commit/uncommit
  42   // Done: the work of memory commit/uncommit has finished but regions have not synchronized yet
  43   enum State {
  44     Idle,
  45     Working,
  46     Done,
  47   };
  48 
  49   volatile State _state;
  50 
  51   Monitor* _start_monitor;  // used notify the start of resize thread
  52   Monitor* _done_monitor;   // used to notify concurrent resize is done
  53 
  54   void sleep_before_next_cycle();
  55 
  56   void run_service();
  57   void stop_service();
  58 
  59  public:
  60   // Constructor
  61   G1ConcurrentHeapResizeThread(G1ConcurrentHeapResize*  heap_resize);
  62 
  63   G1ConcurrentHeapResize* concurrent_heap_resize() { return _concurrent_heap_resize; }
  64 
  65   void set_idle()          { assert(_state != Working, "must not be working"); _state = Idle; }
  66   bool idle() const        { return _state == Idle; }
  67   void set_working()       { assert(_state == Idle, "must be idle"); _state = Working; }
  68   bool working() const     { return _state == Working; }
  69   void set_done()          { assert(_state == Working, "must be working"); _state = Done; }
  70   bool done() const        { return _state == Done; }
  71 
  72   // Returns true if a concurrent resizing cycle is in progress
  73   bool during_cycle()      { return !idle(); }
  74 
  75   Monitor* start_monitor() { return _start_monitor; }
  76   Monitor* done_monitor()  { return _done_monitor; }
  77 };
  78 
  79 #endif // SHARE_GC_G1_G1CONCURRENTHEAPRESIZETHREAD_HPP