1 /*
   2  * Copyright (c) 2014, 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 vm.mlvm.indy.share;
  25 
  26 import java.lang.invoke.MethodHandle;
  27 import java.lang.invoke.MethodHandles;
  28 import java.lang.invoke.CallSite;
  29 import vm.mlvm.share.DekkerTest;
  30 
  31 /**
  32  * The class implements Actor for {@link vm.mlvm.share.DekkerTest}.
  33  * <p>
  34  * This Actor switches targets for CallSite objects supplied in constructor.
  35  * Targets are method handles, generated with MethodHandle.constant(), which return true or false.
  36  * CallSite.setTarget() is used for setting appropriate target.
  37  * CallSite.dynamicInvoker().invokeExact() is used to detect which target is currently set.
  38  * No synchronization is used between setter and getter (see {@link vm.mlvm.share.DekkerTest} for details).
  39  *
  40  * @see vm.mlvm.share.DekkerTest
  41  */
  42 public class CallSiteDekkerActor implements DekkerTest.Actor {
  43 
  44     public static final MethodHandle MH_FALSE = MethodHandles.constant(Boolean.class, false);
  45     public static final MethodHandle MH_TRUE = MethodHandles.constant(Boolean.class, true);
  46 
  47     private final CallSite a;
  48     private final CallSite b;
  49 
  50     public CallSiteDekkerActor(CallSite csa, CallSite csb) {
  51         a = csa;
  52         b = csb;
  53     }
  54 
  55     @Override
  56     public void reset() {
  57         a.setTarget(MH_FALSE);
  58         b.setTarget(MH_FALSE);
  59     }
  60 
  61     @Override
  62     public boolean actorA() throws Throwable {
  63         a.setTarget(MH_TRUE);
  64         return (Boolean) b.dynamicInvoker().invokeExact();
  65     }
  66 
  67     @Override
  68     public boolean actorB() throws Throwable {
  69         b.setTarget(MH_TRUE);
  70         return (Boolean) a.dynamicInvoker().invokeExact();
  71     }
  72 }