1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package java.lang;
  25 
  26 import java.util.concurrent.atomic.AtomicInteger;
  27 import java.util.concurrent.CountDownLatch;
  28 import java.util.Arrays;
  29 import java.util.Objects;
  30 
  31 public class PreemptLiveFrames {
  32     public static void main(String[] args) {
  33         try {
  34             PreemptLiveFrames obj = new PreemptLiveFrames();
  35             obj.test1();
  36         } catch (Exception e) {
  37             throw new RuntimeException(e);
  38         }
  39     }
  40 
  41     static final ContinuationScope FOO = new ContinuationScope() {};
  42     CountDownLatch startLatch = new CountDownLatch(1);
  43     volatile boolean run;
  44     volatile int x;
  45 
  46     public void test1() throws Exception {
  47         System.out.println("test1");
  48 
  49         final AtomicInteger result = new AtomicInteger(0);
  50         final Continuation cont = new Continuation(FOO, ()-> {
  51                 double r = 0;
  52                 int k = 1;
  53                 int x = 3;
  54                 String s = "abc";
  55                 r += foo(k);
  56                 result.set((int)r);
  57             });
  58 
  59         final Thread t0 = Thread.currentThread();
  60         Thread t = new Thread(() -> {
  61             try {
  62                 startLatch.await();
  63                 var res = cont.tryPreempt(t0);
  64                 assertEquals(res, Continuation.PreemptStatus.SUCCESS);
  65             } catch (InterruptedException e) {
  66                 throw new RuntimeException(e);
  67             }
  68         });
  69         t.start();
  70 
  71         run = true;
  72 
  73         cont.run();
  74         assertEquals(cont.isDone(), false);
  75         assertEquals(cont.isPreempted(), true);
  76 
  77         testStackWalk(LiveStackFrame.getStackWalker(cont));
  78 
  79         t.join();
  80     }
  81 
  82     double foo(int a) {
  83         long x = 8;
  84         String s = "yyy";
  85         String r = bar(a + 1);
  86         return Integer.parseInt(r)+1;
  87     }
  88 
  89     String bar(long b) {
  90         double x = 9.99;
  91         String s = "zzz";
  92         String r = baz(b + 1);
  93         return "" + r;
  94     }
  95 
  96     String baz(long b) {
  97         double x = 9.99;
  98         String s = "zzz";
  99 
 100         loop();
 101 
 102         long r = b+1;
 103         return "" + r;
 104     }
 105 
 106     void loop() {
 107         while (run) {
 108             x++;
 109             if (startLatch.getCount() > 0) {
 110                 startLatch.countDown();
 111             }
 112         }
 113     }
 114 
 115     static void testStackWalk(StackWalker walker) {
 116         // StackWalker walker = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE);
 117         System.out.println("^&^ start");
 118         walker.forEach(f -> {
 119             try {
 120                 System.out.println("^&^ --==--");
 121                 // System.out.println("^&^ " + f);
 122                 LiveStackFrame lf = (LiveStackFrame)f;
 123                 System.out.println("^&^ locals: " + Arrays.toString(lf.getLocals()));
 124                 System.out.println("^&^ stack: " + Arrays.toString(lf.getStack()));
 125             } catch (Throwable t) {
 126                 t.printStackTrace();
 127                 throw t;
 128             }
 129         });
 130         System.out.println("^&^ end");
 131     }
 132 
 133     static void assertEquals(Object actual, Object expected) {
 134         if (!Objects.equals(actual, expected)) {
 135             throw new AssertionError("Expected: " + expected + ", actual: " + actual);
 136         }
 137     }
 138 }