Theoretical basis

Binder

Binder is an interprocess communication mechanism in Android that uses the CS architecture. Binder framework mainly involves four roles: Client, Server, Service Manager and Binder driver. Client, Server and Service Manager run in user space, and Binder driver runs in kernel space.

The thread pool

Thread pooling is a form of multi-threaded processing in which tasks are added to a queue and then automatically started after a thread is created. Thread pool threads are background threads. Each thread uses the default stack size, runs at the default priority, and is in a multithreaded cell.

Simply put: a thread pool is a collection of threads called a thread pool.

Binder thread pool start process

We know that when a new app application process is created, it starts the Binder thread pool by calling zygoteInitNative, a static member of the RunTimeInit class.

Binder thread pool startup calls several key functions: ZygoteInitNative >onZygoteInit >startThreadPool.

The following source code analysis is mainly based on android5.0 version as an example.

ZygoteInitNative source code analysis

Since the ZygoteInitNative function is code implemented in Java, in practice it ends up calling JNI methods implemented in C++. The following code is derived from the system/frameworks/base/core/jni/androidRuntime CPP file

static void com_android_internal_os_RuntimeInit_nativeZygoteInit(JNIEnv* env, jobject clazz)
{
    //gCurRuntime is a global variable followed by an additional implementation method.
    gCurRuntime->onZygoteInit(a); }Copy the code

OnZygoteInit source code analysis

OnZygoteInit function to the location of the source: / frameworks/base/CMDS/app_process/app_main CPP file.

This function is virtual and has no return value and no argumentsvirtual void onZygoteInit(a)
    {
        // Re-enable tracing now that we're no longer in Zygote.
        atrace_set_tracing_enabled(true);
        // Get process status information
        sp<ProcessState> proc = ProcessState::self(a);// Displays log information
        ALOGV("App process: starting thread pool.\n");
        // Start the thread pool
        proc->startThreadPool(a); }Copy the code

StartThreadPool source code analysis

The startThreadPool system is implemented in the \frameworks\native\libs\binder\ processState.cpp file.

Each process that supports Binder interprocess communication has a unique ProcessState object that starts a thread pool in the current process when its member function StartThreadPool is called for the first time. And set the mThreadPoolStarted member variable to true.

// This function is a no-argument, no-return function
void ProcessState::startThreadPool(a)
{
    AutoMutex _l(mLock);
    // Check whether the thread pool is started. If it is started, set the flag information to true.
    if(! mThreadPoolStarted) { mThreadPoolStarted =true;
        spawnPooledThread(true); }}Copy the code

conclusion

Binder is a very important mechanism in the bottom layer of Android. In the actual project invocation process, we only need to implement our own Binder local object in app application, just like other services, we only need to start it. And register with Server Mananger. The internal implementation is generally not a concern.