IPCThreadState 详解
来源:互联网 发布:淘宝店招图 编辑:程序博客网 时间:2024/05/17 23:49
IPC是Android的精华,找了个时间,详细分析下IPCThreadState 的组织架构及其功能
初始化:用这个方法,我们可以在每个线程里取得IPCThreadState
IPCThreadState* IPCThreadState::self()
{
// 全局标志位,代表是否TSD已经建立
if (gHaveTLS) {
restart:
const pthread_key_t k = gTLS;
// 获取线程私有数据,即IPCThreadState本身,没有则创建。 保证每个线程都有相对应的IPCThreadState
IPCThreadState* st = (IPCThreadState*)pthread_getspecific(k);
if (st) return st;
return new IPCThreadState;
}
if (gShutdown) return NULL;
pthread_mutex_lock(&gTLSMutex);
if (!gHaveTLS) {
// 如果线程私有数据没建立,则创建一个。
if (pthread_key_create(&gTLS, threadDestructor) != 0) {
pthread_mutex_unlock(&gTLSMutex);
return NULL;
}
gHaveTLS = true;
}
pthread_mutex_unlock(&gTLSMutex);
// 创建完,恢复到启始逻辑
goto restart;
}
看了它的构造函数,明显,这就是为每个线程邦定了个结构体。
然后,就是IPCThreadState的构造函数,非常简单简洁
IPCThreadState::IPCThreadState()
: mProcess(ProcessState::self()),
mMyThreadId(androidGetTid()),
mStrictModePolicy(0),
mLastTransactionBinderFlags(0)
{
pthread_setspecific(gTLS, this);
clearCaller();
mIn.setDataCapacity(256);
mOut.setDataCapacity(256);
}
可以看到,mIn和mOut 直接设了个容量,
接下来总结下各个功能
talkWithDriver(false); 细节以后分析,不需要接受命令,但会把当前须传出的命令传下去
#endif
break;
mOut.writeInt32((int32_t)obj);
reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),tr.data_size,reinterpret_cast<const size_t*>(tr.data.ptr.offsets),tr.offsets_size/sizeof(size_t), freeBuffer, this);构造了个parcel, 然后传进来的数据被按进去了const pid_t origPid = mCallingPid;
const uid_t origUid = mCallingUid;
mCallingPid = tr.sender_pid;
mCallingUid = tr.sender_euid;
if (curPrio > ANDROID_PRIORITY_NORMAL) {
// We have inherited a reduced priority from the caller, but do not
// want to run in that state in this process. The driver set our
// priority already (though not our scheduling class), so bounce
// it back to the default before invoking the transaction.
setpriority(PRIO_PROCESS, mMyThreadId, ANDROID_PRIORITY_NORMAL);
}
} else {
if (curPrio >= ANDROID_PRIORITY_BACKGROUND) {
// We want to use the inherited priority from the caller.
// Ensure this thread is in the background scheduling class,
// since the driver won't modify scheduling classes for us.
// The scheduling group is reset to default by the caller
// once this method returns after the transaction is complete.
androidSetThreadSchedulingGroup(mMyThreadId,
ANDROID_TGROUP_BG_NONINTERACT);
}
}
sp<BBinder> b((BBinder*)tr.cookie);
const status_t error = b->transact(tr.code, buffer, &reply, tr.flags);
if (error < NO_ERROR) reply.setError(error);
如果这个BBinder包含了一个可用的object, 即服务存在,则调用服务的transact
} else {
const status_t error = the_context_object->transact(tr.code, buffer, &reply, tr.flags);
if (error < NO_ERROR) reply.setError(error);
}
sendReply(reply, 0);
} else {
LOG_ONEWAY("NOT sending reply to %d!", mCallingPid);
}
mOut.writeInt32(BC_DEAD_BINDER_DONE);
mOut.writeInt32((int32_t)proxy);
void setTheContextObject(sp<BBinder> obj)
{
the_context_object = obj;
}
tr.data_size = data.ipcDataSize();
tr.data.ptr.buffer = data.ipcData();
tr.offsets_size = data.ipcObjectsCount()*sizeof(size_t);
tr.data.ptr.offsets = data.ipcObjects();
} else if (statusBuffer) {
tr.flags |= TF_STATUS_CODE;
*statusBuffer = err;
tr.data_size = sizeof(status_t);
tr.data.ptr.buffer = statusBuffer;
tr.offsets_size = 0;
tr.data.ptr.offsets = NULL;
} else {
return (mLastError = err);
}
最后是写下去
bwr.write_buffer = (long unsigned int)mOut.data();
bwr.read_size = mIn.dataCapacity();
bwr.read_buffer = (long unsigned int)mIn.data();
} else {
bwr.read_size = 0;
bwr.read_buffer = 0;
} 如果需要读,且读buffer确实为空,则设置准备读
status_t err;
do {
if (ioctl(mProcess->mDriverFD, BINDER_WRITE_READ, &bwr) >= 0)
err = NO_ERROR;
else
err = -errno;
if (err >= NO_ERROR) {
if (bwr.write_consumed < (ssize_t)mOut.dataSize())
mOut.remove(0, bwr.write_consumed);
else
mOut.setDataSize(0);
}
if (bwr.read_consumed > 0) {
mIn.setDataSize(bwr.read_consumed);
mIn.setDataPosition(0);
if (err < NO_ERROR) break;
if (mIn.dataAvail() == 0) continue;
goto finish;
err = FAILED_TRANSACTION;
case BR_ACQUIRE_RESULT:{LOG_ASSERT(acquireResult != NULL, "Unexpected brACQUIRE_RESULT");const int32_t result = mIn.readInt32();if (!acquireResult) continue;*acquireResult = result ? NO_ERROR : INVALID_OPERATION;}返回结果,暂时搞不懂下层在什么情况下返回这个,难道是某种状态查询?case BR_REPLY:
{
binder_transaction_data tr;
err = mIn.read(&tr, sizeof(tr));
if (err != NO_ERROR) goto finish;
真的读进了个返回数据
if (reply) { // 参数
if ((tr.flags & TF_STATUS_CODE) == 0) {
reply->ipcSetDataReference(
reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
tr.data_size,
reinterpret_cast<const size_t*>(tr.data.ptr.offsets),
tr.offsets_size/sizeof(size_t),
freeBuffer, this);
需要搞明白这是要干什么,不满足的情况都释放掉
} else {
err = *static_cast<const status_t*>(tr.data.ptr.buffer);
freeBuffer(NULL,
reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
tr.data_size,
reinterpret_cast<const size_t*>(tr.data.ptr.offsets),
tr.offsets_size/sizeof(size_t), this);
}
} else {
freeBuffer(NULL,
reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
tr.data_size,
reinterpret_cast<const size_t*>(tr.data.ptr.offsets),
tr.offsets_size/sizeof(size_t), this);
continue;
}
}
goto finish;
这个要反过来再讲一讲
if (err < NO_ERROR) return err;
return waitForResponse(NULL, NULL);
if (mIn.dataPosition() >= mIn.dataSize()) {
size_t numPending = mPendingWeakDerefs.size();
if (numPending > 0) {
for (size_t i = 0; i < numPending; i++) {
RefBase::weakref_type* refs = mPendingWeakDerefs[i];
refs->decWeak(mProcess.get());
}
mPendingWeakDerefs.clear();
}
numPending = mPendingStrongDerefs.size();
if (numPending > 0) {
for (size_t i = 0; i < numPending; i++) {
BBinder* obj = mPendingStrongDerefs[i];
obj->decStrong(mProcess.get());
}
mPendingStrongDerefs.clear();
}
}
result = talkWithDriver();
if (result >= NO_ERROR) {
size_t IN = mIn.dataAvail();
if (IN < sizeof(int32_t)) continue;
cmd = mIn.readInt32();
IF_LOG_COMMANDS() {
alog << "Processing top-level Command: "
<< getReturnString(cmd) << endl;
}
result = executeCommand(cmd);
}
- IPCThreadState 详解
- IPCThreadState
- IPCThreadState
- IPCThreadState中的TLS
- binder 与 ProcessState & IPCThreadState
- Android - Binder机制 - ProcessState和IPCThreadState
- Android - Binder机制 - ProcessState和IPCThreadState
- Android - Binder机制 - ProcessState和IPCThreadState
- Binder中的ProcessState和IPCThreadState分析
- Android Binder ProcessState & IPCThreadState相关介绍
- Android Binder ProcessState & IPCThreadState相关介绍
- Android - Binder机制 - ProcessState和IPCThreadState
- Android开发之ProcessState和IPCThreadState类分析
- Android开发之ProcessState和IPCThreadState类分析
- Android5.0中Binder相关的ProcessState和IPCThreadState的认识.
- 详解
- 详解
- 详解
- form 表单中日期正则表达式判断
- CentOS下配置MySQL (Linux文件系统移动MySQL数据库的位置)
- Javascript中创建对象的各种模式
- C++中const、volatile、mutable的用法
- 内存泄露打印new和delete重载
- IPCThreadState 详解
- extern 和 external“C”的分析
- android平板上的GridView视图缓存优化
- 定义一个:_ConnectionPtr m_pConnection;老是报错,解决方法:
- javascript中的观察者模式实现
- jsp+Ajax实现上传文件进度条
- Android入门第十三篇之Gallery + ImageSwitcher
- IE6 图片固定在最右下角,不抖解决方法
- [乐意黎原创] 移动硬盘IO /IO 错误