Binder(四) Native层的服务的过程

framework/native/libs/binder
  - Binder.cpp
  - BpBinder.cpp
  - IPCThreadState.cpp
  - ProcessState.cpp
  - IServiceManager.cpp
  - IInterface.cpp
  - Parcel.cpp
defaultServiceManager
sp<IServiceManager> defaultServiceManager()
{
   
    if (gDefaultServiceManager != NULL) return gDefaultServiceManager;
    {
   
        AutoMutex _l(gDefaultServiceManagerLock); 
        while (gDefaultServiceManager == NULL) {
   
              gDefaultServiceManager = interface_cast<IServiceManager>(
                ProcessState::self()->getContextObject(NULL));
            if (gDefaultServiceManager == NULL)
                sleep(1);
        }
    }
    return gDefaultServiceManager;
}
ProcessState 进程状态,单例

ProcessState表示进程状态

//单例模式
sp<ProcessState> ProcessState::self()
{
   
    Mutex::Autolock _l(gProcessMutex);
    if (gProcess != NULL) {
   
        return gProcess;
    }
    gProcess = new ProcessState;
    return gProcess;
}

ProcessState::ProcessState()
    : mDriverFD(open_driver())//打开Binder驱动
    , mVMStart(MAP_FAILED)
   ……
    , mMaxThreads(DEFAULT_MAX_BINDER_THREADS)
   ……
    , mThreadPoolStarted(false)
    , mThreadPoolSeq(1)
{
   
    if (mDriverFD >= 0) {
   
        // mmap the binder, providing a chunk of virtual address space to receive transactions.
        mVMStart = mmap(0, BINDER_VM_SIZE, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, mDriverFD, 0);
        if (mVMStart == MAP_FAILED) {
   
            // *sigh*
            ALOGE("Using /dev/binder failed: unable to mmap transaction memory.\n");
            close(mDriverFD);
            mDriverFD = -1;
        }
    }

    LOG_ALWAYS_FATAL_IF(mDriverFD < 0, "Binder driver could not be opened.  Terminating.");
}
//获取ServiceManager对应的BpBinder代理对象
sp<IBinder> ProcessState::getContextObject(const sp<IBinder>& /*caller*/)
{
   
    return getStrongProxyForHandle(0); 
}
sp<IBinder> ProcessState::getStrongProxyForHandle(int32_t handle)
{
   
    sp<IBinder> result;

    AutoMutex _l(mLock);
    //查找
    handle_entry* e = lookupHandleLocked(handle);

    if (e != NULL) {
   
        IBinder* b = e->binder;
        if (b == NULL || !e->refs->attemptIncWeak(this)) {
   
            if (handle == 0) {
   
                Parcel data;
                status_t status = IPCThreadState::self()->transact(
                        0, IBinder::PING_TRANSACTION, data, NULL, 0);
                if (status == DEAD_OBJECT)
                   return NULL;
            }
            b = new BpBinder(handle);   //新建句柄0对应的BpBinder对象,代理句柄
            e->binder = b;
            if (b) e->refs = b->getWeakRefs();
            result = b;
        } else {
   
            result.force_set(b);
            e->refs->decWeak(this);
        }
    }
    return result;
}
ProcessState::handle_entry* ProcessState::lookupHandleLocked(int32_t handle)
{
   
	//mHandleToObject是ProcessState中保存句柄Vector矢量数组
    const size_t N=mHandleToObject.size();
    if (N <= (size_t)handle) {
   
	   // 新建handle_entry,并将其添加到mHandleToObject中,然后返回handle_entry
        handle_entry e;
        e.binder = NULL;
        e.refs = NULL;
        status_t err = mHandleToObject.insertAt(e, N, handle+1-N);
        if (err < NO_ERROR) return NULL;
    }
    return &mHandleToObject.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值