WEB开发网
开发学院手机开发Android 开发 Android深入浅出之Binder机制 阅读

Android深入浅出之Binder机制

 2012-06-30 10:47:32 来源:WEB开发网   
核心提示:virtual const String16& getInterfaceDescriptor() const;protected:virtual IBinder* onAsBinder();};兑现后变成class BnInterface :public IMediaPlayerServi
virtual const String16& getInterfaceDescriptor() const;

protected:
virtual IBinder* onAsBinder();
};
兑现后变成
class BnInterface :public IMediaPlayerService, public BBinder
BBinder?BpBinder?是不是和BnXXX以及BpXXX对应的呢?如果是,为什么又叫BBinder呢?
BBinder::BBinder()
: mExtras(NULL)
{
//没有打开设备的地方啊?
}
完了?难道我们走错方向了吗?难道不是每个Service都有对应的binder设备fd吗?
.......
回想下,我们的Main_MediaService程序,有哪里打开过binder吗?
int main(int argc,char** argv)
{
//对啊,我在ProcessState中不是打开过binder了吗?

sp<ProcessState> proc(ProcessState::self());
sp<IServiceManager> sm =defaultServiceManager();
MediaPlayerService::instantiate();
......
3.2 looper
啊?原来打开binder设备的地方是和进程相关的啊?一个进程打开一个就可以了。那么,我在哪里进行类似的消息循环looper操作呢?
...
//难道是下面两个?
ProcessState::self()->startThreadPool();
IPCThreadState::self()->joinThreadPool();
看看startThreadPool吧
voidProcessState::startThreadPool()
{
...
spawnPooledThread(true);
}
voidProcessState::spawnPooledThread(bool isMain)
{
sp<Thread> t = newPoolThread(isMain);isMain是TRUE
//创建线程池,然后run起来,和java的Thread何其像也。
t->run(buf);
}
PoolThread从Thread类中派生,那么此时会产生一个线程吗?看看PoolThread和Thread的构造吧
PoolThread::PoolThread(boolisMain)
: mIsMain(isMain)
{
}
Thread::Thread(boolcanCallJava)//canCallJava默认值是true
: mCanCallJava(canCallJava),
mThread(thread_id_t(-1)),
mLock("Thread::mLock"),
mStatus(NO_ERROR),
mExitPending(false), mRunning(false)
{
}
喔,这个时候还没有创建线程呢。然后调用PoolThread::run,实际调用了基类的run。
status_tThread::run(const char* name, int32_t priority, size_t stack)
{
bool res;
if (mCanCallJava) {
res = createThreadEtc(_threadLoop,//线程函数是_threadLoop
this, name, priority, stack,&mThread);
}
//终于,在run函数中,创建线程了。从此
主线程执行
IPCThreadState::self()->joinThreadPool();
新开的线程执行_threadLoop
我们先看看_threadLoop
intThread::_threadLoop(void* user)
{
Thread* const self =static_cast<Thread*>(user);
sp<Thread>strong(self->mHoldSelf);
wp<Thread> weak(strong);
self->mHoldSelf.clear();

do {
...
if (result && !self->mExitPending){
result = self->threadLoop();哇塞,调用自己的threadLoop
}
}
我们是PoolThread对象,所以调用PoolThread的threadLoop函数
virtualbool PoolThread ::threadLoop()
{
//mIsMain为true。
//而且注意,这是一个新的线程,所以必然会创建一个
新的IPCThreadState对象(记得线程本地存储吗?TLS),然后
IPCThreadState::self()->joinThreadPool(mIsMain);
return false;
}
主线程和工作线程都调用了joinThreadPool,看看这个干嘛了!

上一页  7 8 9 10 11 12 13 14 15  下一页

Tags:Android 深入浅出 Binder

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接