The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Andy McFadden | 581bed7 | 2009-10-15 11:24:54 -0700 | [diff] [blame] | 16 | |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 17 | /* |
| 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 Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 33 | */ |
| 34 | #include "Dalvik.h" |
| 35 | |
Carl Shapiro | f0c514c | 2010-04-09 15:03:33 -0700 | [diff] [blame] | 36 | #include <fcntl.h> |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 37 | #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 */ |
| 47 | static const char* kStartBanner = |
| 48 | "<-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#"; |
| 49 | static 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 | */ |
| 59 | typedef struct ExpandingObjectList { |
| 60 | u2 alloc; |
| 61 | u2 count; |
| 62 | Object** list; |
| 63 | } ExpandingObjectList; |
| 64 | |
| 65 | /* fwd */ |
| 66 | static void updateDeadlockPrediction(Thread* self, Object* obj); |
| 67 | static void removeCollectedObject(Object* obj); |
| 68 | static 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 Shapiro | 94338aa | 2009-12-21 11:42:59 -0800 | [diff] [blame] | 82 | * 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 Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 86 | * |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 87 | * 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 Shapiro | 7193802 | 2009-12-22 13:49:53 -0800 | [diff] [blame] | 90 | * |
Carl Shapiro | 94338aa | 2009-12-21 11:42:59 -0800 | [diff] [blame] | 91 | * [31 ---- 19] [18 ---- 3] [2 ---- 1] [0] |
| 92 | * lock count thread id hash state 0 |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 93 | * |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 94 | * When set, the lock is in the "fat" state and its bits are formatted |
Carl Shapiro | 94338aa | 2009-12-21 11:42:59 -0800 | [diff] [blame] | 95 | * as follows: |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 96 | * |
Carl Shapiro | 94338aa | 2009-12-21 11:42:59 -0800 | [diff] [blame] | 97 | * [31 ---- 3] [2 ---- 1] [0] |
| 98 | * pointer hash state 1 |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 99 | * |
| 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 Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 102 | */ |
| 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 | */ |
| 115 | struct 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 Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 120 | Thread* waitSet; /* threads currently waiting on this monitor */ |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 121 | |
| 122 | pthread_mutex_t lock; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 123 | |
| 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 | */ |
| 154 | Monitor* 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 Shapiro | 94338aa | 2009-12-21 11:42:59 -0800 | [diff] [blame] | 163 | if (((u4)mon & 7) != 0) { |
| 164 | LOGE("Misaligned monitor: %p\n", mon); |
| 165 | dvmAbort(); |
| 166 | } |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 167 | mon->obj = obj; |
| 168 | dvmInitMutex(&mon->lock); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 169 | |
| 170 | /* replace the head of the list with the new monitor */ |
| 171 | do { |
| 172 | mon->next = gDvm.monitorList; |
Andy McFadden | 6e10b9a | 2010-06-14 15:24:39 -0700 | [diff] [blame] | 173 | } while (android_atomic_release_cas((int32_t)mon->next, (int32_t)mon, |
| 174 | (int32_t*)(void*)&gDvm.monitorList) != 0); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 175 | |
| 176 | return mon; |
| 177 | } |
| 178 | |
| 179 | /* |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 180 | * Free the monitor list. Only used when shutting the VM down. |
| 181 | */ |
| 182 | void 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 | */ |
| 204 | void 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 | */ |
| 231 | Object* dvmGetMonitorObject(Monitor* mon) |
| 232 | { |
| 233 | if (mon == NULL) |
| 234 | return NULL; |
| 235 | else |
| 236 | return mon->obj; |
| 237 | } |
| 238 | |
| 239 | /* |
Carl Shapiro | 30aa997 | 2010-01-13 22:07:50 -0800 | [diff] [blame] | 240 | * Returns the thread id of the thread owning the given lock. |
| 241 | */ |
| 242 | static 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 McFadden | fd54266 | 2010-03-12 13:39:59 -0800 | [diff] [blame] | 262 | * 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 | */ |
| 267 | Thread* 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 Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 277 | * Checks whether the given thread holds the given |
| 278 | * objects's lock. |
| 279 | */ |
| 280 | bool dvmHoldsLock(Thread* thread, Object* obj) |
| 281 | { |
| 282 | if (thread == NULL || obj == NULL) { |
| 283 | return false; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 284 | } else { |
Carl Shapiro | 30aa997 | 2010-01-13 22:07:50 -0800 | [diff] [blame] | 285 | return thread->threadId == lockOwner(obj); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 286 | } |
| 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 Shapiro | 5a6071b | 2010-01-07 21:35:50 -0800 | [diff] [blame] | 293 | static void freeObjectMonitor(Object* obj) |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 294 | { |
| 295 | Monitor *mon; |
| 296 | |
Carl Shapiro | 5a6071b | 2010-01-07 21:35:50 -0800 | [diff] [blame] | 297 | assert(LW_SHAPE(obj->lock) == LW_SHAPE_FAT); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 298 | |
| 299 | #ifdef WITH_DEADLOCK_PREDICTION |
| 300 | if (gDvm.deadlockPredictMode != kDPOff) |
Carl Shapiro | 5a6071b | 2010-01-07 21:35:50 -0800 | [diff] [blame] | 301 | removeCollectedObject(obj); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 302 | #endif |
| 303 | |
Carl Shapiro | 5a6071b | 2010-01-07 21:35:50 -0800 | [diff] [blame] | 304 | mon = LW_MONITOR(obj->lock); |
| 305 | obj->lock = DVM_LOCK_INITIAL_THIN_VALUE; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 306 | |
| 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 Shapiro | 1ff876d | 2010-04-04 01:56:48 -0700 | [diff] [blame] | 314 | assert(pthread_mutex_trylock(&mon->lock) == 0); |
| 315 | assert(pthread_mutex_unlock(&mon->lock) == 0); |
Carl Shapiro | 980ffb0 | 2010-03-13 22:34:01 -0800 | [diff] [blame] | 316 | dvmDestroyMutex(&mon->lock); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 317 | #ifdef WITH_DEADLOCK_PREDICTION |
Carl Shapiro | 5a6071b | 2010-01-07 21:35:50 -0800 | [diff] [blame] | 318 | expandObjClear(&mon->historyChildren); |
| 319 | expandObjClear(&mon->historyParents); |
| 320 | free(mon->historyRawStackTrace); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 321 | #endif |
Carl Shapiro | 5a6071b | 2010-01-07 21:35:50 -0800 | [diff] [blame] | 322 | free(mon); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 323 | } |
| 324 | |
Carl Shapiro | 5a6071b | 2010-01-07 21:35:50 -0800 | [diff] [blame] | 325 | /* |
| 326 | * Frees monitor objects belonging to unmarked objects. |
| 327 | */ |
| 328 | void dvmSweepMonitorList(Monitor** mon, int (*isUnmarkedObject)(void*)) |
| 329 | { |
| 330 | Monitor handle; |
| 331 | Monitor *prev, *curr; |
| 332 | Object *obj; |
| 333 | |
| 334 | assert(mon != NULL); |
Carl Shapiro | 5a6071b | 2010-01-07 21:35:50 -0800 | [diff] [blame] | 335 | 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 Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 350 | |
Carl Shapiro | f0c514c | 2010-04-09 15:03:33 -0700 | [diff] [blame] | 351 | static char *logWriteInt(char *dst, int value) |
| 352 | { |
| 353 | *dst++ = EVENT_TYPE_INT; |
| 354 | set4LE((u1 *)dst, value); |
| 355 | return dst + 4; |
| 356 | } |
| 357 | |
| 358 | static char *logWriteString(char *dst, const char *value, size_t len) |
| 359 | { |
| 360 | *dst++ = EVENT_TYPE_STRING; |
Carl Shapiro | af69cf8 | 2010-04-16 17:33:15 -0700 | [diff] [blame] | 361 | len = len < 32 ? len : 32; |
Carl Shapiro | f0c514c | 2010-04-09 15:03:33 -0700 | [diff] [blame] | 362 | set4LE((u1 *)dst, len); |
| 363 | dst += 4; |
Carl Shapiro | f0c514c | 2010-04-09 15:03:33 -0700 | [diff] [blame] | 364 | memcpy(dst, value, len); |
| 365 | return dst + len; |
| 366 | } |
| 367 | |
Carl Shapiro | af69cf8 | 2010-04-16 17:33:15 -0700 | [diff] [blame] | 368 | #define EVENT_LOG_TAG_dvm_lock_sample 20003 |
Carl Shapiro | f0c514c | 2010-04-09 15:03:33 -0700 | [diff] [blame] | 369 | |
| 370 | static void logContentionEvent(Thread *self, u4 waitMs, u4 samplePercent) |
| 371 | { |
| 372 | const StackSaveArea *saveArea; |
| 373 | const Method *meth; |
| 374 | u4 relativePc; |
Carl Shapiro | af69cf8 | 2010-04-16 17:33:15 -0700 | [diff] [blame] | 375 | char eventBuffer[132]; |
Carl Shapiro | f0c514c | 2010-04-09 15:03:33 -0700 | [diff] [blame] | 376 | const char *fileName; |
Carl Shapiro | e3c01da | 2010-05-20 22:54:18 -0700 | [diff] [blame] | 377 | char procName[33], *selfName; |
Carl Shapiro | f0c514c | 2010-04-09 15:03:33 -0700 | [diff] [blame] | 378 | char *cp; |
Carl Shapiro | af69cf8 | 2010-04-16 17:33:15 -0700 | [diff] [blame] | 379 | size_t len; |
| 380 | int fd; |
Carl Shapiro | f0c514c | 2010-04-09 15:03:33 -0700 | [diff] [blame] | 381 | |
| 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 Shapiro | af69cf8 | 2010-04-16 17:33:15 -0700 | [diff] [blame] | 391 | memset(procName, 0, sizeof(procName)); |
| 392 | read(fd, procName, sizeof(procName) - 1); |
Carl Shapiro | f0c514c | 2010-04-09 15:03:33 -0700 | [diff] [blame] | 393 | close(fd); |
Carl Shapiro | af69cf8 | 2010-04-16 17:33:15 -0700 | [diff] [blame] | 394 | len = strlen(procName); |
| 395 | cp = logWriteString(cp, procName, len); |
Carl Shapiro | f0c514c | 2010-04-09 15:03:33 -0700 | [diff] [blame] | 396 | |
| 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 Shapiro | af69cf8 | 2010-04-16 17:33:15 -0700 | [diff] [blame] | 422 | android_btWriteLog(EVENT_LOG_TAG_dvm_lock_sample, |
Carl Shapiro | f0c514c | 2010-04-09 15:03:33 -0700 | [diff] [blame] | 423 | EVENT_TYPE_LIST, |
| 424 | eventBuffer, |
| 425 | (size_t)(cp - eventBuffer)); |
| 426 | } |
| 427 | |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 428 | /* |
| 429 | * Lock a monitor. |
| 430 | */ |
| 431 | static void lockMonitor(Thread* self, Monitor* mon) |
| 432 | { |
Carl Shapiro | f0c514c | 2010-04-09 15:03:33 -0700 | [diff] [blame] | 433 | ThreadStatus oldStatus; |
| 434 | u4 waitThreshold, samplePercent; |
| 435 | u8 waitStart, waitEnd, waitMs; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 436 | |
| 437 | if (mon->owner == self) { |
| 438 | mon->lockCount++; |
Carl Shapiro | f0c514c | 2010-04-09 15:03:33 -0700 | [diff] [blame] | 439 | return; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 440 | } |
Carl Shapiro | 045fdc9 | 2010-04-13 16:48:27 -0700 | [diff] [blame] | 441 | if (dvmTryLockMutex(&mon->lock) != 0) { |
Carl Shapiro | f0c514c | 2010-04-09 15:03:33 -0700 | [diff] [blame] | 442 | oldStatus = dvmChangeStatus(self, THREAD_MONITOR); |
| 443 | waitThreshold = gDvm.lockProfThreshold; |
| 444 | if (waitThreshold) { |
| 445 | waitStart = dvmGetRelativeTimeUsec(); |
| 446 | } |
| 447 | dvmLockMutex(&mon->lock); |
| 448 | if (waitThreshold) { |
| 449 | waitEnd = dvmGetRelativeTimeUsec(); |
| 450 | } |
| 451 | dvmChangeStatus(self, oldStatus); |
| 452 | if (waitThreshold) { |
| 453 | waitMs = (waitEnd - waitStart) / 1000; |
| 454 | if (waitMs >= waitThreshold) { |
| 455 | samplePercent = 100; |
| 456 | } else { |
Carl Shapiro | af69cf8 | 2010-04-16 17:33:15 -0700 | [diff] [blame] | 457 | samplePercent = 100 * waitMs / waitThreshold; |
Carl Shapiro | f0c514c | 2010-04-09 15:03:33 -0700 | [diff] [blame] | 458 | } |
Carl Shapiro | b8fcf57 | 2010-04-16 17:33:15 -0700 | [diff] [blame] | 459 | if (samplePercent != 0 && ((u4)rand() % 100 < samplePercent)) { |
Carl Shapiro | f0c514c | 2010-04-09 15:03:33 -0700 | [diff] [blame] | 460 | logContentionEvent(self, waitMs, samplePercent); |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | mon->owner = self; |
| 465 | assert(mon->lockCount == 0); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | /* |
| 469 | * Try to lock a monitor. |
| 470 | * |
| 471 | * Returns "true" on success. |
| 472 | */ |
Carl Shapiro | b31b301 | 2010-05-25 18:35:37 -0700 | [diff] [blame] | 473 | #ifdef WITH_COPYING_GC |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 474 | static bool tryLockMonitor(Thread* self, Monitor* mon) |
| 475 | { |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 476 | if (mon->owner == self) { |
| 477 | mon->lockCount++; |
| 478 | return true; |
| 479 | } else { |
Carl Shapiro | 980ffb0 | 2010-03-13 22:34:01 -0800 | [diff] [blame] | 480 | if (dvmTryLockMutex(&mon->lock) == 0) { |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 481 | mon->owner = self; |
| 482 | assert(mon->lockCount == 0); |
| 483 | return true; |
| 484 | } else { |
| 485 | return false; |
| 486 | } |
| 487 | } |
| 488 | } |
Carl Shapiro | b31b301 | 2010-05-25 18:35:37 -0700 | [diff] [blame] | 489 | #endif |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 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 | */ |
| 497 | static bool unlockMonitor(Thread* self, Monitor* mon) |
| 498 | { |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 499 | assert(self != NULL); |
Carl Shapiro | 9227708 | 2010-04-06 15:35:59 -0700 | [diff] [blame] | 500 | assert(mon != NULL); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 501 | 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 Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 506 | mon->owner = NULL; |
Carl Shapiro | 980ffb0 | 2010-03-13 22:34:01 -0800 | [diff] [blame] | 507 | dvmUnlockMutex(&mon->lock); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 508 | } 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 Shapiro | 8782d7c | 2010-04-19 20:10:35 -0700 | [diff] [blame] | 517 | dvmThrowException("Ljava/lang/IllegalMonitorStateException;", |
| 518 | "unlock of unowned monitor"); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 519 | return false; |
| 520 | } |
| 521 | return true; |
| 522 | } |
| 523 | |
| 524 | /* |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 525 | * Checks the wait set for circular structure. Returns 0 if the list |
Carl Shapiro | b453919 | 2010-01-04 16:50:00 -0800 | [diff] [blame] | 526 | * is not circular. Otherwise, returns 1. Used only by asserts. |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 527 | */ |
Carl Shapiro | b31b301 | 2010-05-25 18:35:37 -0700 | [diff] [blame] | 528 | #ifndef NDEBUG |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 529 | static int waitSetCheck(Monitor *mon) |
| 530 | { |
| 531 | Thread *fast, *slow; |
| 532 | size_t n; |
| 533 | |
| 534 | assert(mon != NULL); |
| 535 | fast = slow = mon->waitSet; |
| 536 | n = 0; |
| 537 | for (;;) { |
| 538 | if (fast == NULL) return 0; |
| 539 | if (fast->waitNext == NULL) return 0; |
Carl Shapiro | 5f56e67 | 2010-01-05 20:38:03 -0800 | [diff] [blame] | 540 | if (fast == slow && n > 0) return 1; |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 541 | n += 2; |
| 542 | fast = fast->waitNext->waitNext; |
| 543 | slow = slow->waitNext; |
| 544 | } |
| 545 | } |
Carl Shapiro | b31b301 | 2010-05-25 18:35:37 -0700 | [diff] [blame] | 546 | #endif |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 547 | |
| 548 | /* |
Carl Shapiro | 30aa997 | 2010-01-13 22:07:50 -0800 | [diff] [blame] | 549 | * Links a thread into a monitor's wait set. The monitor lock must be |
| 550 | * held by the caller of this routine. |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 551 | */ |
| 552 | static void waitSetAppend(Monitor *mon, Thread *thread) |
| 553 | { |
| 554 | Thread *elt; |
| 555 | |
| 556 | assert(mon != NULL); |
Carl Shapiro | 30aa997 | 2010-01-13 22:07:50 -0800 | [diff] [blame] | 557 | assert(mon->owner == dvmThreadSelf()); |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 558 | assert(thread != NULL); |
| 559 | assert(thread->waitNext == NULL); |
| 560 | assert(waitSetCheck(mon) == 0); |
| 561 | if (mon->waitSet == NULL) { |
| 562 | mon->waitSet = thread; |
| 563 | return; |
| 564 | } |
| 565 | elt = mon->waitSet; |
| 566 | while (elt->waitNext != NULL) { |
| 567 | elt = elt->waitNext; |
| 568 | } |
| 569 | elt->waitNext = thread; |
| 570 | } |
| 571 | |
| 572 | /* |
Carl Shapiro | 30aa997 | 2010-01-13 22:07:50 -0800 | [diff] [blame] | 573 | * Unlinks a thread from a monitor's wait set. The monitor lock must |
| 574 | * be held by the caller of this routine. |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 575 | */ |
| 576 | static void waitSetRemove(Monitor *mon, Thread *thread) |
| 577 | { |
| 578 | Thread *elt; |
| 579 | |
| 580 | assert(mon != NULL); |
Carl Shapiro | 30aa997 | 2010-01-13 22:07:50 -0800 | [diff] [blame] | 581 | assert(mon->owner == dvmThreadSelf()); |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 582 | assert(thread != NULL); |
| 583 | assert(waitSetCheck(mon) == 0); |
| 584 | if (mon->waitSet == NULL) { |
| 585 | return; |
| 586 | } |
| 587 | if (mon->waitSet == thread) { |
| 588 | mon->waitSet = thread->waitNext; |
| 589 | thread->waitNext = NULL; |
| 590 | return; |
| 591 | } |
| 592 | elt = mon->waitSet; |
| 593 | while (elt->waitNext != NULL) { |
| 594 | if (elt->waitNext == thread) { |
| 595 | elt->waitNext = thread->waitNext; |
| 596 | thread->waitNext = NULL; |
| 597 | return; |
| 598 | } |
| 599 | elt = elt->waitNext; |
| 600 | } |
| 601 | } |
| 602 | |
Carl Shapiro | b453919 | 2010-01-04 16:50:00 -0800 | [diff] [blame] | 603 | /* |
| 604 | * Converts the given relative waiting time into an absolute time. |
| 605 | */ |
Bill Buzbee | fccb31d | 2010-02-04 16:09:55 -0800 | [diff] [blame] | 606 | void absoluteTime(s8 msec, s4 nsec, struct timespec *ts) |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 607 | { |
| 608 | s8 endSec; |
| 609 | |
| 610 | #ifdef HAVE_TIMEDWAIT_MONOTONIC |
| 611 | clock_gettime(CLOCK_MONOTONIC, ts); |
| 612 | #else |
| 613 | { |
| 614 | struct timeval tv; |
| 615 | gettimeofday(&tv, NULL); |
| 616 | ts->tv_sec = tv.tv_sec; |
| 617 | ts->tv_nsec = tv.tv_usec * 1000; |
| 618 | } |
| 619 | #endif |
| 620 | endSec = ts->tv_sec + msec / 1000; |
| 621 | if (endSec >= 0x7fffffff) { |
| 622 | LOGV("NOTE: end time exceeds epoch\n"); |
| 623 | endSec = 0x7ffffffe; |
| 624 | } |
| 625 | ts->tv_sec = endSec; |
| 626 | ts->tv_nsec = (ts->tv_nsec + (msec % 1000) * 1000000) + nsec; |
| 627 | |
| 628 | /* catch rollover */ |
| 629 | if (ts->tv_nsec >= 1000000000L) { |
| 630 | ts->tv_sec++; |
| 631 | ts->tv_nsec -= 1000000000L; |
| 632 | } |
| 633 | } |
| 634 | |
Bill Buzbee | fccb31d | 2010-02-04 16:09:55 -0800 | [diff] [blame] | 635 | int dvmRelativeCondWait(pthread_cond_t* cond, pthread_mutex_t* mutex, |
| 636 | s8 msec, s4 nsec) |
| 637 | { |
| 638 | int ret; |
| 639 | struct timespec ts; |
| 640 | absoluteTime(msec, nsec, &ts); |
| 641 | #if defined(HAVE_TIMEDWAIT_MONOTONIC) |
| 642 | ret = pthread_cond_timedwait_monotonic(cond, mutex, &ts); |
| 643 | #else |
| 644 | ret = pthread_cond_timedwait(cond, mutex, &ts); |
| 645 | #endif |
| 646 | assert(ret == 0 || ret == ETIMEDOUT); |
| 647 | return ret; |
| 648 | } |
| 649 | |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 650 | /* |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 651 | * Wait on a monitor until timeout, interrupt, or notification. Used for |
| 652 | * Object.wait() and (somewhat indirectly) Thread.sleep() and Thread.join(). |
| 653 | * |
| 654 | * If another thread calls Thread.interrupt(), we throw InterruptedException |
| 655 | * and return immediately if one of the following are true: |
| 656 | * - blocked in wait(), wait(long), or wait(long, int) methods of Object |
| 657 | * - blocked in join(), join(long), or join(long, int) methods of Thread |
| 658 | * - blocked in sleep(long), or sleep(long, int) methods of Thread |
| 659 | * Otherwise, we set the "interrupted" flag. |
| 660 | * |
| 661 | * Checks to make sure that "nsec" is in the range 0-999999 |
| 662 | * (i.e. fractions of a millisecond) and throws the appropriate |
| 663 | * exception if it isn't. |
| 664 | * |
| 665 | * The spec allows "spurious wakeups", and recommends that all code using |
| 666 | * Object.wait() do so in a loop. This appears to derive from concerns |
| 667 | * about pthread_cond_wait() on multiprocessor systems. Some commentary |
| 668 | * on the web casts doubt on whether these can/should occur. |
| 669 | * |
| 670 | * Since we're allowed to wake up "early", we clamp extremely long durations |
| 671 | * to return at the end of the 32-bit time epoch. |
| 672 | */ |
| 673 | static void waitMonitor(Thread* self, Monitor* mon, s8 msec, s4 nsec, |
| 674 | bool interruptShouldThrow) |
| 675 | { |
| 676 | struct timespec ts; |
| 677 | bool wasInterrupted = false; |
| 678 | bool timed; |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 679 | int ret; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 680 | |
Carl Shapiro | 7193802 | 2009-12-22 13:49:53 -0800 | [diff] [blame] | 681 | assert(self != NULL); |
| 682 | assert(mon != NULL); |
| 683 | |
Carl Shapiro | 94338aa | 2009-12-21 11:42:59 -0800 | [diff] [blame] | 684 | /* Make sure that we hold the lock. */ |
Carl Shapiro | 7193802 | 2009-12-22 13:49:53 -0800 | [diff] [blame] | 685 | if (mon->owner != self) { |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 686 | dvmThrowException("Ljava/lang/IllegalMonitorStateException;", |
| 687 | "object not locked by thread before wait()"); |
| 688 | return; |
| 689 | } |
| 690 | |
| 691 | /* |
| 692 | * Enforce the timeout range. |
| 693 | */ |
| 694 | if (msec < 0 || nsec < 0 || nsec > 999999) { |
| 695 | dvmThrowException("Ljava/lang/IllegalArgumentException;", |
| 696 | "timeout arguments out of range"); |
| 697 | return; |
| 698 | } |
| 699 | |
| 700 | /* |
| 701 | * Compute absolute wakeup time, if necessary. |
| 702 | */ |
| 703 | if (msec == 0 && nsec == 0) { |
| 704 | timed = false; |
| 705 | } else { |
Bill Buzbee | fccb31d | 2010-02-04 16:09:55 -0800 | [diff] [blame] | 706 | absoluteTime(msec, nsec, &ts); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 707 | timed = true; |
| 708 | } |
| 709 | |
| 710 | /* |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 711 | * Add ourselves to the set of threads waiting on this monitor, and |
| 712 | * release our hold. We need to let it go even if we're a few levels |
| 713 | * deep in a recursive lock, and we need to restore that later. |
| 714 | * |
Carl Shapiro | 142ef27 | 2010-01-25 12:51:31 -0800 | [diff] [blame] | 715 | * We append to the wait set ahead of clearing the count and owner |
| 716 | * fields so the subroutine can check that the calling thread owns |
| 717 | * the monitor. Aside from that, the order of member updates is |
| 718 | * not order sensitive as we hold the pthread mutex. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 719 | */ |
Carl Shapiro | 142ef27 | 2010-01-25 12:51:31 -0800 | [diff] [blame] | 720 | waitSetAppend(mon, self); |
| 721 | int prevLockCount = mon->lockCount; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 722 | mon->lockCount = 0; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 723 | mon->owner = NULL; |
| 724 | |
| 725 | /* |
| 726 | * Update thread status. If the GC wakes up, it'll ignore us, knowing |
| 727 | * that we won't touch any references in this state, and we'll check |
| 728 | * our suspend mode before we transition out. |
| 729 | */ |
| 730 | if (timed) |
| 731 | dvmChangeStatus(self, THREAD_TIMED_WAIT); |
| 732 | else |
| 733 | dvmChangeStatus(self, THREAD_WAIT); |
| 734 | |
Carl Shapiro | 980ffb0 | 2010-03-13 22:34:01 -0800 | [diff] [blame] | 735 | dvmLockMutex(&self->waitMutex); |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 736 | |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 737 | /* |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 738 | * Set waitMonitor to the monitor object we will be waiting on. |
| 739 | * When waitMonitor is non-NULL a notifying or interrupting thread |
| 740 | * must signal the thread's waitCond to wake it up. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 741 | */ |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 742 | assert(self->waitMonitor == NULL); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 743 | self->waitMonitor = mon; |
| 744 | |
| 745 | /* |
| 746 | * Handle the case where the thread was interrupted before we called |
| 747 | * wait(). |
| 748 | */ |
| 749 | if (self->interrupted) { |
| 750 | wasInterrupted = true; |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 751 | self->waitMonitor = NULL; |
Carl Shapiro | 980ffb0 | 2010-03-13 22:34:01 -0800 | [diff] [blame] | 752 | dvmUnlockMutex(&self->waitMutex); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 753 | goto done; |
| 754 | } |
| 755 | |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 756 | /* |
| 757 | * Release the monitor lock and wait for a notification or |
| 758 | * a timeout to occur. |
| 759 | */ |
Carl Shapiro | 980ffb0 | 2010-03-13 22:34:01 -0800 | [diff] [blame] | 760 | dvmUnlockMutex(&mon->lock); |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 761 | |
| 762 | if (!timed) { |
| 763 | ret = pthread_cond_wait(&self->waitCond, &self->waitMutex); |
| 764 | assert(ret == 0); |
| 765 | } else { |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 766 | #ifdef HAVE_TIMEDWAIT_MONOTONIC |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 767 | ret = pthread_cond_timedwait_monotonic(&self->waitCond, &self->waitMutex, &ts); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 768 | #else |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 769 | ret = pthread_cond_timedwait(&self->waitCond, &self->waitMutex, &ts); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 770 | #endif |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 771 | assert(ret == 0 || ret == ETIMEDOUT); |
| 772 | } |
| 773 | if (self->interrupted) { |
| 774 | wasInterrupted = true; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 775 | } |
| 776 | |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 777 | self->interrupted = false; |
| 778 | self->waitMonitor = NULL; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 779 | |
Carl Shapiro | 980ffb0 | 2010-03-13 22:34:01 -0800 | [diff] [blame] | 780 | dvmUnlockMutex(&self->waitMutex); |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 781 | |
Carl Shapiro | 30aa997 | 2010-01-13 22:07:50 -0800 | [diff] [blame] | 782 | /* Reacquire the monitor lock. */ |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 783 | lockMonitor(self, mon); |
| 784 | |
Carl Shapiro | 142ef27 | 2010-01-25 12:51:31 -0800 | [diff] [blame] | 785 | done: |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 786 | /* |
Carl Shapiro | 07b3592 | 2010-01-25 14:48:30 -0800 | [diff] [blame] | 787 | * We remove our thread from wait set after restoring the count |
| 788 | * and owner fields so the subroutine can check that the calling |
| 789 | * thread owns the monitor. Aside from that, the order of member |
| 790 | * updates is not order sensitive as we hold the pthread mutex. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 791 | */ |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 792 | mon->owner = self; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 793 | mon->lockCount = prevLockCount; |
Carl Shapiro | 07b3592 | 2010-01-25 14:48:30 -0800 | [diff] [blame] | 794 | waitSetRemove(mon, self); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 795 | |
| 796 | /* set self->status back to THREAD_RUNNING, and self-suspend if needed */ |
| 797 | dvmChangeStatus(self, THREAD_RUNNING); |
| 798 | |
| 799 | if (wasInterrupted) { |
| 800 | /* |
| 801 | * We were interrupted while waiting, or somebody interrupted an |
Carl Shapiro | 30aa997 | 2010-01-13 22:07:50 -0800 | [diff] [blame] | 802 | * un-interruptible thread earlier and we're bailing out immediately. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 803 | * |
| 804 | * The doc sayeth: "The interrupted status of the current thread is |
| 805 | * cleared when this exception is thrown." |
| 806 | */ |
| 807 | self->interrupted = false; |
| 808 | if (interruptShouldThrow) |
| 809 | dvmThrowException("Ljava/lang/InterruptedException;", NULL); |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | /* |
| 814 | * Notify one thread waiting on this monitor. |
| 815 | */ |
| 816 | static void notifyMonitor(Thread* self, Monitor* mon) |
| 817 | { |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 818 | Thread* thread; |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 819 | |
Carl Shapiro | 7193802 | 2009-12-22 13:49:53 -0800 | [diff] [blame] | 820 | assert(self != NULL); |
| 821 | assert(mon != NULL); |
| 822 | |
Carl Shapiro | 94338aa | 2009-12-21 11:42:59 -0800 | [diff] [blame] | 823 | /* Make sure that we hold the lock. */ |
Carl Shapiro | 7193802 | 2009-12-22 13:49:53 -0800 | [diff] [blame] | 824 | if (mon->owner != self) { |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 825 | dvmThrowException("Ljava/lang/IllegalMonitorStateException;", |
| 826 | "object not locked by thread before notify()"); |
| 827 | return; |
| 828 | } |
Carl Shapiro | 30aa997 | 2010-01-13 22:07:50 -0800 | [diff] [blame] | 829 | /* Signal the first waiting thread in the wait set. */ |
| 830 | while (mon->waitSet != NULL) { |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 831 | thread = mon->waitSet; |
| 832 | mon->waitSet = thread->waitNext; |
| 833 | thread->waitNext = NULL; |
Carl Shapiro | 980ffb0 | 2010-03-13 22:34:01 -0800 | [diff] [blame] | 834 | dvmLockMutex(&thread->waitMutex); |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 835 | /* Check to see if the thread is still waiting. */ |
| 836 | if (thread->waitMonitor != NULL) { |
| 837 | pthread_cond_signal(&thread->waitCond); |
Carl Shapiro | 980ffb0 | 2010-03-13 22:34:01 -0800 | [diff] [blame] | 838 | dvmUnlockMutex(&thread->waitMutex); |
Carl Shapiro | 30aa997 | 2010-01-13 22:07:50 -0800 | [diff] [blame] | 839 | return; |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 840 | } |
Carl Shapiro | 980ffb0 | 2010-03-13 22:34:01 -0800 | [diff] [blame] | 841 | dvmUnlockMutex(&thread->waitMutex); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 842 | } |
| 843 | } |
| 844 | |
| 845 | /* |
| 846 | * Notify all threads waiting on this monitor. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 847 | */ |
| 848 | static void notifyAllMonitor(Thread* self, Monitor* mon) |
| 849 | { |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 850 | Thread* thread; |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 851 | |
Carl Shapiro | 7193802 | 2009-12-22 13:49:53 -0800 | [diff] [blame] | 852 | assert(self != NULL); |
| 853 | assert(mon != NULL); |
| 854 | |
Carl Shapiro | 94338aa | 2009-12-21 11:42:59 -0800 | [diff] [blame] | 855 | /* Make sure that we hold the lock. */ |
Carl Shapiro | 7193802 | 2009-12-22 13:49:53 -0800 | [diff] [blame] | 856 | if (mon->owner != self) { |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 857 | dvmThrowException("Ljava/lang/IllegalMonitorStateException;", |
| 858 | "object not locked by thread before notifyAll()"); |
| 859 | return; |
| 860 | } |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 861 | /* Signal all threads in the wait set. */ |
| 862 | while (mon->waitSet != NULL) { |
| 863 | thread = mon->waitSet; |
| 864 | mon->waitSet = thread->waitNext; |
| 865 | thread->waitNext = NULL; |
Carl Shapiro | 980ffb0 | 2010-03-13 22:34:01 -0800 | [diff] [blame] | 866 | dvmLockMutex(&thread->waitMutex); |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 867 | /* Check to see if the thread is still waiting. */ |
| 868 | if (thread->waitMonitor != NULL) { |
| 869 | pthread_cond_signal(&thread->waitCond); |
| 870 | } |
Carl Shapiro | 980ffb0 | 2010-03-13 22:34:01 -0800 | [diff] [blame] | 871 | dvmUnlockMutex(&thread->waitMutex); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 872 | } |
| 873 | } |
| 874 | |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 875 | /* |
Carl Shapiro | 66bb7df | 2010-03-12 15:25:37 -0800 | [diff] [blame] | 876 | * Changes the shape of a monitor from thin to fat, preserving the |
| 877 | * internal lock state. The calling thread must own the lock. |
| 878 | */ |
| 879 | static void inflateMonitor(Thread *self, Object *obj) |
| 880 | { |
| 881 | Monitor *mon; |
| 882 | u4 thin; |
| 883 | |
| 884 | assert(self != NULL); |
| 885 | assert(obj != NULL); |
| 886 | assert(LW_SHAPE(obj->lock) == LW_SHAPE_THIN); |
| 887 | assert(LW_LOCK_OWNER(obj->lock) == self->threadId); |
| 888 | /* Allocate and acquire a new monitor. */ |
| 889 | mon = dvmCreateMonitor(obj); |
| 890 | lockMonitor(self, mon); |
| 891 | /* Propagate the lock state. */ |
| 892 | thin = obj->lock; |
| 893 | mon->lockCount = LW_LOCK_COUNT(thin); |
| 894 | thin &= LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT; |
| 895 | thin |= (u4)mon | LW_SHAPE_FAT; |
| 896 | /* Publish the updated lock word. */ |
Carl Shapiro | 4ba5672 | 2010-06-21 11:04:33 -0700 | [diff] [blame] | 897 | android_atomic_release_store(thin, (int32_t *)&obj->lock); |
Carl Shapiro | 66bb7df | 2010-03-12 15:25:37 -0800 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | /* |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 901 | * Implements monitorenter for "synchronized" stuff. |
| 902 | * |
| 903 | * This does not fail or throw an exception (unless deadlock prediction |
| 904 | * is enabled and set to "err" mode). |
| 905 | */ |
| 906 | void dvmLockObject(Thread* self, Object *obj) |
| 907 | { |
Carl Shapiro | 147dd3f | 2010-03-08 14:38:42 -0800 | [diff] [blame] | 908 | volatile u4 *thinp; |
Carl Shapiro | 147dd3f | 2010-03-08 14:38:42 -0800 | [diff] [blame] | 909 | ThreadStatus oldStatus; |
| 910 | useconds_t sleepDelay; |
| 911 | const useconds_t maxSleepDelay = 1 << 20; |
| 912 | u4 thin, newThin, threadId; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 913 | |
Carl Shapiro | 147dd3f | 2010-03-08 14:38:42 -0800 | [diff] [blame] | 914 | assert(self != NULL); |
| 915 | assert(obj != NULL); |
| 916 | threadId = self->threadId; |
| 917 | thinp = &obj->lock; |
| 918 | retry: |
| 919 | thin = *thinp; |
| 920 | if (LW_SHAPE(thin) == LW_SHAPE_THIN) { |
| 921 | /* |
| 922 | * The lock is a thin lock. The owner field is used to |
| 923 | * determine the acquire method, ordered by cost. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 924 | */ |
Carl Shapiro | 147dd3f | 2010-03-08 14:38:42 -0800 | [diff] [blame] | 925 | if (LW_LOCK_OWNER(thin) == threadId) { |
| 926 | /* |
| 927 | * The calling thread owns the lock. Increment the |
| 928 | * value of the recursion count field. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 929 | */ |
Carl Shapiro | 147dd3f | 2010-03-08 14:38:42 -0800 | [diff] [blame] | 930 | obj->lock += 1 << LW_LOCK_COUNT_SHIFT; |
| 931 | } else if (LW_LOCK_OWNER(thin) == 0) { |
| 932 | /* |
| 933 | * The lock is unowned. Install the thread id of the |
| 934 | * calling thread into the owner field. This is the |
| 935 | * common case. In performance critical code the JIT |
| 936 | * will have tried this before calling out to the VM. |
| 937 | */ |
| 938 | newThin = thin | (threadId << LW_LOCK_OWNER_SHIFT); |
Andy McFadden | 6e10b9a | 2010-06-14 15:24:39 -0700 | [diff] [blame] | 939 | if (android_atomic_release_cas(thin, newThin, |
| 940 | (int32_t*)thinp) != 0) { |
Carl Shapiro | 147dd3f | 2010-03-08 14:38:42 -0800 | [diff] [blame] | 941 | /* |
| 942 | * The acquire failed. Try again. |
| 943 | */ |
| 944 | goto retry; |
| 945 | } |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 946 | } else { |
Carl Shapiro | 147dd3f | 2010-03-08 14:38:42 -0800 | [diff] [blame] | 947 | LOG_THIN("(%d) spin on lock %p: %#x (%#x) %#x", |
| 948 | threadId, &obj->lock, 0, *thinp, thin); |
| 949 | /* |
| 950 | * The lock is owned by another thread. Notify the VM |
| 951 | * that we are about to wait. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 952 | */ |
Carl Shapiro | 147dd3f | 2010-03-08 14:38:42 -0800 | [diff] [blame] | 953 | oldStatus = dvmChangeStatus(self, THREAD_MONITOR); |
| 954 | /* |
| 955 | * Spin until the thin lock is released or inflated. |
| 956 | */ |
| 957 | sleepDelay = 0; |
| 958 | for (;;) { |
| 959 | thin = *thinp; |
| 960 | /* |
| 961 | * Check the shape of the lock word. Another thread |
| 962 | * may have inflated the lock while we were waiting. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 963 | */ |
Carl Shapiro | 147dd3f | 2010-03-08 14:38:42 -0800 | [diff] [blame] | 964 | if (LW_SHAPE(thin) == LW_SHAPE_THIN) { |
| 965 | if (LW_LOCK_OWNER(thin) == 0) { |
| 966 | /* |
| 967 | * The lock has been released. Install the |
| 968 | * thread id of the calling thread into the |
| 969 | * owner field. |
| 970 | */ |
| 971 | newThin = thin | (threadId << LW_LOCK_OWNER_SHIFT); |
Andy McFadden | 6e10b9a | 2010-06-14 15:24:39 -0700 | [diff] [blame] | 972 | if (android_atomic_release_cas(thin, newThin, |
| 973 | (int32_t *)thinp) == 0) { |
Carl Shapiro | 147dd3f | 2010-03-08 14:38:42 -0800 | [diff] [blame] | 974 | /* |
| 975 | * The acquire succeed. Break out of the |
| 976 | * loop and proceed to inflate the lock. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 977 | */ |
Carl Shapiro | 147dd3f | 2010-03-08 14:38:42 -0800 | [diff] [blame] | 978 | break; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 979 | } |
Carl Shapiro | 147dd3f | 2010-03-08 14:38:42 -0800 | [diff] [blame] | 980 | } else { |
| 981 | /* |
| 982 | * The lock has not been released. Yield so |
| 983 | * the owning thread can run. |
| 984 | */ |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 985 | if (sleepDelay == 0) { |
| 986 | sched_yield(); |
Carl Shapiro | 147dd3f | 2010-03-08 14:38:42 -0800 | [diff] [blame] | 987 | sleepDelay = 1000; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 988 | } else { |
| 989 | usleep(sleepDelay); |
| 990 | if (sleepDelay < maxSleepDelay / 2) { |
| 991 | sleepDelay *= 2; |
| 992 | } |
| 993 | } |
| 994 | } |
Carl Shapiro | 147dd3f | 2010-03-08 14:38:42 -0800 | [diff] [blame] | 995 | } else { |
| 996 | /* |
| 997 | * The thin lock was inflated by another thread. |
| 998 | * Let the VM know we are no longer waiting and |
| 999 | * try again. |
| 1000 | */ |
| 1001 | LOG_THIN("(%d) lock %p surprise-fattened", |
| 1002 | threadId, &obj->lock); |
| 1003 | dvmChangeStatus(self, oldStatus); |
| 1004 | goto retry; |
| 1005 | } |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1006 | } |
Carl Shapiro | 147dd3f | 2010-03-08 14:38:42 -0800 | [diff] [blame] | 1007 | LOG_THIN("(%d) spin on lock done %p: %#x (%#x) %#x", |
| 1008 | threadId, &obj->lock, 0, *thinp, thin); |
| 1009 | /* |
| 1010 | * We have acquired the thin lock. Let the VM know that |
| 1011 | * we are no longer waiting. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1012 | */ |
Carl Shapiro | 147dd3f | 2010-03-08 14:38:42 -0800 | [diff] [blame] | 1013 | dvmChangeStatus(self, oldStatus); |
| 1014 | /* |
| 1015 | * Fatten the lock. |
| 1016 | */ |
Carl Shapiro | 66bb7df | 2010-03-12 15:25:37 -0800 | [diff] [blame] | 1017 | inflateMonitor(self, obj); |
Carl Shapiro | 147dd3f | 2010-03-08 14:38:42 -0800 | [diff] [blame] | 1018 | LOG_THIN("(%d) lock %p fattened", threadId, &obj->lock); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1019 | } |
Carl Shapiro | 147dd3f | 2010-03-08 14:38:42 -0800 | [diff] [blame] | 1020 | } else { |
| 1021 | /* |
| 1022 | * The lock is a fat lock. |
| 1023 | */ |
| 1024 | assert(LW_MONITOR(obj->lock) != NULL); |
| 1025 | lockMonitor(self, LW_MONITOR(obj->lock)); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1026 | } |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1027 | #ifdef WITH_DEADLOCK_PREDICTION |
| 1028 | /* |
| 1029 | * See if we were allowed to grab the lock at this time. We do it |
| 1030 | * *after* acquiring the lock, rather than before, so that we can |
| 1031 | * freely update the Monitor struct. This seems counter-intuitive, |
| 1032 | * but our goal is deadlock *prediction* not deadlock *prevention*. |
| 1033 | * (If we actually deadlock, the situation is easy to diagnose from |
| 1034 | * a thread dump, so there's no point making a special effort to do |
| 1035 | * the checks before the lock is held.) |
| 1036 | * |
| 1037 | * This needs to happen before we add the object to the thread's |
| 1038 | * monitor list, so we can tell the difference between first-lock and |
| 1039 | * re-lock. |
| 1040 | * |
| 1041 | * It's also important that we do this while in THREAD_RUNNING, so |
| 1042 | * that we don't interfere with cleanup operations in the GC. |
| 1043 | */ |
| 1044 | if (gDvm.deadlockPredictMode != kDPOff) { |
| 1045 | if (self->status != THREAD_RUNNING) { |
| 1046 | LOGE("Bad thread status (%d) in DP\n", self->status); |
| 1047 | dvmDumpThread(self, false); |
| 1048 | dvmAbort(); |
| 1049 | } |
| 1050 | assert(!dvmCheckException(self)); |
| 1051 | updateDeadlockPrediction(self, obj); |
| 1052 | if (dvmCheckException(self)) { |
| 1053 | /* |
| 1054 | * If we're throwing an exception here, we need to free the |
| 1055 | * lock. We add the object to the thread's monitor list so the |
| 1056 | * "unlock" code can remove it. |
| 1057 | */ |
| 1058 | dvmAddToMonitorList(self, obj, false); |
| 1059 | dvmUnlockObject(self, obj); |
| 1060 | LOGV("--- unlocked, pending is '%s'\n", |
| 1061 | dvmGetException(self)->clazz->descriptor); |
| 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | /* |
| 1066 | * Add the locked object, and the current stack trace, to the list |
| 1067 | * held by the Thread object. If deadlock prediction isn't on, |
| 1068 | * don't capture the stack trace. |
| 1069 | */ |
| 1070 | dvmAddToMonitorList(self, obj, gDvm.deadlockPredictMode != kDPOff); |
| 1071 | #elif defined(WITH_MONITOR_TRACKING) |
| 1072 | /* |
| 1073 | * Add the locked object to the list held by the Thread object. |
| 1074 | */ |
| 1075 | dvmAddToMonitorList(self, obj, false); |
| 1076 | #endif |
| 1077 | } |
| 1078 | |
| 1079 | /* |
| 1080 | * Implements monitorexit for "synchronized" stuff. |
| 1081 | * |
| 1082 | * On failure, throws an exception and returns "false". |
| 1083 | */ |
| 1084 | bool dvmUnlockObject(Thread* self, Object *obj) |
| 1085 | { |
Carl Shapiro | 94338aa | 2009-12-21 11:42:59 -0800 | [diff] [blame] | 1086 | u4 thin; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1087 | |
Carl Shapiro | ef5b4d3 | 2010-01-26 13:22:04 -0800 | [diff] [blame] | 1088 | assert(self != NULL); |
| 1089 | assert(self->status == THREAD_RUNNING); |
| 1090 | assert(obj != NULL); |
Carl Shapiro | ef5b4d3 | 2010-01-26 13:22:04 -0800 | [diff] [blame] | 1091 | /* |
| 1092 | * Cache the lock word as its value can change while we are |
| 1093 | * examining its state. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1094 | */ |
Carl Shapiro | 147dd3f | 2010-03-08 14:38:42 -0800 | [diff] [blame] | 1095 | thin = obj->lock; |
Carl Shapiro | ef5b4d3 | 2010-01-26 13:22:04 -0800 | [diff] [blame] | 1096 | if (LW_SHAPE(thin) == LW_SHAPE_THIN) { |
| 1097 | /* |
| 1098 | * The lock is thin. We must ensure that the lock is owned |
| 1099 | * by the given thread before unlocking it. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1100 | */ |
Carl Shapiro | ef5b4d3 | 2010-01-26 13:22:04 -0800 | [diff] [blame] | 1101 | if (LW_LOCK_OWNER(thin) == self->threadId) { |
| 1102 | /* |
| 1103 | * We are the lock owner. It is safe to update the lock |
| 1104 | * without CAS as lock ownership guards the lock itself. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1105 | */ |
Carl Shapiro | ef5b4d3 | 2010-01-26 13:22:04 -0800 | [diff] [blame] | 1106 | if (LW_LOCK_COUNT(thin) == 0) { |
| 1107 | /* |
| 1108 | * The lock was not recursively acquired, the common |
| 1109 | * case. Unlock by clearing all bits except for the |
| 1110 | * hash state. |
| 1111 | */ |
Carl Shapiro | 147dd3f | 2010-03-08 14:38:42 -0800 | [diff] [blame] | 1112 | obj->lock &= (LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT); |
Carl Shapiro | ef5b4d3 | 2010-01-26 13:22:04 -0800 | [diff] [blame] | 1113 | } else { |
| 1114 | /* |
| 1115 | * The object was recursively acquired. Decrement the |
| 1116 | * lock recursion count field. |
| 1117 | */ |
Carl Shapiro | 147dd3f | 2010-03-08 14:38:42 -0800 | [diff] [blame] | 1118 | obj->lock -= 1 << LW_LOCK_COUNT_SHIFT; |
Carl Shapiro | ef5b4d3 | 2010-01-26 13:22:04 -0800 | [diff] [blame] | 1119 | } |
| 1120 | } else { |
| 1121 | /* |
| 1122 | * We do not own the lock. The JVM spec requires that we |
| 1123 | * throw an exception in this case. |
| 1124 | */ |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1125 | dvmThrowException("Ljava/lang/IllegalMonitorStateException;", |
Carl Shapiro | ef5b4d3 | 2010-01-26 13:22:04 -0800 | [diff] [blame] | 1126 | "unlock of unowned monitor"); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1127 | return false; |
| 1128 | } |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1129 | } else { |
Carl Shapiro | ef5b4d3 | 2010-01-26 13:22:04 -0800 | [diff] [blame] | 1130 | /* |
| 1131 | * The lock is fat. We must check to see if unlockMonitor has |
| 1132 | * raised any exceptions before continuing. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1133 | */ |
Carl Shapiro | 8d7f9b2 | 2009-12-21 20:23:45 -0800 | [diff] [blame] | 1134 | assert(LW_MONITOR(obj->lock) != NULL); |
| 1135 | if (!unlockMonitor(self, LW_MONITOR(obj->lock))) { |
Carl Shapiro | ef5b4d3 | 2010-01-26 13:22:04 -0800 | [diff] [blame] | 1136 | /* |
| 1137 | * An exception has been raised. Do not fall through. |
| 1138 | */ |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1139 | return false; |
| 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | #ifdef WITH_MONITOR_TRACKING |
| 1144 | /* |
| 1145 | * Remove the object from the Thread's list. |
| 1146 | */ |
| 1147 | dvmRemoveFromMonitorList(self, obj); |
| 1148 | #endif |
| 1149 | |
| 1150 | return true; |
| 1151 | } |
| 1152 | |
| 1153 | /* |
| 1154 | * Object.wait(). Also called for class init. |
| 1155 | */ |
| 1156 | void dvmObjectWait(Thread* self, Object *obj, s8 msec, s4 nsec, |
| 1157 | bool interruptShouldThrow) |
| 1158 | { |
Carl Shapiro | 66bb7df | 2010-03-12 15:25:37 -0800 | [diff] [blame] | 1159 | Monitor* mon; |
Carl Shapiro | 8d7f9b2 | 2009-12-21 20:23:45 -0800 | [diff] [blame] | 1160 | u4 thin = obj->lock; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1161 | |
| 1162 | /* If the lock is still thin, we need to fatten it. |
| 1163 | */ |
Carl Shapiro | 94338aa | 2009-12-21 11:42:59 -0800 | [diff] [blame] | 1164 | if (LW_SHAPE(thin) == LW_SHAPE_THIN) { |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1165 | /* Make sure that 'self' holds the lock. |
| 1166 | */ |
Carl Shapiro | 94338aa | 2009-12-21 11:42:59 -0800 | [diff] [blame] | 1167 | if (LW_LOCK_OWNER(thin) != self->threadId) { |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1168 | dvmThrowException("Ljava/lang/IllegalMonitorStateException;", |
| 1169 | "object not locked by thread before wait()"); |
| 1170 | return; |
| 1171 | } |
| 1172 | |
| 1173 | /* This thread holds the lock. We need to fatten the lock |
| 1174 | * so 'self' can block on it. Don't update the object lock |
| 1175 | * field yet, because 'self' needs to acquire the lock before |
| 1176 | * any other thread gets a chance. |
| 1177 | */ |
Carl Shapiro | 66bb7df | 2010-03-12 15:25:37 -0800 | [diff] [blame] | 1178 | inflateMonitor(self, obj); |
| 1179 | LOG_THIN("(%d) lock %p fattened by wait() to count %d", |
| 1180 | self->threadId, &obj->lock, mon->lockCount); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1181 | } |
Carl Shapiro | 66bb7df | 2010-03-12 15:25:37 -0800 | [diff] [blame] | 1182 | mon = LW_MONITOR(obj->lock); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1183 | waitMonitor(self, mon, msec, nsec, interruptShouldThrow); |
| 1184 | } |
| 1185 | |
| 1186 | /* |
| 1187 | * Object.notify(). |
| 1188 | */ |
| 1189 | void dvmObjectNotify(Thread* self, Object *obj) |
| 1190 | { |
Carl Shapiro | 8d7f9b2 | 2009-12-21 20:23:45 -0800 | [diff] [blame] | 1191 | u4 thin = obj->lock; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1192 | |
| 1193 | /* If the lock is still thin, there aren't any waiters; |
| 1194 | * waiting on an object forces lock fattening. |
| 1195 | */ |
Carl Shapiro | 94338aa | 2009-12-21 11:42:59 -0800 | [diff] [blame] | 1196 | if (LW_SHAPE(thin) == LW_SHAPE_THIN) { |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1197 | /* Make sure that 'self' holds the lock. |
| 1198 | */ |
Carl Shapiro | 94338aa | 2009-12-21 11:42:59 -0800 | [diff] [blame] | 1199 | if (LW_LOCK_OWNER(thin) != self->threadId) { |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1200 | dvmThrowException("Ljava/lang/IllegalMonitorStateException;", |
| 1201 | "object not locked by thread before notify()"); |
| 1202 | return; |
| 1203 | } |
| 1204 | |
| 1205 | /* no-op; there are no waiters to notify. |
| 1206 | */ |
| 1207 | } else { |
| 1208 | /* It's a fat lock. |
| 1209 | */ |
Carl Shapiro | 94338aa | 2009-12-21 11:42:59 -0800 | [diff] [blame] | 1210 | notifyMonitor(self, LW_MONITOR(thin)); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1211 | } |
| 1212 | } |
| 1213 | |
| 1214 | /* |
| 1215 | * Object.notifyAll(). |
| 1216 | */ |
| 1217 | void dvmObjectNotifyAll(Thread* self, Object *obj) |
| 1218 | { |
Carl Shapiro | 8d7f9b2 | 2009-12-21 20:23:45 -0800 | [diff] [blame] | 1219 | u4 thin = obj->lock; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1220 | |
| 1221 | /* If the lock is still thin, there aren't any waiters; |
| 1222 | * waiting on an object forces lock fattening. |
| 1223 | */ |
Carl Shapiro | 94338aa | 2009-12-21 11:42:59 -0800 | [diff] [blame] | 1224 | if (LW_SHAPE(thin) == LW_SHAPE_THIN) { |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1225 | /* Make sure that 'self' holds the lock. |
| 1226 | */ |
Carl Shapiro | 94338aa | 2009-12-21 11:42:59 -0800 | [diff] [blame] | 1227 | if (LW_LOCK_OWNER(thin) != self->threadId) { |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1228 | dvmThrowException("Ljava/lang/IllegalMonitorStateException;", |
| 1229 | "object not locked by thread before notifyAll()"); |
| 1230 | return; |
| 1231 | } |
| 1232 | |
| 1233 | /* no-op; there are no waiters to notify. |
| 1234 | */ |
| 1235 | } else { |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1236 | /* It's a fat lock. |
| 1237 | */ |
Carl Shapiro | 94338aa | 2009-12-21 11:42:59 -0800 | [diff] [blame] | 1238 | notifyAllMonitor(self, LW_MONITOR(thin)); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1239 | } |
| 1240 | } |
| 1241 | |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1242 | /* |
| 1243 | * This implements java.lang.Thread.sleep(long msec, int nsec). |
| 1244 | * |
| 1245 | * The sleep is interruptible by other threads, which means we can't just |
| 1246 | * plop into an OS sleep call. (We probably could if we wanted to send |
| 1247 | * signals around and rely on EINTR, but that's inefficient and relies |
| 1248 | * on native code respecting our signal mask.) |
| 1249 | * |
| 1250 | * We have to do all of this stuff for Object.wait() as well, so it's |
| 1251 | * easiest to just sleep on a private Monitor. |
| 1252 | * |
| 1253 | * It appears that we want sleep(0,0) to go through the motions of sleeping |
| 1254 | * for a very short duration, rather than just returning. |
| 1255 | */ |
| 1256 | void dvmThreadSleep(u8 msec, u4 nsec) |
| 1257 | { |
| 1258 | Thread* self = dvmThreadSelf(); |
| 1259 | Monitor* mon = gDvm.threadSleepMon; |
| 1260 | |
| 1261 | /* sleep(0,0) wakes up immediately, wait(0,0) means wait forever; adjust */ |
| 1262 | if (msec == 0 && nsec == 0) |
| 1263 | nsec++; |
| 1264 | |
| 1265 | lockMonitor(self, mon); |
| 1266 | waitMonitor(self, mon, msec, nsec, true); |
| 1267 | unlockMonitor(self, mon); |
| 1268 | } |
| 1269 | |
| 1270 | /* |
| 1271 | * Implement java.lang.Thread.interrupt(). |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1272 | */ |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 1273 | void dvmThreadInterrupt(Thread* thread) |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1274 | { |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 1275 | assert(thread != NULL); |
| 1276 | |
Carl Shapiro | 980ffb0 | 2010-03-13 22:34:01 -0800 | [diff] [blame] | 1277 | dvmLockMutex(&thread->waitMutex); |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 1278 | |
| 1279 | /* |
| 1280 | * If the interrupted flag is already set no additional action is |
| 1281 | * required. |
| 1282 | */ |
| 1283 | if (thread->interrupted == true) { |
Carl Shapiro | 980ffb0 | 2010-03-13 22:34:01 -0800 | [diff] [blame] | 1284 | dvmUnlockMutex(&thread->waitMutex); |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 1285 | return; |
| 1286 | } |
| 1287 | |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1288 | /* |
| 1289 | * Raise the "interrupted" flag. This will cause it to bail early out |
| 1290 | * of the next wait() attempt, if it's not currently waiting on |
| 1291 | * something. |
| 1292 | */ |
| 1293 | thread->interrupted = true; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1294 | |
| 1295 | /* |
| 1296 | * Is the thread waiting? |
| 1297 | * |
| 1298 | * Note that fat vs. thin doesn't matter here; waitMonitor |
| 1299 | * is only set when a thread actually waits on a monitor, |
| 1300 | * which implies that the monitor has already been fattened. |
| 1301 | */ |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 1302 | if (thread->waitMonitor != NULL) { |
| 1303 | pthread_cond_signal(&thread->waitCond); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1304 | } |
| 1305 | |
Carl Shapiro | 980ffb0 | 2010-03-13 22:34:01 -0800 | [diff] [blame] | 1306 | dvmUnlockMutex(&thread->waitMutex); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1307 | } |
| 1308 | |
Carl Shapiro | 30aa997 | 2010-01-13 22:07:50 -0800 | [diff] [blame] | 1309 | #ifndef WITH_COPYING_GC |
Carl Shapiro | 94338aa | 2009-12-21 11:42:59 -0800 | [diff] [blame] | 1310 | u4 dvmIdentityHashCode(Object *obj) |
| 1311 | { |
| 1312 | return (u4)obj; |
| 1313 | } |
Carl Shapiro | 30aa997 | 2010-01-13 22:07:50 -0800 | [diff] [blame] | 1314 | #else |
Carl Shapiro | 30aa997 | 2010-01-13 22:07:50 -0800 | [diff] [blame] | 1315 | /* |
| 1316 | * Returns the identity hash code of the given object. |
| 1317 | */ |
| 1318 | u4 dvmIdentityHashCode(Object *obj) |
| 1319 | { |
| 1320 | Thread *self, *thread; |
| 1321 | volatile u4 *lw; |
Carl Shapiro | bfe4dcc | 2010-04-16 17:55:27 -0700 | [diff] [blame] | 1322 | size_t size; |
Carl Shapiro | 30aa997 | 2010-01-13 22:07:50 -0800 | [diff] [blame] | 1323 | u4 lock, owner, hashState; |
| 1324 | |
| 1325 | if (obj == NULL) { |
| 1326 | /* |
| 1327 | * Null is defined to have an identity hash code of 0. |
| 1328 | */ |
| 1329 | return 0; |
| 1330 | } |
| 1331 | lw = &obj->lock; |
| 1332 | retry: |
| 1333 | hashState = LW_HASH_STATE(*lw); |
| 1334 | if (hashState == LW_HASH_STATE_HASHED) { |
| 1335 | /* |
| 1336 | * The object has been hashed but has not had its hash code |
| 1337 | * relocated by the garbage collector. Use the raw object |
| 1338 | * address. |
| 1339 | */ |
| 1340 | return (u4)obj >> 3; |
| 1341 | } else if (hashState == LW_HASH_STATE_HASHED_AND_MOVED) { |
| 1342 | /* |
| 1343 | * The object has been hashed and its hash code has been |
| 1344 | * relocated by the collector. Use the value of the naturally |
| 1345 | * aligned word following the instance data. |
| 1346 | */ |
Carl Shapiro | c48b6d0 | 2010-05-04 11:19:53 -0700 | [diff] [blame] | 1347 | assert(obj->clazz != gDvm.classJavaLangClass); |
Carl Shapiro | 30aa997 | 2010-01-13 22:07:50 -0800 | [diff] [blame] | 1348 | if (IS_CLASS_FLAG_SET(obj->clazz, CLASS_ISARRAY)) { |
Carl Shapiro | bfe4dcc | 2010-04-16 17:55:27 -0700 | [diff] [blame] | 1349 | size = dvmArrayObjectSize((ArrayObject *)obj); |
Carl Shapiro | c48b6d0 | 2010-05-04 11:19:53 -0700 | [diff] [blame] | 1350 | size = (size + 2) & ~2; |
Carl Shapiro | 30aa997 | 2010-01-13 22:07:50 -0800 | [diff] [blame] | 1351 | } else { |
Carl Shapiro | bfe4dcc | 2010-04-16 17:55:27 -0700 | [diff] [blame] | 1352 | size = obj->clazz->objectSize; |
Carl Shapiro | 30aa997 | 2010-01-13 22:07:50 -0800 | [diff] [blame] | 1353 | } |
Carl Shapiro | bfe4dcc | 2010-04-16 17:55:27 -0700 | [diff] [blame] | 1354 | return *(u4 *)(((char *)obj) + size); |
Carl Shapiro | 30aa997 | 2010-01-13 22:07:50 -0800 | [diff] [blame] | 1355 | } 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); |
Andy McFadden | 6e10b9a | 2010-06-14 15:24:39 -0700 | [diff] [blame] | 1380 | if (android_atomic_release_cas( |
Carl Shapiro | 30aa997 | 2010-01-13 22:07:50 -0800 | [diff] [blame] | 1381 | (int32_t)DVM_LOCK_INITIAL_THIN_VALUE, |
Andy McFadden | 6e10b9a | 2010-06-14 15:24:39 -0700 | [diff] [blame] | 1382 | (int32_t)lock, |
| 1383 | (int32_t *)lw) == 0) { |
Carl Shapiro | 30aa997 | 2010-01-13 22:07:50 -0800 | [diff] [blame] | 1384 | /* |
| 1385 | * A new lockword has been installed with a hash state |
| 1386 | * of hashed. Use the raw object address. |
| 1387 | */ |
| 1388 | return (u4)obj >> 3; |
| 1389 | } |
| 1390 | } else { |
| 1391 | if (tryLockMonitor(self, LW_MONITOR(*lw))) { |
| 1392 | /* |
| 1393 | * The monitor lock has been acquired. Change the |
| 1394 | * hash state to hashed and use the raw object |
| 1395 | * address. |
| 1396 | */ |
| 1397 | *lw |= (LW_HASH_STATE_HASHED << LW_HASH_STATE_SHIFT); |
| 1398 | unlockMonitor(self, LW_MONITOR(*lw)); |
| 1399 | return (u4)obj >> 3; |
| 1400 | } |
| 1401 | } |
| 1402 | /* |
| 1403 | * At this point we have failed to acquire the lock. We must |
| 1404 | * identify the owning thread and suspend it. |
| 1405 | */ |
| 1406 | dvmLockThreadList(self); |
| 1407 | /* |
| 1408 | * Cache the lock word as its value can change between |
| 1409 | * determining its shape and retrieving its owner. |
| 1410 | */ |
| 1411 | lock = *lw; |
| 1412 | if (LW_SHAPE(lock) == LW_SHAPE_THIN) { |
| 1413 | /* |
| 1414 | * Find the thread with the corresponding thread id. |
| 1415 | */ |
| 1416 | owner = LW_LOCK_OWNER(lock); |
| 1417 | assert(owner != self->threadId); |
| 1418 | /* |
| 1419 | * If the lock has no owner do not bother scanning the |
| 1420 | * thread list and fall through to the failure handler. |
| 1421 | */ |
| 1422 | thread = owner ? gDvm.threadList : NULL; |
| 1423 | while (thread != NULL) { |
| 1424 | if (thread->threadId == owner) { |
| 1425 | break; |
| 1426 | } |
| 1427 | thread = thread->next; |
| 1428 | } |
| 1429 | } else { |
| 1430 | thread = LW_MONITOR(lock)->owner; |
| 1431 | } |
| 1432 | /* |
| 1433 | * If thread is NULL the object has been released since the |
| 1434 | * thread list lock was acquired. Try again. |
| 1435 | */ |
| 1436 | if (thread == NULL) { |
| 1437 | dvmUnlockThreadList(); |
| 1438 | goto retry; |
| 1439 | } |
| 1440 | /* |
| 1441 | * Wait for the owning thread to suspend. |
| 1442 | */ |
| 1443 | dvmSuspendThread(thread); |
| 1444 | if (dvmHoldsLock(thread, obj)) { |
| 1445 | /* |
| 1446 | * The owning thread has been suspended. We can safely |
| 1447 | * change the hash state to hashed. |
| 1448 | */ |
| 1449 | *lw |= (LW_HASH_STATE_HASHED << LW_HASH_STATE_SHIFT); |
| 1450 | dvmResumeThread(thread); |
| 1451 | dvmUnlockThreadList(); |
| 1452 | return (u4)obj >> 3; |
| 1453 | } |
| 1454 | /* |
| 1455 | * The wrong thread has been suspended. Try again. |
| 1456 | */ |
| 1457 | dvmResumeThread(thread); |
| 1458 | dvmUnlockThreadList(); |
| 1459 | goto retry; |
| 1460 | } |
| 1461 | LOGE("object %p has an unknown hash state %#x", obj, hashState); |
| 1462 | dvmDumpThread(dvmThreadSelf(), false); |
| 1463 | dvmAbort(); |
| 1464 | return 0; /* Quiet the compiler. */ |
| 1465 | } |
| 1466 | #endif /* WITH_COPYING_GC */ |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1467 | |
| 1468 | #ifdef WITH_DEADLOCK_PREDICTION |
| 1469 | /* |
| 1470 | * =========================================================================== |
| 1471 | * Deadlock prediction |
| 1472 | * =========================================================================== |
| 1473 | */ |
| 1474 | /* |
| 1475 | The idea is to predict the possibility of deadlock by recording the order |
| 1476 | in which monitors are acquired. If we see an attempt to acquire a lock |
| 1477 | out of order, we can identify the locks and offending code. |
| 1478 | |
| 1479 | To make this work, we need to keep track of the locks held by each thread, |
| 1480 | and create history trees for each lock. When a thread tries to acquire |
| 1481 | a new lock, we walk through the "history children" of the lock, looking |
| 1482 | for a match with locks the thread already holds. If we find a match, |
| 1483 | it means the thread has made a request that could result in a deadlock. |
| 1484 | |
| 1485 | To support recursive locks, we always allow re-locking a currently-held |
| 1486 | lock, and maintain a recursion depth count. |
| 1487 | |
| 1488 | An ASCII-art example, where letters represent Objects: |
| 1489 | |
| 1490 | A |
| 1491 | /|\ |
| 1492 | / | \ |
| 1493 | B | D |
| 1494 | \ | |
| 1495 | \| |
| 1496 | C |
| 1497 | |
| 1498 | The above is the tree we'd have after handling Object synchronization |
| 1499 | sequences "ABC", "AC", "AD". A has three children, {B, C, D}. C is also |
| 1500 | a child of B. (The lines represent pointers between parent and child. |
| 1501 | Every node can have multiple parents and multiple children.) |
| 1502 | |
| 1503 | If we hold AC, and want to lock B, we recursively search through B's |
| 1504 | children to see if A or C appears. It does, so we reject the attempt. |
| 1505 | (A straightforward way to implement it: add a link from C to B, then |
| 1506 | determine whether the graph starting at B contains a cycle.) |
| 1507 | |
| 1508 | If we hold AC and want to lock D, we would succeed, creating a new link |
| 1509 | from C to D. |
| 1510 | |
| 1511 | The lock history and a stack trace is attached to the Object's Monitor |
| 1512 | struct, which means we need to fatten every Object we lock (thin locking |
| 1513 | is effectively disabled). If we don't need the stack trace we can |
| 1514 | avoid fattening the leaf nodes, only fattening objects that need to hold |
| 1515 | history trees. |
| 1516 | |
| 1517 | Updates to Monitor structs are only allowed for the thread that holds |
| 1518 | the Monitor, so we actually do most of our deadlock prediction work after |
| 1519 | the lock has been acquired. |
| 1520 | |
| 1521 | When an object with a monitor is GCed, we need to remove it from the |
| 1522 | history trees. There are two basic approaches: |
| 1523 | (1) For through the entire set of known monitors, search all child |
| 1524 | lists for the object in question. This is rather slow, resulting |
| 1525 | in GC passes that take upwards of 10 seconds to complete. |
| 1526 | (2) Maintain "parent" pointers in each node. Remove the entries as |
| 1527 | required. This requires additional storage and maintenance for |
| 1528 | every operation, but is significantly faster at GC time. |
| 1529 | For each GCed object, we merge all of the object's children into each of |
| 1530 | the object's parents. |
| 1531 | */ |
| 1532 | |
| 1533 | #if !defined(WITH_MONITOR_TRACKING) |
| 1534 | # error "WITH_DEADLOCK_PREDICTION requires WITH_MONITOR_TRACKING" |
| 1535 | #endif |
| 1536 | |
| 1537 | /* |
| 1538 | * Clear out the contents of an ExpandingObjectList, freeing any |
| 1539 | * dynamic allocations. |
| 1540 | */ |
| 1541 | static void expandObjClear(ExpandingObjectList* pList) |
| 1542 | { |
| 1543 | if (pList->list != NULL) { |
| 1544 | free(pList->list); |
| 1545 | pList->list = NULL; |
| 1546 | } |
| 1547 | pList->alloc = pList->count = 0; |
| 1548 | } |
| 1549 | |
| 1550 | /* |
| 1551 | * Get the number of objects currently stored in the list. |
| 1552 | */ |
| 1553 | static inline int expandBufGetCount(const ExpandingObjectList* pList) |
| 1554 | { |
| 1555 | return pList->count; |
| 1556 | } |
| 1557 | |
| 1558 | /* |
| 1559 | * Get the Nth entry from the list. |
| 1560 | */ |
| 1561 | static inline Object* expandBufGetEntry(const ExpandingObjectList* pList, |
| 1562 | int i) |
| 1563 | { |
| 1564 | return pList->list[i]; |
| 1565 | } |
| 1566 | |
| 1567 | /* |
| 1568 | * Add a new entry to the list. |
| 1569 | * |
| 1570 | * We don't check for or try to enforce uniqueness. It's expected that |
| 1571 | * the higher-level code does this for us. |
| 1572 | */ |
| 1573 | static void expandObjAddEntry(ExpandingObjectList* pList, Object* obj) |
| 1574 | { |
| 1575 | if (pList->count == pList->alloc) { |
| 1576 | /* time to expand */ |
| 1577 | Object** newList; |
| 1578 | |
| 1579 | if (pList->alloc == 0) |
| 1580 | pList->alloc = 4; |
| 1581 | else |
| 1582 | pList->alloc *= 2; |
| 1583 | LOGVV("expanding %p to %d\n", pList, pList->alloc); |
| 1584 | newList = realloc(pList->list, pList->alloc * sizeof(Object*)); |
| 1585 | if (newList == NULL) { |
| 1586 | LOGE("Failed expanding DP object list (alloc=%d)\n", pList->alloc); |
| 1587 | dvmAbort(); |
| 1588 | } |
| 1589 | pList->list = newList; |
| 1590 | } |
| 1591 | |
| 1592 | pList->list[pList->count++] = obj; |
| 1593 | } |
| 1594 | |
| 1595 | /* |
| 1596 | * Returns "true" if the element was successfully removed. |
| 1597 | */ |
| 1598 | static bool expandObjRemoveEntry(ExpandingObjectList* pList, Object* obj) |
| 1599 | { |
| 1600 | int i; |
| 1601 | |
| 1602 | for (i = pList->count-1; i >= 0; i--) { |
| 1603 | if (pList->list[i] == obj) |
| 1604 | break; |
| 1605 | } |
| 1606 | if (i < 0) |
| 1607 | return false; |
| 1608 | |
| 1609 | if (i != pList->count-1) { |
| 1610 | /* |
| 1611 | * The order of elements is not important, so we just copy the |
| 1612 | * last entry into the new slot. |
| 1613 | */ |
| 1614 | //memmove(&pList->list[i], &pList->list[i+1], |
| 1615 | // (pList->count-1 - i) * sizeof(pList->list[0])); |
| 1616 | pList->list[i] = pList->list[pList->count-1]; |
| 1617 | } |
| 1618 | |
| 1619 | pList->count--; |
| 1620 | pList->list[pList->count] = (Object*) 0xdecadead; |
| 1621 | return true; |
| 1622 | } |
| 1623 | |
| 1624 | /* |
| 1625 | * Returns "true" if "obj" appears in the list. |
| 1626 | */ |
| 1627 | static bool expandObjHas(const ExpandingObjectList* pList, Object* obj) |
| 1628 | { |
| 1629 | int i; |
| 1630 | |
| 1631 | for (i = 0; i < pList->count; i++) { |
| 1632 | if (pList->list[i] == obj) |
| 1633 | return true; |
| 1634 | } |
| 1635 | return false; |
| 1636 | } |
| 1637 | |
| 1638 | /* |
| 1639 | * Print the list contents to stdout. For debugging. |
| 1640 | */ |
| 1641 | static void expandObjDump(const ExpandingObjectList* pList) |
| 1642 | { |
| 1643 | int i; |
| 1644 | for (i = 0; i < pList->count; i++) |
| 1645 | printf(" %p", pList->list[i]); |
| 1646 | } |
| 1647 | |
| 1648 | /* |
| 1649 | * Check for duplicate entries. Returns the index of the first instance |
| 1650 | * of the duplicated value, or -1 if no duplicates were found. |
| 1651 | */ |
| 1652 | static int expandObjCheckForDuplicates(const ExpandingObjectList* pList) |
| 1653 | { |
| 1654 | int i, j; |
| 1655 | for (i = 0; i < pList->count-1; i++) { |
| 1656 | for (j = i + 1; j < pList->count; j++) { |
| 1657 | if (pList->list[i] == pList->list[j]) { |
| 1658 | return i; |
| 1659 | } |
| 1660 | } |
| 1661 | } |
| 1662 | |
| 1663 | return -1; |
| 1664 | } |
| 1665 | |
| 1666 | |
| 1667 | /* |
| 1668 | * Determine whether "child" appears in the list of objects associated |
| 1669 | * with the Monitor in "parent". If "parent" is a thin lock, we return |
| 1670 | * false immediately. |
| 1671 | */ |
| 1672 | static bool objectInChildList(const Object* parent, Object* child) |
| 1673 | { |
Carl Shapiro | 8d7f9b2 | 2009-12-21 20:23:45 -0800 | [diff] [blame] | 1674 | u4 lock = parent->lock; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1675 | if (!IS_LOCK_FAT(&lock)) { |
| 1676 | //LOGI("on thin\n"); |
| 1677 | return false; |
| 1678 | } |
| 1679 | |
Carl Shapiro | 8d7f9b2 | 2009-12-21 20:23:45 -0800 | [diff] [blame] | 1680 | return expandObjHas(&LW_MONITOR(lock)->historyChildren, child); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1681 | } |
| 1682 | |
| 1683 | /* |
| 1684 | * Print the child list. |
| 1685 | */ |
| 1686 | static void dumpKids(Object* parent) |
| 1687 | { |
Carl Shapiro | 8d7f9b2 | 2009-12-21 20:23:45 -0800 | [diff] [blame] | 1688 | Monitor* mon = LW_MONITOR(parent->lock); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1689 | |
| 1690 | printf("Children of %p:", parent); |
| 1691 | expandObjDump(&mon->historyChildren); |
| 1692 | printf("\n"); |
| 1693 | } |
| 1694 | |
| 1695 | /* |
| 1696 | * Add "child" to the list of children in "parent", and add "parent" to |
| 1697 | * the list of parents in "child". |
| 1698 | */ |
| 1699 | static void linkParentToChild(Object* parent, Object* child) |
| 1700 | { |
Carl Shapiro | 8d7f9b2 | 2009-12-21 20:23:45 -0800 | [diff] [blame] | 1701 | //assert(LW_MONITOR(parent->lock)->owner == dvmThreadSelf()); // !owned for merge |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1702 | assert(IS_LOCK_FAT(&parent->lock)); |
| 1703 | assert(IS_LOCK_FAT(&child->lock)); |
| 1704 | assert(parent != child); |
| 1705 | Monitor* mon; |
| 1706 | |
Carl Shapiro | 8d7f9b2 | 2009-12-21 20:23:45 -0800 | [diff] [blame] | 1707 | mon = LW_MONITOR(parent->lock); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1708 | assert(!expandObjHas(&mon->historyChildren, child)); |
| 1709 | expandObjAddEntry(&mon->historyChildren, child); |
| 1710 | |
Carl Shapiro | 8d7f9b2 | 2009-12-21 20:23:45 -0800 | [diff] [blame] | 1711 | mon = LW_MONITOR(child->lock); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1712 | assert(!expandObjHas(&mon->historyParents, parent)); |
| 1713 | expandObjAddEntry(&mon->historyParents, parent); |
| 1714 | } |
| 1715 | |
| 1716 | |
| 1717 | /* |
| 1718 | * Remove "child" from the list of children in "parent". |
| 1719 | */ |
| 1720 | static void unlinkParentFromChild(Object* parent, Object* child) |
| 1721 | { |
Carl Shapiro | 8d7f9b2 | 2009-12-21 20:23:45 -0800 | [diff] [blame] | 1722 | //assert(LW_MONITOR(parent->lock)->owner == dvmThreadSelf()); // !owned for GC |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1723 | assert(IS_LOCK_FAT(&parent->lock)); |
| 1724 | assert(IS_LOCK_FAT(&child->lock)); |
| 1725 | assert(parent != child); |
| 1726 | Monitor* mon; |
| 1727 | |
Carl Shapiro | 8d7f9b2 | 2009-12-21 20:23:45 -0800 | [diff] [blame] | 1728 | mon = LW_MONITOR(parent->lock); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1729 | if (!expandObjRemoveEntry(&mon->historyChildren, child)) { |
| 1730 | LOGW("WARNING: child %p not found in parent %p\n", child, parent); |
| 1731 | } |
| 1732 | assert(!expandObjHas(&mon->historyChildren, child)); |
| 1733 | assert(expandObjCheckForDuplicates(&mon->historyChildren) < 0); |
| 1734 | |
Carl Shapiro | 8d7f9b2 | 2009-12-21 20:23:45 -0800 | [diff] [blame] | 1735 | mon = LW_MONITOR(child->lock); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1736 | if (!expandObjRemoveEntry(&mon->historyParents, parent)) { |
| 1737 | LOGW("WARNING: parent %p not found in child %p\n", parent, child); |
| 1738 | } |
| 1739 | assert(!expandObjHas(&mon->historyParents, parent)); |
| 1740 | assert(expandObjCheckForDuplicates(&mon->historyParents) < 0); |
| 1741 | } |
| 1742 | |
| 1743 | |
| 1744 | /* |
| 1745 | * Log the monitors held by the current thread. This is done as part of |
| 1746 | * flagging an error. |
| 1747 | */ |
| 1748 | static void logHeldMonitors(Thread* self) |
| 1749 | { |
| 1750 | char* name = NULL; |
| 1751 | |
| 1752 | name = dvmGetThreadName(self); |
| 1753 | LOGW("Monitors currently held by thread (threadid=%d '%s')\n", |
| 1754 | self->threadId, name); |
| 1755 | LOGW("(most-recently-acquired on top):\n"); |
| 1756 | free(name); |
| 1757 | |
| 1758 | LockedObjectData* lod = self->pLockedObjects; |
| 1759 | while (lod != NULL) { |
| 1760 | LOGW("--- object %p[%d] (%s)\n", |
| 1761 | lod->obj, lod->recursionCount, lod->obj->clazz->descriptor); |
| 1762 | dvmLogRawStackTrace(lod->rawStackTrace, lod->stackDepth); |
| 1763 | |
| 1764 | lod = lod->next; |
| 1765 | } |
| 1766 | } |
| 1767 | |
| 1768 | /* |
| 1769 | * Recursively traverse the object hierarchy starting at "obj". We mark |
| 1770 | * ourselves on entry and clear the mark on exit. If we ever encounter |
| 1771 | * a marked object, we have a cycle. |
| 1772 | * |
| 1773 | * Returns "true" if all is well, "false" if we found a cycle. |
| 1774 | */ |
| 1775 | static bool traverseTree(Thread* self, const Object* obj) |
| 1776 | { |
| 1777 | assert(IS_LOCK_FAT(&obj->lock)); |
Carl Shapiro | 8d7f9b2 | 2009-12-21 20:23:45 -0800 | [diff] [blame] | 1778 | Monitor* mon = LW_MONITOR(obj->lock); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1779 | |
| 1780 | /* |
| 1781 | * Have we been here before? |
| 1782 | */ |
| 1783 | if (mon->historyMark) { |
| 1784 | int* rawStackTrace; |
| 1785 | int stackDepth; |
| 1786 | |
| 1787 | LOGW("%s\n", kStartBanner); |
| 1788 | LOGW("Illegal lock attempt:\n"); |
| 1789 | LOGW("--- object %p (%s)\n", obj, obj->clazz->descriptor); |
| 1790 | |
| 1791 | rawStackTrace = dvmFillInStackTraceRaw(self, &stackDepth); |
| 1792 | dvmLogRawStackTrace(rawStackTrace, stackDepth); |
| 1793 | free(rawStackTrace); |
| 1794 | |
| 1795 | LOGW(" "); |
| 1796 | logHeldMonitors(self); |
| 1797 | |
| 1798 | LOGW(" "); |
| 1799 | LOGW("Earlier, the following lock order (from last to first) was\n"); |
| 1800 | LOGW("established -- stack trace is from first successful lock):\n"); |
| 1801 | return false; |
| 1802 | } |
| 1803 | mon->historyMark = true; |
| 1804 | |
| 1805 | /* |
| 1806 | * Examine the children. We do NOT hold these locks, so they might |
| 1807 | * very well transition from thin to fat or change ownership while |
| 1808 | * we work. |
| 1809 | * |
| 1810 | * NOTE: we rely on the fact that they cannot revert from fat to thin |
| 1811 | * while we work. This is currently a safe assumption. |
| 1812 | * |
| 1813 | * We can safely ignore thin-locked children, because by definition |
| 1814 | * they have no history and are leaf nodes. In the current |
| 1815 | * implementation we always fatten the locks to provide a place to |
| 1816 | * hang the stack trace. |
| 1817 | */ |
| 1818 | ExpandingObjectList* pList = &mon->historyChildren; |
| 1819 | int i; |
| 1820 | for (i = expandBufGetCount(pList)-1; i >= 0; i--) { |
| 1821 | const Object* child = expandBufGetEntry(pList, i); |
Carl Shapiro | 8d7f9b2 | 2009-12-21 20:23:45 -0800 | [diff] [blame] | 1822 | u4 lock = child->lock; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1823 | if (!IS_LOCK_FAT(&lock)) |
| 1824 | continue; |
| 1825 | if (!traverseTree(self, child)) { |
| 1826 | LOGW("--- object %p (%s)\n", obj, obj->clazz->descriptor); |
| 1827 | dvmLogRawStackTrace(mon->historyRawStackTrace, |
| 1828 | mon->historyStackDepth); |
| 1829 | mon->historyMark = false; |
| 1830 | return false; |
| 1831 | } |
| 1832 | } |
| 1833 | |
| 1834 | mon->historyMark = false; |
| 1835 | |
| 1836 | return true; |
| 1837 | } |
| 1838 | |
| 1839 | /* |
| 1840 | * Update the deadlock prediction tree, based on the current thread |
| 1841 | * acquiring "acqObj". This must be called before the object is added to |
| 1842 | * the thread's list of held monitors. |
| 1843 | * |
| 1844 | * If the thread already holds the lock (recursion), or this is a known |
| 1845 | * lock configuration, we return without doing anything. Otherwise, we add |
| 1846 | * a link from the most-recently-acquired lock in this thread to "acqObj" |
| 1847 | * after ensuring that the parent lock is "fat". |
| 1848 | * |
| 1849 | * This MUST NOT be called while a GC is in progress in another thread, |
| 1850 | * because we assume exclusive access to history trees in owned monitors. |
| 1851 | */ |
| 1852 | static void updateDeadlockPrediction(Thread* self, Object* acqObj) |
| 1853 | { |
| 1854 | LockedObjectData* lod; |
| 1855 | LockedObjectData* mrl; |
| 1856 | |
| 1857 | /* |
| 1858 | * Quick check for recursive access. |
| 1859 | */ |
| 1860 | lod = dvmFindInMonitorList(self, acqObj); |
| 1861 | if (lod != NULL) { |
| 1862 | LOGV("+++ DP: recursive %p\n", acqObj); |
| 1863 | return; |
| 1864 | } |
| 1865 | |
| 1866 | /* |
| 1867 | * Make the newly-acquired object's monitor "fat". In some ways this |
| 1868 | * isn't strictly necessary, but we need the GC to tell us when |
| 1869 | * "interesting" objects go away, and right now the only way to make |
| 1870 | * an object look interesting is to give it a monitor. |
| 1871 | * |
| 1872 | * This also gives us a place to hang a stack trace. |
| 1873 | * |
| 1874 | * Our thread holds the lock, so we're allowed to rewrite the lock |
| 1875 | * without worrying that something will change out from under us. |
| 1876 | */ |
| 1877 | if (!IS_LOCK_FAT(&acqObj->lock)) { |
| 1878 | LOGVV("fattening lockee %p (recur=%d)\n", |
Carl Shapiro | 94338aa | 2009-12-21 11:42:59 -0800 | [diff] [blame] | 1879 | acqObj, LW_LOCK_COUNT(acqObj->lock.thin)); |
Carl Shapiro | 66bb7df | 2010-03-12 15:25:37 -0800 | [diff] [blame] | 1880 | inflateMonitor(self, acqObj); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1881 | } |
| 1882 | |
| 1883 | /* if we don't have a stack trace for this monitor, establish one */ |
Carl Shapiro | 8d7f9b2 | 2009-12-21 20:23:45 -0800 | [diff] [blame] | 1884 | if (LW_MONITOR(acqObj->lock)->historyRawStackTrace == NULL) { |
| 1885 | Monitor* mon = LW_MONITOR(acqObj->lock); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1886 | mon->historyRawStackTrace = dvmFillInStackTraceRaw(self, |
| 1887 | &mon->historyStackDepth); |
| 1888 | } |
| 1889 | |
| 1890 | /* |
| 1891 | * We need to examine and perhaps modify the most-recently-locked |
| 1892 | * monitor. We own that, so there's no risk of another thread |
| 1893 | * stepping on us. |
| 1894 | * |
| 1895 | * Retrieve the most-recently-locked entry from our thread. |
| 1896 | */ |
| 1897 | mrl = self->pLockedObjects; |
| 1898 | if (mrl == NULL) |
| 1899 | return; /* no other locks held */ |
| 1900 | |
| 1901 | /* |
| 1902 | * Do a quick check to see if "acqObj" is a direct descendant. We can do |
| 1903 | * this without holding the global lock because of our assertion that |
| 1904 | * a GC is not running in parallel -- nobody except the GC can |
| 1905 | * modify a history list in a Monitor they don't own, and we own "mrl". |
| 1906 | * (There might be concurrent *reads*, but no concurrent *writes.) |
| 1907 | * |
| 1908 | * If we find it, this is a known good configuration, and we're done. |
| 1909 | */ |
| 1910 | if (objectInChildList(mrl->obj, acqObj)) |
| 1911 | return; |
| 1912 | |
| 1913 | /* |
| 1914 | * "mrl" is going to need to have a history tree. If it's currently |
| 1915 | * a thin lock, we make it fat now. The thin lock might have a |
| 1916 | * nonzero recursive lock count, which we need to carry over. |
| 1917 | * |
| 1918 | * Our thread holds the lock, so we're allowed to rewrite the lock |
| 1919 | * without worrying that something will change out from under us. |
| 1920 | */ |
| 1921 | if (!IS_LOCK_FAT(&mrl->obj->lock)) { |
| 1922 | LOGVV("fattening parent %p f/b/o child %p (recur=%d)\n", |
Carl Shapiro | 8d7f9b2 | 2009-12-21 20:23:45 -0800 | [diff] [blame] | 1923 | mrl->obj, acqObj, LW_LOCK_COUNT(mrl->obj->lock)); |
Carl Shapiro | 66bb7df | 2010-03-12 15:25:37 -0800 | [diff] [blame] | 1924 | inflateMonitor(self, mrl->obj); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1925 | } |
| 1926 | |
| 1927 | /* |
| 1928 | * We haven't seen this configuration before. We need to scan down |
| 1929 | * acqObj's tree to see if any of the monitors in self->pLockedObjects |
| 1930 | * appear. We grab a global lock before traversing or updating the |
| 1931 | * history list. |
| 1932 | * |
| 1933 | * If we find a match for any of our held locks, we know that the lock |
| 1934 | * has previously been acquired *after* acqObj, and we throw an error. |
| 1935 | * |
| 1936 | * The easiest way to do this is to create a link from "mrl" to "acqObj" |
| 1937 | * and do a recursive traversal, marking nodes as we cross them. If |
| 1938 | * we cross one a second time, we have a cycle and can throw an error. |
| 1939 | * (We do the flag-clearing traversal before adding the new link, so |
| 1940 | * that we're guaranteed to terminate.) |
| 1941 | * |
| 1942 | * If "acqObj" is a thin lock, it has no history, and we can create a |
| 1943 | * link to it without additional checks. [ We now guarantee that it's |
| 1944 | * always fat. ] |
| 1945 | */ |
| 1946 | bool failed = false; |
| 1947 | dvmLockMutex(&gDvm.deadlockHistoryLock); |
| 1948 | linkParentToChild(mrl->obj, acqObj); |
| 1949 | if (!traverseTree(self, acqObj)) { |
| 1950 | LOGW("%s\n", kEndBanner); |
| 1951 | failed = true; |
| 1952 | |
| 1953 | /* remove the entry so we're still okay when in "warning" mode */ |
| 1954 | unlinkParentFromChild(mrl->obj, acqObj); |
| 1955 | } |
| 1956 | dvmUnlockMutex(&gDvm.deadlockHistoryLock); |
| 1957 | |
| 1958 | if (failed) { |
| 1959 | switch (gDvm.deadlockPredictMode) { |
| 1960 | case kDPErr: |
| 1961 | dvmThrowException("Ldalvik/system/PotentialDeadlockError;", NULL); |
| 1962 | break; |
| 1963 | case kDPAbort: |
| 1964 | LOGE("Aborting due to potential deadlock\n"); |
| 1965 | dvmAbort(); |
| 1966 | break; |
| 1967 | default: |
| 1968 | /* warn only */ |
| 1969 | break; |
| 1970 | } |
| 1971 | } |
| 1972 | } |
| 1973 | |
| 1974 | /* |
| 1975 | * We're removing "child" from existence. We want to pull all of |
| 1976 | * child's children into "parent", filtering out duplicates. This is |
| 1977 | * called during the GC. |
| 1978 | * |
| 1979 | * This does not modify "child", which might have multiple parents. |
| 1980 | */ |
| 1981 | static void mergeChildren(Object* parent, const Object* child) |
| 1982 | { |
| 1983 | Monitor* mon; |
| 1984 | int i; |
| 1985 | |
| 1986 | assert(IS_LOCK_FAT(&child->lock)); |
Carl Shapiro | 8d7f9b2 | 2009-12-21 20:23:45 -0800 | [diff] [blame] | 1987 | mon = LW_MONITOR(child->lock); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1988 | ExpandingObjectList* pList = &mon->historyChildren; |
| 1989 | |
| 1990 | for (i = expandBufGetCount(pList)-1; i >= 0; i--) { |
| 1991 | Object* grandChild = expandBufGetEntry(pList, i); |
| 1992 | |
| 1993 | if (!objectInChildList(parent, grandChild)) { |
| 1994 | LOGVV("+++ migrating %p link to %p\n", grandChild, parent); |
| 1995 | linkParentToChild(parent, grandChild); |
| 1996 | } else { |
| 1997 | LOGVV("+++ parent %p already links to %p\n", parent, grandChild); |
| 1998 | } |
| 1999 | } |
| 2000 | } |
| 2001 | |
| 2002 | /* |
| 2003 | * An object with a fat lock is being collected during a GC pass. We |
| 2004 | * want to remove it from any lock history trees that it is a part of. |
| 2005 | * |
| 2006 | * This may require updating the history trees in several monitors. The |
| 2007 | * monitor semantics guarantee that no other thread will be accessing |
| 2008 | * the history trees at the same time. |
| 2009 | */ |
| 2010 | static void removeCollectedObject(Object* obj) |
| 2011 | { |
| 2012 | Monitor* mon; |
| 2013 | |
| 2014 | LOGVV("+++ collecting %p\n", obj); |
| 2015 | |
| 2016 | #if 0 |
| 2017 | /* |
| 2018 | * We're currently running through the entire set of known monitors. |
| 2019 | * This can be somewhat slow. We may want to keep lists of parents |
| 2020 | * in each child to speed up GC. |
| 2021 | */ |
| 2022 | mon = gDvm.monitorList; |
| 2023 | while (mon != NULL) { |
| 2024 | Object* parent = mon->obj; |
| 2025 | if (parent != NULL) { /* value nulled for deleted entries */ |
| 2026 | if (objectInChildList(parent, obj)) { |
| 2027 | LOGVV("removing child %p from parent %p\n", obj, parent); |
| 2028 | unlinkParentFromChild(parent, obj); |
| 2029 | mergeChildren(parent, obj); |
| 2030 | } |
| 2031 | } |
| 2032 | mon = mon->next; |
| 2033 | } |
| 2034 | #endif |
| 2035 | |
| 2036 | /* |
| 2037 | * For every parent of this object: |
| 2038 | * - merge all of our children into the parent's child list (creates |
| 2039 | * a two-way link between parent and child) |
| 2040 | * - remove ourselves from the parent's child list |
| 2041 | */ |
| 2042 | ExpandingObjectList* pList; |
| 2043 | int i; |
| 2044 | |
| 2045 | assert(IS_LOCK_FAT(&obj->lock)); |
Carl Shapiro | 8d7f9b2 | 2009-12-21 20:23:45 -0800 | [diff] [blame] | 2046 | mon = LW_MONITOR(obj->lock); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2047 | pList = &mon->historyParents; |
| 2048 | for (i = expandBufGetCount(pList)-1; i >= 0; i--) { |
| 2049 | Object* parent = expandBufGetEntry(pList, i); |
Carl Shapiro | 8d7f9b2 | 2009-12-21 20:23:45 -0800 | [diff] [blame] | 2050 | Monitor* parentMon = LW_MONITOR(parent->lock); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2051 | |
| 2052 | if (!expandObjRemoveEntry(&parentMon->historyChildren, obj)) { |
| 2053 | LOGW("WARNING: child %p not found in parent %p\n", obj, parent); |
| 2054 | } |
| 2055 | assert(!expandObjHas(&parentMon->historyChildren, obj)); |
| 2056 | |
| 2057 | mergeChildren(parent, obj); |
| 2058 | } |
| 2059 | |
| 2060 | /* |
| 2061 | * For every child of this object: |
| 2062 | * - remove ourselves from the child's parent list |
| 2063 | */ |
| 2064 | pList = &mon->historyChildren; |
| 2065 | for (i = expandBufGetCount(pList)-1; i >= 0; i--) { |
| 2066 | Object* child = expandBufGetEntry(pList, i); |
Carl Shapiro | 8d7f9b2 | 2009-12-21 20:23:45 -0800 | [diff] [blame] | 2067 | Monitor* childMon = LW_MONITOR(child->lock); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2068 | |
| 2069 | if (!expandObjRemoveEntry(&childMon->historyParents, obj)) { |
| 2070 | LOGW("WARNING: parent %p not found in child %p\n", obj, child); |
| 2071 | } |
| 2072 | assert(!expandObjHas(&childMon->historyParents, obj)); |
| 2073 | } |
| 2074 | } |
| 2075 | |
| 2076 | #endif /*WITH_DEADLOCK_PREDICTION*/ |