社科联网站建设方案策划书,苏州好的网站公司名称,中老年适合在哪个网站做直播,云主机网站的空间在哪文章目录 一个Binder的前生今世 (二):Binder进程和线程的创建binder在进程中的启动小结注释一个Binder的前生今世 (二):Binder进程和线程的创建
前篇文章一个Binder的前生今世 (一):Service的创建 讲了一个Service是如何创建以及如何与客户端建立联系的。讲解中涉及到… 文章目录 一个Binder的前生今世 (二):Binder进程和线程的创建binder在进程中的启动小结注释 一个Binder的前生今世 (二):Binder进程和线程的创建
前篇文章一个Binder的前生今世 (一):Service的创建 讲了一个Service是如何创建以及如何与客户端建立联系的。讲解中涉及到了两个类 ProcessState 和 IPCThreadState ,当时没有详细介绍这两个类是怎么来的,只是介绍了它们在Binder客户端和服务端传递的作用。这篇文章我们就来深入了解下这两个类以及和binder的关系。可以说这两个类整个串联起了Binder驱动和Binder应用的联系,在Binder的架构实现中属于中流砥柱的作用。
binder在进程中的启动
首先要明确一个概念: 一个进程中对应一个Binder进程(也就是后文说的ProcessState)用来管理与Binder驱动的通讯和Binder对应的应用线程(后文说的IPCThreadState)。
要说明Binder在进程中如何启动,我们就需要先了解一个android的应用是如何创建起来的。当然我们这篇文章不介绍Android应用是如何创建的,不了解的可以网上查看其他的资料,很多,我也会另写文章记录。
Android的应用都会通过app_main.cpp来创建,惯例,我们先来明确用到的类的路径:
app_main.cpp : Android/frameworks/base/cmds/app_process/app_main.cpp
IPCThreadState : Android/frameworks/native/libs/binder/IPCThreadState.cpp
ProcessState : Android/frameworks/native/libs/binder/ProcessState.cpp好,我们了解到,Android应用的启动都会走到app_main.cpp中的AppRuntime类的onZygoteInit函数:
virtual void onZygoteInit(){spProcessState proc = ProcessState::self();ALOGV("App process: starting thread pool.\n");proc-startThreadPool();}好,这里我们今天的第一个主角类登场了:ProcessState。先来了解下这个类。 这个类和IPCthreadState在Android的Binder架构体系中启动连接应用层和驱动层的作用,应用和驱动层打交道的所有接口调用都是在这两个类中的。 所以,每当一个应用初始化的时候,都会调用到这里,那我们就接着往下看这个ProcessState是如何初始化的:
spProcessState ProcessState::self()
{Mutex::Autolock _l(gProcessMutex);if (gProcess != nullptr) {return gProcess;}gProcess = new ProcessState(kDefaultDriver);return gProcess;
}这个self函数就是一个单例模式,创建ProcessState:
ProcessState::ProcessState(const char *driver): mDriverName(String8(driver)), mDriverFD(open_driver(driver)), mVMStart(MAP_FAILED), mThreadCountLock(PTHREAD_MUTEX_INITIALIZER), mThreadCountDecrement(PTHREAD_COND_INITIALIZER), mExecutingThreadsCount(0), mMaxThreads(DEFAULT_MAX_BINDER_THREADS), mStarvationStartTimeMs(0), mBinderContextCheckFunc(nullptr), mBinderContextUserData(nullptr), mThreadPoolStarted(false), mThreadPoolSeq(1), mCallRestriction(CallRestriction::NONE)
{// TODO(b/139016109): enforce in build system
#if defined(__ANDROID_APEX__)LOG_ALWAYS_FATAL("Cannot use libbinder in APEX (only system.img libbinder) since it is not stable.");
#endifif (mDriverFD = 0) {// mmap the binder, providing a chunk of virtual address space to receive transactions.mVMStart = mmap(nullptr, BINDER_VM_SIZE, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, mDriverFD, 0);if (mVMStart == MAP_FAILED) {// *sigh*ALOGE("Using %s failed: unable to mmap transaction memory.\n", mDriverName.c_str());close(mDriverFD);mDriverFD = -1;mDriverName.clear();}}#ifdef __ANDROID__LOG_ALWAYS_FATAL_IF(mDriverFD 0, "Binder driver '%s' could not be opened. Terminating.", driver);
#endif
}这里可以大概看出这个类是管理一个线程池的作用,另外还有管理binder驱动,我们可以看到第3行open_driver ,这里就是打开了Bidner驱动:
static int open_driver(const char *driver)
{int fd = open(driver, O_RDWR | O_CLOEXEC);if (fd = 0) {int vers = 0;status_t result = ioctl(fd, BINDER_VERSION, vers);if (result == -1) {ALOGE("Binder ioctl to obtain version failed: %s", strerror(errno));close(fd);fd = -1;}if (result != 0 || vers != BINDER_CURRENT_PROTOCOL_VERSION) {ALOGE("Binder driver protocol(%d) does not match user space protocol(%d)! ioctl() return value: %d",vers, BINDER_CURRENT_PROTOCOL_VERSION, result);close(fd);fd = -1;}size_t maxThreads = DEFAULT_MAX_BINDER_THREADS;result = ioctl(fd, BINDER_SET_MAX_THREADS, maxThreads);if (result == -1) {ALOGE("Binder ioctl to set max threads failed: %s", strerror(errno));}} else {ALOGW("Opening '%s' failed: %s\n", driver, strerror(errno));}return fd;
}
这里除了open驱动外,还设置了最大线程数,这里DEFAULT_MAX_BINDER_THREADS为15:
#define DEFAULT_MAX_BINDER_THREADS 15然后,我们在回过头继续看ProcessState构造函数,接着初始化了一个锁mThreadCountLock 1和 一个条件变量 mThreadCountDecrement 2,最后初始花了内存映射:
mVMStart = mmap(nullptr, BINDER_VM_SIZE, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, mDriverFD, 0);这里可以看到一个进程的mVMStart 内存映射大小为:
#define BINDER_VM_SIZE ((1 * 1024 * 1024) - sysconf(_SC_PAGE_SIZE) * 2)好了,到这里ProcessState的初始化就完成了,它是一个线程池,管理进程中所有的Binder线程,它还负责进程的Binder驱动打开和初始化操作,然后就是开启了与Binder驱动的内存映射。
我们接着来看proc-startThreadPool():
void ProcessState::startThreadPool()
{AutoMutex _l(mLock);if (!mThreadPoolStarted) {mThreadPoolStarted = true;spawnPooledThread(true);}
}从名字可以看出这个方法的作用就是启动线程池了。我们继续往下跟踪:
void ProcessState::spawnPooledThread(bool isMain)
{if (mThreadPoolStarted) {String8 name = makeBinderThreadName();ALOGV("Spawning new pooled thread, name=%s\n", name.string());spThread t = new PoolThread(isMain);t-run(name.string());}
}这里的作用就是启动一个Binder线程了,接着我们就来到了查看PoolThread是个什么东东:
class PoolThread : public Thread
{
public:explicit PoolThread(bool isMain): mIsMain(isMain){}protected:virtual bool threadLoop(){IPCThreadState::self( 文章转载自: http://www.morning.incmt.com.gov.cn.incmt.com http://www.morning.fldk.cn.gov.cn.fldk.cn http://www.morning.qsy39.cn.gov.cn.qsy39.cn http://www.morning.wsgyq.cn.gov.cn.wsgyq.cn http://www.morning.sooong.com.gov.cn.sooong.com http://www.morning.hmxrs.cn.gov.cn.hmxrs.cn http://www.morning.kxrhj.cn.gov.cn.kxrhj.cn http://www.morning.dgckn.cn.gov.cn.dgckn.cn http://www.morning.mcmpq.cn.gov.cn.mcmpq.cn http://www.morning.rqnzh.cn.gov.cn.rqnzh.cn http://www.morning.wklhn.cn.gov.cn.wklhn.cn http://www.morning.jlschmy.com.gov.cn.jlschmy.com http://www.morning.wjqyt.cn.gov.cn.wjqyt.cn http://www.morning.fbmzm.cn.gov.cn.fbmzm.cn http://www.morning.gwmjy.cn.gov.cn.gwmjy.cn http://www.morning.dfckx.cn.gov.cn.dfckx.cn http://www.morning.rjmd.cn.gov.cn.rjmd.cn http://www.morning.nptls.cn.gov.cn.nptls.cn http://www.morning.sbrrf.cn.gov.cn.sbrrf.cn http://www.morning.trjp.cn.gov.cn.trjp.cn http://www.morning.xyrw.cn.gov.cn.xyrw.cn http://www.morning.wxwall.com.gov.cn.wxwall.com http://www.morning.pqcrz.cn.gov.cn.pqcrz.cn http://www.morning.sfsjh.cn.gov.cn.sfsjh.cn http://www.morning.tymnr.cn.gov.cn.tymnr.cn http://www.morning.mgzjz.cn.gov.cn.mgzjz.cn http://www.morning.hslgq.cn.gov.cn.hslgq.cn http://www.morning.xtlty.cn.gov.cn.xtlty.cn http://www.morning.htsrm.cn.gov.cn.htsrm.cn http://www.morning.kcbml.cn.gov.cn.kcbml.cn http://www.morning.lzrpy.cn.gov.cn.lzrpy.cn http://www.morning.tssmk.cn.gov.cn.tssmk.cn http://www.morning.qymqh.cn.gov.cn.qymqh.cn http://www.morning.lgmgn.cn.gov.cn.lgmgn.cn http://www.morning.ylzdx.cn.gov.cn.ylzdx.cn http://www.morning.lmbm.cn.gov.cn.lmbm.cn http://www.morning.pzdxg.cn.gov.cn.pzdxg.cn http://www.morning.xpzrx.cn.gov.cn.xpzrx.cn http://www.morning.kbynw.cn.gov.cn.kbynw.cn http://www.morning.jfnbh.cn.gov.cn.jfnbh.cn http://www.morning.tkcct.cn.gov.cn.tkcct.cn http://www.morning.lkfsk.cn.gov.cn.lkfsk.cn http://www.morning.fnrkh.cn.gov.cn.fnrkh.cn http://www.morning.ckhyj.cn.gov.cn.ckhyj.cn http://www.morning.clfct.cn.gov.cn.clfct.cn http://www.morning.wsyst.cn.gov.cn.wsyst.cn http://www.morning.wkmpx.cn.gov.cn.wkmpx.cn http://www.morning.dygsz.cn.gov.cn.dygsz.cn http://www.morning.tsnq.cn.gov.cn.tsnq.cn http://www.morning.hxhrg.cn.gov.cn.hxhrg.cn http://www.morning.wttzp.cn.gov.cn.wttzp.cn http://www.morning.hsjrk.cn.gov.cn.hsjrk.cn http://www.morning.sffwz.cn.gov.cn.sffwz.cn http://www.morning.gjxr.cn.gov.cn.gjxr.cn http://www.morning.clbzy.cn.gov.cn.clbzy.cn http://www.morning.rkdhh.cn.gov.cn.rkdhh.cn http://www.morning.xxhc.cn.gov.cn.xxhc.cn http://www.morning.dansj.com.gov.cn.dansj.com http://www.morning.jrslj.cn.gov.cn.jrslj.cn http://www.morning.kjcll.cn.gov.cn.kjcll.cn http://www.morning.fqlxg.cn.gov.cn.fqlxg.cn http://www.morning.fykrm.cn.gov.cn.fykrm.cn http://www.morning.gwjsm.cn.gov.cn.gwjsm.cn http://www.morning.weiwt.com.gov.cn.weiwt.com http://www.morning.mtbsd.cn.gov.cn.mtbsd.cn http://www.morning.ymhjb.cn.gov.cn.ymhjb.cn http://www.morning.xxlz.cn.gov.cn.xxlz.cn http://www.morning.fjtnh.cn.gov.cn.fjtnh.cn http://www.morning.xlbyx.cn.gov.cn.xlbyx.cn http://www.morning.sthgm.cn.gov.cn.sthgm.cn http://www.morning.rjljb.cn.gov.cn.rjljb.cn http://www.morning.rcrnw.cn.gov.cn.rcrnw.cn http://www.morning.yfqhc.cn.gov.cn.yfqhc.cn http://www.morning.mspqw.cn.gov.cn.mspqw.cn http://www.morning.skmpj.cn.gov.cn.skmpj.cn http://www.morning.pwwjs.cn.gov.cn.pwwjs.cn http://www.morning.xoaz.cn.gov.cn.xoaz.cn http://www.morning.qsy37.cn.gov.cn.qsy37.cn http://www.morning.mtbsd.cn.gov.cn.mtbsd.cn http://www.morning.ysbrz.cn.gov.cn.ysbrz.cn