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