1 /*
   2  * Copyright (c) 2017, 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 #include "precompiled.hpp"
  26 #include "gc/shared/concurrentGCPhaseManager.hpp"
  27 #include "runtime/mutexLocker.hpp"
  28 #include "runtime/thread.hpp"
  29 
  30 #define assert_ConcurrentGC_thread() \
  31   assert(Thread::current()->is_ConcurrentGC_thread(), "precondition")
  32 
  33 #define assert_not_enter_unconstrained(phase) \
  34   assert((phase) != UNCONSTRAINED_PHASE, "Cannot enter \"unconstrained\" phase")
  35 
  36 #define assert_manager_is_tos(manager, stack, kind)  \
  37   assert((manager) == (stack)->_top, kind " manager is not top of stack")
  38 
  39 ConcurrentGCPhaseManager::Stack::Stack() :
  40   _requested_phase(UNCONSTRAINED_PHASE),
  41   _top(NULL)
  42 { }
  43 
  44 ConcurrentGCPhaseManager::ConcurrentGCPhaseManager(int phase, Stack* stack) :
  45   _phase(phase),
  46   _active(true),
  47   _prev(NULL),
  48   _stack(stack)
  49 {
  50   assert_ConcurrentGC_thread();
  51   assert_not_enter_unconstrained(phase);
  52   assert(stack != NULL, "precondition");
  53   MonitorLockerEx ml(CGCPhaseManager_lock);
  54   if (stack->_top != NULL) {
  55     assert(stack->_top->_active, "precondition");
  56     _prev = stack->_top;
  57   }
  58   stack->_top = this;
  59   ml.notify_all();
  60 }
  61 
  62 ConcurrentGCPhaseManager::~ConcurrentGCPhaseManager() {
  63   assert_ConcurrentGC_thread();
  64   MonitorLockerEx ml(CGCPhaseManager_lock);
  65   assert_manager_is_tos(this, _stack, "This");
  66   wait_when_requested_impl();
  67   _stack->_top = _prev;
  68   ml.notify_all();
  69 }
  70 
  71 bool ConcurrentGCPhaseManager::is_requested() const {
  72   assert_ConcurrentGC_thread();
  73   MonitorLockerEx ml(CGCPhaseManager_lock);
  74   assert_manager_is_tos(this, _stack, "This");
  75   return _active && (_stack->_requested_phase == _phase);
  76 }
  77 
  78 bool ConcurrentGCPhaseManager::wait_when_requested_impl() const {
  79   assert_ConcurrentGC_thread();
  80   assert_lock_strong(CGCPhaseManager_lock);
  81   bool waited = false;
  82   while (_active && (_stack->_requested_phase == _phase)) {
  83     waited = true;
  84     // wait safepoint check should be a property of the thread type also.
  85     CGCPhaseManager_lock->wait(Mutex::_no_safepoint_check_flag);
  86   }
  87   return waited;
  88 }
  89 
  90 bool ConcurrentGCPhaseManager::wait_when_requested() const {
  91   assert_ConcurrentGC_thread();
  92   MonitorLockerEx ml(CGCPhaseManager_lock);
  93   assert_manager_is_tos(this, _stack, "This");
  94   return wait_when_requested_impl();
  95 }
  96 
  97 void ConcurrentGCPhaseManager::set_phase(int phase, bool force) {
  98   assert_ConcurrentGC_thread();
  99   assert_not_enter_unconstrained(phase);
 100   MonitorLockerEx ml(CGCPhaseManager_lock);
 101   assert_manager_is_tos(this, _stack, "This");
 102   if (!force) wait_when_requested_impl();
 103   _phase = phase;
 104   ml.notify_all();
 105 }
 106 
 107 void ConcurrentGCPhaseManager::deactivate() {
 108   assert_ConcurrentGC_thread();
 109   MonitorLockerEx ml(CGCPhaseManager_lock);
 110   assert_manager_is_tos(this, _stack, "This");
 111   _active = false;
 112   ml.notify_all();
 113 }
 114 
 115 bool ConcurrentGCPhaseManager::wait_for_phase(int phase, Stack* stack) {
 116   assert(Thread::current()->is_Java_thread(), "precondition");
 117   assert(stack != NULL, "precondition");
 118   MonitorLockerEx ml(CGCPhaseManager_lock);
 119   // Update request and notify service of change.
 120   if (stack->_requested_phase != phase) {
 121     stack->_requested_phase = phase;
 122     ml.notify_all();
 123   }
 124 
 125   if (phase == UNCONSTRAINED_PHASE) {
 126     return true;
 127   }
 128 
 129   // Wait until phase or IDLE is active.
 130   while (true) {
 131     bool idle = false;
 132     for (ConcurrentGCPhaseManager* manager = stack->_top;
 133          manager != NULL;
 134          manager = manager->_prev) {
 135       if (manager->_phase == phase) {
 136         return true;            // phase is active.
 137       } else if (manager->_phase == IDLE_PHASE) {
 138         idle = true;            // Note idle active, continue search for phase.
 139       }
 140     }
 141     if (idle) {
 142       return false;             // idle is active and phase is not.
 143     } else {
 144       ml.wait();                // Wait for phase change.
 145     }
 146   }
 147 }
--- EOF ---