1 /*
   2  * Copyright (c) 1997, 2010, 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 "memory/gcLocker.inline.hpp"
  27 #include "memory/resourceArea.hpp"
  28 #include "memory/sharedHeap.hpp"
  29 
  30 volatile jint GC_locker::_jni_lock_count = 0;
  31 volatile jint GC_locker::_lock_count     = 0;
  32 volatile bool GC_locker::_needs_gc       = false;
  33 volatile bool GC_locker::_doing_gc       = false;
  34 
  35 void GC_locker::stall_until_clear() {
  36   assert(!JavaThread::current()->in_critical(), "Would deadlock");
  37   if (PrintJNIGCStalls && PrintGCDetails) {
  38     ResourceMark rm; // JavaThread::name() allocates to convert to UTF8
  39     gclog_or_tty->print_cr(
  40       "Allocation failed. Thread \"%s\" is stalled by JNI critical section.",
  41       JavaThread::current()->name());
  42   }
  43   MutexLocker   ml(JNICritical_lock);
  44   // Wait for _needs_gc  to be cleared
  45   while (GC_locker::needs_gc()) {
  46     JNICritical_lock->wait();
  47   }
  48 }
  49 
  50 void GC_locker::jni_lock_slow() {
  51   MutexLocker mu(JNICritical_lock);
  52   // Block entering threads if we know at least one thread is in a
  53   // JNI critical region and we need a GC.
  54   // We check that at least one thread is in a critical region before
  55   // blocking because blocked threads are woken up by a thread exiting
  56   // a JNI critical region.
  57   while ((is_jni_active() && needs_gc()) || _doing_gc) {
  58     JNICritical_lock->wait();
  59   }
  60   jni_lock();
  61 }
  62 
  63 void GC_locker::jni_unlock_slow() {
  64   MutexLocker mu(JNICritical_lock);
  65   jni_unlock();
  66   if (needs_gc() && !is_jni_active()) {
  67     // We're the last thread out. Cause a GC to occur.
  68     // GC will also check is_active, so this check is not
  69     // strictly needed. It's added here to make it clear that
  70     // the GC will NOT be performed if any other caller
  71     // of GC_locker::lock() still needs GC locked.
  72     if (!is_active()) {
  73       _doing_gc = true;
  74       {
  75         // Must give up the lock while at a safepoint
  76         MutexUnlocker munlock(JNICritical_lock);
  77         Universe::heap()->collect(GCCause::_gc_locker);
  78       }
  79       _doing_gc = false;
  80     }
  81     clear_needs_gc();
  82     JNICritical_lock->notify_all();
  83   }
  84 }
  85 
  86 // Implementation of No_GC_Verifier
  87 
  88 #ifdef ASSERT
  89 
  90 No_GC_Verifier::No_GC_Verifier(bool verifygc) {
  91   _verifygc = verifygc;
  92   if (_verifygc) {
  93     CollectedHeap* h = Universe::heap();
  94     assert(!h->is_gc_active(), "GC active during No_GC_Verifier");
  95     _old_invocations = h->total_collections();
  96   }
  97 }
  98 
  99 
 100 No_GC_Verifier::~No_GC_Verifier() {
 101   if (_verifygc) {
 102     CollectedHeap* h = Universe::heap();
 103     assert(!h->is_gc_active(), "GC active during No_GC_Verifier");
 104     if (_old_invocations != h->total_collections()) {
 105       fatal("collection in a No_GC_Verifier secured function");
 106     }
 107   }
 108 }
 109 
 110 Pause_No_GC_Verifier::Pause_No_GC_Verifier(No_GC_Verifier * ngcv) {
 111   _ngcv = ngcv;
 112   if (_ngcv->_verifygc) {
 113     // if we were verifying, then make sure that nothing is
 114     // wrong before we "pause" verification
 115     CollectedHeap* h = Universe::heap();
 116     assert(!h->is_gc_active(), "GC active during No_GC_Verifier");
 117     if (_ngcv->_old_invocations != h->total_collections()) {
 118       fatal("collection in a No_GC_Verifier secured function");
 119     }
 120   }
 121 }
 122 
 123 
 124 Pause_No_GC_Verifier::~Pause_No_GC_Verifier() {
 125   if (_ngcv->_verifygc) {
 126     // if we were verifying before, then reenable verification
 127     CollectedHeap* h = Universe::heap();
 128     assert(!h->is_gc_active(), "GC active during No_GC_Verifier");
 129     _ngcv->_old_invocations = h->total_collections();
 130   }
 131 }
 132 
 133 
 134 // JRT_LEAF rules:
 135 // A JRT_LEAF method may not interfere with safepointing by
 136 //   1) acquiring or blocking on a Mutex or JavaLock - checked
 137 //   2) allocating heap memory - checked
 138 //   3) executing a VM operation - checked
 139 //   4) executing a system call (including malloc) that could block or grab a lock
 140 //   5) invoking GC
 141 //   6) reaching a safepoint
 142 //   7) running too long
 143 // Nor may any method it calls.
 144 JRT_Leaf_Verifier::JRT_Leaf_Verifier()
 145   : No_Safepoint_Verifier(true, JRT_Leaf_Verifier::should_verify_GC())
 146 {
 147 }
 148 
 149 JRT_Leaf_Verifier::~JRT_Leaf_Verifier()
 150 {
 151 }
 152 
 153 bool JRT_Leaf_Verifier::should_verify_GC() {
 154   switch (JavaThread::current()->thread_state()) {
 155   case _thread_in_Java:
 156     // is in a leaf routine, there must be no safepoint.
 157     return true;
 158   case _thread_in_native:
 159     // A native thread is not subject to safepoints.
 160     // Even while it is in a leaf routine, GC is ok
 161     return false;
 162   default:
 163     // Leaf routines cannot be called from other contexts.
 164     ShouldNotReachHere();
 165     return false;
 166   }
 167 }
 168 #endif