1 /*
   2  * Copyright (c) 1997, 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/collectedHeap.hpp"
  27 #include "gc/shared/gcLocker.hpp"
  28 #include "memory/resourceArea.hpp"
  29 #include "logging/log.hpp"
  30 #include "runtime/atomic.hpp"
  31 #include "runtime/thread.inline.hpp"
  32 #include "runtime/threadSMR.hpp"
  33 
  34 volatile jint GCLocker::_jni_lock_count = 0;
  35 volatile bool GCLocker::_needs_gc       = false;
  36 volatile bool GCLocker::_doing_gc       = false;
  37 
  38 #ifdef ASSERT
  39 volatile jint GCLocker::_debug_jni_lock_count = 0;
  40 #endif
  41 
  42 
  43 #ifdef ASSERT
  44 void GCLocker::verify_critical_count() {
  45   if (SafepointSynchronize::is_at_safepoint()) {
  46     assert(!needs_gc() || _debug_jni_lock_count == _jni_lock_count, "must agree");
  47     int count = 0;
  48     // Count the number of threads with critical operations in progress
  49     JavaThreadIteratorWithHandle jtiwh;
  50     for (; JavaThread *thr = jtiwh.next(); ) {
  51       if (thr->in_critical()) {
  52         count++;
  53       }
  54     }
  55     if (_jni_lock_count != count) {
  56       log_error(gc, verify)("critical counts don't match: %d != %d", _jni_lock_count, count);
  57       jtiwh.rewind();
  58       for (; JavaThread *thr = jtiwh.next(); ) {
  59         if (thr->in_critical()) {
  60           log_error(gc, verify)(INTPTR_FORMAT " in_critical %d", p2i(thr), thr->in_critical());
  61         }
  62       }
  63     }
  64     assert(_jni_lock_count == count, "must be equal");
  65   }
  66 }
  67 
  68 // In debug mode track the locking state at all times
  69 void GCLocker::increment_debug_jni_lock_count() {
  70   assert(_debug_jni_lock_count >= 0, "bad value");
  71   Atomic::inc(&_debug_jni_lock_count);
  72 }
  73 
  74 void GCLocker::decrement_debug_jni_lock_count() {
  75   assert(_debug_jni_lock_count > 0, "bad value");
  76   Atomic::dec(&_debug_jni_lock_count);
  77 }
  78 #endif
  79 
  80 void GCLocker::log_debug_jni(const char* msg) {
  81   Log(gc, jni) log;
  82   if (log.is_debug()) {
  83     ResourceMark rm; // JavaThread::name() allocates to convert to UTF8
  84     log.debug("%s Thread \"%s\" %d locked.", msg, Thread::current()->name(), _jni_lock_count);
  85   }
  86 }
  87 
  88 bool GCLocker::is_at_safepoint() {
  89   return SafepointSynchronize::is_at_safepoint();
  90 }
  91 
  92 bool GCLocker::check_active_before_gc() {
  93   assert(SafepointSynchronize::is_at_safepoint(), "only read at safepoint");
  94   if (is_active() && !_needs_gc) {
  95     verify_critical_count();
  96     _needs_gc = true;
  97     log_debug_jni("Setting _needs_gc.");
  98   }
  99   return is_active();
 100 }
 101 
 102 void GCLocker::stall_until_clear() {
 103   assert(!JavaThread::current()->in_critical(), "Would deadlock");
 104   MutexLocker   ml(JNICritical_lock);
 105 
 106   if (needs_gc()) {
 107     log_debug_jni("Allocation failed. Thread stalled by JNI critical section.");
 108   }
 109 
 110   // Wait for _needs_gc  to be cleared
 111   while (needs_gc()) {
 112     JNICritical_lock->wait();
 113   }
 114 }
 115 
 116 void GCLocker::jni_lock(JavaThread* thread) {
 117   assert(!thread->in_critical(), "shouldn't currently be in a critical region");
 118   MutexLocker mu(JNICritical_lock);
 119   // Block entering threads if we know at least one thread is in a
 120   // JNI critical region and we need a GC.
 121   // We check that at least one thread is in a critical region before
 122   // blocking because blocked threads are woken up by a thread exiting
 123   // a JNI critical region.
 124   while (is_active_and_needs_gc() || _doing_gc) {
 125     JNICritical_lock->wait();
 126   }
 127   thread->enter_critical();
 128   _jni_lock_count++;
 129   increment_debug_jni_lock_count();
 130 }
 131 
 132 void GCLocker::jni_unlock(JavaThread* thread) {
 133   assert(thread->in_last_critical(), "should be exiting critical region");
 134   MutexLocker mu(JNICritical_lock);
 135   _jni_lock_count--;
 136   decrement_debug_jni_lock_count();
 137   thread->exit_critical();
 138   if (needs_gc() && !is_active_internal()) {
 139     // We're the last thread out. Cause a GC to occur.
 140     _doing_gc = true;
 141     {
 142       // Must give up the lock while at a safepoint
 143       MutexUnlocker munlock(JNICritical_lock);
 144       log_debug_jni("Performing GC after exiting critical section.");
 145       Universe::heap()->collect(GCCause::_gc_locker);
 146     }
 147     _doing_gc = false;
 148     _needs_gc = false;
 149     JNICritical_lock->notify_all();
 150   }
 151 }