1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mozglue/build/Nuwa.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,171 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim: set sw=2 ts=2 autoindent cindent expandtab: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#ifndef __NUWA_H_ 1.11 +#define __NUWA_H_ 1.12 + 1.13 +#include <setjmp.h> 1.14 +#include <unistd.h> 1.15 + 1.16 +#include "mozilla/Types.h" 1.17 + 1.18 +/** 1.19 + * Nuwa is a goddess who created mankind according to her figure in the 1.20 + * ancient Chinese mythology. 1.21 + */ 1.22 + 1.23 +// Max number of top level protocol that can be handled by Nuwa process. 1.24 +#ifndef NUWA_TOPLEVEL_MAX 1.25 +#define NUWA_TOPLEVEL_MAX 8 1.26 +#endif 1.27 + 1.28 +struct NuwaProtoFdInfo { 1.29 + int protoId; 1.30 + int originFd; 1.31 + int newFds[2]; 1.32 +}; 1.33 + 1.34 +#define NUWA_NEWFD_PARENT 0 1.35 +#define NUWA_NEWFD_CHILD 1 1.36 + 1.37 +extern "C" { 1.38 + 1.39 +/** 1.40 + * The following 3 methods are used to synchronously spawn the Nuwa process. 1.41 + * The typical usage is: 1.42 + * 1.43 + * The IPC thread The main thread 1.44 + * 1. Receives the request. 1.45 + * 2. NuwaSpawnPrepare(). 1.46 + * 3. Request main thread to 1.47 + * spawn. --------------------------------------+ 1.48 + * 4. NuwaSpawnWait(). V 1.49 + * 5. NuwaSpawn() to clone all 1.50 + * the threads in the Nuwa 1.51 + * process (including main 1.52 + * & IPC threads). 1.53 + * 6. NuwaSpawn() finishes and 1.54 + * +-------------------------------------- signals NuwaSpawnWait(). 1.55 + * V 1.56 + * 7. NuwaSpawnWait() returns. 1.57 + */ 1.58 + 1.59 +/** 1.60 + * Prepare for spawning a new process. The calling thread (typically the IPC 1.61 + * thread) will block further requests to spawn a new process. 1.62 + */ 1.63 +MFBT_API void NuwaSpawnPrepare(); 1.64 + 1.65 +/** 1.66 + * Called on the thread that receives the request. Used to wait until 1.67 + * NuwaSpawn() finishes and maintain the stack of the calling thread. 1.68 + */ 1.69 +MFBT_API void NuwaSpawnWait(); 1.70 + 1.71 +/** 1.72 + * Spawn a new content process, called in the Nuwa process. This has to run on 1.73 + * the main thread. 1.74 + * 1.75 + * @return The process ID of the new process. 1.76 + */ 1.77 +MFBT_API pid_t NuwaSpawn(); 1.78 + 1.79 +/** 1.80 + * The following methods are for marking threads created in the Nuwa process so 1.81 + * they can be frozen and then recreated in the spawned process. All threads 1.82 + * except the main thread must be marked by one of the following 4 methods; 1.83 + * otherwise, we have a thread that is created but unknown to us. The Nuwa 1.84 + * process will never become ready and this needs to be fixed. 1.85 + */ 1.86 + 1.87 +/** 1.88 + * Mark the current thread as supporting Nuwa. The thread will implicitly freeze 1.89 + * by calling a blocking method such as pthread_mutex_lock() or epoll_wait(). 1.90 + * 1.91 + * @param recreate The custom function that will be called in the spawned 1.92 + * process, after the thread is recreated. Can be nullptr if no 1.93 + * custom function to be called after the thread is recreated. 1.94 + * @param arg The argument passed to the custom function. Can be nullptr. 1.95 + */ 1.96 +MFBT_API void NuwaMarkCurrentThread(void (*recreate)(void *), void *arg); 1.97 + 1.98 +/** 1.99 + * Mark the current thread as not supporting Nuwa. The calling thread will not 1.100 + * be automatically cloned from the Nuwa process to spawned process. If this 1.101 + * thread needs to be created in the spawned process, use NuwaAddConstructor() 1.102 + * or NuwaAddFinalConstructor() to do it. 1.103 + */ 1.104 +MFBT_API void NuwaSkipCurrentThread(); 1.105 + 1.106 +/** 1.107 + * Force the current thread to freeze explicitly at the current point. 1.108 + */ 1.109 +MFBT_API void NuwaFreezeCurrentThread(); 1.110 + 1.111 +/** 1.112 + * Helper functions for the NuwaCheckpointCurrentThread() macro. 1.113 + */ 1.114 +MFBT_API jmp_buf* NuwaCheckpointCurrentThread1(); 1.115 + 1.116 +MFBT_API bool NuwaCheckpointCurrentThread2(int setjmpCond); 1.117 + 1.118 +/** 1.119 + * Set the point to recover after thread recreation. The calling thread is not 1.120 + * blocked. This is used for the thread that cannot freeze (i.e. the IPC 1.121 + * thread). 1.122 + */ 1.123 +#define NuwaCheckpointCurrentThread() \ 1.124 + NuwaCheckpointCurrentThread2(setjmp(*NuwaCheckpointCurrentThread1())) 1.125 + 1.126 +/** 1.127 + * The following methods are called in the initialization stage of the Nuwa 1.128 + * process. 1.129 + */ 1.130 + 1.131 +/** 1.132 + * Prepare for making the Nuwa process. 1.133 + */ 1.134 +MFBT_API void PrepareNuwaProcess(); 1.135 + 1.136 +/** 1.137 + * Make the current process a Nuwa process. Start to freeze the threads. 1.138 + */ 1.139 +MFBT_API void MakeNuwaProcess(); 1.140 + 1.141 +/** 1.142 + * Register a method to be invoked after a new process is spawned. The method 1.143 + * will be invoked on the main thread. 1.144 + * 1.145 + * @param construct The method to be invoked. 1.146 + * @param arg The argument passed to the method. 1.147 + */ 1.148 +MFBT_API void NuwaAddConstructor(void (*construct)(void *), void *arg); 1.149 + 1.150 +/** 1.151 + * Register a method to be invoked after a new process is spawned and threads 1.152 + * are recreated. The method will be invoked on the main thread. 1.153 + * 1.154 + * @param construct The method to be invoked. 1.155 + * @param arg The argument passed to the method. 1.156 + */ 1.157 +MFBT_API void NuwaAddFinalConstructor(void (*construct)(void *), void *arg); 1.158 + 1.159 +/** 1.160 + * The methods to query the Nuwa-related states. 1.161 + */ 1.162 + 1.163 +/** 1.164 + * @return If the current process is the Nuwa process. 1.165 + */ 1.166 +MFBT_API bool IsNuwaProcess(); 1.167 + 1.168 +/** 1.169 + * @return If the Nuwa process is ready for spawning new processes. 1.170 + */ 1.171 +MFBT_API bool IsNuwaReady(); 1.172 +}; 1.173 + 1.174 +#endif /* __NUWA_H_ */