blob: 8b8c1f526f1c62a60189740dfb444cab4ad0b387 [file] [log] [blame]
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001/*
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 */
Andy McFadden581bed72009-10-15 11:24:54 -070016
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080017/*
18 * Fundamental synchronization mechanisms.
19 *
20 * The top part of the file has operations on "monitor" structs; the
21 * next part has the native calls on objects.
22 *
23 * The current implementation uses "thin locking" to avoid allocating
24 * an Object's full Monitor struct until absolutely necessary (i.e.,
25 * during contention or a call to wait()).
26 *
27 * TODO: make improvements to thin locking
28 * We may be able to improve performance and reduce memory requirements by:
29 * - reverting to a thin lock once the Monitor is no longer necessary
30 * - using a pool of monitor objects, with some sort of recycling scheme
31 *
32 * TODO: recycle native-level monitors when objects are garbage collected.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080033 */
34#include "Dalvik.h"
35
Carl Shapirof0c514c2010-04-09 15:03:33 -070036#include <fcntl.h>
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080037#include <stdlib.h>
38#include <unistd.h>
39#include <pthread.h>
40#include <time.h>
41#include <sys/time.h>
42#include <errno.h>
43
44#define LOG_THIN LOGV
45
46#ifdef WITH_DEADLOCK_PREDICTION /* fwd */
47static const char* kStartBanner =
48 "<-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#";
49static const char* kEndBanner =
50 "#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#->";
51
52/*
53 * Unsorted, expanding list of objects.
54 *
55 * This is very similar to PointerSet (which came into existence after this),
56 * but these are unsorted, uniqueness is not enforced by the "add" function,
57 * and the base object isn't allocated on the heap.
58 */
59typedef struct ExpandingObjectList {
60 u2 alloc;
61 u2 count;
62 Object** list;
63} ExpandingObjectList;
64
65/* fwd */
66static void updateDeadlockPrediction(Thread* self, Object* obj);
67static void removeCollectedObject(Object* obj);
68static void expandObjClear(ExpandingObjectList* pList);
69#endif
70
71/*
72 * Every Object has a monitor associated with it, but not every Object is
73 * actually locked. Even the ones that are locked do not need a
74 * full-fledged monitor until a) there is actual contention or b) wait()
75 * is called on the Object.
76 *
77 * For Dalvik, we have implemented a scheme similar to the one described
78 * in Bacon et al.'s "Thin locks: featherweight synchronization for Java"
79 * (ACM 1998). Things are even easier for us, though, because we have
80 * a full 32 bits to work with.
81 *
Carl Shapiro94338aa2009-12-21 11:42:59 -080082 * The two states of an Object's lock are referred to as "thin" and
83 * "fat". A lock may transition from the "thin" state to the "fat"
84 * state and this transition is referred to as inflation. Once a lock
85 * has been inflated it remains in the "fat" state indefinitely.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080086 *
Carl Shapiro77f52eb2009-12-24 19:56:53 -080087 * The lock value itself is stored in Object.lock. The LSB of the
88 * lock encodes its state. When cleared, the lock is in the "thin"
89 * state and its bits are formatted as follows:
Carl Shapiro71938022009-12-22 13:49:53 -080090 *
Carl Shapiro94338aa2009-12-21 11:42:59 -080091 * [31 ---- 19] [18 ---- 3] [2 ---- 1] [0]
92 * lock count thread id hash state 0
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080093 *
Carl Shapiro77f52eb2009-12-24 19:56:53 -080094 * When set, the lock is in the "fat" state and its bits are formatted
Carl Shapiro94338aa2009-12-21 11:42:59 -080095 * as follows:
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080096 *
Carl Shapiro94338aa2009-12-21 11:42:59 -080097 * [31 ---- 3] [2 ---- 1] [0]
98 * pointer hash state 1
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080099 *
100 * For an in-depth description of the mechanics of thin-vs-fat locking,
101 * read the paper referred to above.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800102 */
103
104/*
105 * Monitors provide:
106 * - mutually exclusive access to resources
107 * - a way for multiple threads to wait for notification
108 *
109 * In effect, they fill the role of both mutexes and condition variables.
110 *
111 * Only one thread can own the monitor at any time. There may be several
112 * threads waiting on it (the wait call unlocks it). One or more waiting
113 * threads may be getting interrupted or notified at any given time.
114 */
115struct Monitor {
116 Thread* owner; /* which thread currently owns the lock? */
117 int lockCount; /* owner's recursive lock depth */
118 Object* obj; /* what object are we part of [debug only] */
119
Carl Shapiro77f52eb2009-12-24 19:56:53 -0800120 Thread* waitSet; /* threads currently waiting on this monitor */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800121
122 pthread_mutex_t lock;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800123
124 Monitor* next;
125
126#ifdef WITH_DEADLOCK_PREDICTION
127 /*
128 * Objects that have been locked immediately after this one in the
129 * past. We use an expanding flat array, allocated on first use, to
130 * minimize allocations. Deletions from the list, expected to be
131 * infrequent, are crunched down.
132 */
133 ExpandingObjectList historyChildren;
134
135 /*
136 * We also track parents. This isn't strictly necessary, but it makes
137 * the cleanup at GC time significantly faster.
138 */
139 ExpandingObjectList historyParents;
140
141 /* used during cycle detection */
142 bool historyMark;
143
144 /* stack trace, established the first time we locked the object */
145 int historyStackDepth;
146 int* historyRawStackTrace;
147#endif
148};
149
150
151/*
152 * Create and initialize a monitor.
153 */
154Monitor* dvmCreateMonitor(Object* obj)
155{
156 Monitor* mon;
157
158 mon = (Monitor*) calloc(1, sizeof(Monitor));
159 if (mon == NULL) {
160 LOGE("Unable to allocate monitor\n");
161 dvmAbort();
162 }
Carl Shapiro94338aa2009-12-21 11:42:59 -0800163 if (((u4)mon & 7) != 0) {
164 LOGE("Misaligned monitor: %p\n", mon);
165 dvmAbort();
166 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800167 mon->obj = obj;
168 dvmInitMutex(&mon->lock);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800169
170 /* replace the head of the list with the new monitor */
171 do {
172 mon->next = gDvm.monitorList;
173 } while (!ATOMIC_CMP_SWAP((int32_t*)(void*)&gDvm.monitorList,
174 (int32_t)mon->next, (int32_t)mon));
175
176 return mon;
177}
178
179/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800180 * Free the monitor list. Only used when shutting the VM down.
181 */
182void dvmFreeMonitorList(void)
183{
184 Monitor* mon;
185 Monitor* nextMon;
186
187 mon = gDvm.monitorList;
188 while (mon != NULL) {
189 nextMon = mon->next;
190
191#ifdef WITH_DEADLOCK_PREDICTION
192 expandObjClear(&mon->historyChildren);
193 expandObjClear(&mon->historyParents);
194 free(mon->historyRawStackTrace);
195#endif
196 free(mon);
197 mon = nextMon;
198 }
199}
200
201/*
202 * Log some info about our monitors.
203 */
204void dvmDumpMonitorInfo(const char* msg)
205{
206#if QUIET_ZYGOTE_MONITOR
207 if (gDvm.zygote) {
208 return;
209 }
210#endif
211
212 int totalCount;
213 int liveCount;
214
215 totalCount = liveCount = 0;
216 Monitor* mon = gDvm.monitorList;
217 while (mon != NULL) {
218 totalCount++;
219 if (mon->obj != NULL)
220 liveCount++;
221 mon = mon->next;
222 }
223
224 LOGD("%s: monitor list has %d entries (%d live)\n",
225 msg, totalCount, liveCount);
226}
227
228/*
229 * Get the object that a monitor is part of.
230 */
231Object* dvmGetMonitorObject(Monitor* mon)
232{
233 if (mon == NULL)
234 return NULL;
235 else
236 return mon->obj;
237}
238
239/*
Carl Shapiro30aa9972010-01-13 22:07:50 -0800240 * Returns the thread id of the thread owning the given lock.
241 */
242static u4 lockOwner(Object* obj)
243{
244 Thread *owner;
245 u4 lock;
246
247 assert(obj != NULL);
248 /*
249 * Since we're reading the lock value multiple times, latch it so
250 * that it doesn't change out from under us if we get preempted.
251 */
252 lock = obj->lock;
253 if (LW_SHAPE(lock) == LW_SHAPE_THIN) {
254 return LW_LOCK_OWNER(lock);
255 } else {
256 owner = LW_MONITOR(lock)->owner;
257 return owner ? owner->threadId : 0;
258 }
259}
260
261/*
Andy McFaddenfd542662010-03-12 13:39:59 -0800262 * Get the thread that holds the lock on the specified object. The
263 * object may be unlocked, thin-locked, or fat-locked.
264 *
265 * The caller must lock the thread list before calling here.
266 */
267Thread* dvmGetObjectLockHolder(Object* obj)
268{
269 u4 threadId = lockOwner(obj);
270
271 if (threadId == 0)
272 return NULL;
273 return dvmGetThreadByThreadId(threadId);
274}
275
276/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800277 * Checks whether the given thread holds the given
278 * objects's lock.
279 */
280bool dvmHoldsLock(Thread* thread, Object* obj)
281{
282 if (thread == NULL || obj == NULL) {
283 return false;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800284 } else {
Carl Shapiro30aa9972010-01-13 22:07:50 -0800285 return thread->threadId == lockOwner(obj);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800286 }
287}
288
289/*
290 * Free the monitor associated with an object and make the object's lock
291 * thin again. This is called during garbage collection.
292 */
Carl Shapiro5a6071b2010-01-07 21:35:50 -0800293static void freeObjectMonitor(Object* obj)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800294{
295 Monitor *mon;
296
Carl Shapiro5a6071b2010-01-07 21:35:50 -0800297 assert(LW_SHAPE(obj->lock) == LW_SHAPE_FAT);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800298
299#ifdef WITH_DEADLOCK_PREDICTION
300 if (gDvm.deadlockPredictMode != kDPOff)
Carl Shapiro5a6071b2010-01-07 21:35:50 -0800301 removeCollectedObject(obj);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800302#endif
303
Carl Shapiro5a6071b2010-01-07 21:35:50 -0800304 mon = LW_MONITOR(obj->lock);
305 obj->lock = DVM_LOCK_INITIAL_THIN_VALUE;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800306
307 /* This lock is associated with an object
308 * that's being swept. The only possible way
309 * anyone could be holding this lock would be
310 * if some JNI code locked but didn't unlock
311 * the object, in which case we've got some bad
312 * native code somewhere.
313 */
Carl Shapiro1ff876d2010-04-04 01:56:48 -0700314 assert(pthread_mutex_trylock(&mon->lock) == 0);
315 assert(pthread_mutex_unlock(&mon->lock) == 0);
Carl Shapiro980ffb02010-03-13 22:34:01 -0800316 dvmDestroyMutex(&mon->lock);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800317#ifdef WITH_DEADLOCK_PREDICTION
Carl Shapiro5a6071b2010-01-07 21:35:50 -0800318 expandObjClear(&mon->historyChildren);
319 expandObjClear(&mon->historyParents);
320 free(mon->historyRawStackTrace);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800321#endif
Carl Shapiro5a6071b2010-01-07 21:35:50 -0800322 free(mon);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800323}
324
Carl Shapiro5a6071b2010-01-07 21:35:50 -0800325/*
326 * Frees monitor objects belonging to unmarked objects.
327 */
328void dvmSweepMonitorList(Monitor** mon, int (*isUnmarkedObject)(void*))
329{
330 Monitor handle;
331 Monitor *prev, *curr;
332 Object *obj;
333
334 assert(mon != NULL);
Carl Shapiro5a6071b2010-01-07 21:35:50 -0800335 assert(isUnmarkedObject != NULL);
336 prev = &handle;
337 prev->next = curr = *mon;
338 while (curr != NULL) {
339 obj = curr->obj;
340 if (obj != NULL && (*isUnmarkedObject)(obj) != 0) {
341 prev->next = curr = curr->next;
342 freeObjectMonitor(obj);
343 } else {
344 prev = curr;
345 curr = curr->next;
346 }
347 }
348 *mon = handle.next;
349}
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800350
Carl Shapirof0c514c2010-04-09 15:03:33 -0700351static char *logWriteInt(char *dst, int value)
352{
353 *dst++ = EVENT_TYPE_INT;
354 set4LE((u1 *)dst, value);
355 return dst + 4;
356}
357
358static char *logWriteString(char *dst, const char *value, size_t len)
359{
360 *dst++ = EVENT_TYPE_STRING;
Carl Shapiroaf69cf82010-04-16 17:33:15 -0700361 len = len < 32 ? len : 32;
Carl Shapirof0c514c2010-04-09 15:03:33 -0700362 set4LE((u1 *)dst, len);
363 dst += 4;
Carl Shapirof0c514c2010-04-09 15:03:33 -0700364 memcpy(dst, value, len);
365 return dst + len;
366}
367
Carl Shapiroaf69cf82010-04-16 17:33:15 -0700368#define EVENT_LOG_TAG_dvm_lock_sample 20003
Carl Shapirof0c514c2010-04-09 15:03:33 -0700369
370static void logContentionEvent(Thread *self, u4 waitMs, u4 samplePercent)
371{
372 const StackSaveArea *saveArea;
373 const Method *meth;
374 u4 relativePc;
Carl Shapiroaf69cf82010-04-16 17:33:15 -0700375 char eventBuffer[132];
Carl Shapirof0c514c2010-04-09 15:03:33 -0700376 const char *fileName;
Carl Shapiroaf69cf82010-04-16 17:33:15 -0700377 char procName[33], *selfName, *ownerName;
Carl Shapirof0c514c2010-04-09 15:03:33 -0700378 char *cp;
Carl Shapiroaf69cf82010-04-16 17:33:15 -0700379 size_t len;
380 int fd;
Carl Shapirof0c514c2010-04-09 15:03:33 -0700381
382 saveArea = SAVEAREA_FROM_FP(self->curFrame);
383 meth = saveArea->method;
384 cp = eventBuffer;
385
386 /* Emit the event list length, 1 byte. */
387 *cp++ = 7;
388
389 /* Emit the process name, <= 37 bytes. */
390 fd = open("/proc/self/cmdline", O_RDONLY);
Carl Shapiroaf69cf82010-04-16 17:33:15 -0700391 memset(procName, 0, sizeof(procName));
392 read(fd, procName, sizeof(procName) - 1);
Carl Shapirof0c514c2010-04-09 15:03:33 -0700393 close(fd);
Carl Shapiroaf69cf82010-04-16 17:33:15 -0700394 len = strlen(procName);
395 cp = logWriteString(cp, procName, len);
Carl Shapirof0c514c2010-04-09 15:03:33 -0700396
397 /* Emit the main thread status, 5 bytes. */
398 bool isMainThread = (self->systemTid == getpid());
399 cp = logWriteInt(cp, isMainThread);
400
401 /* Emit self thread name string, <= 37 bytes. */
402 selfName = dvmGetThreadName(self);
403 cp = logWriteString(cp, selfName, strlen(selfName));
404 free(selfName);
405
406 /* Emit the wait time, 5 bytes. */
407 cp = logWriteInt(cp, waitMs);
408
409 /* Emit the source code file name, <= 37 bytes. */
410 fileName = dvmGetMethodSourceFile(meth);
411 if (fileName == NULL) fileName = "";
412 cp = logWriteString(cp, fileName, strlen(fileName));
413
414 /* Emit the source code line number, 5 bytes. */
415 relativePc = saveArea->xtra.currentPc - saveArea->method->insns;
416 cp = logWriteInt(cp, dvmLineNumFromPC(meth, relativePc));
417
418 /* Emit the sample percentage, 5 bytes. */
419 cp = logWriteInt(cp, samplePercent);
420
421 assert((size_t)(cp - eventBuffer) <= sizeof(eventBuffer));
Carl Shapiroaf69cf82010-04-16 17:33:15 -0700422 android_btWriteLog(EVENT_LOG_TAG_dvm_lock_sample,
Carl Shapirof0c514c2010-04-09 15:03:33 -0700423 EVENT_TYPE_LIST,
424 eventBuffer,
425 (size_t)(cp - eventBuffer));
426}
427
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800428/*
429 * Lock a monitor.
430 */
431static void lockMonitor(Thread* self, Monitor* mon)
432{
Carl Shapirof0c514c2010-04-09 15:03:33 -0700433 Thread *owner;
434 ThreadStatus oldStatus;
435 u4 waitThreshold, samplePercent;
436 u8 waitStart, waitEnd, waitMs;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800437
438 if (mon->owner == self) {
439 mon->lockCount++;
Carl Shapirof0c514c2010-04-09 15:03:33 -0700440 return;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800441 }
Carl Shapiro045fdc92010-04-13 16:48:27 -0700442 if (dvmTryLockMutex(&mon->lock) != 0) {
Carl Shapirof0c514c2010-04-09 15:03:33 -0700443 oldStatus = dvmChangeStatus(self, THREAD_MONITOR);
444 waitThreshold = gDvm.lockProfThreshold;
445 if (waitThreshold) {
446 waitStart = dvmGetRelativeTimeUsec();
447 }
448 dvmLockMutex(&mon->lock);
449 if (waitThreshold) {
450 waitEnd = dvmGetRelativeTimeUsec();
451 }
452 dvmChangeStatus(self, oldStatus);
453 if (waitThreshold) {
454 waitMs = (waitEnd - waitStart) / 1000;
455 if (waitMs >= waitThreshold) {
456 samplePercent = 100;
457 } else {
Carl Shapiroaf69cf82010-04-16 17:33:15 -0700458 samplePercent = 100 * waitMs / waitThreshold;
Carl Shapirof0c514c2010-04-09 15:03:33 -0700459 }
Carl Shapirob8fcf572010-04-16 17:33:15 -0700460 if (samplePercent != 0 && ((u4)rand() % 100 < samplePercent)) {
Carl Shapirof0c514c2010-04-09 15:03:33 -0700461 logContentionEvent(self, waitMs, samplePercent);
462 }
463 }
464 }
465 mon->owner = self;
466 assert(mon->lockCount == 0);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800467}
468
469/*
470 * Try to lock a monitor.
471 *
472 * Returns "true" on success.
473 */
474static bool tryLockMonitor(Thread* self, Monitor* mon)
475{
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800476 if (mon->owner == self) {
477 mon->lockCount++;
478 return true;
479 } else {
Carl Shapiro980ffb02010-03-13 22:34:01 -0800480 if (dvmTryLockMutex(&mon->lock) == 0) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800481 mon->owner = self;
482 assert(mon->lockCount == 0);
483 return true;
484 } else {
485 return false;
486 }
487 }
488}
489
490
491/*
492 * Unlock a monitor.
493 *
494 * Returns true if the unlock succeeded.
495 * If the unlock failed, an exception will be pending.
496 */
497static bool unlockMonitor(Thread* self, Monitor* mon)
498{
Carl Shapiro77f52eb2009-12-24 19:56:53 -0800499 assert(self != NULL);
Carl Shapiro92277082010-04-06 15:35:59 -0700500 assert(mon != NULL);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800501 if (mon->owner == self) {
502 /*
503 * We own the monitor, so nobody else can be in here.
504 */
505 if (mon->lockCount == 0) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800506 mon->owner = NULL;
Carl Shapiro980ffb02010-03-13 22:34:01 -0800507 dvmUnlockMutex(&mon->lock);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800508 } else {
509 mon->lockCount--;
510 }
511 } else {
512 /*
513 * We don't own this, so we're not allowed to unlock it.
514 * The JNI spec says that we should throw IllegalMonitorStateException
515 * in this case.
516 */
Carl Shapiro8782d7c2010-04-19 20:10:35 -0700517 dvmThrowException("Ljava/lang/IllegalMonitorStateException;",
518 "unlock of unowned monitor");
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800519 return false;
520 }
521 return true;
522}
523
524/*
Carl Shapiro77f52eb2009-12-24 19:56:53 -0800525 * Checks the wait set for circular structure. Returns 0 if the list
Carl Shapirob4539192010-01-04 16:50:00 -0800526 * is not circular. Otherwise, returns 1. Used only by asserts.
Carl Shapiro77f52eb2009-12-24 19:56:53 -0800527 */
528static int waitSetCheck(Monitor *mon)
529{
530 Thread *fast, *slow;
531 size_t n;
532
533 assert(mon != NULL);
534 fast = slow = mon->waitSet;
535 n = 0;
536 for (;;) {
537 if (fast == NULL) return 0;
538 if (fast->waitNext == NULL) return 0;
Carl Shapiro5f56e672010-01-05 20:38:03 -0800539 if (fast == slow && n > 0) return 1;
Carl Shapiro77f52eb2009-12-24 19:56:53 -0800540 n += 2;
541 fast = fast->waitNext->waitNext;
542 slow = slow->waitNext;
543 }
544}
545
546/*
Carl Shapiro30aa9972010-01-13 22:07:50 -0800547 * Links a thread into a monitor's wait set. The monitor lock must be
548 * held by the caller of this routine.
Carl Shapiro77f52eb2009-12-24 19:56:53 -0800549 */
550static void waitSetAppend(Monitor *mon, Thread *thread)
551{
552 Thread *elt;
553
554 assert(mon != NULL);
Carl Shapiro30aa9972010-01-13 22:07:50 -0800555 assert(mon->owner == dvmThreadSelf());
Carl Shapiro77f52eb2009-12-24 19:56:53 -0800556 assert(thread != NULL);
557 assert(thread->waitNext == NULL);
558 assert(waitSetCheck(mon) == 0);
559 if (mon->waitSet == NULL) {
560 mon->waitSet = thread;
561 return;
562 }
563 elt = mon->waitSet;
564 while (elt->waitNext != NULL) {
565 elt = elt->waitNext;
566 }
567 elt->waitNext = thread;
568}
569
570/*
Carl Shapiro30aa9972010-01-13 22:07:50 -0800571 * Unlinks a thread from a monitor's wait set. The monitor lock must
572 * be held by the caller of this routine.
Carl Shapiro77f52eb2009-12-24 19:56:53 -0800573 */
574static void waitSetRemove(Monitor *mon, Thread *thread)
575{
576 Thread *elt;
577
578 assert(mon != NULL);
Carl Shapiro30aa9972010-01-13 22:07:50 -0800579 assert(mon->owner == dvmThreadSelf());
Carl Shapiro77f52eb2009-12-24 19:56:53 -0800580 assert(thread != NULL);
581 assert(waitSetCheck(mon) == 0);
582 if (mon->waitSet == NULL) {
583 return;
584 }
585 if (mon->waitSet == thread) {
586 mon->waitSet = thread->waitNext;
587 thread->waitNext = NULL;
588 return;
589 }
590 elt = mon->waitSet;
591 while (elt->waitNext != NULL) {
592 if (elt->waitNext == thread) {
593 elt->waitNext = thread->waitNext;
594 thread->waitNext = NULL;
595 return;
596 }
597 elt = elt->waitNext;
598 }
599}
600
Carl Shapirob4539192010-01-04 16:50:00 -0800601/*
602 * Converts the given relative waiting time into an absolute time.
603 */
Bill Buzbeefccb31d2010-02-04 16:09:55 -0800604void absoluteTime(s8 msec, s4 nsec, struct timespec *ts)
Carl Shapiro77f52eb2009-12-24 19:56:53 -0800605{
606 s8 endSec;
607
608#ifdef HAVE_TIMEDWAIT_MONOTONIC
609 clock_gettime(CLOCK_MONOTONIC, ts);
610#else
611 {
612 struct timeval tv;
613 gettimeofday(&tv, NULL);
614 ts->tv_sec = tv.tv_sec;
615 ts->tv_nsec = tv.tv_usec * 1000;
616 }
617#endif
618 endSec = ts->tv_sec + msec / 1000;
619 if (endSec >= 0x7fffffff) {
620 LOGV("NOTE: end time exceeds epoch\n");
621 endSec = 0x7ffffffe;
622 }
623 ts->tv_sec = endSec;
624 ts->tv_nsec = (ts->tv_nsec + (msec % 1000) * 1000000) + nsec;
625
626 /* catch rollover */
627 if (ts->tv_nsec >= 1000000000L) {
628 ts->tv_sec++;
629 ts->tv_nsec -= 1000000000L;
630 }
631}
632
Bill Buzbeefccb31d2010-02-04 16:09:55 -0800633int dvmRelativeCondWait(pthread_cond_t* cond, pthread_mutex_t* mutex,
634 s8 msec, s4 nsec)
635{
636 int ret;
637 struct timespec ts;
638 absoluteTime(msec, nsec, &ts);
639#if defined(HAVE_TIMEDWAIT_MONOTONIC)
640 ret = pthread_cond_timedwait_monotonic(cond, mutex, &ts);
641#else
642 ret = pthread_cond_timedwait(cond, mutex, &ts);
643#endif
644 assert(ret == 0 || ret == ETIMEDOUT);
645 return ret;
646}
647
Carl Shapiro77f52eb2009-12-24 19:56:53 -0800648/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800649 * Wait on a monitor until timeout, interrupt, or notification. Used for
650 * Object.wait() and (somewhat indirectly) Thread.sleep() and Thread.join().
651 *
652 * If another thread calls Thread.interrupt(), we throw InterruptedException
653 * and return immediately if one of the following are true:
654 * - blocked in wait(), wait(long), or wait(long, int) methods of Object
655 * - blocked in join(), join(long), or join(long, int) methods of Thread
656 * - blocked in sleep(long), or sleep(long, int) methods of Thread
657 * Otherwise, we set the "interrupted" flag.
658 *
659 * Checks to make sure that "nsec" is in the range 0-999999
660 * (i.e. fractions of a millisecond) and throws the appropriate
661 * exception if it isn't.
662 *
663 * The spec allows "spurious wakeups", and recommends that all code using
664 * Object.wait() do so in a loop. This appears to derive from concerns
665 * about pthread_cond_wait() on multiprocessor systems. Some commentary
666 * on the web casts doubt on whether these can/should occur.
667 *
668 * Since we're allowed to wake up "early", we clamp extremely long durations
669 * to return at the end of the 32-bit time epoch.
670 */
671static void waitMonitor(Thread* self, Monitor* mon, s8 msec, s4 nsec,
672 bool interruptShouldThrow)
673{
674 struct timespec ts;
675 bool wasInterrupted = false;
676 bool timed;
Carl Shapiro77f52eb2009-12-24 19:56:53 -0800677 int ret;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800678
Carl Shapiro71938022009-12-22 13:49:53 -0800679 assert(self != NULL);
680 assert(mon != NULL);
681
Carl Shapiro94338aa2009-12-21 11:42:59 -0800682 /* Make sure that we hold the lock. */
Carl Shapiro71938022009-12-22 13:49:53 -0800683 if (mon->owner != self) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800684 dvmThrowException("Ljava/lang/IllegalMonitorStateException;",
685 "object not locked by thread before wait()");
686 return;
687 }
688
689 /*
690 * Enforce the timeout range.
691 */
692 if (msec < 0 || nsec < 0 || nsec > 999999) {
693 dvmThrowException("Ljava/lang/IllegalArgumentException;",
694 "timeout arguments out of range");
695 return;
696 }
697
698 /*
699 * Compute absolute wakeup time, if necessary.
700 */
701 if (msec == 0 && nsec == 0) {
702 timed = false;
703 } else {
Bill Buzbeefccb31d2010-02-04 16:09:55 -0800704 absoluteTime(msec, nsec, &ts);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800705 timed = true;
706 }
707
708 /*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800709 * Add ourselves to the set of threads waiting on this monitor, and
710 * release our hold. We need to let it go even if we're a few levels
711 * deep in a recursive lock, and we need to restore that later.
712 *
Carl Shapiro142ef272010-01-25 12:51:31 -0800713 * We append to the wait set ahead of clearing the count and owner
714 * fields so the subroutine can check that the calling thread owns
715 * the monitor. Aside from that, the order of member updates is
716 * not order sensitive as we hold the pthread mutex.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800717 */
Carl Shapiro142ef272010-01-25 12:51:31 -0800718 waitSetAppend(mon, self);
719 int prevLockCount = mon->lockCount;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800720 mon->lockCount = 0;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800721 mon->owner = NULL;
722
723 /*
724 * Update thread status. If the GC wakes up, it'll ignore us, knowing
725 * that we won't touch any references in this state, and we'll check
726 * our suspend mode before we transition out.
727 */
728 if (timed)
729 dvmChangeStatus(self, THREAD_TIMED_WAIT);
730 else
731 dvmChangeStatus(self, THREAD_WAIT);
732
Carl Shapiro980ffb02010-03-13 22:34:01 -0800733 dvmLockMutex(&self->waitMutex);
Carl Shapiro77f52eb2009-12-24 19:56:53 -0800734
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800735 /*
Carl Shapiro77f52eb2009-12-24 19:56:53 -0800736 * Set waitMonitor to the monitor object we will be waiting on.
737 * When waitMonitor is non-NULL a notifying or interrupting thread
738 * must signal the thread's waitCond to wake it up.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800739 */
Carl Shapiro77f52eb2009-12-24 19:56:53 -0800740 assert(self->waitMonitor == NULL);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800741 self->waitMonitor = mon;
742
743 /*
744 * Handle the case where the thread was interrupted before we called
745 * wait().
746 */
747 if (self->interrupted) {
748 wasInterrupted = true;
Carl Shapiro77f52eb2009-12-24 19:56:53 -0800749 self->waitMonitor = NULL;
Carl Shapiro980ffb02010-03-13 22:34:01 -0800750 dvmUnlockMutex(&self->waitMutex);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800751 goto done;
752 }
753
Carl Shapiro77f52eb2009-12-24 19:56:53 -0800754 /*
755 * Release the monitor lock and wait for a notification or
756 * a timeout to occur.
757 */
Carl Shapiro980ffb02010-03-13 22:34:01 -0800758 dvmUnlockMutex(&mon->lock);
Carl Shapiro77f52eb2009-12-24 19:56:53 -0800759
760 if (!timed) {
761 ret = pthread_cond_wait(&self->waitCond, &self->waitMutex);
762 assert(ret == 0);
763 } else {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800764#ifdef HAVE_TIMEDWAIT_MONOTONIC
Carl Shapiro77f52eb2009-12-24 19:56:53 -0800765 ret = pthread_cond_timedwait_monotonic(&self->waitCond, &self->waitMutex, &ts);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800766#else
Carl Shapiro77f52eb2009-12-24 19:56:53 -0800767 ret = pthread_cond_timedwait(&self->waitCond, &self->waitMutex, &ts);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800768#endif
Carl Shapiro77f52eb2009-12-24 19:56:53 -0800769 assert(ret == 0 || ret == ETIMEDOUT);
770 }
771 if (self->interrupted) {
772 wasInterrupted = true;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800773 }
774
Carl Shapiro77f52eb2009-12-24 19:56:53 -0800775 self->interrupted = false;
776 self->waitMonitor = NULL;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800777
Carl Shapiro980ffb02010-03-13 22:34:01 -0800778 dvmUnlockMutex(&self->waitMutex);
Carl Shapiro77f52eb2009-12-24 19:56:53 -0800779
Carl Shapiro30aa9972010-01-13 22:07:50 -0800780 /* Reacquire the monitor lock. */
Carl Shapiro77f52eb2009-12-24 19:56:53 -0800781 lockMonitor(self, mon);
782
Carl Shapiro142ef272010-01-25 12:51:31 -0800783done:
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800784 /*
Carl Shapiro07b35922010-01-25 14:48:30 -0800785 * We remove our thread from wait set after restoring the count
786 * and owner fields so the subroutine can check that the calling
787 * thread owns the monitor. Aside from that, the order of member
788 * updates is not order sensitive as we hold the pthread mutex.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800789 */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800790 mon->owner = self;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800791 mon->lockCount = prevLockCount;
Carl Shapiro07b35922010-01-25 14:48:30 -0800792 waitSetRemove(mon, self);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800793
794 /* set self->status back to THREAD_RUNNING, and self-suspend if needed */
795 dvmChangeStatus(self, THREAD_RUNNING);
796
797 if (wasInterrupted) {
798 /*
799 * We were interrupted while waiting, or somebody interrupted an
Carl Shapiro30aa9972010-01-13 22:07:50 -0800800 * un-interruptible thread earlier and we're bailing out immediately.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800801 *
802 * The doc sayeth: "The interrupted status of the current thread is
803 * cleared when this exception is thrown."
804 */
805 self->interrupted = false;
806 if (interruptShouldThrow)
807 dvmThrowException("Ljava/lang/InterruptedException;", NULL);
808 }
809}
810
811/*
812 * Notify one thread waiting on this monitor.
813 */
814static void notifyMonitor(Thread* self, Monitor* mon)
815{
Carl Shapiro77f52eb2009-12-24 19:56:53 -0800816 Thread* thread;
Carl Shapiro77f52eb2009-12-24 19:56:53 -0800817
Carl Shapiro71938022009-12-22 13:49:53 -0800818 assert(self != NULL);
819 assert(mon != NULL);
820
Carl Shapiro94338aa2009-12-21 11:42:59 -0800821 /* Make sure that we hold the lock. */
Carl Shapiro71938022009-12-22 13:49:53 -0800822 if (mon->owner != self) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800823 dvmThrowException("Ljava/lang/IllegalMonitorStateException;",
824 "object not locked by thread before notify()");
825 return;
826 }
Carl Shapiro30aa9972010-01-13 22:07:50 -0800827 /* Signal the first waiting thread in the wait set. */
828 while (mon->waitSet != NULL) {
Carl Shapiro77f52eb2009-12-24 19:56:53 -0800829 thread = mon->waitSet;
830 mon->waitSet = thread->waitNext;
831 thread->waitNext = NULL;
Carl Shapiro980ffb02010-03-13 22:34:01 -0800832 dvmLockMutex(&thread->waitMutex);
Carl Shapiro77f52eb2009-12-24 19:56:53 -0800833 /* Check to see if the thread is still waiting. */
834 if (thread->waitMonitor != NULL) {
835 pthread_cond_signal(&thread->waitCond);
Carl Shapiro980ffb02010-03-13 22:34:01 -0800836 dvmUnlockMutex(&thread->waitMutex);
Carl Shapiro30aa9972010-01-13 22:07:50 -0800837 return;
Carl Shapiro77f52eb2009-12-24 19:56:53 -0800838 }
Carl Shapiro980ffb02010-03-13 22:34:01 -0800839 dvmUnlockMutex(&thread->waitMutex);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800840 }
841}
842
843/*
844 * Notify all threads waiting on this monitor.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800845 */
846static void notifyAllMonitor(Thread* self, Monitor* mon)
847{
Carl Shapiro77f52eb2009-12-24 19:56:53 -0800848 Thread* thread;
Carl Shapiro77f52eb2009-12-24 19:56:53 -0800849
Carl Shapiro71938022009-12-22 13:49:53 -0800850 assert(self != NULL);
851 assert(mon != NULL);
852
Carl Shapiro94338aa2009-12-21 11:42:59 -0800853 /* Make sure that we hold the lock. */
Carl Shapiro71938022009-12-22 13:49:53 -0800854 if (mon->owner != self) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800855 dvmThrowException("Ljava/lang/IllegalMonitorStateException;",
856 "object not locked by thread before notifyAll()");
857 return;
858 }
Carl Shapiro77f52eb2009-12-24 19:56:53 -0800859 /* Signal all threads in the wait set. */
860 while (mon->waitSet != NULL) {
861 thread = mon->waitSet;
862 mon->waitSet = thread->waitNext;
863 thread->waitNext = NULL;
Carl Shapiro980ffb02010-03-13 22:34:01 -0800864 dvmLockMutex(&thread->waitMutex);
Carl Shapiro77f52eb2009-12-24 19:56:53 -0800865 /* Check to see if the thread is still waiting. */
866 if (thread->waitMonitor != NULL) {
867 pthread_cond_signal(&thread->waitCond);
868 }
Carl Shapiro980ffb02010-03-13 22:34:01 -0800869 dvmUnlockMutex(&thread->waitMutex);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800870 }
871}
872
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800873/*
Carl Shapiro66bb7df2010-03-12 15:25:37 -0800874 * Changes the shape of a monitor from thin to fat, preserving the
875 * internal lock state. The calling thread must own the lock.
876 */
877static void inflateMonitor(Thread *self, Object *obj)
878{
879 Monitor *mon;
880 u4 thin;
881
882 assert(self != NULL);
883 assert(obj != NULL);
884 assert(LW_SHAPE(obj->lock) == LW_SHAPE_THIN);
885 assert(LW_LOCK_OWNER(obj->lock) == self->threadId);
886 /* Allocate and acquire a new monitor. */
887 mon = dvmCreateMonitor(obj);
888 lockMonitor(self, mon);
889 /* Propagate the lock state. */
890 thin = obj->lock;
891 mon->lockCount = LW_LOCK_COUNT(thin);
892 thin &= LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT;
893 thin |= (u4)mon | LW_SHAPE_FAT;
894 /* Publish the updated lock word. */
895 MEM_BARRIER();
896 obj->lock = thin;
897}
898
899/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800900 * Implements monitorenter for "synchronized" stuff.
901 *
902 * This does not fail or throw an exception (unless deadlock prediction
903 * is enabled and set to "err" mode).
904 */
905void dvmLockObject(Thread* self, Object *obj)
906{
Carl Shapiro147dd3f2010-03-08 14:38:42 -0800907 volatile u4 *thinp;
Carl Shapiro147dd3f2010-03-08 14:38:42 -0800908 ThreadStatus oldStatus;
909 useconds_t sleepDelay;
910 const useconds_t maxSleepDelay = 1 << 20;
911 u4 thin, newThin, threadId;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800912
Carl Shapiro147dd3f2010-03-08 14:38:42 -0800913 assert(self != NULL);
914 assert(obj != NULL);
915 threadId = self->threadId;
916 thinp = &obj->lock;
917retry:
918 thin = *thinp;
919 if (LW_SHAPE(thin) == LW_SHAPE_THIN) {
920 /*
921 * The lock is a thin lock. The owner field is used to
922 * determine the acquire method, ordered by cost.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800923 */
Carl Shapiro147dd3f2010-03-08 14:38:42 -0800924 if (LW_LOCK_OWNER(thin) == threadId) {
925 /*
926 * The calling thread owns the lock. Increment the
927 * value of the recursion count field.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800928 */
Carl Shapiro147dd3f2010-03-08 14:38:42 -0800929 obj->lock += 1 << LW_LOCK_COUNT_SHIFT;
930 } else if (LW_LOCK_OWNER(thin) == 0) {
931 /*
932 * The lock is unowned. Install the thread id of the
933 * calling thread into the owner field. This is the
934 * common case. In performance critical code the JIT
935 * will have tried this before calling out to the VM.
936 */
937 newThin = thin | (threadId << LW_LOCK_OWNER_SHIFT);
938 if (!ATOMIC_CMP_SWAP((int32_t *)thinp, thin, newThin)) {
939 /*
940 * The acquire failed. Try again.
941 */
942 goto retry;
943 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800944 } else {
Carl Shapiro147dd3f2010-03-08 14:38:42 -0800945 LOG_THIN("(%d) spin on lock %p: %#x (%#x) %#x",
946 threadId, &obj->lock, 0, *thinp, thin);
947 /*
948 * The lock is owned by another thread. Notify the VM
949 * that we are about to wait.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800950 */
Carl Shapiro147dd3f2010-03-08 14:38:42 -0800951 oldStatus = dvmChangeStatus(self, THREAD_MONITOR);
952 /*
953 * Spin until the thin lock is released or inflated.
954 */
955 sleepDelay = 0;
956 for (;;) {
957 thin = *thinp;
958 /*
959 * Check the shape of the lock word. Another thread
960 * may have inflated the lock while we were waiting.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800961 */
Carl Shapiro147dd3f2010-03-08 14:38:42 -0800962 if (LW_SHAPE(thin) == LW_SHAPE_THIN) {
963 if (LW_LOCK_OWNER(thin) == 0) {
964 /*
965 * The lock has been released. Install the
966 * thread id of the calling thread into the
967 * owner field.
968 */
969 newThin = thin | (threadId << LW_LOCK_OWNER_SHIFT);
970 if (ATOMIC_CMP_SWAP((int32_t *)thinp,
971 thin, newThin)) {
972 /*
973 * The acquire succeed. Break out of the
974 * loop and proceed to inflate the lock.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800975 */
Carl Shapiro147dd3f2010-03-08 14:38:42 -0800976 break;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800977 }
Carl Shapiro147dd3f2010-03-08 14:38:42 -0800978 } else {
979 /*
980 * The lock has not been released. Yield so
981 * the owning thread can run.
982 */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800983 if (sleepDelay == 0) {
984 sched_yield();
Carl Shapiro147dd3f2010-03-08 14:38:42 -0800985 sleepDelay = 1000;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800986 } else {
987 usleep(sleepDelay);
988 if (sleepDelay < maxSleepDelay / 2) {
989 sleepDelay *= 2;
990 }
991 }
992 }
Carl Shapiro147dd3f2010-03-08 14:38:42 -0800993 } else {
994 /*
995 * The thin lock was inflated by another thread.
996 * Let the VM know we are no longer waiting and
997 * try again.
998 */
999 LOG_THIN("(%d) lock %p surprise-fattened",
1000 threadId, &obj->lock);
1001 dvmChangeStatus(self, oldStatus);
1002 goto retry;
1003 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001004 }
Carl Shapiro147dd3f2010-03-08 14:38:42 -08001005 LOG_THIN("(%d) spin on lock done %p: %#x (%#x) %#x",
1006 threadId, &obj->lock, 0, *thinp, thin);
1007 /*
1008 * We have acquired the thin lock. Let the VM know that
1009 * we are no longer waiting.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001010 */
Carl Shapiro147dd3f2010-03-08 14:38:42 -08001011 dvmChangeStatus(self, oldStatus);
1012 /*
1013 * Fatten the lock.
1014 */
Carl Shapiro66bb7df2010-03-12 15:25:37 -08001015 inflateMonitor(self, obj);
Carl Shapiro147dd3f2010-03-08 14:38:42 -08001016 LOG_THIN("(%d) lock %p fattened", threadId, &obj->lock);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001017 }
Carl Shapiro147dd3f2010-03-08 14:38:42 -08001018 } else {
1019 /*
1020 * The lock is a fat lock.
1021 */
1022 assert(LW_MONITOR(obj->lock) != NULL);
1023 lockMonitor(self, LW_MONITOR(obj->lock));
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001024 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001025#ifdef WITH_DEADLOCK_PREDICTION
1026 /*
1027 * See if we were allowed to grab the lock at this time. We do it
1028 * *after* acquiring the lock, rather than before, so that we can
1029 * freely update the Monitor struct. This seems counter-intuitive,
1030 * but our goal is deadlock *prediction* not deadlock *prevention*.
1031 * (If we actually deadlock, the situation is easy to diagnose from
1032 * a thread dump, so there's no point making a special effort to do
1033 * the checks before the lock is held.)
1034 *
1035 * This needs to happen before we add the object to the thread's
1036 * monitor list, so we can tell the difference between first-lock and
1037 * re-lock.
1038 *
1039 * It's also important that we do this while in THREAD_RUNNING, so
1040 * that we don't interfere with cleanup operations in the GC.
1041 */
1042 if (gDvm.deadlockPredictMode != kDPOff) {
1043 if (self->status != THREAD_RUNNING) {
1044 LOGE("Bad thread status (%d) in DP\n", self->status);
1045 dvmDumpThread(self, false);
1046 dvmAbort();
1047 }
1048 assert(!dvmCheckException(self));
1049 updateDeadlockPrediction(self, obj);
1050 if (dvmCheckException(self)) {
1051 /*
1052 * If we're throwing an exception here, we need to free the
1053 * lock. We add the object to the thread's monitor list so the
1054 * "unlock" code can remove it.
1055 */
1056 dvmAddToMonitorList(self, obj, false);
1057 dvmUnlockObject(self, obj);
1058 LOGV("--- unlocked, pending is '%s'\n",
1059 dvmGetException(self)->clazz->descriptor);
1060 }
1061 }
1062
1063 /*
1064 * Add the locked object, and the current stack trace, to the list
1065 * held by the Thread object. If deadlock prediction isn't on,
1066 * don't capture the stack trace.
1067 */
1068 dvmAddToMonitorList(self, obj, gDvm.deadlockPredictMode != kDPOff);
1069#elif defined(WITH_MONITOR_TRACKING)
1070 /*
1071 * Add the locked object to the list held by the Thread object.
1072 */
1073 dvmAddToMonitorList(self, obj, false);
1074#endif
1075}
1076
1077/*
1078 * Implements monitorexit for "synchronized" stuff.
1079 *
1080 * On failure, throws an exception and returns "false".
1081 */
1082bool dvmUnlockObject(Thread* self, Object *obj)
1083{
Carl Shapiro94338aa2009-12-21 11:42:59 -08001084 u4 thin;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001085
Carl Shapiroef5b4d32010-01-26 13:22:04 -08001086 assert(self != NULL);
1087 assert(self->status == THREAD_RUNNING);
1088 assert(obj != NULL);
Carl Shapiroef5b4d32010-01-26 13:22:04 -08001089 /*
1090 * Cache the lock word as its value can change while we are
1091 * examining its state.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001092 */
Carl Shapiro147dd3f2010-03-08 14:38:42 -08001093 thin = obj->lock;
Carl Shapiroef5b4d32010-01-26 13:22:04 -08001094 if (LW_SHAPE(thin) == LW_SHAPE_THIN) {
1095 /*
1096 * The lock is thin. We must ensure that the lock is owned
1097 * by the given thread before unlocking it.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001098 */
Carl Shapiroef5b4d32010-01-26 13:22:04 -08001099 if (LW_LOCK_OWNER(thin) == self->threadId) {
1100 /*
1101 * We are the lock owner. It is safe to update the lock
1102 * without CAS as lock ownership guards the lock itself.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001103 */
Carl Shapiroef5b4d32010-01-26 13:22:04 -08001104 if (LW_LOCK_COUNT(thin) == 0) {
1105 /*
1106 * The lock was not recursively acquired, the common
1107 * case. Unlock by clearing all bits except for the
1108 * hash state.
1109 */
Carl Shapiro147dd3f2010-03-08 14:38:42 -08001110 obj->lock &= (LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT);
Carl Shapiroef5b4d32010-01-26 13:22:04 -08001111 } else {
1112 /*
1113 * The object was recursively acquired. Decrement the
1114 * lock recursion count field.
1115 */
Carl Shapiro147dd3f2010-03-08 14:38:42 -08001116 obj->lock -= 1 << LW_LOCK_COUNT_SHIFT;
Carl Shapiroef5b4d32010-01-26 13:22:04 -08001117 }
1118 } else {
1119 /*
1120 * We do not own the lock. The JVM spec requires that we
1121 * throw an exception in this case.
1122 */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001123 dvmThrowException("Ljava/lang/IllegalMonitorStateException;",
Carl Shapiroef5b4d32010-01-26 13:22:04 -08001124 "unlock of unowned monitor");
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001125 return false;
1126 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001127 } else {
Carl Shapiroef5b4d32010-01-26 13:22:04 -08001128 /*
1129 * The lock is fat. We must check to see if unlockMonitor has
1130 * raised any exceptions before continuing.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001131 */
Carl Shapiro8d7f9b22009-12-21 20:23:45 -08001132 assert(LW_MONITOR(obj->lock) != NULL);
1133 if (!unlockMonitor(self, LW_MONITOR(obj->lock))) {
Carl Shapiroef5b4d32010-01-26 13:22:04 -08001134 /*
1135 * An exception has been raised. Do not fall through.
1136 */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001137 return false;
1138 }
1139 }
1140
1141#ifdef WITH_MONITOR_TRACKING
1142 /*
1143 * Remove the object from the Thread's list.
1144 */
1145 dvmRemoveFromMonitorList(self, obj);
1146#endif
1147
1148 return true;
1149}
1150
1151/*
1152 * Object.wait(). Also called for class init.
1153 */
1154void dvmObjectWait(Thread* self, Object *obj, s8 msec, s4 nsec,
1155 bool interruptShouldThrow)
1156{
Carl Shapiro66bb7df2010-03-12 15:25:37 -08001157 Monitor* mon;
Carl Shapiro8d7f9b22009-12-21 20:23:45 -08001158 u4 thin = obj->lock;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001159
1160 /* If the lock is still thin, we need to fatten it.
1161 */
Carl Shapiro94338aa2009-12-21 11:42:59 -08001162 if (LW_SHAPE(thin) == LW_SHAPE_THIN) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001163 /* Make sure that 'self' holds the lock.
1164 */
Carl Shapiro94338aa2009-12-21 11:42:59 -08001165 if (LW_LOCK_OWNER(thin) != self->threadId) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001166 dvmThrowException("Ljava/lang/IllegalMonitorStateException;",
1167 "object not locked by thread before wait()");
1168 return;
1169 }
1170
1171 /* This thread holds the lock. We need to fatten the lock
1172 * so 'self' can block on it. Don't update the object lock
1173 * field yet, because 'self' needs to acquire the lock before
1174 * any other thread gets a chance.
1175 */
Carl Shapiro66bb7df2010-03-12 15:25:37 -08001176 inflateMonitor(self, obj);
1177 LOG_THIN("(%d) lock %p fattened by wait() to count %d",
1178 self->threadId, &obj->lock, mon->lockCount);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001179 }
Carl Shapiro66bb7df2010-03-12 15:25:37 -08001180 mon = LW_MONITOR(obj->lock);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001181 waitMonitor(self, mon, msec, nsec, interruptShouldThrow);
1182}
1183
1184/*
1185 * Object.notify().
1186 */
1187void dvmObjectNotify(Thread* self, Object *obj)
1188{
Carl Shapiro8d7f9b22009-12-21 20:23:45 -08001189 u4 thin = obj->lock;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001190
1191 /* If the lock is still thin, there aren't any waiters;
1192 * waiting on an object forces lock fattening.
1193 */
Carl Shapiro94338aa2009-12-21 11:42:59 -08001194 if (LW_SHAPE(thin) == LW_SHAPE_THIN) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001195 /* Make sure that 'self' holds the lock.
1196 */
Carl Shapiro94338aa2009-12-21 11:42:59 -08001197 if (LW_LOCK_OWNER(thin) != self->threadId) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001198 dvmThrowException("Ljava/lang/IllegalMonitorStateException;",
1199 "object not locked by thread before notify()");
1200 return;
1201 }
1202
1203 /* no-op; there are no waiters to notify.
1204 */
1205 } else {
1206 /* It's a fat lock.
1207 */
Carl Shapiro94338aa2009-12-21 11:42:59 -08001208 notifyMonitor(self, LW_MONITOR(thin));
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001209 }
1210}
1211
1212/*
1213 * Object.notifyAll().
1214 */
1215void dvmObjectNotifyAll(Thread* self, Object *obj)
1216{
Carl Shapiro8d7f9b22009-12-21 20:23:45 -08001217 u4 thin = obj->lock;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001218
1219 /* If the lock is still thin, there aren't any waiters;
1220 * waiting on an object forces lock fattening.
1221 */
Carl Shapiro94338aa2009-12-21 11:42:59 -08001222 if (LW_SHAPE(thin) == LW_SHAPE_THIN) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001223 /* Make sure that 'self' holds the lock.
1224 */
Carl Shapiro94338aa2009-12-21 11:42:59 -08001225 if (LW_LOCK_OWNER(thin) != self->threadId) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001226 dvmThrowException("Ljava/lang/IllegalMonitorStateException;",
1227 "object not locked by thread before notifyAll()");
1228 return;
1229 }
1230
1231 /* no-op; there are no waiters to notify.
1232 */
1233 } else {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001234 /* It's a fat lock.
1235 */
Carl Shapiro94338aa2009-12-21 11:42:59 -08001236 notifyAllMonitor(self, LW_MONITOR(thin));
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001237 }
1238}
1239
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001240/*
1241 * This implements java.lang.Thread.sleep(long msec, int nsec).
1242 *
1243 * The sleep is interruptible by other threads, which means we can't just
1244 * plop into an OS sleep call. (We probably could if we wanted to send
1245 * signals around and rely on EINTR, but that's inefficient and relies
1246 * on native code respecting our signal mask.)
1247 *
1248 * We have to do all of this stuff for Object.wait() as well, so it's
1249 * easiest to just sleep on a private Monitor.
1250 *
1251 * It appears that we want sleep(0,0) to go through the motions of sleeping
1252 * for a very short duration, rather than just returning.
1253 */
1254void dvmThreadSleep(u8 msec, u4 nsec)
1255{
1256 Thread* self = dvmThreadSelf();
1257 Monitor* mon = gDvm.threadSleepMon;
1258
1259 /* sleep(0,0) wakes up immediately, wait(0,0) means wait forever; adjust */
1260 if (msec == 0 && nsec == 0)
1261 nsec++;
1262
1263 lockMonitor(self, mon);
1264 waitMonitor(self, mon, msec, nsec, true);
1265 unlockMonitor(self, mon);
1266}
1267
1268/*
1269 * Implement java.lang.Thread.interrupt().
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001270 */
Carl Shapiro77f52eb2009-12-24 19:56:53 -08001271void dvmThreadInterrupt(Thread* thread)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001272{
Carl Shapiro77f52eb2009-12-24 19:56:53 -08001273 assert(thread != NULL);
1274
Carl Shapiro980ffb02010-03-13 22:34:01 -08001275 dvmLockMutex(&thread->waitMutex);
Carl Shapiro77f52eb2009-12-24 19:56:53 -08001276
1277 /*
1278 * If the interrupted flag is already set no additional action is
1279 * required.
1280 */
1281 if (thread->interrupted == true) {
Carl Shapiro980ffb02010-03-13 22:34:01 -08001282 dvmUnlockMutex(&thread->waitMutex);
Carl Shapiro77f52eb2009-12-24 19:56:53 -08001283 return;
1284 }
1285
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001286 /*
1287 * Raise the "interrupted" flag. This will cause it to bail early out
1288 * of the next wait() attempt, if it's not currently waiting on
1289 * something.
1290 */
1291 thread->interrupted = true;
1292 MEM_BARRIER();
1293
1294 /*
1295 * Is the thread waiting?
1296 *
1297 * Note that fat vs. thin doesn't matter here; waitMonitor
1298 * is only set when a thread actually waits on a monitor,
1299 * which implies that the monitor has already been fattened.
1300 */
Carl Shapiro77f52eb2009-12-24 19:56:53 -08001301 if (thread->waitMonitor != NULL) {
1302 pthread_cond_signal(&thread->waitCond);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001303 }
1304
Carl Shapiro980ffb02010-03-13 22:34:01 -08001305 dvmUnlockMutex(&thread->waitMutex);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001306}
1307
Carl Shapiro30aa9972010-01-13 22:07:50 -08001308#ifndef WITH_COPYING_GC
Carl Shapiro94338aa2009-12-21 11:42:59 -08001309u4 dvmIdentityHashCode(Object *obj)
1310{
1311 return (u4)obj;
1312}
Carl Shapiro30aa9972010-01-13 22:07:50 -08001313#else
Carl Shapiro30aa9972010-01-13 22:07:50 -08001314/*
1315 * Returns the identity hash code of the given object.
1316 */
1317u4 dvmIdentityHashCode(Object *obj)
1318{
1319 Thread *self, *thread;
1320 volatile u4 *lw;
Carl Shapirobfe4dcc2010-04-16 17:55:27 -07001321 size_t size;
Carl Shapiro30aa9972010-01-13 22:07:50 -08001322 u4 lock, owner, hashState;
1323
1324 if (obj == NULL) {
1325 /*
1326 * Null is defined to have an identity hash code of 0.
1327 */
1328 return 0;
1329 }
1330 lw = &obj->lock;
1331retry:
1332 hashState = LW_HASH_STATE(*lw);
1333 if (hashState == LW_HASH_STATE_HASHED) {
1334 /*
1335 * The object has been hashed but has not had its hash code
1336 * relocated by the garbage collector. Use the raw object
1337 * address.
1338 */
1339 return (u4)obj >> 3;
1340 } else if (hashState == LW_HASH_STATE_HASHED_AND_MOVED) {
1341 /*
1342 * The object has been hashed and its hash code has been
1343 * relocated by the collector. Use the value of the naturally
1344 * aligned word following the instance data.
1345 */
Carl Shapiroc48b6d02010-05-04 11:19:53 -07001346 assert(obj->clazz != gDvm.classJavaLangClass);
1347 assert(obj->clazz != gDvm.unlinkedJavaLangClass);
Carl Shapiro30aa9972010-01-13 22:07:50 -08001348 if (IS_CLASS_FLAG_SET(obj->clazz, CLASS_ISARRAY)) {
Carl Shapirobfe4dcc2010-04-16 17:55:27 -07001349 size = dvmArrayObjectSize((ArrayObject *)obj);
Carl Shapiroc48b6d02010-05-04 11:19:53 -07001350 size = (size + 2) & ~2;
Carl Shapiro30aa9972010-01-13 22:07:50 -08001351 } else {
Carl Shapirobfe4dcc2010-04-16 17:55:27 -07001352 size = obj->clazz->objectSize;
Carl Shapiro30aa9972010-01-13 22:07:50 -08001353 }
Carl Shapirobfe4dcc2010-04-16 17:55:27 -07001354 return *(u4 *)(((char *)obj) + size);
Carl Shapiro30aa9972010-01-13 22:07:50 -08001355 } else if (hashState == LW_HASH_STATE_UNHASHED) {
1356 /*
1357 * The object has never been hashed. Change the hash state to
1358 * hashed and use the raw object address.
1359 */
1360 self = dvmThreadSelf();
1361 if (self->threadId == lockOwner(obj)) {
1362 /*
1363 * We already own the lock so we can update the hash state
1364 * directly.
1365 */
1366 *lw |= (LW_HASH_STATE_HASHED << LW_HASH_STATE_SHIFT);
1367 return (u4)obj >> 3;
1368 }
1369 /*
1370 * We do not own the lock. Try acquiring the lock. Should
1371 * this fail, we must suspend the owning thread.
1372 */
1373 if (LW_SHAPE(*lw) == LW_SHAPE_THIN) {
1374 /*
1375 * If the lock is thin assume it is unowned. We simulate
1376 * an acquire, update, and release with a single CAS.
1377 */
1378 lock = DVM_LOCK_INITIAL_THIN_VALUE;
1379 lock |= (LW_HASH_STATE_HASHED << LW_HASH_STATE_SHIFT);
1380 if (ATOMIC_CMP_SWAP((int32_t *)lw,
1381 (int32_t)DVM_LOCK_INITIAL_THIN_VALUE,
1382 (int32_t)lock)) {
1383 /*
1384 * A new lockword has been installed with a hash state
1385 * of hashed. Use the raw object address.
1386 */
1387 return (u4)obj >> 3;
1388 }
1389 } else {
1390 if (tryLockMonitor(self, LW_MONITOR(*lw))) {
1391 /*
1392 * The monitor lock has been acquired. Change the
1393 * hash state to hashed and use the raw object
1394 * address.
1395 */
1396 *lw |= (LW_HASH_STATE_HASHED << LW_HASH_STATE_SHIFT);
1397 unlockMonitor(self, LW_MONITOR(*lw));
1398 return (u4)obj >> 3;
1399 }
1400 }
1401 /*
1402 * At this point we have failed to acquire the lock. We must
1403 * identify the owning thread and suspend it.
1404 */
1405 dvmLockThreadList(self);
1406 /*
1407 * Cache the lock word as its value can change between
1408 * determining its shape and retrieving its owner.
1409 */
1410 lock = *lw;
1411 if (LW_SHAPE(lock) == LW_SHAPE_THIN) {
1412 /*
1413 * Find the thread with the corresponding thread id.
1414 */
1415 owner = LW_LOCK_OWNER(lock);
1416 assert(owner != self->threadId);
1417 /*
1418 * If the lock has no owner do not bother scanning the
1419 * thread list and fall through to the failure handler.
1420 */
1421 thread = owner ? gDvm.threadList : NULL;
1422 while (thread != NULL) {
1423 if (thread->threadId == owner) {
1424 break;
1425 }
1426 thread = thread->next;
1427 }
1428 } else {
1429 thread = LW_MONITOR(lock)->owner;
1430 }
1431 /*
1432 * If thread is NULL the object has been released since the
1433 * thread list lock was acquired. Try again.
1434 */
1435 if (thread == NULL) {
1436 dvmUnlockThreadList();
1437 goto retry;
1438 }
1439 /*
1440 * Wait for the owning thread to suspend.
1441 */
1442 dvmSuspendThread(thread);
1443 if (dvmHoldsLock(thread, obj)) {
1444 /*
1445 * The owning thread has been suspended. We can safely
1446 * change the hash state to hashed.
1447 */
1448 *lw |= (LW_HASH_STATE_HASHED << LW_HASH_STATE_SHIFT);
1449 dvmResumeThread(thread);
1450 dvmUnlockThreadList();
1451 return (u4)obj >> 3;
1452 }
1453 /*
1454 * The wrong thread has been suspended. Try again.
1455 */
1456 dvmResumeThread(thread);
1457 dvmUnlockThreadList();
1458 goto retry;
1459 }
1460 LOGE("object %p has an unknown hash state %#x", obj, hashState);
1461 dvmDumpThread(dvmThreadSelf(), false);
1462 dvmAbort();
1463 return 0; /* Quiet the compiler. */
1464}
1465#endif /* WITH_COPYING_GC */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001466
1467#ifdef WITH_DEADLOCK_PREDICTION
1468/*
1469 * ===========================================================================
1470 * Deadlock prediction
1471 * ===========================================================================
1472 */
1473/*
1474The idea is to predict the possibility of deadlock by recording the order
1475in which monitors are acquired. If we see an attempt to acquire a lock
1476out of order, we can identify the locks and offending code.
1477
1478To make this work, we need to keep track of the locks held by each thread,
1479and create history trees for each lock. When a thread tries to acquire
1480a new lock, we walk through the "history children" of the lock, looking
1481for a match with locks the thread already holds. If we find a match,
1482it means the thread has made a request that could result in a deadlock.
1483
1484To support recursive locks, we always allow re-locking a currently-held
1485lock, and maintain a recursion depth count.
1486
1487An ASCII-art example, where letters represent Objects:
1488
1489 A
1490 /|\
1491 / | \
1492 B | D
1493 \ |
1494 \|
1495 C
1496
1497The above is the tree we'd have after handling Object synchronization
1498sequences "ABC", "AC", "AD". A has three children, {B, C, D}. C is also
1499a child of B. (The lines represent pointers between parent and child.
1500Every node can have multiple parents and multiple children.)
1501
1502If we hold AC, and want to lock B, we recursively search through B's
1503children to see if A or C appears. It does, so we reject the attempt.
1504(A straightforward way to implement it: add a link from C to B, then
1505determine whether the graph starting at B contains a cycle.)
1506
1507If we hold AC and want to lock D, we would succeed, creating a new link
1508from C to D.
1509
1510The lock history and a stack trace is attached to the Object's Monitor
1511struct, which means we need to fatten every Object we lock (thin locking
1512is effectively disabled). If we don't need the stack trace we can
1513avoid fattening the leaf nodes, only fattening objects that need to hold
1514history trees.
1515
1516Updates to Monitor structs are only allowed for the thread that holds
1517the Monitor, so we actually do most of our deadlock prediction work after
1518the lock has been acquired.
1519
1520When an object with a monitor is GCed, we need to remove it from the
1521history trees. There are two basic approaches:
1522 (1) For through the entire set of known monitors, search all child
1523 lists for the object in question. This is rather slow, resulting
1524 in GC passes that take upwards of 10 seconds to complete.
1525 (2) Maintain "parent" pointers in each node. Remove the entries as
1526 required. This requires additional storage and maintenance for
1527 every operation, but is significantly faster at GC time.
1528For each GCed object, we merge all of the object's children into each of
1529the object's parents.
1530*/
1531
1532#if !defined(WITH_MONITOR_TRACKING)
1533# error "WITH_DEADLOCK_PREDICTION requires WITH_MONITOR_TRACKING"
1534#endif
1535
1536/*
1537 * Clear out the contents of an ExpandingObjectList, freeing any
1538 * dynamic allocations.
1539 */
1540static void expandObjClear(ExpandingObjectList* pList)
1541{
1542 if (pList->list != NULL) {
1543 free(pList->list);
1544 pList->list = NULL;
1545 }
1546 pList->alloc = pList->count = 0;
1547}
1548
1549/*
1550 * Get the number of objects currently stored in the list.
1551 */
1552static inline int expandBufGetCount(const ExpandingObjectList* pList)
1553{
1554 return pList->count;
1555}
1556
1557/*
1558 * Get the Nth entry from the list.
1559 */
1560static inline Object* expandBufGetEntry(const ExpandingObjectList* pList,
1561 int i)
1562{
1563 return pList->list[i];
1564}
1565
1566/*
1567 * Add a new entry to the list.
1568 *
1569 * We don't check for or try to enforce uniqueness. It's expected that
1570 * the higher-level code does this for us.
1571 */
1572static void expandObjAddEntry(ExpandingObjectList* pList, Object* obj)
1573{
1574 if (pList->count == pList->alloc) {
1575 /* time to expand */
1576 Object** newList;
1577
1578 if (pList->alloc == 0)
1579 pList->alloc = 4;
1580 else
1581 pList->alloc *= 2;
1582 LOGVV("expanding %p to %d\n", pList, pList->alloc);
1583 newList = realloc(pList->list, pList->alloc * sizeof(Object*));
1584 if (newList == NULL) {
1585 LOGE("Failed expanding DP object list (alloc=%d)\n", pList->alloc);
1586 dvmAbort();
1587 }
1588 pList->list = newList;
1589 }
1590
1591 pList->list[pList->count++] = obj;
1592}
1593
1594/*
1595 * Returns "true" if the element was successfully removed.
1596 */
1597static bool expandObjRemoveEntry(ExpandingObjectList* pList, Object* obj)
1598{
1599 int i;
1600
1601 for (i = pList->count-1; i >= 0; i--) {
1602 if (pList->list[i] == obj)
1603 break;
1604 }
1605 if (i < 0)
1606 return false;
1607
1608 if (i != pList->count-1) {
1609 /*
1610 * The order of elements is not important, so we just copy the
1611 * last entry into the new slot.
1612 */
1613 //memmove(&pList->list[i], &pList->list[i+1],
1614 // (pList->count-1 - i) * sizeof(pList->list[0]));
1615 pList->list[i] = pList->list[pList->count-1];
1616 }
1617
1618 pList->count--;
1619 pList->list[pList->count] = (Object*) 0xdecadead;
1620 return true;
1621}
1622
1623/*
1624 * Returns "true" if "obj" appears in the list.
1625 */
1626static bool expandObjHas(const ExpandingObjectList* pList, Object* obj)
1627{
1628 int i;
1629
1630 for (i = 0; i < pList->count; i++) {
1631 if (pList->list[i] == obj)
1632 return true;
1633 }
1634 return false;
1635}
1636
1637/*
1638 * Print the list contents to stdout. For debugging.
1639 */
1640static void expandObjDump(const ExpandingObjectList* pList)
1641{
1642 int i;
1643 for (i = 0; i < pList->count; i++)
1644 printf(" %p", pList->list[i]);
1645}
1646
1647/*
1648 * Check for duplicate entries. Returns the index of the first instance
1649 * of the duplicated value, or -1 if no duplicates were found.
1650 */
1651static int expandObjCheckForDuplicates(const ExpandingObjectList* pList)
1652{
1653 int i, j;
1654 for (i = 0; i < pList->count-1; i++) {
1655 for (j = i + 1; j < pList->count; j++) {
1656 if (pList->list[i] == pList->list[j]) {
1657 return i;
1658 }
1659 }
1660 }
1661
1662 return -1;
1663}
1664
1665
1666/*
1667 * Determine whether "child" appears in the list of objects associated
1668 * with the Monitor in "parent". If "parent" is a thin lock, we return
1669 * false immediately.
1670 */
1671static bool objectInChildList(const Object* parent, Object* child)
1672{
Carl Shapiro8d7f9b22009-12-21 20:23:45 -08001673 u4 lock = parent->lock;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001674 if (!IS_LOCK_FAT(&lock)) {
1675 //LOGI("on thin\n");
1676 return false;
1677 }
1678
Carl Shapiro8d7f9b22009-12-21 20:23:45 -08001679 return expandObjHas(&LW_MONITOR(lock)->historyChildren, child);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001680}
1681
1682/*
1683 * Print the child list.
1684 */
1685static void dumpKids(Object* parent)
1686{
Carl Shapiro8d7f9b22009-12-21 20:23:45 -08001687 Monitor* mon = LW_MONITOR(parent->lock);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001688
1689 printf("Children of %p:", parent);
1690 expandObjDump(&mon->historyChildren);
1691 printf("\n");
1692}
1693
1694/*
1695 * Add "child" to the list of children in "parent", and add "parent" to
1696 * the list of parents in "child".
1697 */
1698static void linkParentToChild(Object* parent, Object* child)
1699{
Carl Shapiro8d7f9b22009-12-21 20:23:45 -08001700 //assert(LW_MONITOR(parent->lock)->owner == dvmThreadSelf()); // !owned for merge
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001701 assert(IS_LOCK_FAT(&parent->lock));
1702 assert(IS_LOCK_FAT(&child->lock));
1703 assert(parent != child);
1704 Monitor* mon;
1705
Carl Shapiro8d7f9b22009-12-21 20:23:45 -08001706 mon = LW_MONITOR(parent->lock);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001707 assert(!expandObjHas(&mon->historyChildren, child));
1708 expandObjAddEntry(&mon->historyChildren, child);
1709
Carl Shapiro8d7f9b22009-12-21 20:23:45 -08001710 mon = LW_MONITOR(child->lock);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001711 assert(!expandObjHas(&mon->historyParents, parent));
1712 expandObjAddEntry(&mon->historyParents, parent);
1713}
1714
1715
1716/*
1717 * Remove "child" from the list of children in "parent".
1718 */
1719static void unlinkParentFromChild(Object* parent, Object* child)
1720{
Carl Shapiro8d7f9b22009-12-21 20:23:45 -08001721 //assert(LW_MONITOR(parent->lock)->owner == dvmThreadSelf()); // !owned for GC
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001722 assert(IS_LOCK_FAT(&parent->lock));
1723 assert(IS_LOCK_FAT(&child->lock));
1724 assert(parent != child);
1725 Monitor* mon;
1726
Carl Shapiro8d7f9b22009-12-21 20:23:45 -08001727 mon = LW_MONITOR(parent->lock);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001728 if (!expandObjRemoveEntry(&mon->historyChildren, child)) {
1729 LOGW("WARNING: child %p not found in parent %p\n", child, parent);
1730 }
1731 assert(!expandObjHas(&mon->historyChildren, child));
1732 assert(expandObjCheckForDuplicates(&mon->historyChildren) < 0);
1733
Carl Shapiro8d7f9b22009-12-21 20:23:45 -08001734 mon = LW_MONITOR(child->lock);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001735 if (!expandObjRemoveEntry(&mon->historyParents, parent)) {
1736 LOGW("WARNING: parent %p not found in child %p\n", parent, child);
1737 }
1738 assert(!expandObjHas(&mon->historyParents, parent));
1739 assert(expandObjCheckForDuplicates(&mon->historyParents) < 0);
1740}
1741
1742
1743/*
1744 * Log the monitors held by the current thread. This is done as part of
1745 * flagging an error.
1746 */
1747static void logHeldMonitors(Thread* self)
1748{
1749 char* name = NULL;
1750
1751 name = dvmGetThreadName(self);
1752 LOGW("Monitors currently held by thread (threadid=%d '%s')\n",
1753 self->threadId, name);
1754 LOGW("(most-recently-acquired on top):\n");
1755 free(name);
1756
1757 LockedObjectData* lod = self->pLockedObjects;
1758 while (lod != NULL) {
1759 LOGW("--- object %p[%d] (%s)\n",
1760 lod->obj, lod->recursionCount, lod->obj->clazz->descriptor);
1761 dvmLogRawStackTrace(lod->rawStackTrace, lod->stackDepth);
1762
1763 lod = lod->next;
1764 }
1765}
1766
1767/*
1768 * Recursively traverse the object hierarchy starting at "obj". We mark
1769 * ourselves on entry and clear the mark on exit. If we ever encounter
1770 * a marked object, we have a cycle.
1771 *
1772 * Returns "true" if all is well, "false" if we found a cycle.
1773 */
1774static bool traverseTree(Thread* self, const Object* obj)
1775{
1776 assert(IS_LOCK_FAT(&obj->lock));
Carl Shapiro8d7f9b22009-12-21 20:23:45 -08001777 Monitor* mon = LW_MONITOR(obj->lock);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001778
1779 /*
1780 * Have we been here before?
1781 */
1782 if (mon->historyMark) {
1783 int* rawStackTrace;
1784 int stackDepth;
1785
1786 LOGW("%s\n", kStartBanner);
1787 LOGW("Illegal lock attempt:\n");
1788 LOGW("--- object %p (%s)\n", obj, obj->clazz->descriptor);
1789
1790 rawStackTrace = dvmFillInStackTraceRaw(self, &stackDepth);
1791 dvmLogRawStackTrace(rawStackTrace, stackDepth);
1792 free(rawStackTrace);
1793
1794 LOGW(" ");
1795 logHeldMonitors(self);
1796
1797 LOGW(" ");
1798 LOGW("Earlier, the following lock order (from last to first) was\n");
1799 LOGW("established -- stack trace is from first successful lock):\n");
1800 return false;
1801 }
1802 mon->historyMark = true;
1803
1804 /*
1805 * Examine the children. We do NOT hold these locks, so they might
1806 * very well transition from thin to fat or change ownership while
1807 * we work.
1808 *
1809 * NOTE: we rely on the fact that they cannot revert from fat to thin
1810 * while we work. This is currently a safe assumption.
1811 *
1812 * We can safely ignore thin-locked children, because by definition
1813 * they have no history and are leaf nodes. In the current
1814 * implementation we always fatten the locks to provide a place to
1815 * hang the stack trace.
1816 */
1817 ExpandingObjectList* pList = &mon->historyChildren;
1818 int i;
1819 for (i = expandBufGetCount(pList)-1; i >= 0; i--) {
1820 const Object* child = expandBufGetEntry(pList, i);
Carl Shapiro8d7f9b22009-12-21 20:23:45 -08001821 u4 lock = child->lock;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001822 if (!IS_LOCK_FAT(&lock))
1823 continue;
1824 if (!traverseTree(self, child)) {
1825 LOGW("--- object %p (%s)\n", obj, obj->clazz->descriptor);
1826 dvmLogRawStackTrace(mon->historyRawStackTrace,
1827 mon->historyStackDepth);
1828 mon->historyMark = false;
1829 return false;
1830 }
1831 }
1832
1833 mon->historyMark = false;
1834
1835 return true;
1836}
1837
1838/*
1839 * Update the deadlock prediction tree, based on the current thread
1840 * acquiring "acqObj". This must be called before the object is added to
1841 * the thread's list of held monitors.
1842 *
1843 * If the thread already holds the lock (recursion), or this is a known
1844 * lock configuration, we return without doing anything. Otherwise, we add
1845 * a link from the most-recently-acquired lock in this thread to "acqObj"
1846 * after ensuring that the parent lock is "fat".
1847 *
1848 * This MUST NOT be called while a GC is in progress in another thread,
1849 * because we assume exclusive access to history trees in owned monitors.
1850 */
1851static void updateDeadlockPrediction(Thread* self, Object* acqObj)
1852{
1853 LockedObjectData* lod;
1854 LockedObjectData* mrl;
1855
1856 /*
1857 * Quick check for recursive access.
1858 */
1859 lod = dvmFindInMonitorList(self, acqObj);
1860 if (lod != NULL) {
1861 LOGV("+++ DP: recursive %p\n", acqObj);
1862 return;
1863 }
1864
1865 /*
1866 * Make the newly-acquired object's monitor "fat". In some ways this
1867 * isn't strictly necessary, but we need the GC to tell us when
1868 * "interesting" objects go away, and right now the only way to make
1869 * an object look interesting is to give it a monitor.
1870 *
1871 * This also gives us a place to hang a stack trace.
1872 *
1873 * Our thread holds the lock, so we're allowed to rewrite the lock
1874 * without worrying that something will change out from under us.
1875 */
1876 if (!IS_LOCK_FAT(&acqObj->lock)) {
1877 LOGVV("fattening lockee %p (recur=%d)\n",
Carl Shapiro94338aa2009-12-21 11:42:59 -08001878 acqObj, LW_LOCK_COUNT(acqObj->lock.thin));
Carl Shapiro66bb7df2010-03-12 15:25:37 -08001879 inflateMonitor(self, acqObj);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001880 }
1881
1882 /* if we don't have a stack trace for this monitor, establish one */
Carl Shapiro8d7f9b22009-12-21 20:23:45 -08001883 if (LW_MONITOR(acqObj->lock)->historyRawStackTrace == NULL) {
1884 Monitor* mon = LW_MONITOR(acqObj->lock);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001885 mon->historyRawStackTrace = dvmFillInStackTraceRaw(self,
1886 &mon->historyStackDepth);
1887 }
1888
1889 /*
1890 * We need to examine and perhaps modify the most-recently-locked
1891 * monitor. We own that, so there's no risk of another thread
1892 * stepping on us.
1893 *
1894 * Retrieve the most-recently-locked entry from our thread.
1895 */
1896 mrl = self->pLockedObjects;
1897 if (mrl == NULL)
1898 return; /* no other locks held */
1899
1900 /*
1901 * Do a quick check to see if "acqObj" is a direct descendant. We can do
1902 * this without holding the global lock because of our assertion that
1903 * a GC is not running in parallel -- nobody except the GC can
1904 * modify a history list in a Monitor they don't own, and we own "mrl".
1905 * (There might be concurrent *reads*, but no concurrent *writes.)
1906 *
1907 * If we find it, this is a known good configuration, and we're done.
1908 */
1909 if (objectInChildList(mrl->obj, acqObj))
1910 return;
1911
1912 /*
1913 * "mrl" is going to need to have a history tree. If it's currently
1914 * a thin lock, we make it fat now. The thin lock might have a
1915 * nonzero recursive lock count, which we need to carry over.
1916 *
1917 * Our thread holds the lock, so we're allowed to rewrite the lock
1918 * without worrying that something will change out from under us.
1919 */
1920 if (!IS_LOCK_FAT(&mrl->obj->lock)) {
1921 LOGVV("fattening parent %p f/b/o child %p (recur=%d)\n",
Carl Shapiro8d7f9b22009-12-21 20:23:45 -08001922 mrl->obj, acqObj, LW_LOCK_COUNT(mrl->obj->lock));
Carl Shapiro66bb7df2010-03-12 15:25:37 -08001923 inflateMonitor(self, mrl->obj);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001924 }
1925
1926 /*
1927 * We haven't seen this configuration before. We need to scan down
1928 * acqObj's tree to see if any of the monitors in self->pLockedObjects
1929 * appear. We grab a global lock before traversing or updating the
1930 * history list.
1931 *
1932 * If we find a match for any of our held locks, we know that the lock
1933 * has previously been acquired *after* acqObj, and we throw an error.
1934 *
1935 * The easiest way to do this is to create a link from "mrl" to "acqObj"
1936 * and do a recursive traversal, marking nodes as we cross them. If
1937 * we cross one a second time, we have a cycle and can throw an error.
1938 * (We do the flag-clearing traversal before adding the new link, so
1939 * that we're guaranteed to terminate.)
1940 *
1941 * If "acqObj" is a thin lock, it has no history, and we can create a
1942 * link to it without additional checks. [ We now guarantee that it's
1943 * always fat. ]
1944 */
1945 bool failed = false;
1946 dvmLockMutex(&gDvm.deadlockHistoryLock);
1947 linkParentToChild(mrl->obj, acqObj);
1948 if (!traverseTree(self, acqObj)) {
1949 LOGW("%s\n", kEndBanner);
1950 failed = true;
1951
1952 /* remove the entry so we're still okay when in "warning" mode */
1953 unlinkParentFromChild(mrl->obj, acqObj);
1954 }
1955 dvmUnlockMutex(&gDvm.deadlockHistoryLock);
1956
1957 if (failed) {
1958 switch (gDvm.deadlockPredictMode) {
1959 case kDPErr:
1960 dvmThrowException("Ldalvik/system/PotentialDeadlockError;", NULL);
1961 break;
1962 case kDPAbort:
1963 LOGE("Aborting due to potential deadlock\n");
1964 dvmAbort();
1965 break;
1966 default:
1967 /* warn only */
1968 break;
1969 }
1970 }
1971}
1972
1973/*
1974 * We're removing "child" from existence. We want to pull all of
1975 * child's children into "parent", filtering out duplicates. This is
1976 * called during the GC.
1977 *
1978 * This does not modify "child", which might have multiple parents.
1979 */
1980static void mergeChildren(Object* parent, const Object* child)
1981{
1982 Monitor* mon;
1983 int i;
1984
1985 assert(IS_LOCK_FAT(&child->lock));
Carl Shapiro8d7f9b22009-12-21 20:23:45 -08001986 mon = LW_MONITOR(child->lock);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001987 ExpandingObjectList* pList = &mon->historyChildren;
1988
1989 for (i = expandBufGetCount(pList)-1; i >= 0; i--) {
1990 Object* grandChild = expandBufGetEntry(pList, i);
1991
1992 if (!objectInChildList(parent, grandChild)) {
1993 LOGVV("+++ migrating %p link to %p\n", grandChild, parent);
1994 linkParentToChild(parent, grandChild);
1995 } else {
1996 LOGVV("+++ parent %p already links to %p\n", parent, grandChild);
1997 }
1998 }
1999}
2000
2001/*
2002 * An object with a fat lock is being collected during a GC pass. We
2003 * want to remove it from any lock history trees that it is a part of.
2004 *
2005 * This may require updating the history trees in several monitors. The
2006 * monitor semantics guarantee that no other thread will be accessing
2007 * the history trees at the same time.
2008 */
2009static void removeCollectedObject(Object* obj)
2010{
2011 Monitor* mon;
2012
2013 LOGVV("+++ collecting %p\n", obj);
2014
2015#if 0
2016 /*
2017 * We're currently running through the entire set of known monitors.
2018 * This can be somewhat slow. We may want to keep lists of parents
2019 * in each child to speed up GC.
2020 */
2021 mon = gDvm.monitorList;
2022 while (mon != NULL) {
2023 Object* parent = mon->obj;
2024 if (parent != NULL) { /* value nulled for deleted entries */
2025 if (objectInChildList(parent, obj)) {
2026 LOGVV("removing child %p from parent %p\n", obj, parent);
2027 unlinkParentFromChild(parent, obj);
2028 mergeChildren(parent, obj);
2029 }
2030 }
2031 mon = mon->next;
2032 }
2033#endif
2034
2035 /*
2036 * For every parent of this object:
2037 * - merge all of our children into the parent's child list (creates
2038 * a two-way link between parent and child)
2039 * - remove ourselves from the parent's child list
2040 */
2041 ExpandingObjectList* pList;
2042 int i;
2043
2044 assert(IS_LOCK_FAT(&obj->lock));
Carl Shapiro8d7f9b22009-12-21 20:23:45 -08002045 mon = LW_MONITOR(obj->lock);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002046 pList = &mon->historyParents;
2047 for (i = expandBufGetCount(pList)-1; i >= 0; i--) {
2048 Object* parent = expandBufGetEntry(pList, i);
Carl Shapiro8d7f9b22009-12-21 20:23:45 -08002049 Monitor* parentMon = LW_MONITOR(parent->lock);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002050
2051 if (!expandObjRemoveEntry(&parentMon->historyChildren, obj)) {
2052 LOGW("WARNING: child %p not found in parent %p\n", obj, parent);
2053 }
2054 assert(!expandObjHas(&parentMon->historyChildren, obj));
2055
2056 mergeChildren(parent, obj);
2057 }
2058
2059 /*
2060 * For every child of this object:
2061 * - remove ourselves from the child's parent list
2062 */
2063 pList = &mon->historyChildren;
2064 for (i = expandBufGetCount(pList)-1; i >= 0; i--) {
2065 Object* child = expandBufGetEntry(pList, i);
Carl Shapiro8d7f9b22009-12-21 20:23:45 -08002066 Monitor* childMon = LW_MONITOR(child->lock);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002067
2068 if (!expandObjRemoveEntry(&childMon->historyParents, obj)) {
2069 LOGW("WARNING: parent %p not found in child %p\n", obj, child);
2070 }
2071 assert(!expandObjHas(&childMon->historyParents, obj));
2072 }
2073}
2074
2075#endif /*WITH_DEADLOCK_PREDICTION*/