OSDN Git Service

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