1 /*
2  * Copyright (C) 2019 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.CountDownLatch;
18 
19 public class Main {
20    static final CountDownLatch processStarted = new CountDownLatch(1);
21    static final CountDownLatch prioritySet = new CountDownLatch(1);
22 
23    static int initialPlatformPriority = 0;
24    static int maxPlatformPriority = 0;
25 
26    static class MyThread extends Thread {
run()27         public void run() {
28             try {
29                 int priority = getThreadPlatformPriority();
30                 if (priority != initialPlatformPriority) {
31                     throw new Error("Expected " + initialPlatformPriority + ", got " + priority);
32                 }
33                 processStarted.countDown();
34                 prioritySet.await();
35                 priority = getThreadPlatformPriority();
36                 if (priority != maxPlatformPriority) {
37                     throw new Error("Expected " + maxPlatformPriority + ", got " + priority);
38                 }
39             } catch (Exception e) {
40                 throw new Error(e);
41             }
42         }
43     }
44 
main(String[] args)45     public static void main(String[] args) throws Exception {
46         System.loadLibrary(args[0]);
47 
48         // Fetch priorities from the main thread to know what to compare against.
49         int javaPriority = Thread.currentThread().getPriority();
50         initialPlatformPriority = getThreadPlatformPriority();
51         Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
52         maxPlatformPriority = getThreadPlatformPriority();
53         Thread.currentThread().setPriority(javaPriority);
54 
55         MyThread t1 = new MyThread();
56         t1.start();
57         processStarted.await();
58 
59         t1.setPriority(Thread.MAX_PRIORITY);
60         prioritySet.countDown();
61         t1.join();
62     }
63 
getThreadPlatformPriority()64     private static native int getThreadPlatformPriority();
65 }
66