monitor.h revision 94f7b49578b6aaa80de8ffed230648d601393905
1/* 2 * Copyright (C) 2008 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_MONITOR_H_ 18#define ART_RUNTIME_MONITOR_H_ 19 20#include <pthread.h> 21#include <stdint.h> 22 23#include <iosfwd> 24#include <list> 25#include <vector> 26 27#include "atomic.h" 28#include "base/mutex.h" 29#include "gc_root.h" 30#include "object_callbacks.h" 31#include "read_barrier_option.h" 32#include "thread_state.h" 33 34namespace art { 35 36class LockWord; 37template<class T> class Handle; 38class Thread; 39class StackVisitor; 40typedef uint32_t MonitorId; 41 42namespace mirror { 43 class ArtMethod; 44 class Object; 45} // namespace mirror 46 47class Monitor { 48 public: 49 // The default number of spins that are done before thread suspension is used to forcibly inflate 50 // a lock word. See Runtime::max_spins_before_thin_lock_inflation_. 51 constexpr static size_t kDefaultMaxSpinsBeforeThinLockInflation = 50; 52 53 ~Monitor(); 54 55 static bool IsSensitiveThread(); 56 static void Init(uint32_t lock_profiling_threshold, bool (*is_sensitive_thread_hook)()); 57 58 // Return the thread id of the lock owner or 0 when there is no owner. 59 static uint32_t GetLockOwnerThreadId(mirror::Object* obj) 60 NO_THREAD_SAFETY_ANALYSIS; // TODO: Reading lock owner without holding lock is racy. 61 62 static mirror::Object* MonitorEnter(Thread* thread, mirror::Object* obj) 63 EXCLUSIVE_LOCK_FUNCTION(obj) 64 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 65 static bool MonitorExit(Thread* thread, mirror::Object* obj) 66 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) 67 UNLOCK_FUNCTION(obj); 68 69 static void Notify(Thread* self, mirror::Object* obj) 70 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 71 DoNotify(self, obj, false); 72 } 73 static void NotifyAll(Thread* self, mirror::Object* obj) 74 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 75 DoNotify(self, obj, true); 76 } 77 static void Wait(Thread* self, mirror::Object* obj, int64_t ms, int32_t ns, 78 bool interruptShouldThrow, ThreadState why) 79 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 80 81 static void DescribeWait(std::ostream& os, const Thread* thread) 82 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_) 83 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 84 85 // Used to implement JDWP's ThreadReference.CurrentContendedMonitor. 86 static mirror::Object* GetContendedMonitor(Thread* thread) 87 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 88 89 // Calls 'callback' once for each lock held in the single stack frame represented by 90 // the current state of 'stack_visitor'. 91 static void VisitLocks(StackVisitor* stack_visitor, void (*callback)(mirror::Object*, void*), 92 void* callback_context) 93 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 94 95 static bool IsValidLockWord(LockWord lock_word); 96 97 template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier> 98 mirror::Object* GetObject() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 99 return obj_.Read<kReadBarrierOption>(); 100 } 101 102 void SetObject(mirror::Object* object); 103 104 Thread* GetOwner() const NO_THREAD_SAFETY_ANALYSIS { 105 return owner_; 106 } 107 108 int32_t GetHashCode(); 109 110 bool IsLocked() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 111 112 bool HasHashCode() const { 113 return hash_code_.LoadRelaxed() != 0; 114 } 115 116 MonitorId GetMonitorId() const { 117 return monitor_id_; 118 } 119 120 static void InflateThinLocked(Thread* self, Handle<mirror::Object> obj, LockWord lock_word, 121 uint32_t hash_code) NO_THREAD_SAFETY_ANALYSIS; 122 123 static bool Deflate(Thread* self, mirror::Object* obj) 124 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 125 126 private: 127 explicit Monitor(Thread* self, Thread* owner, mirror::Object* obj, int32_t hash_code) 128 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 129 explicit Monitor(Thread* self, Thread* owner, mirror::Object* obj, int32_t hash_code, 130 MonitorId id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 131 132 // Install the monitor into its object, may fail if another thread installs a different monitor 133 // first. 134 bool Install(Thread* self) 135 LOCKS_EXCLUDED(monitor_lock_) 136 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 137 138 void AppendToWaitSet(Thread* thread) EXCLUSIVE_LOCKS_REQUIRED(monitor_lock_); 139 void RemoveFromWaitSet(Thread* thread) EXCLUSIVE_LOCKS_REQUIRED(monitor_lock_); 140 141 static void Inflate(Thread* self, Thread* owner, mirror::Object* obj, int32_t hash_code) 142 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 143 144 void LogContentionEvent(Thread* self, uint32_t wait_ms, uint32_t sample_percent, 145 const char* owner_filename, uint32_t owner_line_number) 146 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 147 148 static void FailedUnlock(mirror::Object* obj, Thread* expected_owner, Thread* found_owner, Monitor* mon) 149 LOCKS_EXCLUDED(Locks::thread_list_lock_) 150 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 151 152 void Lock(Thread* self) 153 LOCKS_EXCLUDED(monitor_lock_) 154 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 155 bool Unlock(Thread* thread) 156 LOCKS_EXCLUDED(monitor_lock_) 157 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 158 159 static void DoNotify(Thread* self, mirror::Object* obj, bool notify_all) 160 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 161 162 void Notify(Thread* self) 163 LOCKS_EXCLUDED(monitor_lock_) 164 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 165 166 void NotifyAll(Thread* self) 167 LOCKS_EXCLUDED(monitor_lock_) 168 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 169 170 171 void Wait(Thread* self, int64_t msec, int32_t nsec, bool interruptShouldThrow, ThreadState why) 172 LOCKS_EXCLUDED(monitor_lock_) 173 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 174 175 // Translates the provided method and pc into its declaring class' source file and line number. 176 void TranslateLocation(mirror::ArtMethod* method, uint32_t pc, 177 const char** source_file, uint32_t* line_number) const 178 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 179 180 uint32_t GetOwnerThreadId(); 181 182 static bool (*is_sensitive_thread_hook_)(); 183 static uint32_t lock_profiling_threshold_; 184 185 Mutex monitor_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; 186 187 ConditionVariable monitor_contenders_ GUARDED_BY(monitor_lock_); 188 189 // Number of people waiting on the condition. 190 size_t num_waiters_ GUARDED_BY(monitor_lock_); 191 192 // Which thread currently owns the lock? 193 Thread* volatile owner_ GUARDED_BY(monitor_lock_); 194 195 // Owner's recursive lock depth. 196 int lock_count_ GUARDED_BY(monitor_lock_); 197 198 // What object are we part of. This is a weak root. Do not access 199 // this directly, use GetObject() to read it so it will be guarded 200 // by a read barrier. 201 GcRoot<mirror::Object> obj_; 202 203 // Threads currently waiting on this monitor. 204 Thread* wait_set_ GUARDED_BY(monitor_lock_); 205 206 // Stored object hash code, generated lazily by GetHashCode. 207 AtomicInteger hash_code_; 208 209 // Method and dex pc where the lock owner acquired the lock, used when lock 210 // sampling is enabled. locking_method_ may be null if the lock is currently 211 // unlocked, or if the lock is acquired by the system when the stack is empty. 212 mirror::ArtMethod* locking_method_ GUARDED_BY(monitor_lock_); 213 uint32_t locking_dex_pc_ GUARDED_BY(monitor_lock_); 214 215 // The denser encoded version of this monitor as stored in the lock word. 216 MonitorId monitor_id_; 217 218#ifdef __LP64__ 219 // Free list for monitor pool. 220 Monitor* next_free_ GUARDED_BY(Locks::allocated_monitor_ids_lock_); 221#endif 222 223 friend class MonitorInfo; 224 friend class MonitorList; 225 friend class MonitorPool; 226 friend class mirror::Object; 227 DISALLOW_COPY_AND_ASSIGN(Monitor); 228}; 229 230class MonitorList { 231 public: 232 MonitorList(); 233 ~MonitorList(); 234 235 void Add(Monitor* m); 236 237 void SweepMonitorList(IsMarkedCallback* callback, void* arg) 238 LOCKS_EXCLUDED(monitor_list_lock_) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 239 void DisallowNewMonitors() LOCKS_EXCLUDED(monitor_list_lock_); 240 void AllowNewMonitors() LOCKS_EXCLUDED(monitor_list_lock_); 241 // Returns how many monitors were deflated. 242 size_t DeflateMonitors() LOCKS_EXCLUDED(monitor_list_lock_) 243 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_); 244 245 private: 246 // During sweeping we may free an object and on a separate thread have an object created using 247 // the newly freed memory. That object may then have its lock-word inflated and a monitor created. 248 // If we allow new monitor registration during sweeping this monitor may be incorrectly freed as 249 // the object wasn't marked when sweeping began. 250 bool allow_new_monitors_ GUARDED_BY(monitor_list_lock_); 251 Mutex monitor_list_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; 252 ConditionVariable monitor_add_condition_ GUARDED_BY(monitor_list_lock_); 253 std::list<Monitor*> list_ GUARDED_BY(monitor_list_lock_); 254 255 friend class Monitor; 256 DISALLOW_COPY_AND_ASSIGN(MonitorList); 257}; 258 259// Collects information about the current state of an object's monitor. 260// This is very unsafe, and must only be called when all threads are suspended. 261// For use only by the JDWP implementation. 262class MonitorInfo { 263 public: 264 explicit MonitorInfo(mirror::Object* o) EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_); 265 266 Thread* owner_; 267 size_t entry_count_; 268 std::vector<Thread*> waiters_; 269 270 private: 271 DISALLOW_COPY_AND_ASSIGN(MonitorInfo); 272}; 273 274} // namespace art 275 276#endif // ART_RUNTIME_MONITOR_H_ 277