Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set sw=2 ts=2 autoindent cindent expandtab: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 5 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifndef __NUWA_H_ |
michael@0 | 8 | #define __NUWA_H_ |
michael@0 | 9 | |
michael@0 | 10 | #include <setjmp.h> |
michael@0 | 11 | #include <unistd.h> |
michael@0 | 12 | |
michael@0 | 13 | #include "mozilla/Types.h" |
michael@0 | 14 | |
michael@0 | 15 | /** |
michael@0 | 16 | * Nuwa is a goddess who created mankind according to her figure in the |
michael@0 | 17 | * ancient Chinese mythology. |
michael@0 | 18 | */ |
michael@0 | 19 | |
michael@0 | 20 | // Max number of top level protocol that can be handled by Nuwa process. |
michael@0 | 21 | #ifndef NUWA_TOPLEVEL_MAX |
michael@0 | 22 | #define NUWA_TOPLEVEL_MAX 8 |
michael@0 | 23 | #endif |
michael@0 | 24 | |
michael@0 | 25 | struct NuwaProtoFdInfo { |
michael@0 | 26 | int protoId; |
michael@0 | 27 | int originFd; |
michael@0 | 28 | int newFds[2]; |
michael@0 | 29 | }; |
michael@0 | 30 | |
michael@0 | 31 | #define NUWA_NEWFD_PARENT 0 |
michael@0 | 32 | #define NUWA_NEWFD_CHILD 1 |
michael@0 | 33 | |
michael@0 | 34 | extern "C" { |
michael@0 | 35 | |
michael@0 | 36 | /** |
michael@0 | 37 | * The following 3 methods are used to synchronously spawn the Nuwa process. |
michael@0 | 38 | * The typical usage is: |
michael@0 | 39 | * |
michael@0 | 40 | * The IPC thread The main thread |
michael@0 | 41 | * 1. Receives the request. |
michael@0 | 42 | * 2. NuwaSpawnPrepare(). |
michael@0 | 43 | * 3. Request main thread to |
michael@0 | 44 | * spawn. --------------------------------------+ |
michael@0 | 45 | * 4. NuwaSpawnWait(). V |
michael@0 | 46 | * 5. NuwaSpawn() to clone all |
michael@0 | 47 | * the threads in the Nuwa |
michael@0 | 48 | * process (including main |
michael@0 | 49 | * & IPC threads). |
michael@0 | 50 | * 6. NuwaSpawn() finishes and |
michael@0 | 51 | * +-------------------------------------- signals NuwaSpawnWait(). |
michael@0 | 52 | * V |
michael@0 | 53 | * 7. NuwaSpawnWait() returns. |
michael@0 | 54 | */ |
michael@0 | 55 | |
michael@0 | 56 | /** |
michael@0 | 57 | * Prepare for spawning a new process. The calling thread (typically the IPC |
michael@0 | 58 | * thread) will block further requests to spawn a new process. |
michael@0 | 59 | */ |
michael@0 | 60 | MFBT_API void NuwaSpawnPrepare(); |
michael@0 | 61 | |
michael@0 | 62 | /** |
michael@0 | 63 | * Called on the thread that receives the request. Used to wait until |
michael@0 | 64 | * NuwaSpawn() finishes and maintain the stack of the calling thread. |
michael@0 | 65 | */ |
michael@0 | 66 | MFBT_API void NuwaSpawnWait(); |
michael@0 | 67 | |
michael@0 | 68 | /** |
michael@0 | 69 | * Spawn a new content process, called in the Nuwa process. This has to run on |
michael@0 | 70 | * the main thread. |
michael@0 | 71 | * |
michael@0 | 72 | * @return The process ID of the new process. |
michael@0 | 73 | */ |
michael@0 | 74 | MFBT_API pid_t NuwaSpawn(); |
michael@0 | 75 | |
michael@0 | 76 | /** |
michael@0 | 77 | * The following methods are for marking threads created in the Nuwa process so |
michael@0 | 78 | * they can be frozen and then recreated in the spawned process. All threads |
michael@0 | 79 | * except the main thread must be marked by one of the following 4 methods; |
michael@0 | 80 | * otherwise, we have a thread that is created but unknown to us. The Nuwa |
michael@0 | 81 | * process will never become ready and this needs to be fixed. |
michael@0 | 82 | */ |
michael@0 | 83 | |
michael@0 | 84 | /** |
michael@0 | 85 | * Mark the current thread as supporting Nuwa. The thread will implicitly freeze |
michael@0 | 86 | * by calling a blocking method such as pthread_mutex_lock() or epoll_wait(). |
michael@0 | 87 | * |
michael@0 | 88 | * @param recreate The custom function that will be called in the spawned |
michael@0 | 89 | * process, after the thread is recreated. Can be nullptr if no |
michael@0 | 90 | * custom function to be called after the thread is recreated. |
michael@0 | 91 | * @param arg The argument passed to the custom function. Can be nullptr. |
michael@0 | 92 | */ |
michael@0 | 93 | MFBT_API void NuwaMarkCurrentThread(void (*recreate)(void *), void *arg); |
michael@0 | 94 | |
michael@0 | 95 | /** |
michael@0 | 96 | * Mark the current thread as not supporting Nuwa. The calling thread will not |
michael@0 | 97 | * be automatically cloned from the Nuwa process to spawned process. If this |
michael@0 | 98 | * thread needs to be created in the spawned process, use NuwaAddConstructor() |
michael@0 | 99 | * or NuwaAddFinalConstructor() to do it. |
michael@0 | 100 | */ |
michael@0 | 101 | MFBT_API void NuwaSkipCurrentThread(); |
michael@0 | 102 | |
michael@0 | 103 | /** |
michael@0 | 104 | * Force the current thread to freeze explicitly at the current point. |
michael@0 | 105 | */ |
michael@0 | 106 | MFBT_API void NuwaFreezeCurrentThread(); |
michael@0 | 107 | |
michael@0 | 108 | /** |
michael@0 | 109 | * Helper functions for the NuwaCheckpointCurrentThread() macro. |
michael@0 | 110 | */ |
michael@0 | 111 | MFBT_API jmp_buf* NuwaCheckpointCurrentThread1(); |
michael@0 | 112 | |
michael@0 | 113 | MFBT_API bool NuwaCheckpointCurrentThread2(int setjmpCond); |
michael@0 | 114 | |
michael@0 | 115 | /** |
michael@0 | 116 | * Set the point to recover after thread recreation. The calling thread is not |
michael@0 | 117 | * blocked. This is used for the thread that cannot freeze (i.e. the IPC |
michael@0 | 118 | * thread). |
michael@0 | 119 | */ |
michael@0 | 120 | #define NuwaCheckpointCurrentThread() \ |
michael@0 | 121 | NuwaCheckpointCurrentThread2(setjmp(*NuwaCheckpointCurrentThread1())) |
michael@0 | 122 | |
michael@0 | 123 | /** |
michael@0 | 124 | * The following methods are called in the initialization stage of the Nuwa |
michael@0 | 125 | * process. |
michael@0 | 126 | */ |
michael@0 | 127 | |
michael@0 | 128 | /** |
michael@0 | 129 | * Prepare for making the Nuwa process. |
michael@0 | 130 | */ |
michael@0 | 131 | MFBT_API void PrepareNuwaProcess(); |
michael@0 | 132 | |
michael@0 | 133 | /** |
michael@0 | 134 | * Make the current process a Nuwa process. Start to freeze the threads. |
michael@0 | 135 | */ |
michael@0 | 136 | MFBT_API void MakeNuwaProcess(); |
michael@0 | 137 | |
michael@0 | 138 | /** |
michael@0 | 139 | * Register a method to be invoked after a new process is spawned. The method |
michael@0 | 140 | * will be invoked on the main thread. |
michael@0 | 141 | * |
michael@0 | 142 | * @param construct The method to be invoked. |
michael@0 | 143 | * @param arg The argument passed to the method. |
michael@0 | 144 | */ |
michael@0 | 145 | MFBT_API void NuwaAddConstructor(void (*construct)(void *), void *arg); |
michael@0 | 146 | |
michael@0 | 147 | /** |
michael@0 | 148 | * Register a method to be invoked after a new process is spawned and threads |
michael@0 | 149 | * are recreated. The method will be invoked on the main thread. |
michael@0 | 150 | * |
michael@0 | 151 | * @param construct The method to be invoked. |
michael@0 | 152 | * @param arg The argument passed to the method. |
michael@0 | 153 | */ |
michael@0 | 154 | MFBT_API void NuwaAddFinalConstructor(void (*construct)(void *), void *arg); |
michael@0 | 155 | |
michael@0 | 156 | /** |
michael@0 | 157 | * The methods to query the Nuwa-related states. |
michael@0 | 158 | */ |
michael@0 | 159 | |
michael@0 | 160 | /** |
michael@0 | 161 | * @return If the current process is the Nuwa process. |
michael@0 | 162 | */ |
michael@0 | 163 | MFBT_API bool IsNuwaProcess(); |
michael@0 | 164 | |
michael@0 | 165 | /** |
michael@0 | 166 | * @return If the Nuwa process is ready for spawning new processes. |
michael@0 | 167 | */ |
michael@0 | 168 | MFBT_API bool IsNuwaReady(); |
michael@0 | 169 | }; |
michael@0 | 170 | |
michael@0 | 171 | #endif /* __NUWA_H_ */ |