1 #ifdef USE_PRAGMA_IDENT_SRC
   2 #pragma ident "@(#)gcLocker.cpp 1.52 07/05/17 15:54:45 JVM"
   3 #endif
   4 /*
   5  * Copyright 1997-2007 Sun Microsystems, Inc.  All Rights Reserved.
   6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   7  *
   8  * This code is free software; you can redistribute it and/or modify it
   9  * under the terms of the GNU General Public License version 2 only, as
  10  * published by the Free Software Foundation.
  11  *
  12  * This code is distributed in the hope that it will be useful, but WITHOUT
  13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15  * version 2 for more details (a copy is included in the LICENSE file that
  16  * accompanied this code).
  17  *
  18  * You should have received a copy of the GNU General Public License version
  19  * 2 along with this work; if not, write to the Free Software Foundation,
  20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21  *
  22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  23  * CA 95054 USA or visit www.sun.com if you need additional information or
  24  * have any questions.
  25  *  
  26  */
  27 
  28 # include "incls/_precompiled.incl"
  29 # include "incls/_gcLocker.cpp.incl"
  30 
  31 volatile jint GC_locker::_jni_lock_count = 0;
  32 volatile jint GC_locker::_lock_count     = 0;
  33 volatile bool GC_locker::_needs_gc       = false;
  34 volatile bool GC_locker::_doing_gc       = false;
  35 
  36 void GC_locker::stall_until_clear() {
  37   assert(!JavaThread::current()->in_critical(), "Would deadlock");
  38   MutexLocker   ml(JNICritical_lock);
  39   // Wait for _needs_gc  to be cleared
  40   while (GC_locker::needs_gc()) {
  41     JNICritical_lock->wait();
  42   }
  43 }
  44 
  45 void GC_locker::jni_lock_slow() {
  46   MutexLocker mu(JNICritical_lock);
  47   // Block entering threads if we know at least one thread is in a
  48   // JNI critical region and we need a GC.
  49   // We check that at least one thread is in a critical region before
  50   // blocking because blocked threads are woken up by a thread exiting
  51   // a JNI critical region.
  52   while ((is_jni_active() && needs_gc()) || _doing_gc) {
  53     JNICritical_lock->wait();
  54   }
  55   jni_lock();
  56 }
  57 
  58 void GC_locker::jni_unlock_slow() {
  59   MutexLocker mu(JNICritical_lock);
  60   jni_unlock();
  61   if (needs_gc() && !is_jni_active()) {
  62     // We're the last thread out. Cause a GC to occur.
  63     // GC will also check is_active, so this check is not
  64     // strictly needed. It's added here to make it clear that
  65     // the GC will NOT be performed if any other caller
  66     // of GC_locker::lock() still needs GC locked.
  67     if (!is_active()) {
  68       _doing_gc = true;
  69       {
  70         // Must give up the lock while at a safepoint
  71         MutexUnlocker munlock(JNICritical_lock);
  72         Universe::heap()->collect(GCCause::_gc_locker);
  73       }
  74       _doing_gc = false;
  75     }
  76     clear_needs_gc();
  77     JNICritical_lock->notify_all();
  78   }
  79 }
  80 
  81 // Implementation of No_GC_Verifier
  82 
  83 #ifdef ASSERT
  84 
  85 No_GC_Verifier::No_GC_Verifier(bool verifygc) {
  86   _verifygc = verifygc;
  87   if (_verifygc) {
  88     CollectedHeap* h = Universe::heap();
  89     assert(!h->is_gc_active(), "GC active during No_GC_Verifier");
  90     _old_invocations = h->total_collections();
  91   }
  92 }
  93 
  94 
  95 No_GC_Verifier::~No_GC_Verifier() {
  96   if (_verifygc) {
  97     CollectedHeap* h = Universe::heap();
  98     assert(!h->is_gc_active(), "GC active during No_GC_Verifier");
  99     if (_old_invocations != h->total_collections()) {
 100       fatal("collection in a No_GC_Verifier secured function");
 101     }
 102   }
 103 }
 104 
 105 Pause_No_GC_Verifier::Pause_No_GC_Verifier(No_GC_Verifier * ngcv) {
 106   _ngcv = ngcv;
 107   if (_ngcv->_verifygc) {
 108     // if we were verifying, then make sure that nothing is
 109     // wrong before we "pause" verification
 110     CollectedHeap* h = Universe::heap();
 111     assert(!h->is_gc_active(), "GC active during No_GC_Verifier");
 112     if (_ngcv->_old_invocations != h->total_collections()) {
 113       fatal("collection in a No_GC_Verifier secured function");
 114     }
 115   }
 116 }
 117 
 118 
 119 Pause_No_GC_Verifier::~Pause_No_GC_Verifier() {
 120   if (_ngcv->_verifygc) {
 121     // if we were verifying before, then reenable verification
 122     CollectedHeap* h = Universe::heap();
 123     assert(!h->is_gc_active(), "GC active during No_GC_Verifier");
 124     _ngcv->_old_invocations = h->total_collections();
 125   }
 126 }
 127 
 128 
 129 // JRT_LEAF rules: 
 130 // A JRT_LEAF method may not interfere with safepointing by
 131 //   1) acquiring or blocking on a Mutex or JavaLock - checked
 132 //   2) allocating heap memory - checked
 133 //   3) executing a VM operation - checked
 134 //   4) executing a system call (including malloc) that could block or grab a lock
 135 //   5) invoking GC
 136 //   6) reaching a safepoint
 137 //   7) running too long
 138 // Nor may any method it calls.
 139 JRT_Leaf_Verifier::JRT_Leaf_Verifier()
 140   : No_Safepoint_Verifier(true, JRT_Leaf_Verifier::should_verify_GC())
 141 {
 142 }
 143 
 144 JRT_Leaf_Verifier::~JRT_Leaf_Verifier()
 145 {
 146 }
 147 
 148 bool JRT_Leaf_Verifier::should_verify_GC() {
 149   switch (JavaThread::current()->thread_state()) {
 150   case _thread_in_Java:
 151     // is in a leaf routine, there must be no safepoint.
 152     return true;
 153   case _thread_in_native:
 154     // A native thread is not subject to safepoints.
 155     // Even while it is in a leaf routine, GC is ok
 156     return false;
 157   default:
 158     // Leaf routines cannot be called from other contexts.
 159     ShouldNotReachHere();
 160     return false;
 161   }
 162 }
 163 #endif