Binder thread pool and handler message loops for System, application processes

Source: Internet
Author: User

First look at an Android system startup flowchart:


One of the two most important indicators of a process is the initiation of the binder thread pool, which is the ability to process binder interprocess communication. Another is the launch of the handler message loop, which enables the use of the message loop mechanism.


1. When did the Systemserver process implement the above two mechanisms? See Code:

The binder thread pool is started. is a child thread pool.

public static final void Zygoteinit (string[] argv)            throws Zygoteinit.methodandargscaller {        ...        Zygoteinitnative ();        ......}

The binder main thread pool is started, such as the following:

extern "C" status_t system_init () {Logi ("entered System_init ()");        sp<processstate> proc (processstate::self ());    sp<iservicemanager> sm = Defaultservicemanager ();        Logi ("ServiceManager:%p\n", Sm.get ());    Sp<grimreaper> grim = new Grimreaper ();        Sm->asbinder ()->linktodeath (Grim, Grim.get (), 0);    Char Propbuf[property_value_max];    Property_get ("System_init.startsurfaceflinger", Propbuf, "1");    if (strcmp (Propbuf, "1") = = 0) {//Start the Surfaceflinger surfaceflinger::instantiate ();    }//Start the Sensor service sensorservice::instantiate (); On the simulator, Audioflinger et al don't get started the//same as on the device, and we need to start them h        ere if (!proc->supportsprocesses ()) {//Start the Audioflinger audioflinger::instantiate ();        Start the Media Playback service mediaplayerservice::instantiate (); Start the camera service CamerAservice::instantiate ();    Start the Audio Policy Service audiopolicyservice::instantiate ();  }//And now start the Android runtime. We have to do this bit//of nastiness because the Android runtime initialization requires//some of the core syste    M services to already is started. All other servers should just start the Android runtime at//the beginning of their processes ' s main (), before call    ing//the init function.        Logi ("System server:starting Android runtime.\n");    androidruntime* runtime = Androidruntime::getruntime ();    Logi ("System server:starting Android services.\n");            Runtime->callstatic ("Com/android/server/systemserver", "init2");  If running in We own process, just go into the thread/pool.    Otherwise, call the initialization finished//func-let this process continue its initilization.        if (proc->supportsprocesses ()) {Logi ("System server:entering thread pool.\n"); ProcEssstate::self ()->startthreadpool ();//The thread pool has been started ipcthreadstate::self ()->jointhreadpool ();//The current main thread increases the thread pool    Logi ("System server:exiting thread pool.\n"); } return no_error;}

Started the hander message loop mechanism, which is the handler of the child thread:

public static final void Init2 () {        slog.i (TAG, "entered the Android system server!");        Thread thr = new Serverthread ();        Thr.setname ("Android.server.ServerThread");        Thr.start ();    }
Class Serverthread extends Thread {@Override public void run () {eventlog.writeevent (Eventlogtags.boot_progress_        System_run, Systemclock.uptimemillis ());        Looper.prepare ();            ...... try {...            SLOG.I (TAG, "Activity Manager");            Context = Activitymanagerservice.main (factorytest);//Initialize Activitytask ...            SLOG.I (TAG, "package Manager"); PM = Packagemanagerservice.main (context, factorytest! = Systemserver.factory_test_off);//Initialize Packageman            Gerservice activitymanagerservice.setsystemprocess (); ... wm = Windowmanagerservice.main (context, power, factorytest! = Systemserver.factory_tes            T_low_level);            Servicemanager.addservice (Context.window_service, WM);        ...... ((Activitymanagerservice) Activitymanagernative.getdefault ()). Systemready (New Runnable () {Publi C void Run () {}});        Looper.loop ();    SLOG.D (TAG, "System Serverthread is exiting!"); }}

2. When does the application process implement the above two mechanisms? See Code:

The binder thread pool is started. As with the above, it is a child thread pool.

public static final void Zygoteinit (string[] argv)            throws Zygoteinit.methodandargscaller {        ...        Zygoteinitnative ();        ......}
started the hander message loop mechanism, which is the handler of the main thread:

public static final void main (string[] args) {        samplingprofilerintegration.start ();        Process.setargv0 ("<pre-initialized>");        Looper.preparemainlooper ();        if (Smainthreadhandler = = null) {            Smainthreadhandler = new Handler ();        }        Activitythread thread = new Activitythread ();        Thread.attach (false);        ....        Looper.loop ();        ......    }

Binder thread pool and handler message loops for System, application processes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.