xref: /aosp_15_r20/art/test/2275-pthread-name/src/Main.java (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2024 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker import java.util.concurrent.CountDownLatch;
18*795d594fSAndroid Build Coastguard Worker import java.util.concurrent.TimeUnit;
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker public class Main {
21*795d594fSAndroid Build Coastguard Worker   static volatile boolean name_was_set = false;
22*795d594fSAndroid Build Coastguard Worker   static final String BIRTH_NAME = "birth name";
23*795d594fSAndroid Build Coastguard Worker   static final String NEW_NAME = "new name";
24*795d594fSAndroid Build Coastguard Worker   static final CountDownLatch child_started = new CountDownLatch(1);
25*795d594fSAndroid Build Coastguard Worker 
26*795d594fSAndroid Build Coastguard Worker   private static class Child implements Runnable {
27*795d594fSAndroid Build Coastguard Worker     @Override
run()28*795d594fSAndroid Build Coastguard Worker     public void run() {
29*795d594fSAndroid Build Coastguard Worker       String bname = Thread.currentThread().getName();
30*795d594fSAndroid Build Coastguard Worker       if (!name_was_set && !bname.equals(BIRTH_NAME)) {
31*795d594fSAndroid Build Coastguard Worker         System.err.println("Wrong birth name: " + bname);
32*795d594fSAndroid Build Coastguard Worker       }
33*795d594fSAndroid Build Coastguard Worker       child_started.countDown();
34*795d594fSAndroid Build Coastguard Worker       while (!name_was_set) {
35*795d594fSAndroid Build Coastguard Worker         try {
36*795d594fSAndroid Build Coastguard Worker           Thread.sleep(10);
37*795d594fSAndroid Build Coastguard Worker         } catch (InterruptedException e) {
38*795d594fSAndroid Build Coastguard Worker           System.out.println("Unexpected interrupt in child");
39*795d594fSAndroid Build Coastguard Worker         }
40*795d594fSAndroid Build Coastguard Worker       }
41*795d594fSAndroid Build Coastguard Worker       System.out.println("Name was set");
42*795d594fSAndroid Build Coastguard Worker       System.out.println("Final child Java name: " + Thread.currentThread().getName());
43*795d594fSAndroid Build Coastguard Worker       System.out.print("Final child pthread name: ");
44*795d594fSAndroid Build Coastguard Worker       printPthreadName();
45*795d594fSAndroid Build Coastguard Worker     }
46*795d594fSAndroid Build Coastguard Worker   }
47*795d594fSAndroid Build Coastguard Worker 
main(String[] args)48*795d594fSAndroid Build Coastguard Worker   public static void main(String[] args) {
49*795d594fSAndroid Build Coastguard Worker     System.loadLibrary(args[0]);
50*795d594fSAndroid Build Coastguard Worker     System.out.print("Main Started; java name: ");
51*795d594fSAndroid Build Coastguard Worker     System.out.println(Thread.currentThread().getName());
52*795d594fSAndroid Build Coastguard Worker     System.out.print("Pthread name: ");
53*795d594fSAndroid Build Coastguard Worker     printPthreadName();
54*795d594fSAndroid Build Coastguard Worker     Thread t = new Thread(new Child(), BIRTH_NAME);
55*795d594fSAndroid Build Coastguard Worker     System.out.print("Child's Java name: ");
56*795d594fSAndroid Build Coastguard Worker     System.out.println(t.getName());
57*795d594fSAndroid Build Coastguard Worker     t.start();
58*795d594fSAndroid Build Coastguard Worker     try {
59*795d594fSAndroid Build Coastguard Worker       if (!child_started.await(2, TimeUnit.SECONDS)) {
60*795d594fSAndroid Build Coastguard Worker         System.out.println("Latch wait timed out");
61*795d594fSAndroid Build Coastguard Worker       }
62*795d594fSAndroid Build Coastguard Worker     } catch (InterruptedException e) {
63*795d594fSAndroid Build Coastguard Worker       System.out.println("Unexpected interrupt in parent");
64*795d594fSAndroid Build Coastguard Worker     }
65*795d594fSAndroid Build Coastguard Worker     System.out.println("Setting name from " + Thread.currentThread().getName());
66*795d594fSAndroid Build Coastguard Worker     t.setName(NEW_NAME);
67*795d594fSAndroid Build Coastguard Worker     if (!t.getName().equals(NEW_NAME)) {
68*795d594fSAndroid Build Coastguard Worker       System.err.println("Wrong new name from main thread: " + t.getName());
69*795d594fSAndroid Build Coastguard Worker     }
70*795d594fSAndroid Build Coastguard Worker     name_was_set = true;
71*795d594fSAndroid Build Coastguard Worker     try {
72*795d594fSAndroid Build Coastguard Worker       t.join();
73*795d594fSAndroid Build Coastguard Worker     } catch (InterruptedException e) {
74*795d594fSAndroid Build Coastguard Worker       System.out.println("Unexpected interrupt in join()");
75*795d594fSAndroid Build Coastguard Worker     }
76*795d594fSAndroid Build Coastguard Worker     System.out.println("Final parent Java name: " + Thread.currentThread().getName());
77*795d594fSAndroid Build Coastguard Worker     System.out.print("Final parent pthread name: ");
78*795d594fSAndroid Build Coastguard Worker     printPthreadName();
79*795d594fSAndroid Build Coastguard Worker   }
80*795d594fSAndroid Build Coastguard Worker 
printPthreadName()81*795d594fSAndroid Build Coastguard Worker   private static native void printPthreadName();
82*795d594fSAndroid Build Coastguard Worker }
83