OSDN Git Service

Merge "Missed use of android_atomic and thread state_."
[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
129   // Install the monitor into its object, may fail if another thread installs a different monitor
130   // first.
131   bool Install(Thread* self)
132       LOCKS_EXCLUDED(monitor_lock_)
133       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
134
135   void AppendToWaitSet(Thread* thread) EXCLUSIVE_LOCKS_REQUIRED(monitor_lock_);
136   void RemoveFromWaitSet(Thread* thread) EXCLUSIVE_LOCKS_REQUIRED(monitor_lock_);
137
138   static void Inflate(Thread* self, Thread* owner, mirror::Object* obj, int32_t hash_code)
139       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
140
141   void LogContentionEvent(Thread* self, uint32_t wait_ms, uint32_t sample_percent,
142                           const char* owner_filename, uint32_t owner_line_number)
143       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
144
145   static void FailedUnlock(mirror::Object* obj, Thread* expected_owner, Thread* found_owner, Monitor* mon)
146       LOCKS_EXCLUDED(Locks::thread_list_lock_)
147       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
148
149   void Lock(Thread* self)
150       LOCKS_EXCLUDED(monitor_lock_)
151       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
152   bool Unlock(Thread* thread)
153       LOCKS_EXCLUDED(monitor_lock_)
154       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
155
156   static void DoNotify(Thread* self, mirror::Object* obj, bool notify_all)
157       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
158
159   void Notify(Thread* self)
160       LOCKS_EXCLUDED(monitor_lock_)
161       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
162
163   void NotifyAll(Thread* self)
164       LOCKS_EXCLUDED(monitor_lock_)
165       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
166
167
168   void Wait(Thread* self, int64_t msec, int32_t nsec, bool interruptShouldThrow, ThreadState why)
169       LOCKS_EXCLUDED(monitor_lock_)
170       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
171
172   // Translates the provided method and pc into its declaring class' source file and line number.
173   void TranslateLocation(mirror::ArtMethod* method, uint32_t pc,
174                          const char** source_file, uint32_t* line_number) const
175       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
176
177   uint32_t GetOwnerThreadId();
178
179   static bool (*is_sensitive_thread_hook_)();
180   static uint32_t lock_profiling_threshold_;
181
182   Mutex monitor_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
183
184   ConditionVariable monitor_contenders_ GUARDED_BY(monitor_lock_);
185
186   // Number of people waiting on the condition.
187   size_t num_waiters_ GUARDED_BY(monitor_lock_);
188
189   // Which thread currently owns the lock?
190   Thread* volatile owner_ GUARDED_BY(monitor_lock_);
191
192   // Owner's recursive lock depth.
193   int lock_count_ GUARDED_BY(monitor_lock_);
194
195   // What object are we part of. This is a weak root. Do not access
196   // this directly, use GetObject() to read it so it will be guarded
197   // by a read barrier.
198   mirror::Object* obj_;
199
200   // Threads currently waiting on this monitor.
201   Thread* wait_set_ GUARDED_BY(monitor_lock_);
202
203   // Stored object hash code, generated lazily by GetHashCode.
204   AtomicInteger hash_code_;
205
206   // Method and dex pc where the lock owner acquired the lock, used when lock
207   // sampling is enabled. locking_method_ may be null if the lock is currently
208   // unlocked, or if the lock is acquired by the system when the stack is empty.
209   mirror::ArtMethod* locking_method_ GUARDED_BY(monitor_lock_);
210   uint32_t locking_dex_pc_ GUARDED_BY(monitor_lock_);
211
212   // The denser encoded version of this monitor as stored in the lock word.
213   MonitorId monitor_id_;
214
215   friend class MonitorInfo;
216   friend class MonitorList;
217   friend class mirror::Object;
218   DISALLOW_COPY_AND_ASSIGN(Monitor);
219 };
220
221 class MonitorList {
222  public:
223   MonitorList();
224   ~MonitorList();
225
226   void Add(Monitor* m);
227
228   void SweepMonitorList(IsMarkedCallback* callback, void* arg)
229       LOCKS_EXCLUDED(monitor_list_lock_) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
230   void DisallowNewMonitors() LOCKS_EXCLUDED(monitor_list_lock_);
231   void AllowNewMonitors() LOCKS_EXCLUDED(monitor_list_lock_);
232   // Returns how many monitors were deflated.
233   size_t DeflateMonitors() LOCKS_EXCLUDED(monitor_list_lock_)
234       EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
235
236  private:
237   // During sweeping we may free an object and on a separate thread have an object created using
238   // the newly freed memory. That object may then have its lock-word inflated and a monitor created.
239   // If we allow new monitor registration during sweeping this monitor may be incorrectly freed as
240   // the object wasn't marked when sweeping began.
241   bool allow_new_monitors_ GUARDED_BY(monitor_list_lock_);
242   Mutex monitor_list_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
243   ConditionVariable monitor_add_condition_ GUARDED_BY(monitor_list_lock_);
244   std::list<Monitor*> list_ GUARDED_BY(monitor_list_lock_);
245
246   friend class Monitor;
247   DISALLOW_COPY_AND_ASSIGN(MonitorList);
248 };
249
250 // Collects information about the current state of an object's monitor.
251 // This is very unsafe, and must only be called when all threads are suspended.
252 // For use only by the JDWP implementation.
253 class MonitorInfo {
254  public:
255   explicit MonitorInfo(mirror::Object* o) EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
256
257   Thread* owner_;
258   size_t entry_count_;
259   std::vector<Thread*> waiters_;
260
261  private:
262   DISALLOW_COPY_AND_ASSIGN(MonitorInfo);
263 };
264
265 }  // namespace art
266
267 #endif  // ART_RUNTIME_MONITOR_H_