nuwa.cpp的作用,fork 对线程进行相关冻结设置复制等。线程级别的设置等。。。。
https://developer.mozilla.org/en-US/docs/Archive/B2G_OS/Security/B2G_IPC_internals
https://blog.csdn.net/hunter___/article/details/82912266
具体介绍如下内容:
通道的建立,通道的连接(b2g (parent )如何连接到通道,nuwa如何连接到通道),fork,进程的孵化,
b2g中主线程和ioloop线程之间进行了通道连接?
Nuwa中也一样,它的主线程与ioloop线程也进行连接?


gecko/mozglue/build/Nuwa.h
|
。。。
extern "C" {
/**
* The following 3 methods are used to synchronously spawn the Nuwa process.
* The typical usage is:
*
* The IPC thread The main thread
* 1. Receives the request.
* 2. NuwaSpawnPrepare().
* 3. Request main thread to
* spawn. --------------------------------------+
* 4. NuwaSpawnWait(). V
* 5. NuwaSpawn() to clone all
* the threads in the Nuwa
* process (including main
* & IPC threads).
* 6. NuwaSpawn() finishes and
* +-------------------------------------- signals NuwaSpawnWait().
* V
* 7. NuwaSpawnWait() returns.
*/
/**
* Prepare for spawning a new process. The calling thread (typically the IPC
* thread) will block further requests to spawn a new process.
*/
MFBT_API void NuwaSpawnPrepare();
/**
* Called on the thread that receives the request. Used to wait until
* NuwaSpawn() finishes and maintain the stack of the calling thread.
*/
MFBT_API void NuwaSpawnWait();
/**
* Spawn a new content process, called in the Nuwa process. This has to run on
* the main thread.
*
* @return The process ID of the new process.
*/
MFBT_API pid_t NuwaSpawn();
/**
* The following methods are for marking threads created in the Nuwa process so
* they can be frozen and then recreated in the spawned process. All threads
* except the main thread must be marked by one of the following 4 methods;
* otherwise, we have a thread that is created but unknown to us. The Nuwa
* process will never become ready and this needs to be fixed.
*/
/**
* Mark the current thread as supporting Nuwa. The thread will implicitly freeze
* by calling a blocking method such as pthread_mutex_lock() or epoll_wait().
*
* @param recreate The custom function that will be called in the spawned
* process, after the thread is recreated. Can be nullptr if no
* custom function to be called after the thread is recreated.
* Note that this function is called duing thread recreation
* while other threads are frozen. It must not perform any
* action (e.g. acquiring a mutex) that might depend on another
* thread that is still blocked.
* @param arg The argument passed to the custom function. Can be nullptr.
*/
MFBT_API void NuwaMarkCurrentThread(void (*recreate)(void *), void *arg);
/**
* Mark the current thread as not supporting Nuwa. The calling thread will not
* be automatically cloned from the Nuwa process to spawned process. If this
* thread needs to be created in the spawned process, use NuwaAddConstructor()
* or NuwaAddFinalConstructor() to do it.
*/
MFBT_API void NuwaSkipCurrentThread();
/**
* Force the current thread to freeze explicitly at the current point.
*/
MFBT_API void NuwaFreezeCurrentThread();
/**
* Helper functions for the NuwaCheckpointCurrentThread() macro.
*/
MFBT_API jmp_buf* NuwaCheckpointCurrentThread1();
MFBT_API bool NuwaCheckpointCurrentThread2(int setjmpCond);
/**
* Set the point to recover after thread recreation. The calling thread is not
* blocked. This is used for the thread that cannot freeze (i.e. the IPC
* thread).
*/
#define NuwaCheckpointCurrentThread() \
NuwaCheckpointCurrentThread2(setjmp(*NuwaCheckpointCurrentThread1()))
/**
* The following methods are called in the initialization stage of the Nuwa
* process.
*/
/**
* Prepare for making the Nuwa process.
*/
MFBT_API void PrepareNuwaProcess();
/**
* Make the current process a Nuwa process. Start to freeze the threads.
*/
MFBT_API void MakeNuwaProcess();
/**
* Register a method to be invoked after a new process is spawned. The method
* will be invoked on the main thread *before* recreating the other threads.
* The registered method must not perform any action (e.g. acquiring a mutex)
* that might depend on another thread that has not yet been recreated.
*
* @param construct The method to be invoked.
* @param arg The argument passed to the method.
*/
MFBT_API void NuwaAddConstructor(void (*construct)(void *), void *arg);
/**
* Register a method to be invoked after a new process is spawned and threads
* are recreated. The method will be invoked on the main thread *after*
* the other threads are recreated and fully functional.
*
* @param construct The method to be invoked.
* @param arg The argument passed to the method.
*/
MFBT_API void NuwaAddFinalConstructor(void (*construct)(void *), void *arg);
/**
* Register a method to be invoked after the current thread is recreated in the
* spawned process. Note that this function is called while other threads are
* frozen. It must not perform any action (e.g. acquiring a mutex) that might
* depend on another thread that is still blocked.
*
* @param construct The method to be invoked.
* @param arg The argument passed to the method.
*/
MFBT_API void NuwaAddThreadConstructor(void (*construct)(void *), void *arg);
/**
* The methods to query the Nuwa-related states.
*/
/**
* @return If the current process is the Nuwa process.
*/
MFBT_API bool IsNuwaProcess();
/**
* @return If the Nuwa process is ready for spawning new processes.
*/
MFBT_API bool IsNuwaReady();
#if defined(DEBUG) || defined(ENABLE_TESTS)
/**
* Asserts that aThread is not frozen.
*/
MFBT_API void NuwaAssertNotFrozen(unsigned int aThread,
const char* aThreadName);
#else
#define NuwaAssertNotFrozen(aThread, aThreadName) do {} while(0);
#endif
};
#endif /* __NUWA_H_ */
|
gecko/dom/ipc/


gecko/dom/ipc/ .cpp .h .ipdl 总体来讲就是dom 中的进程间通信(Ipc)模块
NuwaParent.cpp
dom/ipc/PreallocatedProcessManager.cpp:68: void NuwaFork()决定何时需要创建新进程,在NuwaChild中被调,往上继续可以追溯到APP请求?
PContentParent.h 在b2g main thread 中,SendNuwaFork() 发送fork新进程的消息,
PContentChild.h 在nuwa main thread中,会RecvNuwaFork(),并开始去做真正的fork工作。fork完之后会发消息给b2g让它sendAddNewProcess 。
ioloop 线程专门用于接收进程间的消息。ioloop线程与main thread 建立通道。
socketpair+messageloop+ipdl机制
Nuwa从b2gloader这一线
gecko/b2g/app/B2GLoader.cpp:307, Fuc:RunProcesses-->
ipc/glue/ProcessUtils_linux.cpp:692, Fuc:XRE_ProcLoaderServiceRun-->
gecko/toolkit/xre/nsAppRunner.cpp:4595, Fuc:XRE_InitCommandLine-->
gecko/ipc/glue/ProcessUtils_linux.cpp:626, Fuc:ProcLoaderServiceRun call -- process = new ContentProcess(aPeerPid:180)--->
gecko/ipc/glue/ProcessChild.cpp:56, Fuc:ProcessChild--->
gecko/dom/ipc/ContentChild.cpp:637, Fuc:ContentChild-->
ContentChild和ContentParent都及大,总体作用是什么,
class ContentProcess : public mozilla::ipc::ProcessChild
如下关系,
contentprocess
contentparent
contentchild
nsI这块到底是谁在用?
nsIContentChild
nsIcontentparent
nuwaparent
nuwachild

牢记nuwa在ProcLoaderServiceRun上面运行,
../ipc/glue/ProcessUtils_linux.cpp:22:#include "mozilla/ipc/PProcLoaderChild.h"
../ipc/glue/ProcessUtils_linux.cpp:21:#include "mozilla/ipc/PProcLoaderParent.h" Load的细节,不管了。
ProcessUtils_linux.cpp用于处理接收进程间的load 请求等
nuwa与plugin-container的关系,开始时plugin是必须的,后加入Nuwa以加快速度。所以,目前正常状态,plugin-container只在第一次fork出进程时被nuwa使用,nuwa成熟之后,所有工作就不劳烦plungin,而是全权由nuwa来,nuwa的目的就在于提升速度横插进来的模块。setprocessasnuwa也就可以理解了。
明白了,第一步是b2g请求plugin-container Load nuwa ,此时此进程还不是nuwa进程,只是成长中的nuwa process,本质是plugin-container过渡态进程,直到plugin-container Load 完nuwa,并将此Plungin 进程设置为Nuwa进程。ok,现在nuwa出来了。后面所有进程的创建都丢给nuwa吧。于是后面系统起来之后,分别启动了3个进程,先后应该如下:homescreen,keyboard,preallocated
没错。
