xref: /aosp_15_r20/art/runtime/interpreter/lock_count_data.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2011 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #ifndef ART_RUNTIME_INTERPRETER_LOCK_COUNT_DATA_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_RUNTIME_INTERPRETER_LOCK_COUNT_DATA_H_
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include <memory>
21*795d594fSAndroid Build Coastguard Worker #include <vector>
22*795d594fSAndroid Build Coastguard Worker 
23*795d594fSAndroid Build Coastguard Worker #include "base/locks.h"
24*795d594fSAndroid Build Coastguard Worker #include "base/macros.h"
25*795d594fSAndroid Build Coastguard Worker 
26*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
27*795d594fSAndroid Build Coastguard Worker 
28*795d594fSAndroid Build Coastguard Worker namespace mirror {
29*795d594fSAndroid Build Coastguard Worker class Object;
30*795d594fSAndroid Build Coastguard Worker }  // namespace mirror
31*795d594fSAndroid Build Coastguard Worker 
32*795d594fSAndroid Build Coastguard Worker class Thread;
33*795d594fSAndroid Build Coastguard Worker 
34*795d594fSAndroid Build Coastguard Worker // Counting locks by storing object pointers into a vector. Duplicate entries mark recursive locks.
35*795d594fSAndroid Build Coastguard Worker // The vector will be visited with the ShadowFrame during GC (so all the locked-on objects are
36*795d594fSAndroid Build Coastguard Worker // thread roots).
37*795d594fSAndroid Build Coastguard Worker // Note: implementation is split so that the call sites may be optimized to no-ops in case no
38*795d594fSAndroid Build Coastguard Worker //       lock counting is necessary. The actual implementation is in the cc file to avoid
39*795d594fSAndroid Build Coastguard Worker //       dependencies.
40*795d594fSAndroid Build Coastguard Worker class LockCountData {
41*795d594fSAndroid Build Coastguard Worker  public:
42*795d594fSAndroid Build Coastguard Worker   // Add the given object to the list of monitors, that is, objects that have been locked. This
43*795d594fSAndroid Build Coastguard Worker   // will not throw (but be skipped if there is an exception pending on entry).
44*795d594fSAndroid Build Coastguard Worker   EXPORT void AddMonitor(Thread* self, mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_);
45*795d594fSAndroid Build Coastguard Worker 
46*795d594fSAndroid Build Coastguard Worker   // Try to remove the given object from the monitor list, indicating an unlock operation.
47*795d594fSAndroid Build Coastguard Worker   // This will throw an IllegalMonitorStateException (clearing any already pending exception), in
48*795d594fSAndroid Build Coastguard Worker   // case that there wasn't a lock recorded for the object.
49*795d594fSAndroid Build Coastguard Worker   EXPORT void RemoveMonitorOrThrow(Thread* self,
50*795d594fSAndroid Build Coastguard Worker                                    const mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_);
51*795d594fSAndroid Build Coastguard Worker 
52*795d594fSAndroid Build Coastguard Worker   // Check whether all acquired monitors have been released. This will potentially throw an
53*795d594fSAndroid Build Coastguard Worker   // IllegalMonitorStateException, clearing any already pending exception. Returns true if the
54*795d594fSAndroid Build Coastguard Worker   // check shows that everything is OK wrt/ lock counting, false otherwise.
55*795d594fSAndroid Build Coastguard Worker   EXPORT bool CheckAllMonitorsReleasedOrThrow(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
56*795d594fSAndroid Build Coastguard Worker 
57*795d594fSAndroid Build Coastguard Worker   template <typename T, typename... Args>
VisitMonitors(T visitor,Args &&...args)58*795d594fSAndroid Build Coastguard Worker   void VisitMonitors(T visitor, Args&&... args) REQUIRES_SHARED(Locks::mutator_lock_) {
59*795d594fSAndroid Build Coastguard Worker     if (monitors_ != nullptr) {
60*795d594fSAndroid Build Coastguard Worker       // Visitors may change the Object*. Be careful with the foreach loop.
61*795d594fSAndroid Build Coastguard Worker       for (mirror::Object*& obj : *monitors_) {
62*795d594fSAndroid Build Coastguard Worker         visitor(/* inout */ &obj, std::forward<Args>(args)...);
63*795d594fSAndroid Build Coastguard Worker       }
64*795d594fSAndroid Build Coastguard Worker     }
65*795d594fSAndroid Build Coastguard Worker   }
66*795d594fSAndroid Build Coastguard Worker 
67*795d594fSAndroid Build Coastguard Worker  private:
68*795d594fSAndroid Build Coastguard Worker   // Stores references to the locked-on objects. As noted, this should be visited during thread
69*795d594fSAndroid Build Coastguard Worker   // marking.
70*795d594fSAndroid Build Coastguard Worker   std::unique_ptr<std::vector<mirror::Object*>> monitors_;
71*795d594fSAndroid Build Coastguard Worker };
72*795d594fSAndroid Build Coastguard Worker 
73*795d594fSAndroid Build Coastguard Worker }  // namespace art
74*795d594fSAndroid Build Coastguard Worker 
75*795d594fSAndroid Build Coastguard Worker #endif  // ART_RUNTIME_INTERPRETER_LOCK_COUNT_DATA_H_
76