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), kind " manager is not top of stack")
  38 
  39 ConcurrentGCPhaseManager::ConcurrentGCPhaseManager(int phase,
  40                                                    ConcurrentGCPhaseManager** stack,
  41                                                    int* request) :
  42   _phase(phase),
  43   _active(true),
  44   _request(request),
  45   _link(NULL),
  46   _stack(stack)
  47 {
  48   assert_ConcurrentGC_thread();
  49   assert_not_enter_unconstrained(phase);
  50   assert(stack != NULL, "NULL stack pointer");
  51   assert(request != NULL, "NULL request pointer");
  52   MonitorLockerEx ml(CGCPhaseManager_lock, Mutex::_no_safepoint_check_flag);
  53   assert(*stack == NULL, "Non-empty stack");
  54   *stack = this;
  55   ml.notify_all();
  56 }
  57 
  58 ConcurrentGCPhaseManager::ConcurrentGCPhaseManager(int phase,
  59                                                    ConcurrentGCPhaseManager* prev) :
  60   _phase(phase),
  61   _active(true),
  62   _request(NULL),
  63   _link(prev),
  64   _stack(NULL)
  65 {
  66   assert_ConcurrentGC_thread();
  67   assert_not_enter_unconstrained(phase);
  68   assert(prev != NULL, "NULL previous stack entry");
  69   MonitorLockerEx ml(CGCPhaseManager_lock, Mutex::_no_safepoint_check_flag);
  70   assert_manager_is_tos(prev, prev->_stack, "Previous");
  71   assert(prev->_active, "Previous manager is inactive");
  72   _stack = prev->_stack;
  73   _request = prev->_request;
  74   *_stack = this;
  75   ml.notify_all();
  76 }
  77 
  78 ConcurrentGCPhaseManager::~ConcurrentGCPhaseManager() {
  79   assert_ConcurrentGC_thread();
  80   MonitorLockerEx ml(CGCPhaseManager_lock, Mutex::_no_safepoint_check_flag);
  81   assert_manager_is_tos(this, _stack, "This");
  82   wait_when_requested_impl();
  83   *_stack = _link;
  84   ml.notify_all();
  85 }
  86 
  87 bool ConcurrentGCPhaseManager::is_requested() const {
  88   assert_ConcurrentGC_thread();
  89   MonitorLockerEx ml(CGCPhaseManager_lock, Mutex::_no_safepoint_check_flag);
  90   assert_manager_is_tos(this, _stack, "This");
  91   return _active && (*_request == _phase);
  92 }
  93 
  94 bool ConcurrentGCPhaseManager::wait_when_requested_impl() const {
  95   assert_ConcurrentGC_thread();
  96   assert_lock_strong(CGCPhaseManager_lock);
  97   bool waited = false;
  98   while (_active && (*_request == _phase)) {
  99     waited = true;
 100     CGCPhaseManager_lock->wait(Mutex::_no_safepoint_check_flag);
 101   }
 102   return waited;
 103 }
 104 
 105 bool ConcurrentGCPhaseManager::wait_when_requested() const {
 106   assert_ConcurrentGC_thread();
 107   MonitorLockerEx ml(CGCPhaseManager_lock, Mutex::_no_safepoint_check_flag);
 108   assert_manager_is_tos(this, _stack, "This");
 109   return wait_when_requested_impl();
 110 }
 111 
 112 void ConcurrentGCPhaseManager::set_phase(int phase, bool force) {
 113   assert_ConcurrentGC_thread();
 114   assert_not_enter_unconstrained(phase);
 115   MonitorLockerEx ml(CGCPhaseManager_lock, Mutex::_no_safepoint_check_flag);
 116   assert_manager_is_tos(this, _stack, "This");
 117   if (!force) wait_when_requested_impl();
 118   _phase = phase;
 119   ml.notify_all();
 120 }
 121 
 122 void ConcurrentGCPhaseManager::deactivate() {
 123   assert_ConcurrentGC_thread();
 124   MonitorLockerEx ml(CGCPhaseManager_lock, Mutex::_no_safepoint_check_flag);
 125   assert_manager_is_tos(this, _stack, "This");
 126   _active = false;
 127   ml.notify_all();
 128 }
 129 
 130 bool ConcurrentGCPhaseManager::wait_for_phase(int phase,
 131                                               ConcurrentGCPhaseManager** stack,
 132                                               int* request) {
 133   assert(Thread::current()->is_Java_thread(), "precondition");
 134   MonitorLockerEx ml(CGCPhaseManager_lock);
 135   // Update request and notify service of change.
 136   if (*request != phase) {
 137     *request = phase;
 138     ml.notify_all();
 139   }
 140 
 141   if (phase == UNCONSTRAINED_PHASE) {
 142     return true;
 143   }
 144 
 145   // Wait until phase or IDLE is active.
 146   while (true) {
 147     bool idle = false;
 148     for (ConcurrentGCPhaseManager* manager = *stack;
 149          manager != NULL;
 150          manager = manager->_link) {
 151       if (manager->_phase == phase) {
 152         return true;            // phase is active.
 153       } else if (manager->_phase == IDLE_PHASE) {
 154         idle = true;            // Note idle active, continue search for phase.
 155       }
 156     }
 157     if (idle) {
 158       return false;             // idle is active and phase is not.
 159     } else {
 160       ml.wait();                // Wait for phase change.
 161     }
 162   }
 163 }