1 /*
  2  * Copyright (c) 2019, 2025, 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 #include "classfile/vmClasses.hpp"
 25 #include "memory/resourceArea.hpp"
 26 #include "memory/universe.hpp"
 27 #include "oops/instanceKlass.hpp"
 28 #include "oops/oop.inline.hpp"
 29 #include "runtime/atomic.hpp"
 30 #include "runtime/interfaceSupport.inline.hpp"
 31 #include "runtime/orderAccess.hpp"
 32 #include "runtime/os.hpp"
 33 #include "runtime/semaphore.inline.hpp"
 34 #include "runtime/synchronizer.hpp"
 35 #include "threadHelper.inline.hpp"
 36 #include "unittest.hpp"
 37 #include "utilities/globalDefinitions.hpp"
 38 #include "utilities/ostream.hpp"
 39 
 40 // The test doesn't work for PRODUCT because it needs WizardMode
 41 #ifndef PRODUCT
 42 
 43 static void assert_test_pattern(Handle object, const char* pattern) {
 44   stringStream st;
 45   object->print_on(&st);
 46   ASSERT_THAT(st.base(), testing::HasSubstr(pattern));
 47 }
 48 
 49 class LockerThread : public JavaTestThread {
 50   oop _obj;
 51   public:
 52   LockerThread(Semaphore* post, oop obj) : JavaTestThread(post), _obj(obj) {}
 53   virtual ~LockerThread() {}
 54 
 55   void main_run() {
 56     JavaThread* THREAD = JavaThread::current();
 57     HandleMark hm(THREAD);
 58     Handle h_obj(THREAD, _obj);
 59     ResourceMark rm(THREAD);
 60 
 61     // Wait gets the lock inflated.
 62     // The object will stay locked for the context of 'ol' so the lock will
 63     // still be inflated after the notify_all() call. Deflation can't happen
 64     // while an ObjectMonitor is "busy" and being locked is the most "busy"
 65     // state we have...
 66     ObjectLocker ol(h_obj, THREAD);
 67     ol.notify_all(THREAD);
 68     assert_test_pattern(h_obj, "monitor");
 69   }
 70 };
 71 
 72 
 73 TEST_VM(markWord, printing) {
 74   JavaThread* THREAD = JavaThread::current();
 75   ThreadInVMfromNative invm(THREAD);
 76   ResourceMark rm(THREAD);
 77 
 78   oop obj = vmClasses::Byte_klass()->allocate_instance(THREAD);
 79 
 80   FlagSetting fs(WizardMode, true);
 81 
 82   HandleMark hm(THREAD);
 83   Handle h_obj(THREAD, obj);
 84 
 85   // Thread tries to lock it.
 86   {
 87     ObjectLocker ol(h_obj, THREAD);
 88     assert_test_pattern(h_obj, "locked");
 89   }
 90   assert_test_pattern(h_obj, "is_unlocked no_hash");
 91 
 92   // Hash the object then print it.
 93   intx hash = h_obj->identity_hash();
 94   assert_test_pattern(h_obj, "is_unlocked hash=0x");
 95 
 96   // Wait gets the lock inflated.
 97   {
 98     ObjectLocker ol(h_obj, THREAD);
 99 
100     Semaphore done(0);
101     LockerThread* st;
102     st = new LockerThread(&done, h_obj());
103     st->doit();
104 
105     ol.wait(THREAD);
106     assert_test_pattern(h_obj, "monitor");
107     done.wait_with_safepoint_check(THREAD);  // wait till the thread is done.
108   }
109 }
110 #endif // PRODUCT