* - check to see if the library is valid
* - check config/prelink-linux-arm.map to ensure that the library
* is listed and is not being overrun by the previous entry (if
- * loading suddenly stops working, this is a good one to check)
+ * loading suddenly stops working on a prelinked library, this is
+ * a good one to check)
+ * - write a trivial app that calls sleep() then dlopen(), attach
+ * to it with "strace -p <pid>" while it sleeps, and watch for
+ * attempts to open nonexistent dependent shared libs
+ *
+ * This can execute slowly for a large library on a busy system, so we
+ * want to switch from RUNNING to VMWAIT while it executes. This allows
+ * the GC to ignore us.
*/
+ Thread* self = dvmThreadSelf();
+ int oldStatus = dvmChangeStatus(self, THREAD_VMWAIT);
handle = dlopen(pathName, RTLD_LAZY);
+ dvmChangeStatus(self, oldStatus);
+
if (handle == NULL) {
LOGI("Unable to dlopen(%s): %s\n", pathName, dlerror());
return false;
Object* prevOverride = self->classLoaderOverride;
self->classLoaderOverride = classLoader;
- dvmChangeStatus(NULL, THREAD_NATIVE);
+ oldStatus = dvmChangeStatus(self, THREAD_NATIVE);
version = (*func)(gDvm.vmList, NULL);
- dvmChangeStatus(NULL, THREAD_RUNNING);
+ dvmChangeStatus(self, oldStatus);
self->classLoaderOverride = prevOverride;
if (version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4 &&
if (cc != 0) {
if (!dvmCheckSuspendPending(NULL)) {
/*
- * Could be we hit the window as the suspend or resume
- * was started (i.e. the lock has been grabbed but the
- * other thread hasn't yet set our "please suspend" flag).
+ * Could be that a resume-all is in progress, and something
+ * grabbed the CPU when the wakeup was broadcast. The thread
+ * performing the resume hasn't had a chance to release the
+ * thread suspend lock. (Should no longer be an issue --
+ * we now release before broadcast.)
+ *
+ * Could be we hit the window as a suspend was started,
+ * and the lock has been grabbed but the suspend counts
+ * haven't been incremented yet.
*
* Could be an unusual JNI thread-attach thing.
*
LOGE("threadid=%d: couldn't get thread-suspend lock (%s:%s),"
" bailing\n",
dvmThreadSelf()->threadId, who, getSuspendCauseStr(why));
+ /* threads are not suspended, thread dump could crash */
dvmDumpAllThreads(false);
dvmAbort();
}
*/
dvmUnlockThreadList();
- if (pthread_create(&threadHandle, &threadAttr, interpThreadStart,
- newThread) != 0)
- {
+ int cc, oldStatus;
+ oldStatus = dvmChangeStatus(self, THREAD_VMWAIT);
+ cc = pthread_create(&threadHandle, &threadAttr, interpThreadStart,
+ newThread);
+ oldStatus = dvmChangeStatus(self, oldStatus);
+
+ if (cc != 0) {
/*
* Failure generally indicates that we have exceeded system
* resource limits. VirtualMachineError is probably too severe,
*
* The easiest way to deal with this is to prevent the new thread from
* running until the parent says it's okay. This results in the
- * following sequence of events for a "badly timed" GC:
+ * following (correct) sequence of events for a "badly timed" GC
+ * (where '-' is us, 'o' is the child, and '+' is some other thread):
*
* - call pthread_create()
* - lock thread list
{
const int kMaxRetries = 10;
const int kSpinSleepTime = 750*1000; /* 0.75s */
+ bool complained = false;
int sleepIter = 0;
int retryCount = 0;
self->threadId, (int)self->handle,
thread->threadId, (int)thread->handle);
dumpWedgedThread(thread);
+ complained = true;
// keep going; could be slow due to valgrind
sleepIter = 0;
}
}
}
+
+ if (complained) {
+ LOGW("threadid=%d: spin on suspend resolved\n", self->threadId);
+ //dvmDumpThread(thread, false); /* suspended, so dump is safe */
+ }
}
/*
/* debugger events don't suspend JDWP thread */
if ((why == SUSPEND_FOR_DEBUG || why == SUSPEND_FOR_DEBUG_EVENT) &&
thread->handle == dvmJdwpGetDebugThread(gDvm.jdwpState))
+ {
continue;
+ }
if (thread->suspendCount > 0) {
thread->suspendCount--;
dvmUnlockThreadList();
/*
+ * In some ways it makes sense to continue to hold the thread-suspend
+ * lock while we issue the wakeup broadcast. It allows us to complete
+ * one operation before moving on to the next, which simplifies the
+ * thread activity debug traces.
+ *
+ * This approach caused us some difficulty under Linux, because the
+ * condition variable broadcast not only made the threads runnable,
+ * but actually caused them to execute, and it was a while before
+ * the thread performing the wakeup had an opportunity to release the
+ * thread-suspend lock.
+ *
+ * This is a problem because, when a thread tries to acquire that
+ * lock, it times out after 3 seconds. If at some point the thread
+ * is told to suspend, the clock resets; but since the VM is still
+ * theoretically mid-resume, there's no suspend pending. If, for
+ * example, the GC was waking threads up while the SIGQUIT handler
+ * was trying to acquire the lock, we would occasionally time out on
+ * a busy system and SignalCatcher would abort.
+ *
+ * We now perform the unlock before the wakeup broadcast. The next
+ * suspend can't actually start until the broadcast completes and
+ * returns, because we're holding the thread-suspend-count lock, but the
+ * suspending thread is now able to make progress and we avoid the abort.
+ *
+ * (Technically there is a narrow window between when we release
+ * the thread-suspend lock and grab the thread-suspend-count lock.
+ * This could cause us to send a broadcast to threads with nonzero
+ * suspend counts, but this is expected and they'll all just fall
+ * right back to sleep. It's probably safe to grab the suspend-count
+ * lock before releasing thread-suspend, since we're still following
+ * the correct order of acquisition, but it feels weird.)
+ */
+
+ LOG_THREAD("threadid=%d: ResumeAll waking others\n", self->threadId);
+ unlockThreadSuspend();
+
+ /*
* Broadcast a notification to all suspended threads, some or all of
* which may choose to wake up. No need to wait for them.
*/
assert(cc == 0);
unlockThreadSuspendCount();
- unlockThreadSuspend();
-
LOG_THREAD("threadid=%d: ResumeAll complete\n", self->threadId);
}
threadName, isDaemon ? " daemon" : "",
priority, thread->threadId, kStatusNames[thread->status]);
dvmPrintDebugMessage(target,
- " | group=\"%s\" sCount=%d dsCount=%d s=%c obj=%p\n",
+ " | group=\"%s\" sCount=%d dsCount=%d s=%c obj=%p self=%p\n",
groupName, thread->suspendCount, thread->dbgSuspendCount,
- thread->isSuspended ? 'Y' : 'N', thread->threadObj);
+ thread->isSuspended ? 'Y' : 'N', thread->threadObj, thread);
dvmPrintDebugMessage(target,
" | sysTid=%d nice=%d sched=%d/%d handle=%d\n",
thread->systemTid, getpriority(PRIO_PROCESS, thread->systemTid),