OSDN Git Service

Merge "Missed use of android_atomic and thread state_."
[android-x86/art.git] / runtime / monitor.cc
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 #include "monitor.h"
18
19 #include <vector>
20
21 #include "base/mutex.h"
22 #include "base/stl_util.h"
23 #include "class_linker.h"
24 #include "dex_file-inl.h"
25 #include "dex_instruction.h"
26 #include "lock_word-inl.h"
27 #include "mirror/art_method-inl.h"
28 #include "mirror/class-inl.h"
29 #include "mirror/object-inl.h"
30 #include "mirror/object_array-inl.h"
31 #include "object_utils.h"
32 #include "scoped_thread_state_change.h"
33 #include "thread.h"
34 #include "thread_list.h"
35 #include "verifier/method_verifier.h"
36 #include "well_known_classes.h"
37
38 namespace art {
39
40 /*
41  * Every Object has a monitor associated with it, but not every Object is actually locked.  Even
42  * the ones that are locked do not need a full-fledged monitor until a) there is actual contention
43  * or b) wait() is called on the Object.
44  *
45  * For Android, we have implemented a scheme similar to the one described in Bacon et al.'s
46  * "Thin locks: featherweight synchronization for Java" (ACM 1998).  Things are even easier for us,
47  * though, because we have a full 32 bits to work with.
48  *
49  * The two states of an Object's lock are referred to as "thin" and "fat".  A lock may transition
50  * from the "thin" state to the "fat" state and this transition is referred to as inflation. Once
51  * a lock has been inflated it remains in the "fat" state indefinitely.
52  *
53  * The lock value itself is stored in mirror::Object::monitor_ and the representation is described
54  * in the LockWord value type.
55  *
56  * Monitors provide:
57  *  - mutually exclusive access to resources
58  *  - a way for multiple threads to wait for notification
59  *
60  * In effect, they fill the role of both mutexes and condition variables.
61  *
62  * Only one thread can own the monitor at any time.  There may be several threads waiting on it
63  * (the wait call unlocks it).  One or more waiting threads may be getting interrupted or notified
64  * at any given time.
65  */
66
67 bool (*Monitor::is_sensitive_thread_hook_)() = NULL;
68 uint32_t Monitor::lock_profiling_threshold_ = 0;
69
70 bool Monitor::IsSensitiveThread() {
71   if (is_sensitive_thread_hook_ != NULL) {
72     return (*is_sensitive_thread_hook_)();
73   }
74   return false;
75 }
76
77 void Monitor::Init(uint32_t lock_profiling_threshold, bool (*is_sensitive_thread_hook)()) {
78   lock_profiling_threshold_ = lock_profiling_threshold;
79   is_sensitive_thread_hook_ = is_sensitive_thread_hook;
80 }
81
82 Monitor::Monitor(Thread* self, Thread* owner, mirror::Object* obj, int32_t hash_code)
83     : monitor_lock_("a monitor lock", kMonitorLock),
84       monitor_contenders_("monitor contenders", monitor_lock_),
85       num_waiters_(0),
86       owner_(owner),
87       lock_count_(0),
88       obj_(obj),
89       wait_set_(NULL),
90       hash_code_(hash_code),
91       locking_method_(NULL),
92       locking_dex_pc_(0),
93       monitor_id_(MonitorPool::CreateMonitorId(self, this)) {
94   // We should only inflate a lock if the owner is ourselves or suspended. This avoids a race
95   // with the owner unlocking the thin-lock.
96   CHECK(owner == nullptr || owner == self || owner->IsSuspended());
97   // The identity hash code is set for the life time of the monitor.
98 }
99
100 int32_t Monitor::GetHashCode() {
101   while (!HasHashCode()) {
102     if (hash_code_.CompareExchangeWeakRelaxed(0, mirror::Object::GenerateIdentityHashCode())) {
103       break;
104     }
105   }
106   DCHECK(HasHashCode());
107   return hash_code_.LoadRelaxed();
108 }
109
110 bool Monitor::Install(Thread* self) {
111   MutexLock mu(self, monitor_lock_);  // Uncontended mutex acquisition as monitor isn't yet public.
112   CHECK(owner_ == nullptr || owner_ == self || owner_->IsSuspended());
113   // Propagate the lock state.
114   LockWord lw(GetObject()->GetLockWord(false));
115   switch (lw.GetState()) {
116     case LockWord::kThinLocked: {
117       CHECK_EQ(owner_->GetThreadId(), lw.ThinLockOwner());
118       lock_count_ = lw.ThinLockCount();
119       break;
120     }
121     case LockWord::kHashCode: {
122       CHECK_EQ(hash_code_.LoadRelaxed(), static_cast<int32_t>(lw.GetHashCode()));
123       break;
124     }
125     case LockWord::kFatLocked: {
126       // The owner_ is suspended but another thread beat us to install a monitor.
127       return false;
128     }
129     case LockWord::kUnlocked: {
130       LOG(FATAL) << "Inflating unlocked lock word";
131       break;
132     }
133     default: {
134       LOG(FATAL) << "Invalid monitor state " << lw.GetState();
135       return false;
136     }
137   }
138   LockWord fat(this);
139   // Publish the updated lock word, which may race with other threads.
140   bool success = GetObject()->CasLockWord(lw, fat);
141   // Lock profiling.
142   if (success && owner_ != nullptr && lock_profiling_threshold_ != 0) {
143     locking_method_ = owner_->GetCurrentMethod(&locking_dex_pc_);
144   }
145   return success;
146 }
147
148 Monitor::~Monitor() {
149   MonitorPool::ReleaseMonitorId(monitor_id_);
150   // Deflated monitors have a null object.
151 }
152
153 /*
154  * Links a thread into a monitor's wait set.  The monitor lock must be
155  * held by the caller of this routine.
156  */
157 void Monitor::AppendToWaitSet(Thread* thread) {
158   DCHECK(owner_ == Thread::Current());
159   DCHECK(thread != NULL);
160   DCHECK(thread->GetWaitNext() == nullptr) << thread->GetWaitNext();
161   if (wait_set_ == NULL) {
162     wait_set_ = thread;
163     return;
164   }
165
166   // push_back.
167   Thread* t = wait_set_;
168   while (t->GetWaitNext() != nullptr) {
169     t = t->GetWaitNext();
170   }
171   t->SetWaitNext(thread);
172 }
173
174 /*
175  * Unlinks a thread from a monitor's wait set.  The monitor lock must
176  * be held by the caller of this routine.
177  */
178 void Monitor::RemoveFromWaitSet(Thread *thread) {
179   DCHECK(owner_ == Thread::Current());
180   DCHECK(thread != NULL);
181   if (wait_set_ == NULL) {
182     return;
183   }
184   if (wait_set_ == thread) {
185     wait_set_ = thread->GetWaitNext();
186     thread->SetWaitNext(nullptr);
187     return;
188   }
189
190   Thread* t = wait_set_;
191   while (t->GetWaitNext() != NULL) {
192     if (t->GetWaitNext() == thread) {
193       t->SetWaitNext(thread->GetWaitNext());
194       thread->SetWaitNext(nullptr);
195       return;
196     }
197     t = t->GetWaitNext();
198   }
199 }
200
201 void Monitor::SetObject(mirror::Object* object) {
202   obj_ = object;
203 }
204
205 void Monitor::Lock(Thread* self) {
206   MutexLock mu(self, monitor_lock_);
207   while (true) {
208     if (owner_ == nullptr) {  // Unowned.
209       owner_ = self;
210       CHECK_EQ(lock_count_, 0);
211       // When debugging, save the current monitor holder for future
212       // acquisition failures to use in sampled logging.
213       if (lock_profiling_threshold_ != 0) {
214         locking_method_ = self->GetCurrentMethod(&locking_dex_pc_);
215       }
216       return;
217     } else if (owner_ == self) {  // Recursive.
218       lock_count_++;
219       return;
220     }
221     // Contended.
222     const bool log_contention = (lock_profiling_threshold_ != 0);
223     uint64_t wait_start_ms = log_contention ? 0 : MilliTime();
224     mirror::ArtMethod* owners_method = locking_method_;
225     uint32_t owners_dex_pc = locking_dex_pc_;
226     // Do this before releasing the lock so that we don't get deflated.
227     ++num_waiters_;
228     monitor_lock_.Unlock(self);  // Let go of locks in order.
229     self->SetMonitorEnterObject(GetObject());
230     {
231       ScopedThreadStateChange tsc(self, kBlocked);  // Change to blocked and give up mutator_lock_.
232       MutexLock mu2(self, monitor_lock_);  // Reacquire monitor_lock_ without mutator_lock_ for Wait.
233       if (owner_ != NULL) {  // Did the owner_ give the lock up?
234         monitor_contenders_.Wait(self);  // Still contended so wait.
235         // Woken from contention.
236         if (log_contention) {
237           uint64_t wait_ms = MilliTime() - wait_start_ms;
238           uint32_t sample_percent;
239           if (wait_ms >= lock_profiling_threshold_) {
240             sample_percent = 100;
241           } else {
242             sample_percent = 100 * wait_ms / lock_profiling_threshold_;
243           }
244           if (sample_percent != 0 && (static_cast<uint32_t>(rand() % 100) < sample_percent)) {
245             const char* owners_filename;
246             uint32_t owners_line_number;
247             TranslateLocation(owners_method, owners_dex_pc, &owners_filename, &owners_line_number);
248             LogContentionEvent(self, wait_ms, sample_percent, owners_filename, owners_line_number);
249           }
250         }
251       }
252     }
253     self->SetMonitorEnterObject(nullptr);
254     monitor_lock_.Lock(self);  // Reacquire locks in order.
255     --num_waiters_;
256   }
257 }
258
259 static void ThrowIllegalMonitorStateExceptionF(const char* fmt, ...)
260                                               __attribute__((format(printf, 1, 2)));
261
262 static void ThrowIllegalMonitorStateExceptionF(const char* fmt, ...)
263     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
264   va_list args;
265   va_start(args, fmt);
266   Thread* self = Thread::Current();
267   ThrowLocation throw_location = self->GetCurrentLocationForThrow();
268   self->ThrowNewExceptionV(throw_location, "Ljava/lang/IllegalMonitorStateException;", fmt, args);
269   if (!Runtime::Current()->IsStarted() || VLOG_IS_ON(monitor)) {
270     std::ostringstream ss;
271     self->Dump(ss);
272     LOG(Runtime::Current()->IsStarted() ? INFO : ERROR)
273         << self->GetException(NULL)->Dump() << "\n" << ss.str();
274   }
275   va_end(args);
276 }
277
278 static std::string ThreadToString(Thread* thread) {
279   if (thread == NULL) {
280     return "NULL";
281   }
282   std::ostringstream oss;
283   // TODO: alternatively, we could just return the thread's name.
284   oss << *thread;
285   return oss.str();
286 }
287
288 void Monitor::FailedUnlock(mirror::Object* o, Thread* expected_owner, Thread* found_owner,
289                            Monitor* monitor) {
290   Thread* current_owner = NULL;
291   std::string current_owner_string;
292   std::string expected_owner_string;
293   std::string found_owner_string;
294   {
295     // TODO: isn't this too late to prevent threads from disappearing?
296     // Acquire thread list lock so threads won't disappear from under us.
297     MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
298     // Re-read owner now that we hold lock.
299     current_owner = (monitor != NULL) ? monitor->GetOwner() : NULL;
300     // Get short descriptions of the threads involved.
301     current_owner_string = ThreadToString(current_owner);
302     expected_owner_string = ThreadToString(expected_owner);
303     found_owner_string = ThreadToString(found_owner);
304   }
305   if (current_owner == NULL) {
306     if (found_owner == NULL) {
307       ThrowIllegalMonitorStateExceptionF("unlock of unowned monitor on object of type '%s'"
308                                          " on thread '%s'",
309                                          PrettyTypeOf(o).c_str(),
310                                          expected_owner_string.c_str());
311     } else {
312       // Race: the original read found an owner but now there is none
313       ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'"
314                                          " (where now the monitor appears unowned) on thread '%s'",
315                                          found_owner_string.c_str(),
316                                          PrettyTypeOf(o).c_str(),
317                                          expected_owner_string.c_str());
318     }
319   } else {
320     if (found_owner == NULL) {
321       // Race: originally there was no owner, there is now
322       ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'"
323                                          " (originally believed to be unowned) on thread '%s'",
324                                          current_owner_string.c_str(),
325                                          PrettyTypeOf(o).c_str(),
326                                          expected_owner_string.c_str());
327     } else {
328       if (found_owner != current_owner) {
329         // Race: originally found and current owner have changed
330         ThrowIllegalMonitorStateExceptionF("unlock of monitor originally owned by '%s' (now"
331                                            " owned by '%s') on object of type '%s' on thread '%s'",
332                                            found_owner_string.c_str(),
333                                            current_owner_string.c_str(),
334                                            PrettyTypeOf(o).c_str(),
335                                            expected_owner_string.c_str());
336       } else {
337         ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'"
338                                            " on thread '%s",
339                                            current_owner_string.c_str(),
340                                            PrettyTypeOf(o).c_str(),
341                                            expected_owner_string.c_str());
342       }
343     }
344   }
345 }
346
347 bool Monitor::Unlock(Thread* self) {
348   DCHECK(self != NULL);
349   MutexLock mu(self, monitor_lock_);
350   Thread* owner = owner_;
351   if (owner == self) {
352     // We own the monitor, so nobody else can be in here.
353     if (lock_count_ == 0) {
354       owner_ = NULL;
355       locking_method_ = NULL;
356       locking_dex_pc_ = 0;
357       // Wake a contender.
358       monitor_contenders_.Signal(self);
359     } else {
360       --lock_count_;
361     }
362   } else {
363     // We don't own this, so we're not allowed to unlock it.
364     // The JNI spec says that we should throw IllegalMonitorStateException
365     // in this case.
366     FailedUnlock(GetObject(), self, owner, this);
367     return false;
368   }
369   return true;
370 }
371
372 /*
373  * Wait on a monitor until timeout, interrupt, or notification.  Used for
374  * Object.wait() and (somewhat indirectly) Thread.sleep() and Thread.join().
375  *
376  * If another thread calls Thread.interrupt(), we throw InterruptedException
377  * and return immediately if one of the following are true:
378  *  - blocked in wait(), wait(long), or wait(long, int) methods of Object
379  *  - blocked in join(), join(long), or join(long, int) methods of Thread
380  *  - blocked in sleep(long), or sleep(long, int) methods of Thread
381  * Otherwise, we set the "interrupted" flag.
382  *
383  * Checks to make sure that "ns" is in the range 0-999999
384  * (i.e. fractions of a millisecond) and throws the appropriate
385  * exception if it isn't.
386  *
387  * The spec allows "spurious wakeups", and recommends that all code using
388  * Object.wait() do so in a loop.  This appears to derive from concerns
389  * about pthread_cond_wait() on multiprocessor systems.  Some commentary
390  * on the web casts doubt on whether these can/should occur.
391  *
392  * Since we're allowed to wake up "early", we clamp extremely long durations
393  * to return at the end of the 32-bit time epoch.
394  */
395 void Monitor::Wait(Thread* self, int64_t ms, int32_t ns,
396                    bool interruptShouldThrow, ThreadState why) {
397   DCHECK(self != NULL);
398   DCHECK(why == kTimedWaiting || why == kWaiting || why == kSleeping);
399
400   monitor_lock_.Lock(self);
401
402   // Make sure that we hold the lock.
403   if (owner_ != self) {
404     monitor_lock_.Unlock(self);
405     ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()");
406     return;
407   }
408
409   // We need to turn a zero-length timed wait into a regular wait because
410   // Object.wait(0, 0) is defined as Object.wait(0), which is defined as Object.wait().
411   if (why == kTimedWaiting && (ms == 0 && ns == 0)) {
412     why = kWaiting;
413   }
414
415   // Enforce the timeout range.
416   if (ms < 0 || ns < 0 || ns > 999999) {
417     monitor_lock_.Unlock(self);
418     ThrowLocation throw_location = self->GetCurrentLocationForThrow();
419     self->ThrowNewExceptionF(throw_location, "Ljava/lang/IllegalArgumentException;",
420                              "timeout arguments out of range: ms=%" PRId64 " ns=%d", ms, ns);
421     return;
422   }
423
424   /*
425    * Add ourselves to the set of threads waiting on this monitor, and
426    * release our hold.  We need to let it go even if we're a few levels
427    * deep in a recursive lock, and we need to restore that later.
428    *
429    * We append to the wait set ahead of clearing the count and owner
430    * fields so the subroutine can check that the calling thread owns
431    * the monitor.  Aside from that, the order of member updates is
432    * not order sensitive as we hold the pthread mutex.
433    */
434   AppendToWaitSet(self);
435   ++num_waiters_;
436   int prev_lock_count = lock_count_;
437   lock_count_ = 0;
438   owner_ = NULL;
439   mirror::ArtMethod* saved_method = locking_method_;
440   locking_method_ = NULL;
441   uintptr_t saved_dex_pc = locking_dex_pc_;
442   locking_dex_pc_ = 0;
443
444   /*
445    * Update thread state. If the GC wakes up, it'll ignore us, knowing
446    * that we won't touch any references in this state, and we'll check
447    * our suspend mode before we transition out.
448    */
449   self->TransitionFromRunnableToSuspended(why);
450
451   bool was_interrupted = false;
452   {
453     // Pseudo-atomically wait on self's wait_cond_ and release the monitor lock.
454     MutexLock mu(self, *self->GetWaitMutex());
455
456     // Set wait_monitor_ to the monitor object we will be waiting on. When wait_monitor_ is
457     // non-NULL a notifying or interrupting thread must signal the thread's wait_cond_ to wake it
458     // up.
459     DCHECK(self->GetWaitMonitor() == nullptr);
460     self->SetWaitMonitor(this);
461
462     // Release the monitor lock.
463     monitor_contenders_.Signal(self);
464     monitor_lock_.Unlock(self);
465
466     // Handle the case where the thread was interrupted before we called wait().
467     if (self->IsInterruptedLocked()) {
468       was_interrupted = true;
469     } else {
470       // Wait for a notification or a timeout to occur.
471       if (why == kWaiting) {
472         self->GetWaitConditionVariable()->Wait(self);
473       } else {
474         DCHECK(why == kTimedWaiting || why == kSleeping) << why;
475         self->GetWaitConditionVariable()->TimedWait(self, ms, ns);
476       }
477       if (self->IsInterruptedLocked()) {
478         was_interrupted = true;
479       }
480       self->SetInterruptedLocked(false);
481     }
482   }
483
484   // Set self->status back to kRunnable, and self-suspend if needed.
485   self->TransitionFromSuspendedToRunnable();
486
487   {
488     // We reset the thread's wait_monitor_ field after transitioning back to runnable so
489     // that a thread in a waiting/sleeping state has a non-null wait_monitor_ for debugging
490     // and diagnostic purposes. (If you reset this earlier, stack dumps will claim that threads
491     // are waiting on "null".)
492     MutexLock mu(self, *self->GetWaitMutex());
493     DCHECK(self->GetWaitMonitor() != nullptr);
494     self->SetWaitMonitor(nullptr);
495   }
496
497   // Re-acquire the monitor and lock.
498   Lock(self);
499   monitor_lock_.Lock(self);
500   self->GetWaitMutex()->AssertNotHeld(self);
501
502   /*
503    * We remove our thread from wait set after restoring the count
504    * and owner fields so the subroutine can check that the calling
505    * thread owns the monitor. Aside from that, the order of member
506    * updates is not order sensitive as we hold the pthread mutex.
507    */
508   owner_ = self;
509   lock_count_ = prev_lock_count;
510   locking_method_ = saved_method;
511   locking_dex_pc_ = saved_dex_pc;
512   --num_waiters_;
513   RemoveFromWaitSet(self);
514
515   monitor_lock_.Unlock(self);
516
517   if (was_interrupted) {
518     /*
519      * We were interrupted while waiting, or somebody interrupted an
520      * un-interruptible thread earlier and we're bailing out immediately.
521      *
522      * The doc sayeth: "The interrupted status of the current thread is
523      * cleared when this exception is thrown."
524      */
525     {
526       MutexLock mu(self, *self->GetWaitMutex());
527       self->SetInterruptedLocked(false);
528     }
529     if (interruptShouldThrow) {
530       ThrowLocation throw_location = self->GetCurrentLocationForThrow();
531       self->ThrowNewException(throw_location, "Ljava/lang/InterruptedException;", NULL);
532     }
533   }
534 }
535
536 void Monitor::Notify(Thread* self) {
537   DCHECK(self != NULL);
538   MutexLock mu(self, monitor_lock_);
539   // Make sure that we hold the lock.
540   if (owner_ != self) {
541     ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()");
542     return;
543   }
544   // Signal the first waiting thread in the wait set.
545   while (wait_set_ != NULL) {
546     Thread* thread = wait_set_;
547     wait_set_ = thread->GetWaitNext();
548     thread->SetWaitNext(nullptr);
549
550     // Check to see if the thread is still waiting.
551     MutexLock mu(self, *thread->GetWaitMutex());
552     if (thread->GetWaitMonitor() != nullptr) {
553       thread->GetWaitConditionVariable()->Signal(self);
554       return;
555     }
556   }
557 }
558
559 void Monitor::NotifyAll(Thread* self) {
560   DCHECK(self != NULL);
561   MutexLock mu(self, monitor_lock_);
562   // Make sure that we hold the lock.
563   if (owner_ != self) {
564     ThrowIllegalMonitorStateExceptionF("object not locked by thread before notifyAll()");
565     return;
566   }
567   // Signal all threads in the wait set.
568   while (wait_set_ != NULL) {
569     Thread* thread = wait_set_;
570     wait_set_ = thread->GetWaitNext();
571     thread->SetWaitNext(nullptr);
572     thread->Notify();
573   }
574 }
575
576 bool Monitor::Deflate(Thread* self, mirror::Object* obj) {
577   DCHECK(obj != nullptr);
578   // Don't need volatile since we only deflate with mutators suspended.
579   LockWord lw(obj->GetLockWord(false));
580   // If the lock isn't an inflated monitor, then we don't need to deflate anything.
581   if (lw.GetState() == LockWord::kFatLocked) {
582     Monitor* monitor = lw.FatLockMonitor();
583     DCHECK(monitor != nullptr);
584     MutexLock mu(self, monitor->monitor_lock_);
585     // Can't deflate if we have anybody waiting on the CV.
586     if (monitor->num_waiters_ > 0) {
587       return false;
588     }
589     Thread* owner = monitor->owner_;
590     if (owner != nullptr) {
591       // Can't deflate if we are locked and have a hash code.
592       if (monitor->HasHashCode()) {
593         return false;
594       }
595       // Can't deflate if our lock count is too high.
596       if (monitor->lock_count_ > LockWord::kThinLockMaxCount) {
597         return false;
598       }
599       // Deflate to a thin lock.
600       obj->SetLockWord(LockWord::FromThinLockId(owner->GetThreadId(), monitor->lock_count_), false);
601       VLOG(monitor) << "Deflated " << obj << " to thin lock " << owner->GetTid() << " / "
602           << monitor->lock_count_;
603     } else if (monitor->HasHashCode()) {
604       obj->SetLockWord(LockWord::FromHashCode(monitor->GetHashCode()), false);
605       VLOG(monitor) << "Deflated " << obj << " to hash monitor " << monitor->GetHashCode();
606     } else {
607       // No lock and no hash, just put an empty lock word inside the object.
608       obj->SetLockWord(LockWord(), false);
609       VLOG(monitor) << "Deflated" << obj << " to empty lock word";
610     }
611     // The monitor is deflated, mark the object as nullptr so that we know to delete it during the
612     // next GC.
613     monitor->obj_ = nullptr;
614   }
615   return true;
616 }
617
618 /*
619  * Changes the shape of a monitor from thin to fat, preserving the internal lock state. The calling
620  * thread must own the lock or the owner must be suspended. There's a race with other threads
621  * inflating the lock and so the caller should read the monitor following the call.
622  */
623 void Monitor::Inflate(Thread* self, Thread* owner, mirror::Object* obj, int32_t hash_code) {
624   DCHECK(self != NULL);
625   DCHECK(obj != NULL);
626   // Allocate and acquire a new monitor.
627   std::unique_ptr<Monitor> m(new Monitor(self, owner, obj, hash_code));
628   if (m->Install(self)) {
629     if (owner != nullptr) {
630       VLOG(monitor) << "monitor: thread" << owner->GetThreadId()
631           << " created monitor " << m.get() << " for object " << obj;
632     } else {
633       VLOG(monitor) << "monitor: Inflate with hashcode " << hash_code
634           << " created monitor " << m.get() << " for object " << obj;
635     }
636     Runtime::Current()->GetMonitorList()->Add(m.release());
637     CHECK_EQ(obj->GetLockWord(true).GetState(), LockWord::kFatLocked);
638   }
639 }
640
641 void Monitor::InflateThinLocked(Thread* self, Handle<mirror::Object> obj, LockWord lock_word,
642                                 uint32_t hash_code) {
643   DCHECK_EQ(lock_word.GetState(), LockWord::kThinLocked);
644   uint32_t owner_thread_id = lock_word.ThinLockOwner();
645   if (owner_thread_id == self->GetThreadId()) {
646     // We own the monitor, we can easily inflate it.
647     Inflate(self, self, obj.Get(), hash_code);
648   } else {
649     ThreadList* thread_list = Runtime::Current()->GetThreadList();
650     // Suspend the owner, inflate. First change to blocked and give up mutator_lock_.
651     self->SetMonitorEnterObject(obj.Get());
652     bool timed_out;
653     Thread* owner;
654     {
655       ScopedThreadStateChange tsc(self, kBlocked);
656       owner = thread_list->SuspendThreadByThreadId(owner_thread_id, false, &timed_out);
657     }
658     if (owner != nullptr) {
659       // We succeeded in suspending the thread, check the lock's status didn't change.
660       lock_word = obj->GetLockWord(true);
661       if (lock_word.GetState() == LockWord::kThinLocked &&
662           lock_word.ThinLockOwner() == owner_thread_id) {
663         // Go ahead and inflate the lock.
664         Inflate(self, owner, obj.Get(), hash_code);
665       }
666       thread_list->Resume(owner, false);
667     }
668     self->SetMonitorEnterObject(nullptr);
669   }
670 }
671
672 // Fool annotalysis into thinking that the lock on obj is acquired.
673 static mirror::Object* FakeLock(mirror::Object* obj)
674     EXCLUSIVE_LOCK_FUNCTION(obj) NO_THREAD_SAFETY_ANALYSIS {
675   return obj;
676 }
677
678 // Fool annotalysis into thinking that the lock on obj is release.
679 static mirror::Object* FakeUnlock(mirror::Object* obj)
680     UNLOCK_FUNCTION(obj) NO_THREAD_SAFETY_ANALYSIS {
681   return obj;
682 }
683
684 mirror::Object* Monitor::MonitorEnter(Thread* self, mirror::Object* obj) {
685   DCHECK(self != NULL);
686   DCHECK(obj != NULL);
687   obj = FakeLock(obj);
688   uint32_t thread_id = self->GetThreadId();
689   size_t contention_count = 0;
690   StackHandleScope<1> hs(self);
691   Handle<mirror::Object> h_obj(hs.NewHandle(obj));
692   while (true) {
693     LockWord lock_word = h_obj->GetLockWord(true);
694     switch (lock_word.GetState()) {
695       case LockWord::kUnlocked: {
696         LockWord thin_locked(LockWord::FromThinLockId(thread_id, 0));
697         if (h_obj->CasLockWord(lock_word, thin_locked)) {
698           // CasLockWord enforces more than the acquire ordering we need here.
699           return h_obj.Get();  // Success!
700         }
701         continue;  // Go again.
702       }
703       case LockWord::kThinLocked: {
704         uint32_t owner_thread_id = lock_word.ThinLockOwner();
705         if (owner_thread_id == thread_id) {
706           // We own the lock, increase the recursion count.
707           uint32_t new_count = lock_word.ThinLockCount() + 1;
708           if (LIKELY(new_count <= LockWord::kThinLockMaxCount)) {
709             LockWord thin_locked(LockWord::FromThinLockId(thread_id, new_count));
710             h_obj->SetLockWord(thin_locked, true);
711             return h_obj.Get();  // Success!
712           } else {
713             // We'd overflow the recursion count, so inflate the monitor.
714             InflateThinLocked(self, h_obj, lock_word, 0);
715           }
716         } else {
717           // Contention.
718           contention_count++;
719           Runtime* runtime = Runtime::Current();
720           if (contention_count <= runtime->GetMaxSpinsBeforeThinkLockInflation()) {
721             NanoSleep(1000);  // Sleep for 1us and re-attempt.
722           } else {
723             contention_count = 0;
724             InflateThinLocked(self, h_obj, lock_word, 0);
725           }
726         }
727         continue;  // Start from the beginning.
728       }
729       case LockWord::kFatLocked: {
730         Monitor* mon = lock_word.FatLockMonitor();
731         mon->Lock(self);
732         return h_obj.Get();  // Success!
733       }
734       case LockWord::kHashCode:
735         // Inflate with the existing hashcode.
736         Inflate(self, nullptr, h_obj.Get(), lock_word.GetHashCode());
737         continue;  // Start from the beginning.
738       default: {
739         LOG(FATAL) << "Invalid monitor state " << lock_word.GetState();
740         return h_obj.Get();
741       }
742     }
743   }
744 }
745
746 bool Monitor::MonitorExit(Thread* self, mirror::Object* obj) {
747   DCHECK(self != NULL);
748   DCHECK(obj != NULL);
749   obj = FakeUnlock(obj);
750   LockWord lock_word = obj->GetLockWord(true);
751   StackHandleScope<1> hs(self);
752   Handle<mirror::Object> h_obj(hs.NewHandle(obj));
753   switch (lock_word.GetState()) {
754     case LockWord::kHashCode:
755       // Fall-through.
756     case LockWord::kUnlocked:
757       FailedUnlock(h_obj.Get(), self, nullptr, nullptr);
758       return false;  // Failure.
759     case LockWord::kThinLocked: {
760       uint32_t thread_id = self->GetThreadId();
761       uint32_t owner_thread_id = lock_word.ThinLockOwner();
762       if (owner_thread_id != thread_id) {
763         // TODO: there's a race here with the owner dying while we unlock.
764         Thread* owner =
765             Runtime::Current()->GetThreadList()->FindThreadByThreadId(lock_word.ThinLockOwner());
766         FailedUnlock(h_obj.Get(), self, owner, nullptr);
767         return false;  // Failure.
768       } else {
769         // We own the lock, decrease the recursion count.
770         if (lock_word.ThinLockCount() != 0) {
771           uint32_t new_count = lock_word.ThinLockCount() - 1;
772           LockWord thin_locked(LockWord::FromThinLockId(thread_id, new_count));
773           h_obj->SetLockWord(thin_locked, true);
774         } else {
775           h_obj->SetLockWord(LockWord(), true);
776         }
777         return true;  // Success!
778       }
779     }
780     case LockWord::kFatLocked: {
781       Monitor* mon = lock_word.FatLockMonitor();
782       return mon->Unlock(self);
783     }
784     default: {
785       LOG(FATAL) << "Invalid monitor state " << lock_word.GetState();
786       return false;
787     }
788   }
789 }
790
791 /*
792  * Object.wait().  Also called for class init.
793  */
794 void Monitor::Wait(Thread* self, mirror::Object *obj, int64_t ms, int32_t ns,
795                    bool interruptShouldThrow, ThreadState why) {
796   DCHECK(self != nullptr);
797   DCHECK(obj != nullptr);
798   LockWord lock_word = obj->GetLockWord(true);
799   switch (lock_word.GetState()) {
800     case LockWord::kHashCode:
801       // Fall-through.
802     case LockWord::kUnlocked:
803       ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()");
804       return;  // Failure.
805     case LockWord::kThinLocked: {
806       uint32_t thread_id = self->GetThreadId();
807       uint32_t owner_thread_id = lock_word.ThinLockOwner();
808       if (owner_thread_id != thread_id) {
809         ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()");
810         return;  // Failure.
811       } else {
812         // We own the lock, inflate to enqueue ourself on the Monitor.
813         Inflate(self, self, obj, 0);
814         lock_word = obj->GetLockWord(true);
815       }
816       break;
817     }
818     case LockWord::kFatLocked:
819       break;  // Already set for a wait.
820     default: {
821       LOG(FATAL) << "Invalid monitor state " << lock_word.GetState();
822       return;
823     }
824   }
825   Monitor* mon = lock_word.FatLockMonitor();
826   mon->Wait(self, ms, ns, interruptShouldThrow, why);
827 }
828
829 void Monitor::DoNotify(Thread* self, mirror::Object* obj, bool notify_all) {
830   DCHECK(self != nullptr);
831   DCHECK(obj != nullptr);
832   LockWord lock_word = obj->GetLockWord(true);
833   switch (lock_word.GetState()) {
834     case LockWord::kHashCode:
835       // Fall-through.
836     case LockWord::kUnlocked:
837       ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()");
838       return;  // Failure.
839     case LockWord::kThinLocked: {
840       uint32_t thread_id = self->GetThreadId();
841       uint32_t owner_thread_id = lock_word.ThinLockOwner();
842       if (owner_thread_id != thread_id) {
843         ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()");
844         return;  // Failure.
845       } else {
846         // We own the lock but there's no Monitor and therefore no waiters.
847         return;  // Success.
848       }
849     }
850     case LockWord::kFatLocked: {
851       Monitor* mon = lock_word.FatLockMonitor();
852       if (notify_all) {
853         mon->NotifyAll(self);
854       } else {
855         mon->Notify(self);
856       }
857       return;  // Success.
858     }
859     default: {
860       LOG(FATAL) << "Invalid monitor state " << lock_word.GetState();
861       return;
862     }
863   }
864 }
865
866 uint32_t Monitor::GetLockOwnerThreadId(mirror::Object* obj) {
867   DCHECK(obj != nullptr);
868   LockWord lock_word = obj->GetLockWord(true);
869   switch (lock_word.GetState()) {
870     case LockWord::kHashCode:
871       // Fall-through.
872     case LockWord::kUnlocked:
873       return ThreadList::kInvalidThreadId;
874     case LockWord::kThinLocked:
875       return lock_word.ThinLockOwner();
876     case LockWord::kFatLocked: {
877       Monitor* mon = lock_word.FatLockMonitor();
878       return mon->GetOwnerThreadId();
879     }
880     default: {
881       LOG(FATAL) << "Unreachable";
882       return ThreadList::kInvalidThreadId;
883     }
884   }
885 }
886
887 void Monitor::DescribeWait(std::ostream& os, const Thread* thread) {
888   // Determine the wait message and object we're waiting or blocked upon.
889   mirror::Object* pretty_object = nullptr;
890   const char* wait_message = nullptr;
891   uint32_t lock_owner = ThreadList::kInvalidThreadId;
892   ThreadState state = thread->GetState();
893   if (state == kWaiting || state == kTimedWaiting || state == kSleeping) {
894     wait_message = (state == kSleeping) ? "  - sleeping on " : "  - waiting on ";
895     Thread* self = Thread::Current();
896     MutexLock mu(self, *thread->GetWaitMutex());
897     Monitor* monitor = thread->GetWaitMonitor();
898     if (monitor != nullptr) {
899       pretty_object = monitor->GetObject();
900     }
901   } else if (state == kBlocked) {
902     wait_message = "  - waiting to lock ";
903     pretty_object = thread->GetMonitorEnterObject();
904     if (pretty_object != nullptr) {
905       lock_owner = pretty_object->GetLockOwnerThreadId();
906     }
907   }
908
909   if (wait_message != nullptr) {
910     if (pretty_object == nullptr) {
911       os << wait_message << "an unknown object";
912     } else {
913       if ((pretty_object->GetLockWord(true).GetState() == LockWord::kThinLocked) &&
914           Locks::mutator_lock_->IsExclusiveHeld(Thread::Current())) {
915         // Getting the identity hashcode here would result in lock inflation and suspension of the
916         // current thread, which isn't safe if this is the only runnable thread.
917         os << wait_message << StringPrintf("<@addr=0x%" PRIxPTR "> (a %s)",
918                                            reinterpret_cast<intptr_t>(pretty_object),
919                                            PrettyTypeOf(pretty_object).c_str());
920       } else {
921         // - waiting on <0x6008c468> (a java.lang.Class<java.lang.ref.ReferenceQueue>)
922         os << wait_message << StringPrintf("<0x%08x> (a %s)", pretty_object->IdentityHashCode(),
923                                            PrettyTypeOf(pretty_object).c_str());
924       }
925     }
926     // - waiting to lock <0x613f83d8> (a java.lang.Object) held by thread 5
927     if (lock_owner != ThreadList::kInvalidThreadId) {
928       os << " held by thread " << lock_owner;
929     }
930     os << "\n";
931   }
932 }
933
934 mirror::Object* Monitor::GetContendedMonitor(Thread* thread) {
935   // This is used to implement JDWP's ThreadReference.CurrentContendedMonitor, and has a bizarre
936   // definition of contended that includes a monitor a thread is trying to enter...
937   mirror::Object* result = thread->GetMonitorEnterObject();
938   if (result == NULL) {
939     // ...but also a monitor that the thread is waiting on.
940     MutexLock mu(Thread::Current(), *thread->GetWaitMutex());
941     Monitor* monitor = thread->GetWaitMonitor();
942     if (monitor != NULL) {
943       result = monitor->GetObject();
944     }
945   }
946   return result;
947 }
948
949 void Monitor::VisitLocks(StackVisitor* stack_visitor, void (*callback)(mirror::Object*, void*),
950                          void* callback_context) {
951   mirror::ArtMethod* m = stack_visitor->GetMethod();
952   CHECK(m != NULL);
953
954   // Native methods are an easy special case.
955   // TODO: use the JNI implementation's table of explicit MonitorEnter calls and dump those too.
956   if (m->IsNative()) {
957     if (m->IsSynchronized()) {
958       mirror::Object* jni_this = stack_visitor->GetCurrentHandleScope()->GetReference(0);
959       callback(jni_this, callback_context);
960     }
961     return;
962   }
963
964   // Proxy methods should not be synchronized.
965   if (m->IsProxyMethod()) {
966     CHECK(!m->IsSynchronized());
967     return;
968   }
969
970   // <clinit> is another special case. The runtime holds the class lock while calling <clinit>.
971   if (m->IsClassInitializer()) {
972     callback(m->GetDeclaringClass(), callback_context);
973     // Fall through because there might be synchronization in the user code too.
974   }
975
976   // Is there any reason to believe there's any synchronization in this method?
977   const DexFile::CodeItem* code_item = m->GetCodeItem();
978   CHECK(code_item != NULL) << PrettyMethod(m);
979   if (code_item->tries_size_ == 0) {
980     return;  // No "tries" implies no synchronization, so no held locks to report.
981   }
982
983   // Ask the verifier for the dex pcs of all the monitor-enter instructions corresponding to
984   // the locks held in this stack frame.
985   std::vector<uint32_t> monitor_enter_dex_pcs;
986   verifier::MethodVerifier::FindLocksAtDexPc(m, stack_visitor->GetDexPc(), &monitor_enter_dex_pcs);
987   if (monitor_enter_dex_pcs.empty()) {
988     return;
989   }
990
991   for (size_t i = 0; i < monitor_enter_dex_pcs.size(); ++i) {
992     // The verifier works in terms of the dex pcs of the monitor-enter instructions.
993     // We want the registers used by those instructions (so we can read the values out of them).
994     uint32_t dex_pc = monitor_enter_dex_pcs[i];
995     uint16_t monitor_enter_instruction = code_item->insns_[dex_pc];
996
997     // Quick sanity check.
998     if ((monitor_enter_instruction & 0xff) != Instruction::MONITOR_ENTER) {
999       LOG(FATAL) << "expected monitor-enter @" << dex_pc << "; was "
1000                  << reinterpret_cast<void*>(monitor_enter_instruction);
1001     }
1002
1003     uint16_t monitor_register = ((monitor_enter_instruction >> 8) & 0xff);
1004     mirror::Object* o = reinterpret_cast<mirror::Object*>(stack_visitor->GetVReg(m, monitor_register,
1005                                                                                  kReferenceVReg));
1006     callback(o, callback_context);
1007   }
1008 }
1009
1010 bool Monitor::IsValidLockWord(LockWord lock_word) {
1011   switch (lock_word.GetState()) {
1012     case LockWord::kUnlocked:
1013       // Nothing to check.
1014       return true;
1015     case LockWord::kThinLocked:
1016       // Basic sanity check of owner.
1017       return lock_word.ThinLockOwner() != ThreadList::kInvalidThreadId;
1018     case LockWord::kFatLocked: {
1019       // Check the  monitor appears in the monitor list.
1020       Monitor* mon = lock_word.FatLockMonitor();
1021       MonitorList* list = Runtime::Current()->GetMonitorList();
1022       MutexLock mu(Thread::Current(), list->monitor_list_lock_);
1023       for (Monitor* list_mon : list->list_) {
1024         if (mon == list_mon) {
1025           return true;  // Found our monitor.
1026         }
1027       }
1028       return false;  // Fail - unowned monitor in an object.
1029     }
1030     case LockWord::kHashCode:
1031       return true;
1032     default:
1033       LOG(FATAL) << "Unreachable";
1034       return false;
1035   }
1036 }
1037
1038 bool Monitor::IsLocked() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1039   MutexLock mu(Thread::Current(), monitor_lock_);
1040   return owner_ != nullptr;
1041 }
1042
1043 void Monitor::TranslateLocation(mirror::ArtMethod* method, uint32_t dex_pc,
1044                                 const char** source_file, uint32_t* line_number) const {
1045   // If method is null, location is unknown
1046   if (method == NULL) {
1047     *source_file = "";
1048     *line_number = 0;
1049     return;
1050   }
1051   *source_file = method->GetDeclaringClassSourceFile();
1052   if (*source_file == NULL) {
1053     *source_file = "";
1054   }
1055   *line_number = method->GetLineNumFromDexPC(dex_pc);
1056 }
1057
1058 uint32_t Monitor::GetOwnerThreadId() {
1059   MutexLock mu(Thread::Current(), monitor_lock_);
1060   Thread* owner = owner_;
1061   if (owner != NULL) {
1062     return owner->GetThreadId();
1063   } else {
1064     return ThreadList::kInvalidThreadId;
1065   }
1066 }
1067
1068 MonitorList::MonitorList()
1069     : allow_new_monitors_(true), monitor_list_lock_("MonitorList lock", kMonitorListLock),
1070       monitor_add_condition_("MonitorList disallow condition", monitor_list_lock_) {
1071 }
1072
1073 MonitorList::~MonitorList() {
1074   MutexLock mu(Thread::Current(), monitor_list_lock_);
1075   STLDeleteElements(&list_);
1076 }
1077
1078 void MonitorList::DisallowNewMonitors() {
1079   MutexLock mu(Thread::Current(), monitor_list_lock_);
1080   allow_new_monitors_ = false;
1081 }
1082
1083 void MonitorList::AllowNewMonitors() {
1084   Thread* self = Thread::Current();
1085   MutexLock mu(self, monitor_list_lock_);
1086   allow_new_monitors_ = true;
1087   monitor_add_condition_.Broadcast(self);
1088 }
1089
1090 void MonitorList::Add(Monitor* m) {
1091   Thread* self = Thread::Current();
1092   MutexLock mu(self, monitor_list_lock_);
1093   while (UNLIKELY(!allow_new_monitors_)) {
1094     monitor_add_condition_.WaitHoldingLocks(self);
1095   }
1096   list_.push_front(m);
1097 }
1098
1099 void MonitorList::SweepMonitorList(IsMarkedCallback* callback, void* arg) {
1100   MutexLock mu(Thread::Current(), monitor_list_lock_);
1101   for (auto it = list_.begin(); it != list_.end(); ) {
1102     Monitor* m = *it;
1103     // Disable the read barrier in GetObject() as this is called by GC.
1104     mirror::Object* obj = m->GetObject<kWithoutReadBarrier>();
1105     // The object of a monitor can be null if we have deflated it.
1106     mirror::Object* new_obj = obj != nullptr ? callback(obj, arg) : nullptr;
1107     if (new_obj == nullptr) {
1108       VLOG(monitor) << "freeing monitor " << m << " belonging to unmarked object "
1109                     << obj;
1110       delete m;
1111       it = list_.erase(it);
1112     } else {
1113       m->SetObject(new_obj);
1114       ++it;
1115     }
1116   }
1117 }
1118
1119 struct MonitorDeflateArgs {
1120   MonitorDeflateArgs() : self(Thread::Current()), deflate_count(0) {}
1121   Thread* const self;
1122   size_t deflate_count;
1123 };
1124
1125 static mirror::Object* MonitorDeflateCallback(mirror::Object* object, void* arg)
1126     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1127   MonitorDeflateArgs* args = reinterpret_cast<MonitorDeflateArgs*>(arg);
1128   if (Monitor::Deflate(args->self, object)) {
1129     DCHECK_NE(object->GetLockWord(true).GetState(), LockWord::kFatLocked);
1130     ++args->deflate_count;
1131     // If we deflated, return nullptr so that the monitor gets removed from the array.
1132     return nullptr;
1133   }
1134   return object;  // Monitor was not deflated.
1135 }
1136
1137 size_t MonitorList::DeflateMonitors() {
1138   MonitorDeflateArgs args;
1139   Locks::mutator_lock_->AssertExclusiveHeld(args.self);
1140   SweepMonitorList(MonitorDeflateCallback, &args);
1141   return args.deflate_count;
1142 }
1143
1144 MonitorInfo::MonitorInfo(mirror::Object* obj) : owner_(NULL), entry_count_(0) {
1145   DCHECK(obj != nullptr);
1146   LockWord lock_word = obj->GetLockWord(true);
1147   switch (lock_word.GetState()) {
1148     case LockWord::kUnlocked:
1149       // Fall-through.
1150     case LockWord::kForwardingAddress:
1151       // Fall-through.
1152     case LockWord::kHashCode:
1153       break;
1154     case LockWord::kThinLocked:
1155       owner_ = Runtime::Current()->GetThreadList()->FindThreadByThreadId(lock_word.ThinLockOwner());
1156       entry_count_ = 1 + lock_word.ThinLockCount();
1157       // Thin locks have no waiters.
1158       break;
1159     case LockWord::kFatLocked: {
1160       Monitor* mon = lock_word.FatLockMonitor();
1161       owner_ = mon->owner_;
1162       entry_count_ = 1 + mon->lock_count_;
1163       for (Thread* waiter = mon->wait_set_; waiter != NULL; waiter = waiter->GetWaitNext()) {
1164         waiters_.push_back(waiter);
1165       }
1166       break;
1167     }
1168   }
1169 }
1170
1171 }  // namespace art