1 /*
  2  * Copyright (c) 2022, Red Hat, Inc. 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 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHOBJECTUTILS_INLINE_HPP
 26 #define SHARE_GC_SHENANDOAH_SHENANDOAHOBJECTUTILS_INLINE_HPP
 27 
 28 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
 29 #include "gc/shenandoah/shenandoahObjectUtils.hpp"
 30 #include "oops/klass.hpp"
 31 #include "oops/markWord.inline.hpp"
 32 #include "oops/oop.inline.hpp"
 33 #include "runtime/objectMonitor.inline.hpp"
 34 #include "runtime/thread.hpp"
 35 
 36 // This is a variant of ObjectSynchronizer::stable_mark(), which does the same thing, but also
 37 // handles forwarded objects. This is intended to be used by concurrent evacuation only. No other
 38 // code is supposed to observe from-space objects.
 39 #ifdef _LP64
 40 markWord ShenandoahObjectUtils::stable_mark(oop obj) {
 41   ShenandoahHeap* heap = ShenandoahHeap::heap();
 42   for (;;) {
 43     assert(heap->is_in(obj), "object not in heap: " PTR_FORMAT, p2i(obj));
 44     markWord mark = obj->mark_acquire();
 45 
 46     // The mark can be in one of the following states:
 47     // *  Inflated     - just return mark from inflated monitor
 48     // *  Stack-locked - coerce it to inflating, and then return displaced mark
 49     // *  INFLATING    - busy wait for conversion to complete
 50     // *  Neutral      - return mark
 51     // *  Marked       - object is forwarded, try again on forwardee
 52 
 53     // Most common case first.
 54     if (mark.is_neutral()) {
 55       return mark;
 56     }
 57 
 58     // If object is already forwarded, then resolve it, and try again.
 59     if (mark.is_marked()) {
 60       if (heap->is_full_gc_move_in_progress()) {
 61         // In these cases, we want to return the header as-is: the Klass* would not be overloaded.
 62         return mark;
 63       }
 64       obj = cast_to_oop(mark.decode_pointer());
 65       continue;
 66     }
 67 
 68     // CASE: inflated
 69     if (mark.has_monitor()) {
 70       // It is safe to access the object monitor because all Java and GC worker threads
 71       // participate in the monitor deflation protocol (i.e, they react to handshakes and STS requests).
 72       ObjectMonitor* inf = mark.monitor();
 73       markWord dmw = inf->header();
 74       assert(dmw.is_neutral(), "invariant: header=" INTPTR_FORMAT ", original mark: " INTPTR_FORMAT, dmw.value(), mark.value());
 75       return dmw;
 76     }
 77 
 78     // CASE: inflating
 79     if (mark.is_being_inflated()) {
 80       // Interference, try again.
 81       continue;
 82     }
 83 
 84     // CASE: stack-locked
 85     if (mark.has_locker()) {
 86       if (Thread::current()->is_lock_owned((address)mark.locker())) {
 87         // This thread owns the lock. We can safely access it.
 88         markWord dmw = mark.displaced_mark_helper();
 89         assert(dmw.is_neutral(), "invariant: header=" INTPTR_FORMAT ", original mark: " INTPTR_FORMAT, dmw.value(), mark.value());
 90         return dmw;
 91       }
 92 
 93       // Else we try to install INFLATING into the header. This will (temporarily) prevent other
 94       // threads from stack-locking or evacuating the object.
 95       markWord cmp = obj->cas_set_mark(markWord::INFLATING(), mark);
 96       if (cmp != mark) {
 97         continue;       // Interference -- just retry
 98       }
 99 
100       // We've successfully installed INFLATING (0) into the mark-word.
101       // This is the only case where 0 will appear in a mark-word.
102       // Only the singular thread that successfully swings the mark-word
103       // to 0 can fetch the stack-lock and safely read the displaced header.
104 
105       // fetch the displaced mark from the owner's stack.
106       // The owner can't die or unwind past the lock while our INFLATING
107       // object is in the mark.  Furthermore the owner can't complete
108       // an unlock on the object, either. No other thread can do evacuation, either.
109       markWord dmw = mark.displaced_mark_helper();
110       // Catch if the object's header is not neutral (not locked and
111       // not marked is what we care about here).
112       assert(dmw.is_neutral(), "invariant: header=" INTPTR_FORMAT, dmw.value());
113 
114       // Must preserve store ordering. The monitor state must
115       // be stable at the time of publishing the monitor address.
116       guarantee(obj->mark() == markWord::INFLATING(), "invariant");
117       // Release semantics so that above set_object() is seen first.
118       obj->release_set_mark(mark);
119 
120       return dmw;
121     }
122   }
123 }
124 #endif
125 
126 Klass* ShenandoahObjectUtils::klass(oop obj) {
127 #ifdef _LP64
128   markWord header = stable_mark(obj);
129   assert(header.narrow_klass() != 0, "klass must not be NULL: " INTPTR_FORMAT, header.value());
130   return header.klass();
131 #else
132   return obj->klass();
133 #endif
134 }
135 
136 size_t ShenandoahObjectUtils::size(oop obj) {
137   Klass* kls = klass(obj);
138   return obj->size_given_klass(kls);
139 }
140 
141 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHOBJECTUTILS_HPP