1 /*
   2  * Copyright (c) 1997, 2015, 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 "classfile/javaClasses.hpp"
  27 #include "classfile/systemDictionary.hpp"
  28 #include "gc_interface/collectedHeap.inline.hpp"
  29 #include "memory/genCollectedHeap.hpp"
  30 #include "memory/specialized_oop_closures.hpp"
  31 #include "oops/instanceRefKlass.inline.hpp"
  32 #include "oops/oop.inline.hpp"
  33 #include "utilities/preserveException.hpp"
  34 #include "utilities/macros.hpp"
  35 
  36 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
  37 
  38 void InstanceRefKlass::update_nonstatic_oop_maps(Klass* k) {
  39   // Clear the nonstatic oop-map entries corresponding to referent
  40   // and nextPending field.  They are treated specially by the
  41   // garbage collector.
  42   // The discovered field is used only by the garbage collector
  43   // and is also treated specially.
  44   InstanceKlass* ik = InstanceKlass::cast(k);
  45 
  46   // Check that we have the right class
  47   debug_only(static bool first_time = true);
  48   assert(k == SystemDictionary::Reference_klass() && first_time,
  49          "Invalid update of maps");
  50   debug_only(first_time = false);
  51   assert(ik->nonstatic_oop_map_count() == 1, "just checking");
  52 
  53   OopMapBlock* map = ik->start_of_nonstatic_oop_maps();
  54 
  55   // Check that the current map is (2,4) - currently points at field with
  56   // offset 2 (words) and has 4 map entries.
  57   debug_only(int offset = java_lang_ref_Reference::referent_offset);
  58   debug_only(unsigned int count = ((java_lang_ref_Reference::discovered_offset -
  59     java_lang_ref_Reference::referent_offset)/heapOopSize) + 1);
  60 
  61   if (UseSharedSpaces) {
  62     assert(map->offset() == java_lang_ref_Reference::queue_offset &&
  63            map->count() == 1, "just checking");
  64   } else {
  65     assert(map->offset() == offset && map->count() == count,
  66            "just checking");
  67 
  68     // Update map to (3,1) - point to offset of 3 (words) with 1 map entry.
  69     map->set_offset(java_lang_ref_Reference::queue_offset);
  70     map->set_count(1);
  71   }
  72 }
  73 
  74 
  75 // Verification
  76 
  77 void InstanceRefKlass::oop_verify_on(oop obj, outputStream* st) {
  78   InstanceKlass::oop_verify_on(obj, st);
  79   // Verify referent field
  80   oop referent = java_lang_ref_Reference::referent(obj);
  81 
  82   // We should make this general to all heaps
  83   GenCollectedHeap* gch = NULL;
  84   if (Universe::heap()->kind() == CollectedHeap::GenCollectedHeap)
  85     gch = GenCollectedHeap::heap();
  86 
  87   if (referent != NULL) {
  88     guarantee(referent->is_oop(), "referent field heap failed");
  89   }
  90   // Verify next field
  91   oop next = java_lang_ref_Reference::next(obj);
  92   if (next != NULL) {
  93     guarantee(next->is_oop(), "next field verify failed");
  94     guarantee(next->is_instanceRef(), "next field verify failed");
  95   }
  96 }
  97 
  98 bool InstanceRefKlass::owns_pending_list_lock(JavaThread* thread) {
  99   if (java_lang_ref_Reference::pending_list_lock() == NULL) return false;
 100   Handle h_lock(thread, java_lang_ref_Reference::pending_list_lock());
 101   return ObjectSynchronizer::current_thread_holds_lock(thread, h_lock);
 102 }
 103 
 104 void InstanceRefKlass::acquire_pending_list_lock(BasicLock *pending_list_basic_lock) {
 105   // we may enter this with pending exception set
 106   PRESERVE_EXCEPTION_MARK;  // exceptions are never thrown, needed for TRAPS argument
 107 
 108   // Create a HandleMark in case we retry a GC multiple times.
 109   // Each time we attempt the GC, we allocate the handle below
 110   // to hold the pending list lock. We want to free this handle.
 111   HandleMark hm;
 112 
 113   Handle h_lock(THREAD, java_lang_ref_Reference::pending_list_lock());
 114   ObjectSynchronizer::fast_enter(h_lock, pending_list_basic_lock, false, THREAD);
 115   assert(ObjectSynchronizer::current_thread_holds_lock(
 116            JavaThread::current(), h_lock),
 117          "Locking should have succeeded");
 118   if (HAS_PENDING_EXCEPTION) CLEAR_PENDING_EXCEPTION;
 119 }
 120 
 121 void InstanceRefKlass::release_and_notify_pending_list_lock(
 122   BasicLock *pending_list_basic_lock) {
 123   // we may enter this with pending exception set
 124   PRESERVE_EXCEPTION_MARK;  // exceptions are never thrown, needed for TRAPS argument
 125 
 126   // Create a HandleMark in case we retry a GC multiple times.
 127   // Each time we attempt the GC, we allocate the handle below
 128   // to hold the pending list lock. We want to free this handle.
 129   HandleMark hm;
 130 
 131   Handle h_lock(THREAD, java_lang_ref_Reference::pending_list_lock());
 132   assert(ObjectSynchronizer::current_thread_holds_lock(
 133            JavaThread::current(), h_lock),
 134          "Lock should be held");
 135   // Notify waiters on pending lists lock if there is any reference.
 136   if (java_lang_ref_Reference::pending_list() != NULL) {
 137     ObjectSynchronizer::notifyall(h_lock, THREAD);
 138   }
 139   ObjectSynchronizer::fast_exit(h_lock(), pending_list_basic_lock, THREAD);
 140   if (HAS_PENDING_EXCEPTION) CLEAR_PENDING_EXCEPTION;
 141 }