1 /*
  2  * Copyright (c) 1997, 2024, 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 "oops/markWord.hpp"
 27 #include "runtime/javaThread.hpp"
 28 #include "runtime/objectMonitor.inline.hpp"
 29 #include "utilities/ostream.hpp"
 30 
 31 #ifdef _LP64
 32 STATIC_ASSERT((markWord::klass_shadow_mask_inplace & markWord::klass_mask_in_place) == 0);
 33 STATIC_ASSERT((markWord::klass_load_shift + markWord::klass_shadow_bits) == markWord::klass_shift);
 34 STATIC_ASSERT(markWord::klass_shift + markWord::klass_bits == 64);
 35 // The hash (preceding nKlass) shall be a direct neighbor but not interleave
 36 STATIC_ASSERT(markWord::klass_shift == markWord::hash_bits_compact + markWord::hash_shift_compact);
 37 #endif
 38 
 39 markWord markWord::displaced_mark_helper() const {
 40   assert(has_displaced_mark_helper(), "check");
 41   if (has_monitor()) {
 42     // Has an inflated monitor. Must be checked before has_locker().
 43     ObjectMonitor* monitor = this->monitor();
 44     return monitor->header();
 45   }
 46   if (has_locker()) {  // has a stack lock
 47     BasicLock* locker = this->locker();
 48     return locker->displaced_header();
 49   }
 50   // This should never happen:
 51   fatal("bad header=" INTPTR_FORMAT, value());
 52   return markWord(value());
 53 }
 54 
 55 void markWord::set_displaced_mark_helper(markWord m) const {
 56   assert(has_displaced_mark_helper(), "check");
 57   if (has_monitor()) {
 58     // Has an inflated monitor. Must be checked before has_locker().
 59     ObjectMonitor* monitor = this->monitor();
 60     monitor->set_header(m);
 61     return;
 62   }
 63   if (has_locker()) {  // has a stack lock
 64     BasicLock* locker = this->locker();
 65     locker->set_displaced_header(m);
 66     return;
 67   }
 68   // This should never happen:
 69   fatal("bad header=" INTPTR_FORMAT, value());
 70 }
 71 
 72 void markWord::print_on(outputStream* st, bool print_monitor_info) const {
 73   if (is_marked()) {  // last bits = 11
 74     st->print(" marked(" INTPTR_FORMAT ")", value());
 75   } else if (has_monitor()) {  // last bits = 10
 76     // have to check has_monitor() before is_locked()
 77     st->print(" monitor(" INTPTR_FORMAT ")=", value());
 78     if (print_monitor_info && LockingMode != LM_LIGHTWEIGHT) {
 79       // TODO[OMWorld]: Cleanup, even if the monitor read is racy, something should be printed.
 80       ObjectMonitor* mon = monitor();
 81       if (mon == nullptr) {
 82         st->print("null (this should never be seen!)");
 83       } else {
 84         mon->print_on(st);
 85       }
 86     }
 87   } else if (is_locked()) {  // last bits != 01 => 00
 88     // thin locked
 89     st->print(" locked(" INTPTR_FORMAT ")", value());
 90   } else {
 91     st->print(" mark(");
 92     if (is_unlocked()) {   // last bits = 01
 93       st->print("is_unlocked");
 94       if (has_no_hash()) {
 95         st->print(" no_hash");
 96       } else {
 97         st->print(" hash=" INTPTR_FORMAT, hash());
 98       }
 99     } else {
100       st->print("??");
101     }
102     st->print(" age=%d)", age());
103   }
104 }