1 /*
   2  * Copyright (c) 1997, 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_RUNTIME_SAFEPOINTVERIFIERS_HPP
  26 #define SHARE_RUNTIME_SAFEPOINTVERIFIERS_HPP
  27 
  28 #include "memory/allocation.hpp"
  29 #include "runtime/thread.hpp"
  30 
  31 // A NoGCVerifier object can be placed in methods where one assumes that
  32 // no garbage collection will occur. The destructor will verify this property
  33 // unless the constructor is called with argument false (not verifygc).
  34 //
  35 // The check will only be done in debug mode and if verifygc true.
  36 
  37 class NoGCVerifier: public StackObj {
  38  friend class PauseNoGCVerifier;
  39 
  40  protected:
  41   bool _verifygc;
  42   unsigned int _old_invocations;
  43 
  44  public:
  45 #ifdef ASSERT
  46   NoGCVerifier(bool verifygc = true);
  47   ~NoGCVerifier();
  48 #else
  49   NoGCVerifier(bool verifygc = true) {}
  50   ~NoGCVerifier() {}
  51 #endif
  52 };
  53 
  54 // A PauseNoGCVerifier is used to temporarily pause the behavior
  55 // of a NoGCVerifier object. If we are not in debug mode or if the
  56 // NoGCVerifier object has a _verifygc value of false, then there
  57 // is nothing to do.
  58 
  59 class PauseNoGCVerifier: public StackObj {
  60  private:
  61   NoGCVerifier * _ngcv;
  62 
  63  public:
  64 #ifdef ASSERT
  65   PauseNoGCVerifier(NoGCVerifier * ngcv);
  66   ~PauseNoGCVerifier();
  67 #else
  68   PauseNoGCVerifier(NoGCVerifier * ngcv) {}
  69   ~PauseNoGCVerifier() {}
  70 #endif
  71 };
  72 
  73 
  74 // A NoSafepointVerifier object will throw an assertion failure if
  75 // the current thread passes a possible safepoint while this object is
  76 // instantiated. A safepoint, will either be: an oop allocation, blocking
  77 // on a Mutex or JavaLock, or executing a VM operation.
  78 //
  79 // If StrictSafepointChecks is turned off, it degrades into a NoGCVerifier
  80 //
  81 class NoSafepointVerifier : public NoGCVerifier {
  82  friend class PauseNoSafepointVerifier;
  83 
  84  private:
  85   bool _activated;
  86   Thread *_thread;
  87  public:
  88 #ifdef ASSERT
  89   NoSafepointVerifier(bool activated = true, bool verifygc = true ) :
  90     NoGCVerifier(verifygc),
  91     _activated(activated) {
  92     _thread = Thread::current();
  93     if (_activated) {
  94       _thread->_allow_safepoint_count++;
  95     }
  96   }
  97 
  98   ~NoSafepointVerifier() {
  99     if (_activated) {
 100       _thread->_allow_safepoint_count--;
 101     }
 102   }
 103 #else
 104   NoSafepointVerifier(bool activated = true, bool verifygc = true) : NoGCVerifier(verifygc){}
 105   ~NoSafepointVerifier() {}
 106 #endif
 107 };
 108 
 109 // A PauseNoSafepointVerifier is used to temporarily pause the
 110 // behavior of a NoSafepointVerifier object. If we are not in debug
 111 // mode then there is nothing to do. If the NoSafepointVerifier
 112 // object has an _activated value of false, then there is nothing to
 113 // do for safepoint and allocation checking, but there may still be
 114 // something to do for the underlying NoGCVerifier object.
 115 
 116 class PauseNoSafepointVerifier : public PauseNoGCVerifier {
 117  private:
 118   NoSafepointVerifier * _nsv;
 119 
 120  public:
 121 #ifdef ASSERT
 122   PauseNoSafepointVerifier(NoSafepointVerifier * nsv)
 123     : PauseNoGCVerifier(nsv) {
 124 
 125     _nsv = nsv;
 126     if (_nsv->_activated) {
 127       _nsv->_thread->_allow_safepoint_count--;
 128     }
 129   }
 130 
 131   ~PauseNoSafepointVerifier() {
 132     if (_nsv->_activated) {
 133       _nsv->_thread->_allow_safepoint_count++;
 134     }
 135   }
 136 #else
 137   PauseNoSafepointVerifier(NoSafepointVerifier * nsv)
 138     : PauseNoGCVerifier(nsv) {}
 139   ~PauseNoSafepointVerifier() {}
 140 #endif
 141 };
 142 
 143 #endif // SHARE_RUNTIME_SAFEPOINTVERIFIERS_HPP