Android应用程序进程启动过程的源代码分析(3)
-
class AppRuntime : public AndroidRuntime
-
{
-
......
-
-
virtual void onZygoteInit()
-
{
-
sp<ProcessState> proc = ProcessState::self();
-
if (proc->supportsProcesses()) {
-
LOGV("App process: starting thread pool.\n");
-
proc->startThreadPool();
-
}
-
}
-
-
......
-
};
Step 11. ProcessState.startThreadPool
这个函数定义在frameworks/base/libs/binder/ProcessState.cpp文件中:
-
void ProcessState::startThreadPool()
-
{
-
AutoMutex _l(mLock);
-
if (!mThreadPoolStarted) {
-
mThreadPoolStarted = true;
-
spawnPooledThread(true);
-
}
-
}
-
void ProcessState::spawnPooledThread(bool isMain)
-
{
-
if (mThreadPoolStarted) {
-
int32_t s = android_atomic_add(1, &mThreadPoolSeq);
-
char buf[32];
-
sprintf(buf, "Binder Thread #%d", s);
-
LOGV("Spawning new pooled thread, name=%s\n", buf);
-
sp<Thread> t = new PoolThread(isMain);
-
t->run(buf);
-
}
-
}
-
class PoolThread : public Thread
-
{
-
public:
-
PoolThread(bool isMain)
-
: mIsMain(isMain)
-
{
-
}
-
-
protected:
-
virtual bool threadLoop()
-
{
-
IPCThreadState::self()->joinThreadPool(mIsMain);
-
return false;
-
}
-
-
const bool mIsMain;
-
};
-
void IPCThreadState::joinThreadPool(bool isMain)
-
{
-
......
-
-
mOut.writeInt32(isMain ? BC_ENTER_LOOPER : BC_REGISTER_LOOPER);
-
-
......
-
-
status_t result;
-
do {
-
int32_t cmd;
-
-
......
-
-
// now get the next command to be processed, waiting if necessary
-
result = talkWithDriver();
-
if (result >= NO_ERROR) {
-
size_t IN = mIn.dataAvail();
-
if (IN < sizeof(int32_t)) continue;
-
cmd = mIn.readInt32();
-
......
-
-
result = executeCommand(cmd);
-
}
-
-
......
-
} while (result != -ECONNREFUSED && result != -EBADF);
-
-
......
-
-
mOut.writeInt32(BC_EXIT_LOOPER);
-
talkWithDriver(false);
-
}
-
mOut.writeInt32(isMain ? BC_ENTER_LOOPER : BC_REGISTER_LOOPER);
-
result = talkWithDriver();
-
result = executeCommand(cmd);
-
mOut.writeInt32(BC_EXIT_LOOPER);
-
talkWithDriver(false);