源码位置:
frameworks/native/libs/binder/IPCThreadState.cpp
https://blog.csdn.net/CrazyMo_/article/details/129143626
目录
方法
实例化
每个线程都有一个IPCThreadState
-
构造函数
802 IPCThreadState::IPCThreadState() 803 : mProcess(ProcessState::self()), 804 mWorkSource(kUnsetWorkSource), 805 mPropagateWorkSource(false), 806 mStrictModePolicy(0), 807 mLastTransactionBinderFlags(0), 808 mCallRestriction(mProcess->mCallRestriction) 809 { 810 pthread_setspecific(gTLS, this); 811 clearCaller(); 812 mIn.setDataCapacity(256); 813 mOut.setDataCapacity(256); 814 mIPCThreadStateBase = IPCThreadStateBase::self(); 815 }
mProcess(ProcessState::self())
将当前线程的进程指针记录起来
pthread_setspecific(gTLS, this);
将IPCThreadState实例与线程绑定 -
self
281 static bool gHaveTLS = false; 282 static pthread_key_t gTLS = 0; 286 IPCThreadState* IPCThreadState::self() 287 { 288 if (gHaveTLS) { 289 restart: 290 const pthread_key_t k = gTLS; 291 IPCThreadState* st = (IPCThreadState*)pthread_getspecific(k); 292 if (st) return st; 293 return new IPCThreadState; 294 } 295 296 if (gShutdown) { 297 ALOGW("Calling IPCThreadState::self() during shutdown is dangerous, expect a crash.\n"); 298 return nullptr; 299 } 300 301 pthread_mutex_lock(&gTLSMutex); 302 if (!gHaveTLS) { 303 int key_create_value = pthread_key_create(&gTLS, threadDestructor); 304 if (key_create_value != 0) { 305 pthread_mutex_unlock(&gTLSMutex); 306 ALOGW("IPCThreadState::self() unable to create TLS key, expect a crash: %s\n", 307 strerror(key_create_value)); 308 return nullptr; 309 } 310 gHaveTLS = true; 311 } 312 pthread_mutex_unlock(&gTLSMutex); 313 goto restart; 314 }
talkWithDriver(与binder驱动交互的核心方法)
ioctl(mProcess->mDriverFD, BINDER_WRITE_READ, &bwr)
各种xxHandle()方法
- incWeakHandle
737 void IPCThreadState::incWeakHandle(int32_t handle, BpBinder *proxy)
738 {
739 LOG_REMOTEREFS("IPCThreadState::incWeakHandle(%d)\n", handle);
740 mOut.writeInt32(BC_INCREFS);
741 mOut.writeInt32(handle);
742 // Create a temp reference until the driver has handled this command.
743 proxy->getWeakRefs()->incWeak(mProcess.get());
744 mPostWriteWeakDerefs.push(proxy->getWeakRefs());
745 }
0 条评论