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