Home | History | Annotate | Download | only in system
      1 /*
      2  * Copyright (C) 2006 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package dalvik.system;
     18 
     19 import java.io.File;
     20 
     21 /**
     22  * Provides hooks for the zygote to call back into the runtime to perform
     23  * parent or child specific initialization..
     24  *
     25  * @hide
     26  */
     27 public final class ZygoteHooks {
     28     private long token;
     29 
     30     /**
     31      * Called by the zygote when starting up. It marks the point when any thread
     32      * start should be an error, as only internal daemon threads are allowed there.
     33      */
     34     public static native void startZygoteNoThreadCreation();
     35 
     36     /**
     37      * Called by the zygote when startup is finished. It marks the point when it is
     38      * conceivable that threads would be started again, e.g., restarting daemons.
     39      */
     40     public static native void stopZygoteNoThreadCreation();
     41 
     42     /**
     43      * Called by the zygote prior to every fork. Each call to {@code preFork}
     44      * is followed by a matching call to {@link #postForkChild(int, String)} on the child
     45      * process and {@link #postForkCommon()} on both the parent and the child
     46      * process. {@code postForkCommon} is called after {@code postForkChild} in
     47      * the child process.
     48      */
     49     public void preFork() {
     50         Daemons.stop();
     51         waitUntilAllThreadsStopped();
     52         token = nativePreFork();
     53     }
     54 
     55     /**
     56      * Called by the zygote in the child process after every fork. The runtime
     57      * flags from {@code runtimeFlags} are applied to the child process. The string
     58      * {@code instructionSet} determines whether to use a native bridge.
     59      */
     60     public void postForkChild(int runtimeFlags, boolean isSystemServer, boolean isZygote,
     61             String instructionSet) {
     62         nativePostForkChild(token, runtimeFlags, isSystemServer, isZygote, instructionSet);
     63 
     64         Math.setRandomSeedInternal(System.currentTimeMillis());
     65     }
     66 
     67     /**
     68      * Called by the zygote in both the parent and child processes after
     69      * every fork. In the child process, this method is called after
     70      * {@code postForkChild}.
     71      */
     72     public void postForkCommon() {
     73         Daemons.startPostZygoteFork();
     74     }
     75 
     76     private static native long nativePreFork();
     77     private static native void nativePostForkChild(long token, int runtimeFlags,
     78                                                    boolean isSystemServer, boolean isZygote,
     79                                                    String instructionSet);
     80 
     81     /**
     82      * We must not fork until we're single-threaded again. Wait until /proc shows we're
     83      * down to just one thread.
     84      */
     85     private static void waitUntilAllThreadsStopped() {
     86         File tasks = new File("/proc/self/task");
     87         // All Java daemons are stopped already. We're just waiting for their OS counterparts to
     88         // finish as well. This shouldn't take much time so spinning is ok here.
     89         while (tasks.list().length > 1) {
     90           Thread.yield();
     91         }
     92     }
     93 }
     94