Andreas Gampe | 36a296f | 2017-06-13 14:11:11 -0700 | [diff] [blame] | 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/mutex.h" |
| 24 | |
| 25 | namespace art { |
| 26 | |
| 27 | namespace mirror { |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame^] | 28 | class Object; |
Andreas Gampe | 36a296f | 2017-06-13 14:11:11 -0700 | [diff] [blame] | 29 | } // namespace mirror |
| 30 | |
| 31 | class Thread; |
| 32 | |
| 33 | // Counting locks by storing object pointers into a vector. Duplicate entries mark recursive locks. |
| 34 | // The vector will be visited with the ShadowFrame during GC (so all the locked-on objects are |
| 35 | // thread roots). |
| 36 | // Note: implementation is split so that the call sites may be optimized to no-ops in case no |
| 37 | // lock counting is necessary. The actual implementation is in the cc file to avoid |
| 38 | // dependencies. |
| 39 | class LockCountData { |
| 40 | public: |
| 41 | // Add the given object to the list of monitors, that is, objects that have been locked. This |
| 42 | // will not throw (but be skipped if there is an exception pending on entry). |
| 43 | void AddMonitor(Thread* self, mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_); |
| 44 | |
| 45 | // Try to remove the given object from the monitor list, indicating an unlock operation. |
| 46 | // This will throw an IllegalMonitorStateException (clearing any already pending exception), in |
| 47 | // case that there wasn't a lock recorded for the object. |
| 48 | void RemoveMonitorOrThrow(Thread* self, |
| 49 | const mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_); |
| 50 | |
| 51 | // Check whether all acquired monitors have been released. This will potentially throw an |
| 52 | // IllegalMonitorStateException, clearing any already pending exception. Returns true if the |
| 53 | // check shows that everything is OK wrt/ lock counting, false otherwise. |
| 54 | bool CheckAllMonitorsReleasedOrThrow(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_); |
| 55 | |
| 56 | template <typename T, typename... Args> |
| 57 | void VisitMonitors(T visitor, Args&&... args) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 58 | if (monitors_ != nullptr) { |
| 59 | // Visitors may change the Object*. Be careful with the foreach loop. |
| 60 | for (mirror::Object*& obj : *monitors_) { |
| 61 | visitor(/* inout */ &obj, std::forward<Args>(args)...); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | private: |
| 67 | // Stores references to the locked-on objects. As noted, this should be visited during thread |
| 68 | // marking. |
| 69 | std::unique_ptr<std::vector<mirror::Object*>> monitors_; |
| 70 | }; |
| 71 | |
| 72 | } // namespace art |
| 73 | |
| 74 | #endif // ART_RUNTIME_INTERPRETER_LOCK_COUNT_DATA_H_ |