2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
27 * Every Object has a monitor associated with it, but not every Object is
28 * actually locked. Even the ones that are locked do not need a
29 * full-fledged monitor until a) there is actual contention or b) wait()
30 * is called on the Object.
32 * For Dalvik, we have implemented a scheme similar to the one described
33 * in Bacon et al.'s "Thin locks: featherweight synchronization for Java"
34 * (ACM 1998). Things are even easier for us, though, because we have
35 * a full 32 bits to work with.
37 * The two states of an Object's lock are referred to as "thin" and
38 * "fat". A lock may transition from the "thin" state to the "fat"
39 * state and this transition is referred to as inflation. Once a lock
40 * has been inflated it remains in the "fat" state indefinitely.
42 * The lock value itself is stored in Object.lock. The LSB of the
43 * lock encodes its state. When cleared, the lock is in the "thin"
44 * state and its bits are formatted as follows:
46 * [31 ---- 19] [18 ---- 3] [2 ---- 1] [0]
47 * lock count thread id hash state 0
49 * When set, the lock is in the "fat" state and its bits are formatted
52 * [31 ---- 3] [2 ---- 1] [0]
53 * pointer hash state 1
55 * For an in-depth description of the mechanics of thin-vs-fat locking,
56 * read the paper referred to above.
61 * - mutually exclusive access to resources
62 * - a way for multiple threads to wait for notification
64 * In effect, they fill the role of both mutexes and condition variables.
66 * Only one thread can own the monitor at any time. There may be several
67 * threads waiting on it (the wait call unlocks it). One or more waiting
68 * threads may be getting interrupted or notified at any given time.
70 * TODO: the various members of monitor are not SMP-safe.
73 Thread* owner; /* which thread currently owns the lock? */
74 int lockCount; /* owner's recursive lock depth */
75 Object* obj; /* what object are we part of [debug only] */
77 Thread* waitSet; /* threads currently waiting on this monitor */
84 * Who last acquired this monitor, when lock sampling is enabled.
85 * Even when enabled, ownerMethod may be NULL.
87 const Method* ownerMethod;
93 * Create and initialize a monitor.
95 Monitor* dvmCreateMonitor(Object* obj)
99 mon = (Monitor*) calloc(1, sizeof(Monitor));
101 ALOGE("Unable to allocate monitor");
105 dvmInitMutex(&mon->lock);
107 /* replace the head of the list with the new monitor */
109 mon->next = gDvm.monitorList;
110 } while (android_atomic_release_cas((int32_t)mon->next, (int32_t)mon,
111 (int32_t*)(void*)&gDvm.monitorList) != 0);
117 * Free the monitor list. Only used when shutting the VM down.
119 void dvmFreeMonitorList()
124 mon = gDvm.monitorList;
125 while (mon != NULL) {
133 * Get the object that a monitor is part of.
135 Object* dvmGetMonitorObject(Monitor* mon)
144 * Returns the thread id of the thread owning the given lock.
146 static u4 lockOwner(Object* obj)
153 * Since we're reading the lock value multiple times, latch it so
154 * that it doesn't change out from under us if we get preempted.
157 if (LW_SHAPE(lock) == LW_SHAPE_THIN) {
158 return LW_LOCK_OWNER(lock);
160 owner = LW_MONITOR(lock)->owner;
161 return owner ? owner->threadId : 0;
166 * Get the thread that holds the lock on the specified object. The
167 * object may be unlocked, thin-locked, or fat-locked.
169 * The caller must lock the thread list before calling here.
171 Thread* dvmGetObjectLockHolder(Object* obj)
173 u4 threadId = lockOwner(obj);
177 return dvmGetThreadByThreadId(threadId);
181 * Checks whether the given thread holds the given
184 bool dvmHoldsLock(Thread* thread, Object* obj)
186 if (thread == NULL || obj == NULL) {
189 return thread->threadId == lockOwner(obj);
194 * Free the monitor associated with an object and make the object's lock
195 * thin again. This is called during garbage collection.
197 static void freeMonitor(Monitor *mon)
200 assert(mon->obj != NULL);
201 assert(LW_SHAPE(mon->obj->lock) == LW_SHAPE_FAT);
203 /* This lock is associated with an object
204 * that's being swept. The only possible way
205 * anyone could be holding this lock would be
206 * if some JNI code locked but didn't unlock
207 * the object, in which case we've got some bad
208 * native code somewhere.
210 assert(pthread_mutex_trylock(&mon->lock) == 0);
211 assert(pthread_mutex_unlock(&mon->lock) == 0);
212 dvmDestroyMutex(&mon->lock);
217 * Frees monitor objects belonging to unmarked objects.
219 void dvmSweepMonitorList(Monitor** mon, int (*isUnmarkedObject)(void*))
222 Monitor *prev, *curr;
226 assert(isUnmarkedObject != NULL);
228 prev->next = curr = *mon;
229 while (curr != NULL) {
231 if (obj != NULL && (*isUnmarkedObject)(obj) != 0) {
232 prev->next = curr->next;
243 static char *logWriteInt(char *dst, int value)
245 *dst++ = EVENT_TYPE_INT;
246 set4LE((u1 *)dst, value);
250 static char *logWriteString(char *dst, const char *value, size_t len)
252 *dst++ = EVENT_TYPE_STRING;
253 len = len < 32 ? len : 32;
254 set4LE((u1 *)dst, len);
256 memcpy(dst, value, len);
260 #define EVENT_LOG_TAG_dvm_lock_sample 20003
262 static void logContentionEvent(Thread *self, u4 waitMs, u4 samplePercent,
263 const char *ownerFileName, u4 ownerLineNumber)
265 const StackSaveArea *saveArea;
268 char eventBuffer[174];
269 const char *fileName;
275 /* When a thread is being destroyed it is normal that the frame depth is zero */
276 if (self->interpSave.curFrame == NULL) {
280 saveArea = SAVEAREA_FROM_FP(self->interpSave.curFrame);
281 meth = saveArea->method;
284 /* Emit the event list length, 1 byte. */
287 /* Emit the process name, <= 37 bytes. */
288 fd = open("/proc/self/cmdline", O_RDONLY);
289 memset(procName, 0, sizeof(procName));
290 read(fd, procName, sizeof(procName) - 1);
292 len = strlen(procName);
293 cp = logWriteString(cp, procName, len);
295 /* Emit the sensitive thread ("main thread") status, 5 bytes. */
296 bool isSensitive = false;
297 if (gDvm.isSensitiveThreadHook != NULL) {
298 isSensitive = gDvm.isSensitiveThreadHook();
300 cp = logWriteInt(cp, isSensitive);
302 /* Emit self thread name string, <= 37 bytes. */
303 std::string selfName = dvmGetThreadName(self);
304 cp = logWriteString(cp, selfName.c_str(), selfName.size());
306 /* Emit the wait time, 5 bytes. */
307 cp = logWriteInt(cp, waitMs);
309 /* Emit the source code file name, <= 37 bytes. */
310 fileName = dvmGetMethodSourceFile(meth);
311 if (fileName == NULL) fileName = "";
312 cp = logWriteString(cp, fileName, strlen(fileName));
314 /* Emit the source code line number, 5 bytes. */
315 relativePc = saveArea->xtra.currentPc - saveArea->method->insns;
316 cp = logWriteInt(cp, dvmLineNumFromPC(meth, relativePc));
318 /* Emit the lock owner source code file name, <= 37 bytes. */
319 if (ownerFileName == NULL) {
321 } else if (strcmp(fileName, ownerFileName) == 0) {
322 /* Common case, so save on log space. */
325 cp = logWriteString(cp, ownerFileName, strlen(ownerFileName));
327 /* Emit the source code line number, 5 bytes. */
328 cp = logWriteInt(cp, ownerLineNumber);
330 /* Emit the sample percentage, 5 bytes. */
331 cp = logWriteInt(cp, samplePercent);
333 assert((size_t)(cp - eventBuffer) <= sizeof(eventBuffer));
334 android_btWriteLog(EVENT_LOG_TAG_dvm_lock_sample,
337 (size_t)(cp - eventBuffer));
343 static void lockMonitor(Thread* self, Monitor* mon)
345 ThreadStatus oldStatus;
346 u4 waitThreshold, samplePercent;
347 u8 waitStart, waitEnd, waitMs;
349 if (mon->owner == self) {
353 if (dvmTryLockMutex(&mon->lock) != 0) {
354 oldStatus = dvmChangeStatus(self, THREAD_MONITOR);
355 waitThreshold = gDvm.lockProfThreshold;
357 waitStart = dvmGetRelativeTimeUsec();
360 const Method* currentOwnerMethod = mon->ownerMethod;
361 u4 currentOwnerPc = mon->ownerPc;
363 dvmLockMutex(&mon->lock);
365 waitEnd = dvmGetRelativeTimeUsec();
367 dvmChangeStatus(self, oldStatus);
369 waitMs = (waitEnd - waitStart) / 1000;
370 if (waitMs >= waitThreshold) {
373 samplePercent = 100 * waitMs / waitThreshold;
375 if (samplePercent != 0 && ((u4)rand() % 100 < samplePercent)) {
376 const char* currentOwnerFileName = "no_method";
377 u4 currentOwnerLineNumber = 0;
378 if (currentOwnerMethod != NULL) {
379 currentOwnerFileName = dvmGetMethodSourceFile(currentOwnerMethod);
380 if (currentOwnerFileName == NULL) {
381 currentOwnerFileName = "no_method_file";
383 currentOwnerLineNumber = dvmLineNumFromPC(currentOwnerMethod, currentOwnerPc);
385 logContentionEvent(self, waitMs, samplePercent,
386 currentOwnerFileName, currentOwnerLineNumber);
391 assert(mon->lockCount == 0);
393 // When debugging, save the current monitor holder for future
394 // acquisition failures to use in sampled logging.
395 if (gDvm.lockProfThreshold > 0) {
396 mon->ownerMethod = NULL;
398 if (self->interpSave.curFrame == NULL) {
401 const StackSaveArea* saveArea = SAVEAREA_FROM_FP(self->interpSave.curFrame);
402 if (saveArea == NULL) {
405 mon->ownerMethod = saveArea->method;
406 mon->ownerPc = (saveArea->xtra.currentPc - saveArea->method->insns);
411 * Try to lock a monitor.
413 * Returns "true" on success.
415 #ifdef WITH_COPYING_GC
416 static bool tryLockMonitor(Thread* self, Monitor* mon)
418 if (mon->owner == self) {
422 if (dvmTryLockMutex(&mon->lock) == 0) {
424 assert(mon->lockCount == 0);
436 * Returns true if the unlock succeeded.
437 * If the unlock failed, an exception will be pending.
439 static bool unlockMonitor(Thread* self, Monitor* mon)
441 assert(self != NULL);
443 if (mon->owner == self) {
445 * We own the monitor, so nobody else can be in here.
447 if (mon->lockCount == 0) {
449 mon->ownerMethod = NULL;
451 dvmUnlockMutex(&mon->lock);
457 * We don't own this, so we're not allowed to unlock it.
458 * The JNI spec says that we should throw IllegalMonitorStateException
461 dvmThrowIllegalMonitorStateException("unlock of unowned monitor");
468 * Checks the wait set for circular structure. Returns 0 if the list
469 * is not circular. Otherwise, returns 1. Used only by asserts.
472 static int waitSetCheck(Monitor *mon)
478 fast = slow = mon->waitSet;
481 if (fast == NULL) return 0;
482 if (fast->waitNext == NULL) return 0;
483 if (fast == slow && n > 0) return 1;
485 fast = fast->waitNext->waitNext;
486 slow = slow->waitNext;
492 * Links a thread into a monitor's wait set. The monitor lock must be
493 * held by the caller of this routine.
495 static void waitSetAppend(Monitor *mon, Thread *thread)
500 assert(mon->owner == dvmThreadSelf());
501 assert(thread != NULL);
502 assert(thread->waitNext == NULL);
503 assert(waitSetCheck(mon) == 0);
504 if (mon->waitSet == NULL) {
505 mon->waitSet = thread;
509 while (elt->waitNext != NULL) {
512 elt->waitNext = thread;
516 * Unlinks a thread from a monitor's wait set. The monitor lock must
517 * be held by the caller of this routine.
519 static void waitSetRemove(Monitor *mon, Thread *thread)
524 assert(mon->owner == dvmThreadSelf());
525 assert(thread != NULL);
526 assert(waitSetCheck(mon) == 0);
527 if (mon->waitSet == NULL) {
530 if (mon->waitSet == thread) {
531 mon->waitSet = thread->waitNext;
532 thread->waitNext = NULL;
536 while (elt->waitNext != NULL) {
537 if (elt->waitNext == thread) {
538 elt->waitNext = thread->waitNext;
539 thread->waitNext = NULL;
547 * Converts the given relative waiting time into an absolute time.
549 static void absoluteTime(s8 msec, s4 nsec, struct timespec *ts)
553 #ifdef HAVE_TIMEDWAIT_MONOTONIC
554 clock_gettime(CLOCK_MONOTONIC, ts);
558 gettimeofday(&tv, NULL);
559 ts->tv_sec = tv.tv_sec;
560 ts->tv_nsec = tv.tv_usec * 1000;
563 endSec = ts->tv_sec + msec / 1000;
564 if (endSec >= 0x7fffffff) {
565 ALOGV("NOTE: end time exceeds epoch");
569 ts->tv_nsec = (ts->tv_nsec + (msec % 1000) * 1000000) + nsec;
572 if (ts->tv_nsec >= 1000000000L) {
574 ts->tv_nsec -= 1000000000L;
578 int dvmRelativeCondWait(pthread_cond_t* cond, pthread_mutex_t* mutex,
583 absoluteTime(msec, nsec, &ts);
584 #if defined(HAVE_TIMEDWAIT_MONOTONIC)
585 ret = pthread_cond_timedwait_monotonic(cond, mutex, &ts);
587 ret = pthread_cond_timedwait(cond, mutex, &ts);
589 assert(ret == 0 || ret == ETIMEDOUT);
594 * Wait on a monitor until timeout, interrupt, or notification. Used for
595 * Object.wait() and (somewhat indirectly) Thread.sleep() and Thread.join().
597 * If another thread calls Thread.interrupt(), we throw InterruptedException
598 * and return immediately if one of the following are true:
599 * - blocked in wait(), wait(long), or wait(long, int) methods of Object
600 * - blocked in join(), join(long), or join(long, int) methods of Thread
601 * - blocked in sleep(long), or sleep(long, int) methods of Thread
602 * Otherwise, we set the "interrupted" flag.
604 * Checks to make sure that "nsec" is in the range 0-999999
605 * (i.e. fractions of a millisecond) and throws the appropriate
606 * exception if it isn't.
608 * The spec allows "spurious wakeups", and recommends that all code using
609 * Object.wait() do so in a loop. This appears to derive from concerns
610 * about pthread_cond_wait() on multiprocessor systems. Some commentary
611 * on the web casts doubt on whether these can/should occur.
613 * Since we're allowed to wake up "early", we clamp extremely long durations
614 * to return at the end of the 32-bit time epoch.
616 static void waitMonitor(Thread* self, Monitor* mon, s8 msec, s4 nsec,
617 bool interruptShouldThrow)
620 bool wasInterrupted = false;
624 assert(self != NULL);
627 /* Make sure that we hold the lock. */
628 if (mon->owner != self) {
629 dvmThrowIllegalMonitorStateException(
630 "object not locked by thread before wait()");
635 * Enforce the timeout range.
637 if (msec < 0 || nsec < 0 || nsec > 999999) {
638 dvmThrowIllegalArgumentException("timeout arguments out of range");
643 * Compute absolute wakeup time, if necessary.
645 if (msec == 0 && nsec == 0) {
648 absoluteTime(msec, nsec, &ts);
653 * Add ourselves to the set of threads waiting on this monitor, and
654 * release our hold. We need to let it go even if we're a few levels
655 * deep in a recursive lock, and we need to restore that later.
657 * We append to the wait set ahead of clearing the count and owner
658 * fields so the subroutine can check that the calling thread owns
659 * the monitor. Aside from that, the order of member updates is
660 * not order sensitive as we hold the pthread mutex.
662 waitSetAppend(mon, self);
663 int prevLockCount = mon->lockCount;
667 const Method* savedMethod = mon->ownerMethod;
668 u4 savedPc = mon->ownerPc;
669 mon->ownerMethod = NULL;
673 * Update thread status. If the GC wakes up, it'll ignore us, knowing
674 * that we won't touch any references in this state, and we'll check
675 * our suspend mode before we transition out.
678 dvmChangeStatus(self, THREAD_TIMED_WAIT);
680 dvmChangeStatus(self, THREAD_WAIT);
682 dvmLockMutex(&self->waitMutex);
685 * Set waitMonitor to the monitor object we will be waiting on.
686 * When waitMonitor is non-NULL a notifying or interrupting thread
687 * must signal the thread's waitCond to wake it up.
689 assert(self->waitMonitor == NULL);
690 self->waitMonitor = mon;
693 * Handle the case where the thread was interrupted before we called
696 if (self->interrupted) {
697 wasInterrupted = true;
698 self->waitMonitor = NULL;
699 dvmUnlockMutex(&self->waitMutex);
704 * Release the monitor lock and wait for a notification or
705 * a timeout to occur.
707 dvmUnlockMutex(&mon->lock);
710 ret = pthread_cond_wait(&self->waitCond, &self->waitMutex);
713 #ifdef HAVE_TIMEDWAIT_MONOTONIC
714 ret = pthread_cond_timedwait_monotonic(&self->waitCond, &self->waitMutex, &ts);
716 ret = pthread_cond_timedwait(&self->waitCond, &self->waitMutex, &ts);
718 assert(ret == 0 || ret == ETIMEDOUT);
720 if (self->interrupted) {
721 wasInterrupted = true;
724 self->interrupted = false;
725 self->waitMonitor = NULL;
727 dvmUnlockMutex(&self->waitMutex);
729 /* Reacquire the monitor lock. */
730 lockMonitor(self, mon);
734 * We remove our thread from wait set after restoring the count
735 * and owner fields so the subroutine can check that the calling
736 * thread owns the monitor. Aside from that, the order of member
737 * updates is not order sensitive as we hold the pthread mutex.
740 mon->lockCount = prevLockCount;
741 mon->ownerMethod = savedMethod;
742 mon->ownerPc = savedPc;
743 waitSetRemove(mon, self);
745 /* set self->status back to THREAD_RUNNING, and self-suspend if needed */
746 dvmChangeStatus(self, THREAD_RUNNING);
748 if (wasInterrupted) {
750 * We were interrupted while waiting, or somebody interrupted an
751 * un-interruptible thread earlier and we're bailing out immediately.
753 * The doc sayeth: "The interrupted status of the current thread is
754 * cleared when this exception is thrown."
756 self->interrupted = false;
757 if (interruptShouldThrow) {
758 dvmThrowInterruptedException(NULL);
764 * Notify one thread waiting on this monitor.
766 static void notifyMonitor(Thread* self, Monitor* mon)
770 assert(self != NULL);
773 /* Make sure that we hold the lock. */
774 if (mon->owner != self) {
775 dvmThrowIllegalMonitorStateException(
776 "object not locked by thread before notify()");
779 /* Signal the first waiting thread in the wait set. */
780 while (mon->waitSet != NULL) {
781 thread = mon->waitSet;
782 mon->waitSet = thread->waitNext;
783 thread->waitNext = NULL;
784 dvmLockMutex(&thread->waitMutex);
785 /* Check to see if the thread is still waiting. */
786 if (thread->waitMonitor != NULL) {
787 pthread_cond_signal(&thread->waitCond);
788 dvmUnlockMutex(&thread->waitMutex);
791 dvmUnlockMutex(&thread->waitMutex);
796 * Notify all threads waiting on this monitor.
798 static void notifyAllMonitor(Thread* self, Monitor* mon)
802 assert(self != NULL);
805 /* Make sure that we hold the lock. */
806 if (mon->owner != self) {
807 dvmThrowIllegalMonitorStateException(
808 "object not locked by thread before notifyAll()");
811 /* Signal all threads in the wait set. */
812 while (mon->waitSet != NULL) {
813 thread = mon->waitSet;
814 mon->waitSet = thread->waitNext;
815 thread->waitNext = NULL;
816 dvmLockMutex(&thread->waitMutex);
817 /* Check to see if the thread is still waiting. */
818 if (thread->waitMonitor != NULL) {
819 pthread_cond_signal(&thread->waitCond);
821 dvmUnlockMutex(&thread->waitMutex);
826 * Changes the shape of a monitor from thin to fat, preserving the
827 * internal lock state. The calling thread must own the lock.
829 static void inflateMonitor(Thread *self, Object *obj)
834 assert(self != NULL);
836 assert(LW_SHAPE(obj->lock) == LW_SHAPE_THIN);
837 assert(LW_LOCK_OWNER(obj->lock) == self->threadId);
838 /* Allocate and acquire a new monitor. */
839 mon = dvmCreateMonitor(obj);
840 lockMonitor(self, mon);
841 /* Propagate the lock state. */
843 mon->lockCount = LW_LOCK_COUNT(thin);
844 thin &= LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT;
845 thin |= (u4)mon | LW_SHAPE_FAT;
846 /* Publish the updated lock word. */
847 android_atomic_release_store(thin, (int32_t *)&obj->lock);
851 * Implements monitorenter for "synchronized" stuff.
853 * This does not fail or throw an exception (unless deadlock prediction
854 * is enabled and set to "err" mode).
856 void dvmLockObject(Thread* self, Object *obj)
859 ThreadStatus oldStatus;
862 long minSleepDelayNs = 1000000; /* 1 millisecond */
863 long maxSleepDelayNs = 1000000000; /* 1 second */
864 u4 thin, newThin, threadId;
866 assert(self != NULL);
868 threadId = self->threadId;
872 if (LW_SHAPE(thin) == LW_SHAPE_THIN) {
874 * The lock is a thin lock. The owner field is used to
875 * determine the acquire method, ordered by cost.
877 if (LW_LOCK_OWNER(thin) == threadId) {
879 * The calling thread owns the lock. Increment the
880 * value of the recursion count field.
882 obj->lock += 1 << LW_LOCK_COUNT_SHIFT;
883 if (LW_LOCK_COUNT(obj->lock) == LW_LOCK_COUNT_MASK) {
885 * The reacquisition limit has been reached. Inflate
886 * the lock so the next acquire will not overflow the
887 * recursion count field.
889 inflateMonitor(self, obj);
891 } else if (LW_LOCK_OWNER(thin) == 0) {
893 * The lock is unowned. Install the thread id of the
894 * calling thread into the owner field. This is the
895 * common case. In performance critical code the JIT
896 * will have tried this before calling out to the VM.
898 newThin = thin | (threadId << LW_LOCK_OWNER_SHIFT);
899 if (android_atomic_acquire_cas(thin, newThin,
900 (int32_t*)thinp) != 0) {
902 * The acquire failed. Try again.
907 ALOGV("(%d) spin on lock %p: %#x (%#x) %#x",
908 threadId, &obj->lock, 0, *thinp, thin);
910 * The lock is owned by another thread. Notify the VM
911 * that we are about to wait.
913 oldStatus = dvmChangeStatus(self, THREAD_MONITOR);
915 * Spin until the thin lock is released or inflated.
921 * Check the shape of the lock word. Another thread
922 * may have inflated the lock while we were waiting.
924 if (LW_SHAPE(thin) == LW_SHAPE_THIN) {
925 if (LW_LOCK_OWNER(thin) == 0) {
927 * The lock has been released. Install the
928 * thread id of the calling thread into the
931 newThin = thin | (threadId << LW_LOCK_OWNER_SHIFT);
932 if (android_atomic_acquire_cas(thin, newThin,
933 (int32_t *)thinp) == 0) {
935 * The acquire succeed. Break out of the
936 * loop and proceed to inflate the lock.
942 * The lock has not been released. Yield so
943 * the owning thread can run.
945 if (sleepDelayNs == 0) {
947 sleepDelayNs = minSleepDelayNs;
950 tm.tv_nsec = sleepDelayNs;
951 nanosleep(&tm, NULL);
953 * Prepare the next delay value. Wrap to
954 * avoid once a second polls for eternity.
956 if (sleepDelayNs < maxSleepDelayNs / 2) {
959 sleepDelayNs = minSleepDelayNs;
965 * The thin lock was inflated by another thread.
966 * Let the VM know we are no longer waiting and
969 ALOGV("(%d) lock %p surprise-fattened",
970 threadId, &obj->lock);
971 dvmChangeStatus(self, oldStatus);
975 ALOGV("(%d) spin on lock done %p: %#x (%#x) %#x",
976 threadId, &obj->lock, 0, *thinp, thin);
978 * We have acquired the thin lock. Let the VM know that
979 * we are no longer waiting.
981 dvmChangeStatus(self, oldStatus);
985 inflateMonitor(self, obj);
986 ALOGV("(%d) lock %p fattened", threadId, &obj->lock);
990 * The lock is a fat lock.
992 assert(LW_MONITOR(obj->lock) != NULL);
993 lockMonitor(self, LW_MONITOR(obj->lock));
998 * Implements monitorexit for "synchronized" stuff.
1000 * On failure, throws an exception and returns "false".
1002 bool dvmUnlockObject(Thread* self, Object *obj)
1006 assert(self != NULL);
1007 assert(self->status == THREAD_RUNNING);
1008 assert(obj != NULL);
1010 * Cache the lock word as its value can change while we are
1011 * examining its state.
1013 thin = *(volatile u4 *)&obj->lock;
1014 if (LW_SHAPE(thin) == LW_SHAPE_THIN) {
1016 * The lock is thin. We must ensure that the lock is owned
1017 * by the given thread before unlocking it.
1019 if (LW_LOCK_OWNER(thin) == self->threadId) {
1021 * We are the lock owner. It is safe to update the lock
1022 * without CAS as lock ownership guards the lock itself.
1024 if (LW_LOCK_COUNT(thin) == 0) {
1026 * The lock was not recursively acquired, the common
1027 * case. Unlock by clearing all bits except for the
1030 thin &= (LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT);
1031 android_atomic_release_store(thin, (int32_t*)&obj->lock);
1034 * The object was recursively acquired. Decrement the
1035 * lock recursion count field.
1037 obj->lock -= 1 << LW_LOCK_COUNT_SHIFT;
1041 * We do not own the lock. The JVM spec requires that we
1042 * throw an exception in this case.
1044 dvmThrowIllegalMonitorStateException("unlock of unowned monitor");
1049 * The lock is fat. We must check to see if unlockMonitor has
1050 * raised any exceptions before continuing.
1052 assert(LW_MONITOR(obj->lock) != NULL);
1053 if (!unlockMonitor(self, LW_MONITOR(obj->lock))) {
1055 * An exception has been raised. Do not fall through.
1064 * Object.wait(). Also called for class init.
1066 void dvmObjectWait(Thread* self, Object *obj, s8 msec, s4 nsec,
1067 bool interruptShouldThrow)
1070 u4 thin = *(volatile u4 *)&obj->lock;
1072 /* If the lock is still thin, we need to fatten it.
1074 if (LW_SHAPE(thin) == LW_SHAPE_THIN) {
1075 /* Make sure that 'self' holds the lock.
1077 if (LW_LOCK_OWNER(thin) != self->threadId) {
1078 dvmThrowIllegalMonitorStateException(
1079 "object not locked by thread before wait()");
1083 /* This thread holds the lock. We need to fatten the lock
1084 * so 'self' can block on it. Don't update the object lock
1085 * field yet, because 'self' needs to acquire the lock before
1086 * any other thread gets a chance.
1088 inflateMonitor(self, obj);
1089 ALOGV("(%d) lock %p fattened by wait()", self->threadId, &obj->lock);
1091 mon = LW_MONITOR(obj->lock);
1092 waitMonitor(self, mon, msec, nsec, interruptShouldThrow);
1098 void dvmObjectNotify(Thread* self, Object *obj)
1100 u4 thin = *(volatile u4 *)&obj->lock;
1102 /* If the lock is still thin, there aren't any waiters;
1103 * waiting on an object forces lock fattening.
1105 if (LW_SHAPE(thin) == LW_SHAPE_THIN) {
1106 /* Make sure that 'self' holds the lock.
1108 if (LW_LOCK_OWNER(thin) != self->threadId) {
1109 dvmThrowIllegalMonitorStateException(
1110 "object not locked by thread before notify()");
1114 /* no-op; there are no waiters to notify.
1119 notifyMonitor(self, LW_MONITOR(thin));
1124 * Object.notifyAll().
1126 void dvmObjectNotifyAll(Thread* self, Object *obj)
1128 u4 thin = *(volatile u4 *)&obj->lock;
1130 /* If the lock is still thin, there aren't any waiters;
1131 * waiting on an object forces lock fattening.
1133 if (LW_SHAPE(thin) == LW_SHAPE_THIN) {
1134 /* Make sure that 'self' holds the lock.
1136 if (LW_LOCK_OWNER(thin) != self->threadId) {
1137 dvmThrowIllegalMonitorStateException(
1138 "object not locked by thread before notifyAll()");
1142 /* no-op; there are no waiters to notify.
1147 notifyAllMonitor(self, LW_MONITOR(thin));
1152 * This implements java.lang.Thread.sleep(long msec, int nsec).
1154 * The sleep is interruptible by other threads, which means we can't just
1155 * plop into an OS sleep call. (We probably could if we wanted to send
1156 * signals around and rely on EINTR, but that's inefficient and relies
1157 * on native code respecting our signal mask.)
1159 * We have to do all of this stuff for Object.wait() as well, so it's
1160 * easiest to just sleep on a private Monitor.
1162 * It appears that we want sleep(0,0) to go through the motions of sleeping
1163 * for a very short duration, rather than just returning.
1165 void dvmThreadSleep(u8 msec, u4 nsec)
1167 Thread* self = dvmThreadSelf();
1168 Monitor* mon = gDvm.threadSleepMon;
1170 /* sleep(0,0) wakes up immediately, wait(0,0) means wait forever; adjust */
1171 if (msec == 0 && nsec == 0)
1174 lockMonitor(self, mon);
1175 waitMonitor(self, mon, msec, nsec, true);
1176 unlockMonitor(self, mon);
1180 * Implement java.lang.Thread.interrupt().
1182 void dvmThreadInterrupt(Thread* thread)
1184 assert(thread != NULL);
1186 dvmLockMutex(&thread->waitMutex);
1189 * If the interrupted flag is already set no additional action is
1192 if (thread->interrupted == true) {
1193 dvmUnlockMutex(&thread->waitMutex);
1198 * Raise the "interrupted" flag. This will cause it to bail early out
1199 * of the next wait() attempt, if it's not currently waiting on
1202 thread->interrupted = true;
1205 * Is the thread waiting?
1207 * Note that fat vs. thin doesn't matter here; waitMonitor
1208 * is only set when a thread actually waits on a monitor,
1209 * which implies that the monitor has already been fattened.
1211 if (thread->waitMonitor != NULL) {
1212 pthread_cond_signal(&thread->waitCond);
1215 dvmUnlockMutex(&thread->waitMutex);
1218 #ifndef WITH_COPYING_GC
1219 u4 dvmIdentityHashCode(Object *obj)
1225 * Returns the identity hash code of the given object.
1227 u4 dvmIdentityHashCode(Object *obj)
1229 Thread *self, *thread;
1232 u4 lock, owner, hashState;
1236 * Null is defined to have an identity hash code of 0.
1242 hashState = LW_HASH_STATE(*lw);
1243 if (hashState == LW_HASH_STATE_HASHED) {
1245 * The object has been hashed but has not had its hash code
1246 * relocated by the garbage collector. Use the raw object
1249 return (u4)obj >> 3;
1250 } else if (hashState == LW_HASH_STATE_HASHED_AND_MOVED) {
1252 * The object has been hashed and its hash code has been
1253 * relocated by the collector. Use the value of the naturally
1254 * aligned word following the instance data.
1256 assert(!dvmIsClassObject(obj));
1257 if (IS_CLASS_FLAG_SET(obj->clazz, CLASS_ISARRAY)) {
1258 size = dvmArrayObjectSize((ArrayObject *)obj);
1259 size = (size + 2) & ~2;
1261 size = obj->clazz->objectSize;
1263 return *(u4 *)(((char *)obj) + size);
1264 } else if (hashState == LW_HASH_STATE_UNHASHED) {
1266 * The object has never been hashed. Change the hash state to
1267 * hashed and use the raw object address.
1269 self = dvmThreadSelf();
1270 if (self->threadId == lockOwner(obj)) {
1272 * We already own the lock so we can update the hash state
1275 *lw |= (LW_HASH_STATE_HASHED << LW_HASH_STATE_SHIFT);
1276 return (u4)obj >> 3;
1279 * We do not own the lock. Try acquiring the lock. Should
1280 * this fail, we must suspend the owning thread.
1282 if (LW_SHAPE(*lw) == LW_SHAPE_THIN) {
1284 * If the lock is thin assume it is unowned. We simulate
1285 * an acquire, update, and release with a single CAS.
1287 lock = (LW_HASH_STATE_HASHED << LW_HASH_STATE_SHIFT);
1288 if (android_atomic_acquire_cas(
1291 (int32_t *)lw) == 0) {
1293 * A new lockword has been installed with a hash state
1294 * of hashed. Use the raw object address.
1296 return (u4)obj >> 3;
1299 if (tryLockMonitor(self, LW_MONITOR(*lw))) {
1301 * The monitor lock has been acquired. Change the
1302 * hash state to hashed and use the raw object
1305 *lw |= (LW_HASH_STATE_HASHED << LW_HASH_STATE_SHIFT);
1306 unlockMonitor(self, LW_MONITOR(*lw));
1307 return (u4)obj >> 3;
1311 * At this point we have failed to acquire the lock. We must
1312 * identify the owning thread and suspend it.
1314 dvmLockThreadList(self);
1316 * Cache the lock word as its value can change between
1317 * determining its shape and retrieving its owner.
1320 if (LW_SHAPE(lock) == LW_SHAPE_THIN) {
1322 * Find the thread with the corresponding thread id.
1324 owner = LW_LOCK_OWNER(lock);
1325 assert(owner != self->threadId);
1327 * If the lock has no owner do not bother scanning the
1328 * thread list and fall through to the failure handler.
1330 thread = owner ? gDvm.threadList : NULL;
1331 while (thread != NULL) {
1332 if (thread->threadId == owner) {
1335 thread = thread->next;
1338 thread = LW_MONITOR(lock)->owner;
1341 * If thread is NULL the object has been released since the
1342 * thread list lock was acquired. Try again.
1344 if (thread == NULL) {
1345 dvmUnlockThreadList();
1349 * Wait for the owning thread to suspend.
1351 dvmSuspendThread(thread);
1352 if (dvmHoldsLock(thread, obj)) {
1354 * The owning thread has been suspended. We can safely
1355 * change the hash state to hashed.
1357 *lw |= (LW_HASH_STATE_HASHED << LW_HASH_STATE_SHIFT);
1358 dvmResumeThread(thread);
1359 dvmUnlockThreadList();
1360 return (u4)obj >> 3;
1363 * The wrong thread has been suspended. Try again.
1365 dvmResumeThread(thread);
1366 dvmUnlockThreadList();
1369 ALOGE("object %p has an unknown hash state %#x", obj, hashState);
1370 dvmDumpThread(dvmThreadSelf(), false);
1372 return 0; /* Quiet the compiler. */
1374 #endif /* WITH_COPYING_GC */