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