1 /*
2  * Copyright (C) 2016 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 import java.util.concurrent.Semaphore;
18 
19 public class Main {
20   static final Semaphore start = new Semaphore(0);
21   static volatile boolean continue_loop = true;
22 
main(String[] args)23   public static void main(String[] args) throws Exception {
24     System.loadLibrary(args[0]);
25     Thread t = new Thread(Main::runTargetThread, "Target Thread");
26 
27     t.start();
28     // Wait for other thread to start.
29     start.acquire();
30 
31     System.out.println("pushing checkpoints");
32     pushCheckpoints(t);
33 
34     System.out.println("checkpoints pushed");
35     continue_loop = false;
36 
37     t.join();
38 
39     checkCheckpointsRun();
40 
41     System.out.println("Passed!");
42   }
43 
pushCheckpoints(Thread t)44   public static native void pushCheckpoints(Thread t);
checkCheckpointsRun()45   public static native boolean checkCheckpointsRun();
46 
doNothing()47   public static void doNothing() {}
runTargetThread()48   public static void runTargetThread() {
49     System.out.println("Other thread running");
50     try {
51       start.release();
52       while (continue_loop) {
53         doNothing();
54       }
55     } catch (Exception e) {
56       throw new Error("Exception occurred!", e);
57     }
58   }
59 }
60