1 服务如何注册到 SM 中
getIServiceManager().addService(name, service, false);
getIServiceManger --- new ServiceManagerProxy(new BinderProxy())
BinderInternal.getContextObject --- 返回 BinderProxy 对象
ProcessState::self()->getContextObject: 创建一个 BpBinder
javaObjectForIBinder -->BinderProxy和BpBinder相互绑定
ServiceManagerNative.asInterface
返回 ServiceManagerProxy
addService
data.writeStrongBinder(service); --service == AMS --> 将 AMS放入data中
mRemote.transact ---mRemote == BinderProxyxx
获取 BpBinder --- IPCThreaState::transact
1. writeTransactionData ---out写入命令 --write ----cmd = BC_TRANSACTION
2. waitForResponse
talkWithDriver --- 非常重要
Binder_transaction
handle == 0 --> sm
1.target_node == sm
2.proc
3.保存todo,wait
4.创建t, tcomplete
5.数据拷贝
6.binder_transaction_binder --转换为--> handle
7.thread-->transaction_stack = t; ---> 方便sm找到client
8.t-->work.type = BINDER_WORK_TRANSACTION; -- 给sm --> 做事
9.tcomplete-->type = BINDER_WORK_TRANSACTION_COMPLETE; --给client--挂起
10.wake_up_interruptible 唤醒sm
client挂起
BR_NOOP, BR_TRANSACTION_COMPLETE
wait_event_freezable --- 挂起
sm处理添加服务
BINDER_WORK_TRANSACTION ---要处理 cmd == BR_TRANSACTION
1. reply 初始化
2.res = func(bs, txn, &msg, &reply); --- 函数指针 --- svcmgr_handler 作用;获取或者添加service。sm 是用 svclist 保存所有服务的
3.binder_send_reply --- bc_reply
4.t-->work.type = BINDER_WORK_TRANSACTION; -- 给 client
tcomplete -->type = BINDER_WORK_TRANSACTION_COMPLETE; 给 SM --被挂起
5.wake_up_interruptible(target_wait); --- 唤醒 Client
client 被唤醒
BINDER_WORK_TRANSACTION --- cmd = BR_REPLY;
SM 处理 onTransact
IPCThreadState::executeCommand
error = reinterpret_cast<BBinder*>(tr.cookie)->transact(tr.code, buffer, &reply, tr.flags);
javaBBinder.ontransact --- C++
jboolean res = env -->CallBooleanMethod(mObject, gBinderOffsets.mExectransact,code, reinterpret_cast<jlong>(&data), reinterpret_cast<jlong>(reply), flags); -- BInder.java.execTransact方法
线程池管理:
1. 主线程 --- 不会退出,非主线程退出
2. 线程最大数 = 15 --- 非主线程
3. 主线程1个,不算在线程最大数中
4. 线程真正最大数 == 15 + 1 + 其它线程