1 /*
 2  * Copyright (c) 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 #ifndef SHARE_RUNTIME_LIGHTWEIGHTSYNCHRONIZER_HPP
26 #define SHARE_RUNTIME_LIGHTWEIGHTSYNCHRONIZER_HPP
27 
28 #include "memory/allStatic.hpp"
29 #include "runtime/javaThread.hpp"
30 #include "runtime/objectMonitor.hpp"
31 #include "runtime/synchronizer.hpp"
32 
33 class ObjectMonitorTable;
34 
35 class LightweightSynchronizer : AllStatic {
36  private:
37   static ObjectMonitor* get_or_insert_monitor_from_table(oop object, JavaThread* current, bool* inserted);
38   static ObjectMonitor* get_or_insert_monitor(oop object, JavaThread* current, ObjectSynchronizer::InflateCause cause);
39 
40   static ObjectMonitor* add_monitor(JavaThread* current, ObjectMonitor* monitor, oop obj);
41   static bool remove_monitor(Thread* current, ObjectMonitor* monitor, oop obj);
42 
43   static void deflate_mark_word(oop object);
44 
45   static void ensure_lock_stack_space(JavaThread* current);
46 
47   class CacheSetter;
48   class LockStackInflateContendedLocks;
49   class VerifyThreadState;
50 
51  public:
52   static void initialize();
53 
54   static bool needs_resize();
55   static bool resize_table(JavaThread* current);
56 
57  private:
58   static inline bool fast_lock_try_enter(oop obj, LockStack& lock_stack, JavaThread* current);
59   static bool fast_lock_spin_enter(oop obj, LockStack& lock_stack, JavaThread* current, bool observed_deflation);
60 
61  public:
62   static void enter_for(Handle obj, BasicLock* lock, JavaThread* locking_thread);
63   static void enter(Handle obj, BasicLock* lock, JavaThread* current);
64   static void exit(oop object, JavaThread* current);
65 
66   static ObjectMonitor* inflate_into_object_header(oop object, ObjectSynchronizer::InflateCause cause, JavaThread* locking_thread, Thread* current);
67   static ObjectMonitor* inflate_locked_or_imse(oop object, ObjectSynchronizer::InflateCause cause, TRAPS);
68   static ObjectMonitor* inflate_fast_locked_object(oop object, ObjectSynchronizer::InflateCause cause, JavaThread* locking_thread, JavaThread* current);
69   static ObjectMonitor* inflate_and_enter(oop object, ObjectSynchronizer::InflateCause cause, JavaThread* locking_thread, JavaThread* current);
70 
71   static void deflate_monitor(Thread* current, oop obj, ObjectMonitor* monitor);
72 
73   static ObjectMonitor* get_monitor_from_table(Thread* current, oop obj);
74 
75   static bool contains_monitor(Thread* current, ObjectMonitor* monitor);
76 
77   static bool quick_enter(oop obj, BasicLock* Lock, JavaThread* current);
78 
79   // NOTE: May not cause monitor inflation
80   static uint32_t get_hash(markWord mark, oop obj);
81   // For CDS path.
82   static uint32_t get_hash(markWord mark, oop obj, Klass* klass);
83 };
84 
85 #endif // SHARE_RUNTIME_LIGHTWEIGHTSYNCHRONIZER_HPP